= Optimize Parameters of !AnyLogic 6 Simulation Models = == Introduction == We assume the reader is familiar with xj technologies [http://www.anylogic.com AnyLogic 6] and has basic understanding of the [http://www.java.com Java] programming language. This howto is written as a tutorial by taking one of the sample models that ships with !AnyLogic and add connectors to optimize its parameters with !HeuristicLab algorithms. If you go through this tutorial you will hopefully get an idea on how to solve your specific problem and implement the connector in your model. If you're interested in the technical details and the backgrounds you can also read the first section of the howto aimed more at developers: [DevelopersOptimizingExternalApplications optimize parameters of simulation models.] == !AnyLogic 6 Supply Chain model == The model which we are going to optimize with !HeuristicLab comes preinstalled with !AnyLogic 6 and is called //Supply Chain//. You can open it either on the //Welcome page// after starting !AnyLogic 6 or by choosing "Sample Models" from the Help menu. After you opened the model you can try to run the Simulation. There is also an //Optimization Experiment// in this model which uses the proprietary optimization package !OptQuest. === Optimization Problem === Let's talk a little about the optimization problem: There are three players in a supply chain of which the first one produces goods from an unlimited source of resources and the last one sells goods to an unlimited number of customers. Each player maintains an inventory of goods out of which it fulfills the demands of the next player. There are ordering costs, holding costs and shipping costs. The optimization consists of defining parameters of an (s,S) inventory policy. The lower //s// in this policy defines the inventory level at which an order will be placed and the upper //S// defines the level which should be filled by the order. The optimization goal is to minimize the sum of all costs in the chain while satisfying a certain service level which is represented as the customer waiting time. If customers have to wait too long then the solution (aka. the parameters of the policy) is considered infeasible. In total we have 6 decision variables, one target variable and a constraint. == Preparing the Simulation Model in !AnyLogic 6 == First save the model in a new location. Click "Save As..." in the File menu. For the sake of simplicity just put "Supply Chain" in the box that asks for a model name and leave everything else. That way it will save it in a folder called "Models" in your user's directory. Now download the [http://dev.heuristiclab.com/trac/hl/core/attachment/wiki/DevelopersOptimizingExternalApplications/HL3ExternalEvaluation.jar HL3 External Evaluation Java library] to a place that you remember (e.g. your Downloads directory; you should not download it into the model folder already). Then switch to !AnyLogic 6 again and click the model icon in the project view (should be called "Supply Chain" if you followed the model name suggestion above). In the properties window of this model in the section "Jar files and class folders required to build the model:" click the add button. Choose the file you just downloaded, make sure "Import to model folder is ticked" and click "Finish" to add the library. Save the model. Now we need to add a new experiment which we can run to serve as our evaluation function. The best type of experiment for this task is a "Parameters Variation" experiment, so choose this and choose a name for it, e.g. "!HeuristicLabOptimizationExperiment". Leave everything else as it is and click "Finish". Now we have created a new experiment with which we can optimize the parameters. In the "Properties" view of this experiment go to the "General" tab and choose "Freeform" instead of "Varied in range" and put a large number in the box asking for the "Number of runs", e.g.: 1,000,000,000. Then type in each field in the column "Expression" the same word as you see on the general property page of the //Simulation// experiment. So instead of the first number 20 you type //sLoRetailer//, then you replace the 80 with //SHiRetailer//, then the next 20 you replace with //sLoWholesaler//, then //SHiWholesaler//, then //sLoFactory//, and finally //SHiFactory//. Click save. Now in the upper right hand corner of this property page is a button called "Create default UI", click that now and you have some user interface elements added to this experiment. Next we need to go to the "Advanced" section of the properties and configure the experiment loop: perform the parameter retrieval, run the simulation, send the quality back, reset the model and continue with another attempt of retrieving parameters. The following code samples need to be entered into the respective boxes. ==== Imports ==== {{{ #!java import java.io.*; import java.text.*; import com.heuristiclab.problems.externalevaluation.*; import com.heuristiclab.problems.externalevaluation.ExternalEvaluationMessages.*; }}} ==== Additional class code ==== {{{ #!java int replications; double quality; com.heuristiclab.problems.externalevaluation.PollService commDriver; SolutionMessage currentSolution = null; private void getMessage() { currentSolution = commDriver.getSolution(); if (currentSolution.getDoubleArrayVarsCount() > 0) { SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0); int i = 0; for (Integer val : vector.getDataList()) { if (i == 0) { sLoRetailer = val.intValue(); } else if (i == 1) { SHiRetailer = val.intValue(); } else if (i == 2) { sLoWholesaler = val.intValue(); } else if (i == 3) { SHiWholesaler = val.intValue(); } else if (i == 4) { sLoFactory = val.intValue(); } else if (i == 5) { SHiFactory = val.intValue(); } i++; } } } }}} 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 ;-) ==== Initial experiment setup ==== {{{ #!java commDriver = new com.heuristiclab.problems.externalevaluation.PollService(new ServerSocketListenerFactory(2112),1); commDriver.start(); }}} The number '''2112''' is important as it is the TCP port that our service will be listening on. This must be reachable by !HeuristicLab through a network connection (yes this means you can run !HeuristicLab on a different computer than your simulation model). You can of course choose another port (basically any number from 1 to 65535, but some numbers like 80 might be used by another application already). ==== Before each experiment run ==== {{{ #!java quality = 0; replications = 0; getMessage(); }}} The call to //getMessage()// will block, that means the simulation will wait here, until we have received a new set of parameters. ==== Before simulation run ==== leave this empty ==== After simulation run ==== {{{ #!java double penalty = 10000 * (1 + root.histWaitingTime.max()); if (root.histWaitingTime.max() < 0.01) quality += root.meanDailyCost(); else quality += root.meanDailyCost() + penalty; replications++; }}} This calls the cost function in the `Main` object called //meanDailyCost()// in this case. This is the target that we want to optimize (note that we add an increasing penalty term if the customer waiting time is above a certain level). ==== After iteration ==== {{{ #!java try { commDriver.sendQuality(currentSolution, quality / replications); } catch (IOException e) { } }}} 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. == Preparing the Optimization Problem in !HeuristicLab 3.3 ==