gemseo / core

Show inherited members

scenario module

The base class for the scenarios.

class gemseo.core.scenario.Scenario(disciplines, formulation, objective_name, design_space, name=None, grammar_type=GrammarType.JSON, maximize_objective=False, **formulation_options)[source]

Bases: MDODiscipline

Base class for the scenarios.

The instantiation of a Scenario creates an OptimizationProblem, by linking MDODiscipline objects with an MDOFormulation and defining both the objective to minimize or maximize and the DesignSpace on which to solve the problem. Constraints can also be added to the OptimizationProblem with the Scenario.add_constraint() method, as well as observables with the Scenario.add_observable() method.

Then, the Scenario.execute() method takes a driver (see DriverLibrary) with options as input data and uses it to solve the optimization problem. This driver is in charge of executing the multidisciplinary process.

To view the results, use the Scenario.post_process() method after execution with one of the available post-processors that can be listed by Scenario.posts.

Initialize self. See help(type(self)) for accurate signature.

Parameters:
  • disciplines (Sequence[MDODiscipline]) – The disciplines used to compute the objective, constraints and observables from the design variables.

  • formulation (str) – The class name of the MDOFormulation, e.g. "MDF", "IDF" or "BiLevel".

  • objective_name (str | Sequence[str]) – The name(s) of the discipline output(s) used as objective. If multiple names are passed, the objective will be a vector.

  • design_space (DesignSpace) – The search space including at least the design variables (some formulations requires additional variables, e.g. IDF with the coupling variables).

  • name (str | None) – The name to be given to this scenario. If None, use the name of the class.

  • grammar_type (MDODiscipline.GrammarType) –

    The grammar for the scenario and the MDO formulation.

    By default it is set to “JSONGrammar”.

  • maximize_objective (bool) –

    Whether to maximize the objective.

    By default it is set to False.

  • **formulation_options (Any) – The options of the MDOFormulation.

class DifferentiationMethod(value)

Bases: StrEnum

The differentiation methods.

CENTERED_DIFFERENCES = 'centered_differences'
COMPLEX_STEP = 'complex_step'
FINITE_DIFFERENCES = 'finite_differences'
NO_DERIVATIVE = 'no_derivative'
USER_GRAD = 'user'
add_constraint(output_name, constraint_type=ConstraintType.EQ, constraint_name=None, value=None, positive=False, **kwargs)[source]

Add a design constraint.

This constraint is in addition to those created by the formulation, e.g. consistency constraints in IDF.

The strategy of repartition of the constraints is defined by the formulation.

Parameters:
  • output_name (str | Sequence[str]) – The names of the outputs to be used as constraints. For instance, if “g_1” is given and constraint_type=”eq”, g_1=0 will be added as constraint to the optimizer. If several names are given, a single discipline must provide all outputs.

  • constraint_type (MDOFunction.ConstraintType) –

    The type of constraint.

    By default it is set to “eq”.

  • constraint_name (str | None) – The name of the constraint to be stored. If None, the name of the constraint is generated from the output name.

  • value (float | None) – The value for which the constraint is active. If None, this value is 0.

  • positive (bool) –

    If True, the inequality constraint is positive.

    By default it is set to False.

Raises:

ValueError – If the constraint type is neither ‘eq’ nor ‘ineq’.

Return type:

None

add_observable(output_names, observable_name=None, discipline=None)[source]

Add an observable to the optimization problem.

The repartition strategy of the observable is defined in the formulation class. When more than one output name is provided, the observable function returns a concatenated array of the output values.

Parameters:
  • output_names (Sequence[str]) – The names of the outputs to observe.

  • observable_name (Sequence[str] | None) – The name to be given to the observable. If None, the output name is used by default.

  • discipline (MDODiscipline | None) – The discipline used to build the observable function. If None, detect the discipline from the inner disciplines.

Return type:

None

get_available_driver_names()[source]

The available drivers.

Return type:

list[str]

get_disciplines_statuses()[source]

Retrieve the statuses of the disciplines.

Returns:

The statuses of the disciplines.

Return type:

dict[str, str]

get_expected_dataflow()[source]

Return the expected data exchange sequence.

This method is used for the XDSM representation.

The default expected data exchange sequence is an empty list.

See also

MDOFormulation.get_expected_dataflow

Returns:

The data exchange arcs.

Return type:

list[tuple[MDODiscipline, MDODiscipline, list[str]]]

get_expected_workflow()[source]

Return the expected execution sequence.

This method is used for the XDSM representation.

The default expected execution sequence is the execution of the discipline itself.

See also

MDOFormulation.get_expected_workflow

Returns:

The expected execution sequence.

Return type:

LoopExecSequence

get_optim_variable_names()[source]

A convenience function to access the optimization variables.

Returns:

The optimization variables of the scenario.

Return type:

list[str]

get_result(name='', **options)[source]

Return the result of the scenario execution.

Parameters:
  • name (str) –

    The class name of the ScenarioResult. If empty, use a default one (see create_scenario_result()).

    By default it is set to “”.

  • **options (Any) – The options of the ScenarioResult.

Returns:

The result of the scenario execution.

Return type:

ScenarioResult

static is_scenario()[source]

Indicate if the current object is a Scenario.

Returns:

True if the current object is a Scenario.

Return type:

bool

post_process(post_name, **options)[source]

Post-process the optimization history.

Parameters:
  • post_name (str) – The name of the post-processor, i.e. the name of a class inheriting from OptPostProcessor.

  • **options (OptPostProcessorOptionType | Path) – The options for the post-processor.

Returns:

The post-processing instance related to the optimization scenario.

Return type:

OptPostProcessor

print_execution_metrics()[source]

Print the total number of executions and cumulated runtime by discipline.

Return type:

None

save_optimization_history(file_path, file_format='hdf5', append=False)[source]

Save the optimization history of the scenario to a file.

Parameters:
  • file_path (str | Path) – The path of the file to save the history.

  • file_format (str) –

    The format of the file, either “hdf5” or “ggobi”.

    By default it is set to “hdf5”.

  • append (bool) –

    If True, the history is appended to the file if not empty.

    By default it is set to False.

Raises:

ValueError – If the file format is not correct.

Return type:

None

set_differentiation_method(method=DifferentiationMethod.USER_GRAD, step=1e-06, cast_default_inputs_to_complex=False)[source]

Set the differentiation method for the process.

When the selected method to differentiate the process is complex_step the DesignSpace current value will be cast to complex128; additionally, if the option cast_default_inputs_to_complex is True, the default inputs of the scenario’s disciplines will be cast as well provided that they are ndarray with dtype float64.

Parameters:
  • method (DifferentiationMethod) –

    The method to use to differentiate the process.

    By default it is set to “user”.

  • step (float) –

    The finite difference step.

    By default it is set to 1e-06.

  • cast_default_inputs_to_complex (bool) –

    Whether to cast all float default inputs of the scenario’s disciplines if the selected method is "complex_step".

    By default it is set to False.

Return type:

None

set_optimization_history_backup(file_path, each_new_iter=False, each_store=True, erase=False, pre_load=False, generate_opt_plot=False)[source]

Set the backup file for the optimization history during the run.

Parameters:
  • file_path (str | Path) – The path to the file to save the history.

  • each_new_iter (bool) –

    Whether the backup file is updated at every iteration of the optimization to store the database.

    By default it is set to False.

  • each_store (bool) –

    Whether the backup file is updated at every function call to store the database.

    By default it is set to True.

  • erase (bool) –

    Whether the backup file is erased before the run.

    By default it is set to False.

  • pre_load (bool) –

    Whether the backup file is loaded before run, useful after a crash.

    By default it is set to False.

  • generate_opt_plot (bool) –

    Whether to plot the optimization history view at each iteration. The plots will be generated only after the first two iterations.

    By default it is set to False.

Raises:

ValueError – If both erase and pre_load are True.

Return type:

None

to_dataset(name='', categorize=True, opt_naming=True, export_gradients=False)[source]

Export the database of the optimization problem to a Dataset.

The variables can be classified into groups: Dataset.DESIGN_GROUP or Dataset.INPUT_GROUP for the design variables and Dataset.FUNCTION_GROUP or Dataset.OUTPUT_GROUP for the functions (objective, constraints and observables).

Parameters:
  • name (str) –

    The name to be given to the dataset. If empty, use the name of the OptimizationProblem.database.

    By default it is set to “”.

  • categorize (bool) –

    Whether to distinguish between the different groups of variables. Otherwise, group all the variables in Dataset.PARAMETER_GROUP`.

    By default it is set to True.

  • opt_naming (bool) –

    Whether to use Dataset.DESIGN_GROUP and Dataset.FUNCTION_GROUP as groups. Otherwise, use Dataset.INPUT_GROUP and Dataset.OUTPUT_GROUP.

    By default it is set to True.

  • export_gradients (bool) –

    Whether to export the gradients of the functions (objective function, constraints and observables) if the latter are available in the database of the optimization problem.

    By default it is set to False.

Returns:

A dataset built from the database of the optimization problem.

Return type:

Dataset

xdsmize(monitor=False, directory_path='.', log_workflow_status=False, file_name='xdsm', show_html=False, save_html=True, save_json=False, save_pdf=False, pdf_build=True, pdf_cleanup=True, pdf_batchmode=True)[source]

Create a XDSM diagram of the scenario.

Parameters:
  • monitor (bool) –

    Whether to update the generated file at each discipline status change.

    By default it is set to False.

  • log_workflow_status (bool) –

    Whether to log the evolution of the workflow’s status.

    By default it is set to False.

  • directory_path (str | Path) –

    The path of the directory to save the files. If show_html=True and output_directory_path=None, the HTML file is stored in a temporary directory.

    By default it is set to “.”.

  • file_name (str) –

    The file name without the file extension.

    By default it is set to “xdsm”.

  • show_html (bool) –

    Whether to open the web browser and display the XDSM.

    By default it is set to False.

  • save_html (bool) –

    Whether to save the XDSM as a HTML file.

    By default it is set to True.

  • save_json (bool) –

    Whether to save the XDSM as a JSON file.

    By default it is set to False.

  • save_pdf (bool) –

    Whether to save the XDSM as a PDF file.

    By default it is set to False.

  • pdf_build (bool) –

    Whether the standalone pdf of the XDSM will be built.

    By default it is set to True.

  • pdf_cleanup (bool) –

    Whether pdflatex built files will be cleaned up after build is complete.

    By default it is set to True.

  • pdf_batchmode (bool) –

    Whether pdflatex is run in batchmode.

    By default it is set to True.

Returns:

A view of the XDSM if monitor is False.

Return type:

XDSM | None

ALGO = 'algo'
ALGO_OPTIONS = 'algo_options'
L_BOUNDS = 'l_bounds'
U_BOUNDS = 'u_bounds'
X_0 = 'x_0'
activate_input_data_check: ClassVar[bool] = True

Whether to check the input data respect the input grammar.

activate_output_data_check: ClassVar[bool] = True

Whether to check the output data respect the output grammar.

cache: AbstractCache | None

The cache containing one or several executions of the discipline according to the cache policy.

data_processor: DataProcessor

A tool to pre- and post-process discipline data.

property design_space: DesignSpace

The design space on which the scenario is performed.

exec_for_lin: bool

Whether the last execution was due to a linearization.

formulation: MDOFormulation

The MDO formulation.

formulation_name: str

The name of the MDO formulation.

input_grammar: BaseGrammar

The input grammar.

jac: MutableMapping[str, MutableMapping[str, ndarray | csr_array | JacobianOperator]]

The Jacobians of the outputs wrt inputs.

The structure is {output: {input: matrix}}.

name: str

The name of the discipline.

optimization_result: OptimizationResult | None

The optimization result if the scenario has been executed; otherwise None.

output_grammar: BaseGrammar

The output grammar.

property post_factory: PostFactory

The factory for post-processors if any.

property posts: list[str]

The available post-processors.

re_exec_policy: ReExecutionPolicy

The policy to re-execute the same discipline.

residual_variables: dict[str, str]

The output variables mapping to their inputs, to be considered as residuals; they shall be equal to zero.

run_solves_residuals: bool

Whether the run method shall solve the residuals.

property use_standardized_objective: bool

Whether to use the standardized objective for logging and post-processing.

The objective is OptimizationProblem.objective.