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

This is awesome .. I had no idea how to get started on building such an algo in perl. Thankyou so much for your help... I think you nailed the problem as far as I can see. To answer some of your questions:

Right now there is a capacity set at 82,080 for each machine ( this is a estimation we are sticking with for now).

There are a limit of nozzles per machine would be 25 - thats a great question I should have thought to put that earlier.

My next questions are how can I download and learn from the code you have made to do this? Was it done in perl? Super excited to take a look. I will have to build a loop around what youve done so I can do it iteravely for a six month period ( day by day), but I think I can handle that. I'll definitely be making a large donation =)

  • Comment on Re^7: Tricky chemicals optimization problem

Replies are listed 'Best First'.
Re^8: Tricky chemicals optimization problem
by BrowserUk (Patriarch) on Jan 11, 2017 at 11:06 UTC

    Here you go:

    #! perl -slw use strict; use List::Util qw[ sum ]; use Data::Dump qw[ pp ]; $Data::Dump::WIDTH = 1000; my %chemsAndFlows; while( <DATA> ) { my( $chem, $mach, $flow ) = split ' '; next if $flow == 0; push @{ $chemsAndFlows{ $chem } }, $flow; } @$_ = sort{ $a <=> $b } @$_ for values %chemsAndFlows; #pp \%chemsAndF +lows; ## Greedy; our $MACHMAX //= 11000; my( $iMach, %machsAssigned ) = 0; while( keys %chemsAndFlows ) { my( $machTotal, $mach ) = ( 0, sprintf "E%03d", ++$iMach); for my $chem ( keys %chemsAndFlows ) { for my $iFlow ( reverse 0 .. $#{ $chemsAndFlows{ $chem } } ) { my $flow = $chemsAndFlows{ $chem }[ $iFlow ]; die "MACHMAX too small for $chem:$flow" unless $flow < $MA +CHMAX; if( ( $machTotal + $flow ) <= $MACHMAX ) { $machTotal += $flow; push @{ $machsAssigned{ $mach }{ $chem } }, $flow; splice @{ $chemsAndFlows{ $chem } }, $iFlow, 1; delete $chemsAndFlows{ $chem } unless @{ $chemsAndFlow +s{ $chem } }; } } } } $_->{ ' total' } = sum( map @$_, values %$_ )for values %machsAssigned +; pp \%machsAssigned; #1st column is unique chemical identifier 2nd column is unique machine + identifier 3rd column is processing seconds __DATA__

    Append your posted data at the end after the __DATA__ line.


    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.
    p
      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'?

      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?

      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?

      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!
        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.