Line sampling#

Description#

This example shows how to perform a line sampling parameter scan using a LineSamplingGenerator and a TemplateEvaluator.

The template simulation script evaluates a simple function of two parameters \(x_0\) and \(x_1\):

\[f(x_0, x_1) = -(x_0 + 10 \cos(x_0)) (x_1 + 5\cos(x_1))\]

and stores the outcome in a text file result.txt. The analysis_func simply reads the value in this file.

You can adapt this example to your needs by replacing this basic template with an actual simulation and writing the corresponding analysis function. See see Running simulations for more details.

The LineSamplingGenerator generates a multidimensional distribution of samples where, as opposed to the GridSamplingGenerator, only one VaryingParameter is varied at a time while the other remain with their default values. Each parameter varies between its lower and upper bound in n_steps equally-spaced steps. In this case, where \(x_0\) and \(x_1\) have a default values of \(5\) and \(6\), respectively, and lower and upper bounds \(l_b=0\) and \(u_b=15\), the grid of samples look like:

../_images/ps_line_sampling-1.png

Scripts#

The two files needed to run this example should be located in the same folder (named e.g., example):

example
├── run_example.py
└── template_simulation_script.py

The example is executed by running

python run_example.py

You can find both example scripts below.

run_example.py (download)#
"""Basic example of parallel line sampling with simulations."""

from gest_api.vocs import VOCS, ContinuousVariable
from optimas.generators import LineSamplingGenerator
from optimas.evaluators import TemplateEvaluator
from optimas.explorations import Exploration


def analyze_simulation(simulation_directory, output_params):
    """Analyze the simulation output.

    This method analyzes the output generated by the simulation to
    obtain the value of the optimization objective and other observables.
    The value of these parameters has to be given to the
    `output_params` dictionary.

    Parameters
    ----------
    simulation_directory : str
        Path to the simulation folder where the output was generated.
    output_params : dict
        Dictionary where the value of the objectives and observables
        will be stored. There is one entry per parameter, where the key
        is the name of the parameter given by the user.

    Returns
    -------
    dict
        The `output_params` dictionary with the results from the analysis.

    """
    # Read back result from file
    with open("result.txt") as f:
        result = float(f.read())
    # Fill in output parameters.
    output_params["f"] = result
    return output_params


# Create VOCS object defining variables, objectives.
vocs = VOCS(
    variables={
        "x0": ContinuousVariable(domain=[0.0, 15.0], default_value=5.0),
        "x1": ContinuousVariable(domain=[0.0, 15.0], default_value=6.0),
    },
    objectives={"f": "MAXIMIZE"},
)


# Create generator.
gen = LineSamplingGenerator(vocs=vocs, n_steps=[5, 7])


# Create evaluator.
ev = TemplateEvaluator(
    sim_template="template_simulation_script.py",
    analysis_func=analyze_simulation,
)


# Create exploration.
exp = Exploration(
    generator=gen, evaluator=ev, max_evals=10, sim_workers=4, run_async=True
)


# To safely perform exploration, run it in the block below (this is needed
# for some flavours of multiprocessing, namely spawn and forkserver)
if __name__ == "__main__":
    exp.run()
template_simulation_script.py (download)#
"""Simple template script used for demonstration.

The script evaluates an analytical expression and stores the results in a
`result.txt` file that is later read by the analysis function.
"""

import numpy as np

# 2D function with multiple minima
result = -({{x0}} + 10 * np.cos({{x0}})) * ({{x1}} + 5 * np.cos({{x1}}))

with open("result.txt", "w") as f:
    f.write("%f" % result)