ctwrap Strategy

The strategy module defines Strategy and derived objects that specify strategies used in batch job simulations.

Batch job strategies are defined using YAML syntax in a ctwrap YAML configuration file. The strategy block defines simulation strategies, where valid entries contain lower case versions of valid Strategy objects (e.g. Sequence, Matrix). within each batch job strategy, parameters to be varied are identified by key/value pairs.

Usage

An example for different options to define a parameter variation of a single parameter foo, - a Sequence, - is given as:

strategy:
  sequence-1: # array of numeric values
    foo: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
  sequence-2: # values generated by numpy.linspace
    foo: { mode: linspace , limits: [0.1, 0.3], npoints: 3 }
  sequence-3: # values generated by numpy.arange
    bar: { mode: arange, limits: [2, -1], step: -1 }

Strategy objects are generated automatically by ctwrap when a SimulationHandler object is instantiated.

Class Definitions

class ctwrap.strategy.Strategy(value={}, name=None)[source]

Base class for batch simulation strategies

The base class Strategy defines methods common to all derived objects. While base objects are not viable strategies themselves, the factory loader Strategy.load() can be used to load derived objects.

As an example, a Sequence object is created from a dictionary strategy_dict that contains a strategy specification sequence-1:

# create a 'Sequence' object specified by 'sequence-1'
seq = ctwrap.Strategy.load(strategy_dict, 'sequence-1')
classmethod load(strategy, name=None)[source]

Factory loader for strategy objects

Example: The following code creates a Sequence object strategy that varies the variable foobar. The dictionary key at the top level has to contain a lower-case string corresponding to a strategy class:

strategy = ctwrap.Strategy.load({sequence_test: {'foobar': [0, 1, 2, 3]}})
Parameters
  • strategy (Dict[str, Dict]) – Dictionary of batch job strategies, where lower-case keys specify the batch simulation strategy and the value holds parameters. Except for the case, the key needs to match the name of a Strategy object.

  • name (Optional[str]) – Name of strategy (can be None if there is only one strategy)

Return type

Strategy

Returns

Instantiated Strategy object

property definition

Definition of batch simulation strategy

property variations

Parameter values to be tested grouped by task

Example: The following code applies a parameter variation to the Sequence object strategy:

# define parameter variation
foo = {'foobar': [0, 1, 2, 3]}
strategy = ctwrap.Sequence(foo)

# generate dictionary of varied parameter values
vars = strategy.variations
Returns

Nested dictionary of parameter values

configurations(defaults)[source]

Configurations to be tested grouped by task

Example: The following code applies a parameter variation to the Sequence object strategy based on a parameter set defaults:

# define parameter variation
foo = {'foobar': [0, 1, 2, 3]}
strategy = ctwrap.Sequence(foo)

# generate dictionary of configurations based on default parameters
defaults = {'foobar': 1, 'spam': 2.0, 'eggs': 3.14}
confs = strategy.configurations(defaults)
Parameters

defaults (Dict[str, Any]) – Dictionary containing default parameters

Returns

Nested dictionary of configurations

create_tasks(defaults)[source]

Create list of parameter sets for a parameter variation

Example: The following code applies a parameter variation to the Sequence object strategy based on a parameter set defaults:

# define parameter variation
foo = {'foobar': [0, 1, 2, 3]}
strategy = ctwrap.Sequence(foo)

# generate list of tasks based on default parameters
defaults = {'foobar': 1, 'spam': 2.0, 'eggs': 3.14}
tasks = strategy.create_tasks(defaults)
Parameters

defaults (Dict[str, Any]) – Dictionary containing default parameters

Return type

List[Dict[str, Any]]

Returns

List of dictionaries with parameter variation

base(task)[source]

Return basis for a restart

class ctwrap.strategy.Sequence(sweep, name=None)[source]

Variation of a single parameter

sequence: # example for a batch sequence
  foo: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
Parameters
  • sweep (Dict[str, Any]) – Dictionary specifying single parameter sequence

  • name (Optional[str]) – Name of batch job strategy

class ctwrap.strategy.Legacy(sweep, name=None)[source]
static convert(items)[source]

Create Sequence from ctwrap 0.1.0 syntax

class ctwrap.strategy.Matrix(matrix, name=None)[source]

Variation of multiple parameters

matrix: # example for a batch matrix
  foo: [0.1, 0.2, 0.3]
  bar: [2, 1, 0]
Parameters
  • matrix (Dict[str, Any]) – Dictionary specifying multiple parameter sequences

  • name (Optional[str]) – Name of batch job strategy

class ctwrap.strategy.Sobol(ranges, name=None)[source]

Pseudo-randomly sampled parameter space via Sobol sequence

Not implemented; there is prior work in Ayoobi and Schoegl, PROCI 2015