idmtools.builders.simulation_builder module

class idmtools.builders.simulation_builder.SimulationBuilder

Bases: object

Class that represents an experiment builder.

Examples

import os
import sys

from idmtools.assets import AssetCollection
from idmtools.builders import SimulationBuilder
from idmtools.core.platform_factory import platform
from idmtools.entities.experiment import Experiment
from idmtools.entities.templated_simulation import TemplatedSimulations
from idmtools_models.python.json_python_task import JSONConfiguredPythonTask
from idmtools_test import COMMON_INPUT_PATH

with platform('BELEGOST'):
    base_task = JSONConfiguredPythonTask(
        script_path=os.path.join(COMMON_INPUT_PATH, "compsplatform", "working_model.py"),
        # add common assets from existing collection
        common_assets=AssetCollection.from_id('41c1b14d-0a04-eb11-a2c7-c4346bcb1553', as_copy=True)
    )

    ts = TemplatedSimulations(base_task=base_task)
    # sweep parameter
    builder = SimulationBuilder()
    builder.add_sweep_definition(JSONConfiguredPythonTask.set_parameter_partial("min_x"), range(-2, 0))
    builder.add_sweep_definition(JSONConfiguredPythonTask.set_parameter_partial("max_x"), range(1, 3))
    ts.add_builder(builder)

    e = Experiment.from_template(ts, name=os.path.split(sys.argv[0])[1])
    e.run(wait_until_done=True)
    # use system status as the exit code
    sys.exit(0 if e.succeeded else -1)

Add tags with builder callbacks:

def update_sim(sim, parameter, value):
    sim.task.set_parameter(parameter, value)
    # set sim tasks,
    return {'custom': 123, parameter:value)

builder = SimulationBuilder()
set_run_number = partial(update_sim, param="Run_Number")
builder.add_sweep_definition(set_run_number, range(0, 2))
# create experiment from builder
exp = Experiment.from_builder(builder, task, name=expname)
SIMULATION_ATTR = 'simulation'
add_sweep_definition(function: Union[Callable[[idmtools.entities.simulation.Simulation, Any], Dict[str, Any]], functools.partial], values: Union[List[Any], Iterable])

Add a parameter sweep definition. A sweep definition is composed of a function and a list of values to call the function with.

Parameters
  • function – The sweep function, which must include a simulation parameter (or whatever is specified in SIMULATION_ATTR). The function also must include EXACTLY ONE free parameter, which the values will be passed to. The function can also be a partial–any Callable type will work.

  • values – The list of values to call the function with.

Examples

Examples of valid function:

def myFunction(simulation, parameter):
    pass

How to deal with functions requiring more than one parameter? Consider the following function:

python
def myFunction(simulation, a, b):
    pass

Partial solution:

python
from functools import partial
func = partial(myFunction, a=3)
eb.add_sweep_definition(func, [1,2,3])

Callable class solution:

class setP:
    def __init__(self, a):
        self.a = a

    def __call__(self, simulation, b):
        return param_update(simulation, self.a, b)

eb.add_sweep_definition(setP(3), [1,2,3])