gemseo / core

Hide inherited members

base_formulation module

The base class for all formulations.

class gemseo.core.base_formulation.BaseFormulation(disciplines, objective_name, design_space, maximize_objective=False, grammar_type=GrammarType.JSON, **options)[source]

Bases: object

Base MDO formulation class to be extended in subclasses for use.

This class creates the MDOFunction instances computing the constraints, objective and observables from the disciplines and add them to the attached opt_problem.

It defines the multidisciplinary process, i.e. dataflow and workflow, implicitly.

By default,

  • the objective is minimized,

  • the type of a constraint is equality,

  • the activation value of a constraint is 0.

The link between the instances of MDODiscipline, the design variables and the names of the discipline outputs used as constraints, objective and observables is made with the MDODisciplineAdapterGenerator, which generates instances of MDOFunction from the disciplines.

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

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

  • maximize_objective (bool) –

    Whether to maximize the objective.

    By default it is set to False.

  • grammar_type (MDODiscipline.GrammarType) –

    The type of the input and output grammars.

    By default it is set to “JSONGrammar”.

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

add_constraint(output_name, constraint_type=ConstraintType.EQ, constraint_name=None, value=None, positive=False)[source]

Add a user constraint.

A user constraint is a design constraint in addition to the formulation specific constraints such as the targets (a.k.a. consistency constraints) in IDF.

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

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 (MDOFunction.ConstraintType) –

    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.

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.

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_option_values(**options)[source]

Return the default values of the sub-options of the formulation.

When some options of the formulation depend on higher level options, the default values of these sub-options may be obtained here, mainly for use in the API.

Parameters:

**options (str) – The options required to deduce the sub-options grammar.

Returns:

Either None or the sub-options default values.

Return type:

dict

abstract 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:

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

abstract 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:

list[ExecutionSequence, tuple[ExecutionSequence]]

get_optim_variable_names()[source]

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)[source]

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:

list[MDODiscipline]

classmethod get_sub_options_grammar(**options)[source]

Get the sub-options grammar.

When some options of the formulation depend on higher level options, the schema of the sub-options may be obtained here, mainly for use in the API.

Parameters:

**options (str) – The options required to deduce the sub-options grammar.

Returns:

Either None or the sub-options grammar.

Return type:

JSONGrammar

get_sub_scenarios()[source]

List the disciplines that are actually scenarios.

Returns:

The scenarios.

Return type:

list[Scenario]

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:

list[MDODiscipline]

get_x_mask_x_swap_order(masking_data_names, all_data_names=None)[source]

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:

ValueError – If the sizes or the sizes of variables are inconsistent.

Return type:

ndarray

get_x_names_of_disc(discipline)[source]

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_x_swap_order(masking_data_names, x_vect, all_data_names=None)[source]

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_x_swap_order(masking_data_names, x_masked, all_data_names=None, x_full=None)[source]

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.

  • x_masked (ndarray) – The boolean vector to unmask.

  • 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

DEFAULT_SCENARIO_RESULT_CLASS_NAME: ClassVar[str] = 'ScenarioResult'

The name of the ScenarioResult class to be used for post-processing.

NAME: ClassVar[str] = 'MDOFormulation'

The name of the MDO formulation.

property design_space: DesignSpace

The design space on which the formulation is applied.

property disciplines: list[MDODiscipline]

The disciplines of the MDO process.

opt_problem: OptimizationProblem

The optimization problem generated by the formulation from the disciplines.

class gemseo.core.base_formulation.BaseFormulationsFactory[source]

Bases: BaseFactory

A factory of BaseFormulation.

Return type:

Any

create(formulation_name, disciplines, objective_name, design_space, maximize_objective=False, **options)[source]

Create a formulation.

Parameters:
  • formulation_name (str) – The name of a class implementing a formulation.

  • disciplines (Sequence[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.

  • **options (Any) – The options for the creation of the formulation.

Returns:

The instance of the class.

Raises:

TypeError – If the class cannot be instantiated.

Return type:

BaseFormulation

get_class(name)

Return a class from its name.

Parameters:

name (str) – The name of the class.

Returns:

The class.

Raises:

ImportError – If the class is not available.

Return type:

type

get_default_option_values(name)

Return the constructor kwargs default values of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their default values.

Return type:

dict[str, str | int | float | bool]

get_default_sub_option_values(name, **options)

Return the default values of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_library_name(name)

Return the name of the library related to the name of a class.

Parameters:

name (str) – The name of the class.

Returns:

The name of the library.

Return type:

str

get_options_doc(name)

Return the constructor documentation of a class.

Parameters:

name (str) – The name of the class.

Returns:

The mapping from the argument names to their documentation.

Return type:

dict[str, str]

get_options_grammar(name, write_schema=False, schema_path=None)

Return the options JSON grammar for a class.

Attempt to generate a JSONGrammar from the arguments of the __init__ method of the class.

Parameters:
  • name (str) – The name of the class.

  • write_schema (bool) –

    If True, write the JSON schema to a file.

    By default it is set to False.

  • schema_path (str | None) – The path to the JSON schema file. If None, the file is saved in the current directory in a file named after the name of the class.

Returns:

The JSON grammar.

Return type:

JSONGrammar

get_sub_options_grammar(name, **options)

Return the JSONGrammar of the sub options of a class.

Parameters:
  • name (str) – The name of the class.

  • **options (str) – The options to be passed to the class required to deduce the sub options.

Returns:

The JSON grammar.

Return type:

JSONGrammar

is_available(name)

Return whether a class can be instantiated.

Parameters:

name (str) – The name of the class.

Returns:

Whether the class can be instantiated.

Return type:

bool

update()

Search for the classes that can be instantiated.

The search is done in the following order:
  1. The fully qualified module names

  2. The plugin packages

  3. The packages from the environment variables

Return type:

None

PLUGIN_ENTRY_POINT: ClassVar[str] = 'gemseo_plugins'

The name of the setuptools entry point for declaring plugins.

property class_names: list[str]

The sorted names of the available classes.

failed_imports: dict[str, str]

The class names bound to the import errors.

property formulations: list[str]

The available formulations.