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

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

--

### Legend:

Unmodified
 v3 private void getMessage() { currentSolution = commDriver.getSolution(); if (currentSolution.getDoubleArrayVarsCount() > 0) { if (currentSolution.getIntegerArrayVarsCount() > 0) { SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0); int i = 0; i++; } int h; if (sLoRetailer > SHiRetailer) { h = sLoRetailer; sLoRetailer = SHiRetailer; SHiRetailer = h; } if (sLoWholesaler > SHiWholesaler) { h = sLoWholesaler; sLoWholesaler = SHiWholesaler; SHiWholesaler = h; } if (sLoFactory > SHiFactory) { h = sLoFactory; sLoFactory = SHiFactory; SHiFactory = h; } } } We 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 ;-) Also 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. ==== Initial experiment setup ==== commDriver.sendQuality(currentSolution, quality / replications); } catch (IOException e) { } quality = 0; replications = 0; getMessage(); }}} 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. This 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. == Preparing the Optimization Problem in !HeuristicLab 3.3 == In !HeuristicLab we need to define the problem and choose an algorithm with which we can optimize it. === Defining the problem === We 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. In 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. In the Parameters box you'll see a number of different parameters. Let's configure the client first. ==== Client ==== Click 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 ("127.0.0.1" 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. Next we'll configure the parameter Evaluator. Click on this parameter to get its list of parameters. ==== Evaluator ==== In the "Collected values" section click on the yellow plus icon and add a LookupParameter 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! So 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. ==== Operators ==== By clicking on the yellow plus button add following operators from the !HeuristicLab.Encodings.!IntegerVectorEncoding namespace: * SinglePointCrossover * UniformOnePositionManipulator If 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 and choose IntValue for T. Name one of them "Minimum" and the other "Maximum". Choose the value 1 for Minimum and 100 for Maximum. Now click on and configure the last parameter called !SolutionCreator ==== !SolutionCreator ==== Click 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 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). === Choosing an Algorithm === We'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. So create a new Genetic Algorithm by choosing "New" from the File menu. Drag 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). == Optimizing the model == Now 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. Go to !HeuristicLab and click the green arrow at the bottom to start the algorithm. Now 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.