# Changes between Version 9 and Version 10 of Documentation/Howto/OptimizeAnyLogicModels

Ignore:
Timestamp:
10/02/15 21:37:56 (7 years ago)
Comment:

--

### Legend:

Unmodified
}}} = How to ... optimize parameters of !AnyLogic 6 simulation models = 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: [UsersHowtosOptimizingExternalApplications optimize parameters of simulation models.] 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. == !AnyLogic 6 Supply Chain model == === 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 by 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. The optimization goal is to minimize the sum of all costs in the supply chain while satisfying a certain service level for the end user. Each echelon in the chain uses an (s,S) ordering policy. In total we have 6 decision variables, one target variable and one constraint on the output. == Preparing the Simulation Model in !AnyLogic 6 == private void getMessage() { currentSolution = commDriver.getSolution(); if (currentSolution.getIntegerArrayVarsCount() > 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++; } 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; } } currentSolution = commDriver.getSolution(); if (currentSolution.getIntegerArrayVarsCount() > 0) { SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0); sRetailer = vector.getData(0); SRetailer = sRetailer + vector.getData(1); sWholesaler = vector.getData(2); SWholesaler = sWholesaler + vector.getData(3); sFactory = vector.getData(4); SFactory = sFactory + vector.getData(5); } } }}} 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. We have defined a method //getMessage()// that calls our library to get the next message from !HeuristicLab and extract the parameters. ==== Initial experiment setup ==== #!java try { commDriver.sendQuality(currentSolution, quality / replications); commDriver.sendQuality(currentSolution, quality / replications); } catch (IOException e) { } quality = 0; === 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. Click on the "New item" button in the toolbar (just below the File menu) and look for External Evaluation Problem. Name the problem "AnyLogic Supply Chain Problem". You can also double click the information icon right of the name box to edit the description. 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. In the Parameters box you'll see a number of different parameters. We need to configure the client that hosts the simulation and the solution encoding that we're using. ==== Client ==== Click on the parameter called "Clients" to see the list of clients. For each simulation model that participates in your experiment you have to add a client to this list. So this means you can optimize your parameters faster if you run the simulation on two separate computers (note however, that you need to use the "Parallel Engine" in your algorithm). Per default only one client is configured. Click on EvaluationServiceClient 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). Then we're done with the client. Next we'll configure the parameter Evaluator. Click on this parameter to get its list of parameters. Next we'll configure the parameter Encoding. 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! ==== Encoding ==== Define the problem's encoding in that you click on the pencil icon and select the IntegerVectorEncoding from the corresponding namespace. Click OK in the dialog and change the encoding's parameters: *  IntegerVector.Bounds: Set first column to "0" and second column to "100" - this will be lower and upper bound for every dimension (you can also specify one line for each dimension) * IntegerVector.Length: Set to "6" 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. 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). ==== 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 one parameter called Bounds. We can either add a value to it here, or add a parameter at a higher level (e.g. at the problem level). In this case other operators might also be configured with the same bounds, so it's beneficial to add and configure them at the problem level. Let's add a ValueParameter, choose IntMatrix for T and name it "Bounds" (the dialog to select a type for T may take some time to show up). Make the matrix 1 row and 3 columns and put "1", "101", "1" in those columns. The first column represents the minimum, the second column the exclusive maximum, and the third column holds a step size. So the values can only ever be multiples of that step within that range. 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 to our problem, just like Bounds but called Length and with "IntValue" as its type T. Give this parameter 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). You can now save your problem to a file. === 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. So create a new Genetic Algorithm by choosing "New" from the File menu. Next we create a new Genetic Algorithm by clicking the New Item button in the toolbar again. This time create a new genetic algorithm. In the problem tab click to open the problem and select the file that you just created. 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). Go to the parameters tab and select a mutation operator, e.g. "UniformOnePositionManipulator". == Optimizing the model ==