Changes between Version 3 and Version 4 of Documentation/Howto/OptimizeAnyLogicModels

04/26/11 23:12:50 (9 years ago)



  • Documentation/Howto/OptimizeAnyLogicModels

    v3 v4  
    4646private void getMessage() {
    4747        currentSolution = commDriver.getSolution();
    48         if (currentSolution.getDoubleArrayVarsCount() > 0) {
     48        if (currentSolution.getIntegerArrayVarsCount() > 0) {
    4949                SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0);
    5050                int i = 0;
    6565                        i++;
    6666                }
     67                int h;
     68                if (sLoRetailer > SHiRetailer) { h = sLoRetailer; sLoRetailer = SHiRetailer; SHiRetailer = h; }
     69                if (sLoWholesaler > SHiWholesaler) { h = sLoWholesaler; sLoWholesaler = SHiWholesaler; SHiWholesaler = h; }
     70                if (sLoFactory > SHiFactory) { h = sLoFactory; sLoFactory = SHiFactory; SHiFactory = h; }
    6771        }
    7175We have defined a method //getMessage()// that calls our library to get the next message from !HeuristicLab and extract the parameters. You'll notice that here I have put the same text as before in the box "Expression" which is an intentional coincidence ;-)
     76Also notice that we need to check for invalid configurations, so if the value for the lower bound is greater than that for the upper bound I just swap them.
    7378==== Initial experiment setup ====
    111116        commDriver.sendQuality(currentSolution, quality / replications);
    112117} catch (IOException e) { }
     118quality = 0;
     119replications = 0;
    115 This sends the mean quality of the (possible) replications back to !HeuristicLab. We are done here. Save the model. You can now start the !HeuristicLabOptimizationExperiement, although of course it won't do anything but block until it receives parameters.
     124This sends the mean quality of the (possible) replications back to !HeuristicLab, resets the variables and receives a new message with which to perform the next simulation runs. We are done here. Save the model. You can now start the !HeuristicLabOptimizationExperiement, although of course it won't do anything but block until it receives parameters.
    117126== Preparing the Optimization Problem in !HeuristicLab 3.3 ==
     128In !HeuristicLab we need to define the problem and choose an algorithm with which we can optimize it.
     130=== Defining the problem ===
     131We need to create a new External Evaluation Problem, so switch to the Clipboard (View > Clipboard if it is not visible) and click the yellow plus icon to add a new item. Type "external" without quotes in the search box and you'll find the right item called "External Evaluation Problem". Double click on it and you'll see it added to the clipboard. Double click it again in the clipboard so that we can edit it.
     133In the box you can type a Name, I'll use "!AnyLogic Supply Chain Problem". You can also double click the information icon on the right end of the name box to edit the description.
     135In the Parameters box you'll see a number of different parameters. Let's configure the client first.
     137==== Client ====
     138Click on the parameter called "Client" to which currently an `EvaluationServiceClient` is attached and you'll see another box with parameters. Among those is one called "Channel" which is currently null, meaning that it has no value defined. Click on Channel and then click on the pencil icon to give it a value. Choose the `EvaluationTCPChannel` by double clicking it in the emerging dialog. Now type the IP Address of the computer where the simulation will run ("" in case it will be run on the same computer) and enter the port number (remember we chose "2112" above). Okay we're done with the client.
     140Next we'll configure the parameter Evaluator. Click on this parameter to get its list of parameters.
     142==== Evaluator ====
     143In the "Collected values" section click on the yellow plus icon and add a `LookupParameter<T>` and for `T` choose an `IntegerVector` by double clicking on it (may take a couple of seconds). Name this parameter "!IntegerVector", as always without quotes, and click Ok. Remember what you entered as Name!
     145So we're done with this parameter. The next one is Maximization which we'll not need to touch as it is already false (we're minimizing costs). So we get to the parameter "Operators" by clicking on it.
     147==== Operators ====
     148By clicking on the yellow plus button add following operators from the !HeuristicLab.Encodings.!IntegerVectorEncoding namespace:
     149 * `SinglePointCrossover`
     150 * `UniformOnePositionManipulator`
     152If you click on the just added `UniformOnePositionManipulator` you'll see two more parameters called Maximum and Minimum. We can either add a value to them here, or add a parameter at a higher level (e.g. at the problem level). So let's add two `ValueParameter<T>` and choose `IntValue` for `T`. Name one of them "Minimum" and the other "Maximum". Choose the value 1 for Minimum and 100 for Maximum.
     154Now click on and configure the last parameter called !SolutionCreator
     156==== !SolutionCreator ====
     157Click the yellow plus icon and add a `UniformRandomIntegerVectorCreator`. Click on it to see its parameters. You'll note there is an additional one called "Length". Let's add a new `ValueParameter<T>` just like Minimum and Maximum but called Length to the problem's parameters and give it the value 6, because we have six parameters to optimize. You should save the clipboard now to save the problem within it (you can also save the problem to a separate file if you want).
     159=== Choosing an Algorithm ===
     161We're now done with the problem. We have added a crossover operator and a mutator so we're somewhat limited to a Genetic Algorithm or one of its variants. Algorithms like Tabu Search need more complex operators.
     163So create a new Genetic Algorithm by choosing "New" from the File menu.
     165Drag the problem from the clipboard onto the problem tab of the GA that we have just created. Go to the parameters tab and select the mutator that we have added (no mutator is chosen by default).
     167== Optimizing the model ==
     169Now it's time, we're ready to start the optimization. First switch back to !AnyLogic 6 and start the !HeuristicLabOptimizationExperiment. Click on the button "Run Experiment" to set it into listening mode.
     171Go to !HeuristicLab and click the green arrow at the bottom to start the algorithm.
     173Now in the back the algorithm attempts a TCP connection to the given IP Address and port and exchanges parameters with the simulation model. It will receive quality values and use these to determine the fitness of its solutions.