AnyLogic 9
Expand
Font size

Automating the optimization

We will use the AnyLogic Cloud optimization experiment to find the best values for the Smin and Smax parameters.

We will loop the model with the optimizer. The optimizer will perform a some search strategy in the parameter space and select parameters for the next set of runs based on the objectives of the previous runs.

To prepare the model for optimization, we need to finish creating formal metrics first.

Creating formal metrics

We will use elements of the Statistics palette to automatically collect samples of data during the simulation run and generate the aggregated information: mean, minimum, or maximum.

To create metrics

  1. Switch to the Main agent type.
  2. From the Analysis palette, add the Statistics element.
    Set its properties to the following:
    Name: statsRetailerStock
    Continuous
    Value: retailer.stock

    AnyLogic 9: The Statistics elements on the Main agent diagram

  3. Hold Ctrl, then drag and drop the Statistics element on the diagram to create a copy.
    Set its properties to the following:
    Name: statsOutOfOil
    Continuous
    Value: oilChangeStations.nOutOfOil()
  4. From the same Analysis palette, add the Output element.
    Set its properties to the following:
    Name: meanRetailerStock
    Value: statsRetailerStock.mean()

    AnyLogic 9: The Output elements on the Main agent diagram

  5. Hold Ctrl, then drag and drop the Output element on the diagram to create a copy.
    Set its properties to the following:
    Name: maxPercentOutOfOil
    Value: statsOutOfOil.max() / 500

Both output values are calculated at the end of a simulation run (as seen is the properties of the Output element). Therefore, they will get the last available statistics values covering the entire simulated period. And we will use these metrics in the optimization experiment.

Configuring the optimization

Now, when we have all parameters and metrics set up, let us configure the optimization experiment.

To set up the experiment

  1. Open the drop-down list next to the Run button and select Open Experiments. This will compile the model and take you to the AnyLogic Cloud execution environment.

    AnyLogic 9: The Open experiments button

    Initially you will see the Simulation experiment and the input parameters.
  2. Click New experiment.

    AnyLogic Cloud: The New experiment button

  3. Enter the name of the experiment: Optimization.
  4. Select Experiment type: Optimization.

    AnyLogic Cloud: The New experiment dialog

  5. Click Add.
  6. Next, set up the optimization inputs.
    Click the gear icon next to the newly created Optimization experiment to open the dashboard editor.

    AnyLogic Cloud: Opening the dashboard for configuration

  7. Set the control type of the Smin and Smax parameters to Discrete range.

    AnyLogic Cloud: Ranging the inputs

  8. Make the Stop time input visible, it will be useful for optimization.

    AnyLogic Cloud: Configuring the experiment settings

    Make the Max percent out of oil value visible in Requirements.
  9. Finally, let’s set up the optimization outputs.
    Scroll down the dashboard and click Add output.

    AnyLogic Cloud: Adding outputs

  10. In the new output, select the Single value type, click Select input, and select Smin.

    AnyLogic Cloud: Configuring the output

    It may seem strange to set up inputs in the Outputs section, but in the case of the optimization experiment, the optimal inputs are exactly the desired result of the simulation run.
  11. Save the dashboard.

    AnyLogic Cloud: The Save dashboard button button

Running the optimization

The optimization experiment is performed using AnyLogic Cloud.

To start the experiment

  1. Set the ranges and the steps:
    Smax: 100..500, step 10
    Smin: 0..400, step 10
  2. Set the Objective to Mean retailer stock: Minimize.
  3. Set the Number of iterations: 200.
  4. Set the requirement that Max percent out of oil is less than or equal to 0.01.

    AnyLogic Cloud: Preparing the optimization experiment

    Having Retailer stock as the objective and % out of oil as a requirement is natural for our problem: as the retailer, we want the stock to be as low as possible, while for the quality of service we have a hard upper bound of 1%. If we also wanted to minimize the % out of oil, we would have created a weighted sum objective function.
  5. Run the Optimization experiment and watch its progress.
  6. When the optimization is complete, you will see the results. They may differ from those shown in the figure.

    AnyLogic Cloud: The optimization results

    These results correspond to the simulation run that generates the best objective and is feasible, that is, satisfies the requirements.

Verifying the result

With the results visible in the outputs you created, you can enter them as parameters to the simulation experiment and verify their feasibility.

However, there is no need to go to another experiment: you can perform a single run of a simulation experiment right from the Optimization experiment screen. Staying in the Optimization experiment, click the Play button: the animated run is displayed in the dialog.

AnyLogic Cloud: The animated simulation experiment

You can manually adjust the Smax and Smin parameters to the feasible values and see how this affects the simulation.

How can we improve this article?