Free cookie consent management tool by TermsFeed Policy Generator

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


Ignore:
Timestamp:
10/02/15 21:37:56 (9 years ago)
Author:
abeham
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/Howto/OptimizeAnyLogicModels

    v9 v10  
    1 = Documentation not up-to-date =
    2 {{{
    3 #!html
    4 <p style="color: darkred; font-weight: bold">
    5 As of HeuristicLab 3.3.11 configuring the ExternalEvaluationProblem has been made considerably easier. This is not yet reflected in this documentation.
    6 </p>
    7 }}}
    8 
    91= How to ... optimize parameters of !AnyLogic 6 simulation models =
    102
     
    124We 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.
    135
    14 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.]
     6This 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.
    157
    168== !AnyLogic 6 Supply Chain model ==
     
    1810
    1911=== Optimization Problem ===
    20 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.
     12The 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.
    2113
    2214== Preparing the Simulation Model in !AnyLogic 6 ==
     
    5345
    5446private void getMessage() {
    55         currentSolution = commDriver.getSolution();
    56         if (currentSolution.getIntegerArrayVarsCount() > 0) {
    57                 SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0);
    58                 int i = 0;
    59                 for (Integer val : vector.getDataList()) {
    60                         if (i == 0) {
    61                                 sLoRetailer = val.intValue();
    62                         } else if (i == 1) {
    63                                 SHiRetailer = val.intValue();
    64                         } else if (i == 2) {
    65                                 sLoWholesaler = val.intValue();
    66                         } else if (i == 3) {
    67                                 SHiWholesaler = val.intValue();
    68                         } else if (i == 4) {
    69                                 sLoFactory = val.intValue();
    70                         } else if (i == 5) {
    71                                 SHiFactory = val.intValue();
    72                         }
    73                         i++;
    74                 }
    75                 int h;
    76                 if (sLoRetailer > SHiRetailer) { h = sLoRetailer; sLoRetailer = SHiRetailer; SHiRetailer = h; }
    77                 if (sLoWholesaler > SHiWholesaler) { h = sLoWholesaler; sLoWholesaler = SHiWholesaler; SHiWholesaler = h; }
    78                 if (sLoFactory > SHiFactory) { h = sLoFactory; sLoFactory = SHiFactory; SHiFactory = h; }
    79         }
     47  currentSolution = commDriver.getSolution();
     48  if (currentSolution.getIntegerArrayVarsCount() > 0) {
     49    SolutionMessage.IntegerArrayVariable vector = currentSolution.getIntegerArrayVars(0);
     50    sRetailer = vector.getData(0);
     51    SRetailer = sRetailer + vector.getData(1);
     52    sWholesaler = vector.getData(2);
     53    SWholesaler = sWholesaler + vector.getData(3);
     54    sFactory = vector.getData(4);
     55    SFactory = sFactory + vector.getData(5);
     56  }
    8057}
    8158}}}
    8259
    83 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.
     60We have defined a method //getMessage()// that calls our library to get the next message from !HeuristicLab and extract the parameters.
    8461
    8562==== Initial experiment setup ====
     
    12198#!java
    12299try {
    123         commDriver.sendQuality(currentSolution, quality / replications);
     100  commDriver.sendQuality(currentSolution, quality / replications);
    124101} catch (IOException e) { }
    125102quality = 0;
     
    136113
    137114=== Defining the problem ===
    138 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.
     115Click 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.
    139116
    140 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.
    141 
    142 In the Parameters box you'll see a number of different parameters. Let's configure the client first.
     117In 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.
    143118
    144119==== Client ====
    145120Click 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.
    146121
    147 Next we'll configure the parameter Evaluator. Click on this parameter to get its list of parameters.
     122Next we'll configure the parameter Encoding. Click on this parameter to get its list of parameters.
    148123
    149 ==== Evaluator ====
    150 In 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!
     124==== Encoding ====
     125Define 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:
     126 *  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)
     127 * IntegerVector.Length: Set to "6"
    151128
    152 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.
     129So 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).
    153130
    154 ==== Operators ====
    155 By clicking on the yellow plus button add following operators from the !HeuristicLab.Encodings.!IntegerVectorEncoding namespace:
    156  * `SinglePointCrossover`
    157  * `UniformOnePositionManipulator`
    158 
    159 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<T>`, 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.
    160 
    161 Now click on and configure the last parameter called !SolutionCreator
    162 
    163 ==== !SolutionCreator ====
    164 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<T>` 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).
     131You can now save your problem to a file.
    165132
    166133=== Choosing an Algorithm ===
    167134
    168 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.
     135Next 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.
    169136
    170 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).
     137Go to the parameters tab and select a mutation operator, e.g. "UniformOnePositionManipulator".
    171138
    172139== Optimizing the model ==