This works really awesome..... some of the code a bit over my head so I'm trying to break it down and understand all the pieces. Couple questions:
1) the 'our' declaration - I read about the definition and usage but still don't really understand why you use it here instead of ''my'?

See the -s switch in perlrun.

In a nutshell: -s allows me to supply -MACHMAX=200000 on the command line to the script and it sets the global variable $MACHMAX to the value supplied.

Without the our I would get a warning: "Global symbol "$MACHMAX" requires explicit package name ...".

If I used my $MACHMAX; I would still get a warning: "Name "main::MACHMAX" used only once: possible typo" and it would be a different variable from the one assigned from the command line. I just wouldn't work.

2) to make sure I understood your code and the process, I set out to solve the problem if we want to minimize the number of nozzles per machine rather than minimize the number of machines. In other words we would want to minimize the average chemical per machine. How would you alter your code above to accomplish this?

I don't understand how you can "minimixe the number of nozzles"? Don't you need all the different flows to be separate? If not, why are there multiple flows of the same chemical on the same machine in the existing scheme?

If you don't care how many machines, just have a separate machine for each chemical; and if the total flow required for a given chemical exceeds the machines capacity, add another for that chemical.

The code I supplied does a pretty good job of putting as many of the flows for a given chemical onto the same machine. If you can combine flows to a single nozzle, simply adding all the flows for a given chemical on a given machine into single nozzle will do a pretty good job of minimizing the number of nozzles.

The code I posted already puts all the flows for a given chemical on a given machine into a separate array, so combining them would be trivial, assuming that there in no limit on the flow from a nozzle other than the machine limit. (A question I did ask but you didn't answer as far as I've noticed.)

3) I get a bit lost when you get to the 'splice' and 'delete' lines of your code... I follow that you want to use splice on the HoHoA because after you assign the flow you want to remove it from consideration... but what is the delete part serving to do?

The delete removes the chemical from the hash once all its flows have been assigned to a machine. Once the hash is empty the while loop terminates.

4) do you have a good book recommendation or online exercises recommendation on these types of dynamic programming? The only language I know (barely) is perl... thanks again for your help... it's made huge difference in my learning!

Not personally. I learnt perl via this site -- initially asking, then answering questions and reading other peoples answers. -- and by using the extraordinarily comprehensive documentation and just trying things out.

But if you do a search here for "books" & "perl" there are numerous existing threads that answer that question.


In reply to Re^10: Tricky chemicals optimization problem by BrowserUk
in thread Tricky chemicals optimization problem by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.