gemseo / formulations

# bilevel module¶

A Bi-level formulation.

class gemseo.formulations.bilevel.BiLevel(disciplines, objective_name, design_space, maximize_objective=False, main_mda_name='MDAChain', inner_mda_name='MDAJacobi', parallel_scenarios=False, multithread_scenarios=True, apply_cstr_tosub_scenarios=True, apply_cstr_to_system=True, reset_x0_before_opt=False, grammar_type='JSONGrammar', **main_mda_options)[source]

Bases: MDOFormulation

A bi-level formulation.

This formulation draws an optimization architecture that involves multiple optimization problems to be solved to obtain the solution of the MDO problem.

Here, at each iteration on the global design variables, the bi-level MDO formulation implementation performs:

1. a first MDA to compute the coupling variables,

2. several disciplinary optimizations on the local design variables in parallel,

3. a second MDA to update the coupling variables.

Parameters:
• disciplines (list[MDODiscipline]) – The disciplines.

• objective_name (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 design space.

• maximize_objective (bool) –

Whether to maximize the objective.

By default it is set to False.

• main_mda_name (str) –

The name of the class used for the main MDA, typically the MDAChain, but one can force to use MDAGaussSeidel for instance.

By default it is set to “MDAChain”.

• inner_mda_name (str) –

The name of the class used for the inner-MDA of the main MDA, if any; typically when the main MDA is an MDAChain.

By default it is set to “MDAJacobi”.

• parallel_scenarios (bool) –

Whether to run the sub-scenarios in parallel.

By default it is set to False.

If True and parallel_scenarios=True, the sub-scenarios are run in parallel using multi-threading; if False and parallel_scenarios=True, multiprocessing is used.

By default it is set to True.

• apply_cstr_tosub_scenarios (bool) –

Whether the add_constraint() method adds the constraint to the optimization problem of the sub-scenario capable of computing the constraint.

By default it is set to True.

• apply_cstr_to_system (bool) –

Whether the add_constraint() method adds the constraint to the optimization problem of the system scenario.

By default it is set to True.

• reset_x0_before_opt (bool) –

Whether to restart the sub optimizations from the initial guesses, otherwise warm start them.

By default it is set to False.

• grammar_type (str) –

The type of the input and output grammars, either MDODiscipline.JSON_GRAMMAR_TYPE or MDODiscipline.SIMPLE_GRAMMAR_TYPE.

By default it is set to “JSONGrammar”.

• **main_mda_options (Any) – The options of the main MDA, which may include those of the inner-MDA.

add_constraint(output_name, constraint_type='eq', constraint_name=None, value=None, positive=False, levels=None)[source]

Add a constraint to the formulation.

Parameters:
• output_name (str) – The name of the output to be used as a constraint. For instance, if g_1 is given and constraint_type=”eq”, g_1=0 will be added as a constraint to the optimizer.

• constraint_type (str) –

The type of constraint, either “eq” for equality constraint or “ineq” for inequality constraint.

By default it is set to “eq”.

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

• value (float | None) – The value of activation of the constraint. If None, the value is equal to 0.

• positive (bool) –

Whether to consider an inequality constraint as positive.

By default it is set to False.

• levels (list[str] | None) – The levels at which the constraint is to be added (sublist of Bilevel.LEVELS). By default, the policy set at the initialization of the formulation is enforced.

Raises:

ValueError – When the constraint levels are not a sublist of BiLevel.LEVELS.

Return type:

None

Add an observable to the optimization problem.

The repartition strategy of the observable is defined in the formulation class.

Parameters:
• output_names (str | Sequence[str]) – The name(s) of the output(s) to observe.

• observable_name (str | None) – The name of the observable.

• discipline (MDODiscipline | None) – The discipline computing the observed outputs. If None, the discipline is detected from inner disciplines.

Return type:

None

classmethod get_default_sub_options_values(**options)[source]
Raises:

ValueError – When the MDA name is not provided.

Parameters:

options (str) –

Return type:

Mapping[str, str | int | float | bool | None] | None

get_expected_dataflow()[source]

Get the expected data exchange sequence.

This method is used for the XDSM representation and can be overloaded by subclasses.

Returns:

The expected sequence of data exchange where the i-th item is described by the starting discipline, the ending discipline and the coupling variables.

Return type:
get_expected_workflow()[source]

Get the expected sequence of execution of the disciplines.

This method is used for the XDSM representation and can be overloaded by subclasses.

For instance:

• [A, B] denotes the execution of A, then the execution of B

• (A, B) denotes the concurrent execution of A and B

• [A, (B, C), D] denotes the execution of A, then the concurrent execution of B and C, then the execution of D.

Returns:

A sequence of elements which are either an ExecutionSequence or a tuple of ExecutionSequence for concurrent execution.

Return type:
get_optim_variables_names()

Get the optimization unknown names to be provided to the optimizer.

This is different from the design variable names provided by the user, since it depends on the formulation, and can include target values for coupling for instance in IDF.

Returns:

The optimization variable names.

Return type:

list[str]

get_sub_disciplines(recursive=False)

Accessor to the sub-disciplines.

This method lists the sub scenarios’ disciplines. It will list up to one level of disciplines contained inside another one unless the recursive argument is set to True.

Parameters:

recursive (bool) –

If True, the method will look inside any discipline that has other disciplines inside until it reaches a discipline without sub-disciplines, in this case the return value will not include any discipline that has sub-disciplines. If False, the method will list up to one level of disciplines contained inside another one, in this case the return value may include disciplines that contain sub-disciplines.

By default it is set to False.

Returns:

The sub-disciplines.

Return type:
classmethod get_sub_options_grammar(**options)[source]

Return the grammar of the selected MDA.

Parameters:

**options (str) – The options of the BiLevel formulation.

Returns:

The MDA grammar.

Raises:

ValueError – When the MDA name is not provided.

Return type:

JSONGrammar

get_sub_scenarios()

List the disciplines that are actually scenarios.

Returns:

The scenarios.

Return type:
get_top_level_disc()[source]

Return the disciplines which inputs are required to run the scenario.

A formulation seeks to compute the objective and constraints from the input variables. It structures the optimization problem into multiple levels of disciplines. The disciplines directly depending on these inputs are called top level disciplines.

By default, this method returns all disciplines. This method can be overloaded by subclasses.

Returns:

The top level disciplines.

Return type:

Mask a vector from a subset of names, with respect to a set of names.

This method eventually swaps the order of the values if the order of the data names is inconsistent between these sets.

Parameters:
• masking_data_names (Iterable[str]) – The names of the kept data.

• all_data_names (Iterable[str] | None) – The set of all names. If None, use the design variables stored in the design space.

Returns:

The masked version of the input vector.

Raises:
• IndexError – when the sizes of variables are inconsistent.

• ValueError – when the names of variables are inconsistent.

Return type:

ndarray

get_x_names_of_disc(discipline)

Get the design variables names of a given discipline.

Parameters:

discipline (MDODiscipline) – The discipline.

Returns:

The names of the design variables.

Return type:

list[str]

Mask a vector from a subset of names, with respect to a set of names.

This method eventually swaps the order of the values if the order of the data names is inconsistent between these sets.

Parameters:
• masking_data_names (Iterable[str]) – The names of the kept data.

• x_vect (ndarray) – The vector to mask.

• all_data_names (Iterable[str] | None) – The set of all names. If None, use the design variables stored in the design space.

Returns:

The masked version of the input vector.

Raises:

IndexError – when the sizes of variables are inconsistent.

Return type:

ndarray

Unmask a vector from a subset of names, with respect to a set of names.

This method eventually swaps the order of the values if the order of the data names is inconsistent between these sets.

Parameters:
• masking_data_names (Iterable[str]) – The names of the kept data.

• all_data_names (Iterable[str] | None) – The set of all names. If None, use the design variables stored in the design space.

• x_full (ndarray) – The default values for the full vector. If None, use the zero vector.

Returns:

The vector related to the input mask.

Raises:

IndexError – when the sizes of variables are inconsistent.

Return type:

ndarray

LEVELS = ('system', 'sub-scenarios')
NAME: ClassVar[str] = 'MDOFormulation'

The name of the MDO formulation.

SUBSCENARIOS_LEVEL = 'sub-scenarios'
SYSTEM_LEVEL = 'system'
property design_space: DesignSpace

The design space on which the formulation is applied.

property disciplines: list[gemseo.core.discipline.MDODiscipline]

The disciplines of the MDO process.

property mda1: MDODiscipline

The MDA1 instance.

property mda2: MDODiscipline

The MDA2 instance.

opt_problem: OptimizationProblem

The optimization problem generated by the formulation from the disciplines.