in reply to Re^13: Tricky chemicals optimization problem
in thread Tricky chemicals optimization problem

which parts of the code I need to focus on to accomplish the change to minimizing nozzles before minimizing number of machines?

Well, the minimum number of nozzles is simply the number of chemicals; plus that number required to ensure that no single nozzle is delivering more than your machine limit.

This code, simply reads your sample data, discards any 'zero flows' and accumulates all flows for each chemical.

The provisional result is that there are 406 non-zero flows for 61 chemicals. But, some of those chemicals have combined flows that are greater than the 85,000 seconds/machine you've stipulated. Those need to be split across 2 or more machines. Doing so results in a requirement for 71 machines to dispense the 61 chemicals:

But now, some of the machines (eg.those running C057 & C039) are grossly under-utilised and those chemicals need to be combined into fewer machines.

The simplest approach to that is a variation of the greedy knapsack algorithm which I will call (because I haven't seen it described anywhere) and the max-min algorithm.

Basically you order the nozzles largest to smallest. You look at the largest and then track backward from the smallest until you find the largest (single chemical) nozzle that can be combined with that largest nozzle without breaking the machine limit. Combine them into a single machine and remove the small one from them from consideration. Rinse and repeat. When the smallest left is too big to be combined, remove the current largest from consideration also. Rinse and repeat until no nozzles are left.

This isn't guaranteed to produce the absolute optimal solution in terms of minimizing the number of machines, but it will always produce the minimum number of nozzles and very close to the minimal number of machines.

I don't have code for this approach this evening, but I'll try to post some tomorrow.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^15: Tricky chemicals optimization problem
by Anonymous Monk on Jan 16, 2017 at 05:26 UTC
    Ok I think I am following..... - I will also try to code it and see how far I can get. Just to make it easier problem don't even worry about minimizing number of machines at all. I can go back and tinker later. And thanks again.... I realize how much time you're already given to this post, and it's very appreciated.

      You'll probably have questions, but rather than me trying to guess what they are and answering all the wrong ones, I'll just post the code and then answer any you ask:

      Output:

      C:\test>1179307-3 After flow consolidation: Total machines:72 total nozzles:72 Total idle capacity:3663164 (~44 machines) E001 : total=84937.49 C015 : [ 24504.1632, 703.4688, 791.19, 1430.16, 1436.2488, 1 +501.668, 1716.192, 2386.0308, 3468.138, 4380.3252, 5255.838, 5720.64, + 6234.5772, 12250.6656 ] C029 : [ 6377.0976 ] C044 : [ 6781.0824 ] E002 : total=58143.30 C050 : [ 19529.6136, 5350.9932, 3023.16, 1996.56 ] C022 : [ 14198.232, 9324.7848, 3760.6128, 959.34 ] E003 : total=75112.85 C013 : [ 15214.92, 13190.04, 7315.1268, 7042.3344, 6690.6, 5 +977.29, 3874.53, 2442.6 ] C063 : [ 10392.8736, 1585.3536, 880.752, 506.4324 ] E004 : total=84087.11 C019 : [ 23934.8604, 4471.02, 2891.2596 ] C036 : [ 14996.502, 6818.5356, 2777.13, 883.23, 883.23 ] C017 : [ 14695.0356, 7298.2056, 2465.61, 1972.488 ] E005 : total=76289.48 C027 : [ 29056.32, 3101.04, 4062.15, 4230.3, 9409.32, 15682. +2 ] C038 : [ 9604.728, 1143.42 ] E006 : total=77284.01 C027 : [ 26777.6928, 19602.75, 21090.6828 ] C037 : [ 6626.88, 3186 ] E007 : total=77315.65 C027 : [ 25997.76, 21151.5, 21892.3512 ] C054 : [ 5200.8264, 3073.2156 ] E008 : total=83867.70 C065 : [ 18787.488, 9060.1344, 5267.52, 3072.72, 1773.3984, +1755.84, 877.92 ] C033 : [ 16761.7584, 4028.3784 ] C027 : [ 22482.54 ] E009 : total=74406.41 C012 : [ 12292.2252, 11228.88, 11145.5484, 11068.3056, 7965. +8496, 7258.9824 ] C011 : [ 13446.6192 ] E010 : total=83929.15 C012 : [ 14273.28, 343.026, 350.0352, 462.1824, 490.1484, 52 +5.9732, 536.31, 552.24, 566.4, 571.71, 575.25, 1022.9184, 1029.7152, +1087.488, 1669.3932, 2195.3664, 2265.6, 2812.8132, 2832, 2876.25, 321 +7.86, 3452.3496, 3735.762, 4463.232, 5671.3632, 5819.3352, 5841.2124, + 6071.808, 6281.73 ] C067 : [ 1869.12, 467.28 ] E011 : total=78670.27 C016 : [ 34161.9912, 22882.56, 13821.93 ] C032 : [ 7803.7884 ] E012 : total=84392.33 C016 : [ 36411.8736, 3546.7968, 4261.8768, 4290.48, 5810.91, + 5819.76, 6375.3984, 7274.7, 9457.11 ] C039 : [ 571.71, 571.71 ] E013 : total=74026.92 C071 : [ 17055.72, 10316.1264, 9819.96, 4651.56, 4134.72, 41 +34.72, 4134.72, 4134.72, 1550.52 ] C008 : [ 7624.8768, 4450.2048, 1133.154, 803.5092, 82.4112 ] E014 : total=75762.51 C026 : [ 11102.9268, 10570.1568, 7970.2392, 6393.24, 5860.47 +, 5860.47, 3729.39, 3729.39, 2755.3944, 2663.85, 2173.7016, 2131.08 ] C041 : [ 10822.2048 ] E015 : total=67035.49 C047 : [ 21905.52, 11044.8, 5614.44, 4141.8, 3552.744, 2282. +592 ] C058 : [ 10519.11, 5970.8472, 2003.64 ] E016 : total=58391.95 C055 : [ 17815.05, 9458.172, 1619.55, 1619.55, 539.85 ] C018 : [ 23211.426, 3075.198, 1053.15 ] E017 : total=84824.77 C002 : [ 27401.2992, 279.66, 472.1652, 909.78, 1078.992, 129 +7.41, 1348.74, 1751.1672, 3788.4372, 3992.2704, 4600.584, 6743.7, 674 +9.4348, 7975.9032, 9097.8 ] C069 : [ 1383.1488, 476.13, 476.13, 467.28 ] C004 : [ 3023.16, 1511.58 ] E018 : total=75579.42 C059 : [ 16037.0496, 11652.972, 8889.8604, 6949.02, 6096.729 +6, 4212.1752, 3185.8584, 2208.96, 1104.48, 1104.48, 847.6884, 506.22, + 62.8704 ] C066 : [ 6855.6348, 5865.426 ] E019 : total=58385.64 C045 : [ 13927.4928, 13855.7016, 861.4944, 861.4944, 717.912 + ] C015 : [ 14837.91, 13323.6396 ] E020 : total=58224.36 C025 : [ 14218.41, 9081.516, 7354.35 ] C048 : [ 7948.4328, 7151.3664, 6549.8496, 5920.4376 ] E021 : total=84547.09 C025 : [ 18737.5032, 73.9152, 75.048, 461.97, 490.29, 490.29 +, 573.48, 879.336, 923.94, 1731.06, 1731.06, 1790.532, 2288.964, 2451 +.45, 3165.0432, 3392.8776, 3432.03, 3714.876, 3765.852, 4106.1168, 46 +52.976, 4902.9, 6373.77, 6595.02, 6822.8544 ] C057 : [ 923.94 ] E022 : total=68531.07 C043 : [ 23977.128, 15787.5504, 10575.5376, 1462.02, 555.567 +6 ] C060 : [ 8658.2028, 6731.8056, 529.23, 254.0304 ] E023 : total=84261.84 C011 : [ 21495.3048, 534.54, 534.54, 536.31, 536.31, 536.31, + 571.71, 571.71, 1026.3168, 1072.62, 1074.8148, 1143.42, 1566.0252, 1 +603.62, 1603.62, 1692.2616, 2123.7876, 2145.24, 2469.0792, 2523.0288, + 2629.9368, 2681.55, 3207.24, 4233.5568, 4276.32, 4276.32, 5302.6368, + 5901.3216, 6392.3904 ] E024 : total=82420.12 C011 : [ 13817.8944, 6435.72, 6991.7832, 7376.652, 7440.7968 +, 9052.9128, 12648.2076, 12807.0828 ] C024 : [ 4854.3312, 994.74 ] E025 : total=69001.26 C028 : [ 8562.4812, 6603.87, 4862.8272, 4571.91, 4571.91, 30 +47.94, 2519.6304, 2418.7404, 2158.8336, 1894.1832, 1599.7968, 1523.97 +, 1523.97, 1199.8476, 1015.98, 1015.98, 507.99, 483.21, 457.0848 ] C030 : [ 12659.04, 3164.76, 2109.84, 527.46 ] E026 : total=82588.70 C046 : [ 19876.1088, 9586.8864, 2633.76 ] C062 : [ 13215.2448, 9672.342, 1086.78, 543.39, 412.9764 ] C068 : [ 8094.21, 5085.0684, 3320.52, 2884.1088, 2846.16, 19 +04.52, 952.26, 474.36 ] E027 : total=80294.56 C070 : [ 19788.6, 11496.5748, 1633.71, 576.0288, 456.0228 ] C056 : [ 6869.724, 5901.0384, 4301.808, 2578.6068, 2186.8704 +, 548.7, 383.5944 ] C023 : [ 23573.2848 ] E028 : total=81603.30 C003 : [ 31858.23, 4610.0004, 2331.09, 1522.9788, 1505.4912 +] C061 : [ 6980.3136, 5961.1476, 3811.5888, 1546.272, 1457.913 +6 ] C064 : [ 19976.22, 42.0552 ] E029 : total=66818.42 C020 : [ 17930.808, 15611.4, 9302.8368, 2884.392, 1331.748 ] C042 : [ 18219.2472, 1537.9884 ] E030 : total=84469.07 C009 : [ 10448.3808, 10068.1848, 8391.924, 6775.7016, 3822.9 +876, 3735.9036, 2851.6824, 2035.0044, 1522.9788, 1305.4104, 759.33, 7 +27.47, 727.47, 698.3712, 58.1976 ] C010 : [ 10531.146, 4449.78 ] C021 : [ 15559.1496 ] E031 : total=76700.97 C014 : [ 39789.6, 12931.62, 4476.33, 3978.96, 2984.22 ] C002 : [ 12540.2376 ] E032 : total=75958.56 C023 : [ 24447.6648, 4371.9, 7869.42, 13990.08, 14864.46 ] C005 : [ 9588.444, 826.59 ] After machine consolidation: Total machines:32 total nozzles:71 Total idle capacity:292138 (~4 machines)

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.
      6855.6348, 5865.426