# A from scratch example on the Sellar problem¶

## Introduction¶

In this example, we will create an MDO scenario based on the Sellar’s problem from scratch. Contrary to the GEMSEO in ten minutes tutorial, all the disciplines will be implemented from scratch by sub-classing the MDODiscipline class for each discipline of the Sellar problem.

## The Sellar problem¶

We will consider in this example the Sellar problem:

\begin{split}\begin{aligned} \text{minimize the objective function }&obj=x_{local}^2 + x_{shared,2} +y_1^2+e^{-y_2} \\ \text{with respect to the design variables }&x_{shared},\,x_{local} \\ \text{subject to the general constraints } & c_1 \leq 0\\ & c_2 \leq 0\\ \text{subject to the bound constraints } & -10 \leq x_{shared,1} \leq 10\\ & 0 \leq x_{shared,2} \leq 10\\ & 0 \leq x_{local} \leq 10. \end{aligned}\end{split}

where the coupling variables are

$\text{Discipline 1: } y_1 = \sqrt{x_{shared,1}^2 + x_{shared,2} + x_{local} - 0.2\,y_2},$

and

$\text{Discipline 2: }y_2 = |y_1| + x_{shared,1} + x_{shared,2}.$

and where the general constraints are

\begin{align}\begin{aligned}c_1 = 3.16 - y_1^2\\c_2 = y_2 - 24.\end{aligned}\end{align}

## Imports¶

All the imports needed for the tutorials are performed here.

from __future__ import annotations

from math import exp

from gemseo.algos.design_space import DesignSpace
from gemseo.api import configure_logger
from gemseo.api import create_scenario
from gemseo.core.discipline import MDODiscipline
from numpy import array
from numpy import ones

configure_logger()

<RootLogger root (INFO)>


## Create the disciplinary classes¶

In this section, we define the Sellar disciplines by sub-classing the MDODiscipline class. For each class, the constructor and the _run method are overriden:

• In the constructor, the input and output grammar are created. They define which inputs and outputs variables are allowed at the discipline execution. The default inputs are also defined, in case of the user does not provide them at the discipline execution.

• In the _run method is implemented the concrete computation of the discipline. The inputs data are fetch by using the MDODiscipline.get_inputs_by_name() method. The returned NumPy arrays can then be used to compute the output values. They can then be stored in the MDODiscipline.local_data dictionary. If the discipline execution is successful.

Note that we do not define the Jacobians in the disciplines. In this example, we will approximate the derivatives using the finite differences method.

### Create the SellarSystem class¶

class SellarSystem(MDODiscipline):
def __init__(self):
super().__init__()
# Initialize the grammars to define inputs and outputs
self.input_grammar.update(["x", "z", "y_1", "y_2"])
self.output_grammar.update(["obj", "c_1", "c_2"])
# Default inputs define what data to use when the inputs are not
# provided to the execute method
self.default_inputs = {
"x": ones(1),
"z": array([4.0, 3.0]),
"y_1": ones(1),
"y_2": ones(1),
}

def _run(self):
# The run method defines what happens at execution
# ie how outputs are computed from inputs
x, z, y_1, y_2 = self.get_inputs_by_name(["x", "z", "y_1", "y_2"])
# The ouputs are stored here
self.local_data["obj"] = array([x ** 2 + z + y_1 ** 2 + exp(-y_2)])
self.local_data["c_1"] = array([3.16 - y_1 ** 2])
self.local_data["c_2"] = array([y_2 - 24.0])


### Create the Sellar1 class¶

class Sellar1(MDODiscipline):
def __init__(self):
super().__init__()
self.input_grammar.update(["x", "z", "y_2"])
self.output_grammar.update(["y_1"])
self.default_inputs = {
"x": ones(1),
"z": array([4.0, 3.0]),
"y_1": ones(1),
"y_2": ones(1),
}

def _run(self):
x, z, y_2 = self.get_inputs_by_name(["x", "z", "y_2"])
self.local_data["y_1"] = array(
[(z ** 2 + z + x - 0.2 * y_2) ** 0.5]
)


### Create the Sellar2 class¶

class Sellar2(MDODiscipline):
def __init__(self):
super().__init__()
self.input_grammar.update(["z", "y_1"])
self.output_grammar.update(["y_2"])
self.default_inputs = {
"x": ones(1),
"z": array([4.0, 3.0]),
"y_1": ones(1),
"y_2": ones(1),
}

def _run(self):
z, y_1 = self.get_inputs_by_name(["z", "y_1"])
self.local_data["y_2"] = array([abs(y_1) + z + z])


## Create and execute the scenario¶

### Instantiate disciplines¶

We can now instantiate the disciplines and store the instances in a list which will be used below.

disciplines = [Sellar1(), Sellar2(), SellarSystem()]


### Create the design space¶

In this section, we define the design space which will be used for the creation of the MDOScenario. Note that the coupling variables are defined in the design space. Indeed, as we are going to select the IDF formulation to solve the MDO scenario, the coupling variables will be unknowns of the optimization problem and consequently they have to be included in the design space. Conversely, it would not have been necessary to include them if we aimed to select an MDF formulation.

design_space = DesignSpace()
"z", 2, l_b=(-10, 0.0), u_b=(10.0, 10.0), value=array([4.0, 3.0])
)


### Create the scenario¶

In this section, we build the MDO scenario which links the disciplines with the formulation, the design space and the objective function.

scenario = create_scenario(
disciplines, formulation="IDF", objective_name="obj", design_space=design_space
)


Then, we have to set the design constraints

scenario.add_constraint("c_1", "ineq")


As previously mentioned, we are going to use finite differences to approximate the derivatives since the disciplines do not provide them.

scenario.set_differentiation_method("finite_differences")


### Execute the scenario¶

Then, we execute the MDO scenario with the inputs of the MDO scenario as a dictionary. In this example, the gradient-based SLSQP optimizer is selected, with 10 iterations at maximum:

scenario.execute(input_data={"max_iter": 10, "algo": "SLSQP"})

    INFO - 17:24:50:
INFO - 17:24:50: *** Start MDOScenario execution ***
INFO - 17:24:50: MDOScenario
INFO - 17:24:50:    Disciplines: Sellar1 Sellar2 SellarSystem
INFO - 17:24:50:    MDO formulation: IDF
INFO - 17:24:50: Optimization problem:
INFO - 17:24:50:    minimize obj(x, z, y_1, y_2)
INFO - 17:24:50:    with respect to x, y_1, y_2, z
INFO - 17:24:50:    subject to constraints:
INFO - 17:24:50:       c_1(x, z, y_1, y_2) <= 0.0
INFO - 17:24:50:       c_2(x, z, y_1, y_2) <= 0.0
INFO - 17:24:50:       y_1: y_1(x, z, y_2): y_1(x, z, y_2) - y_1 == 0.0
INFO - 17:24:50:       y_2: y_2(z, y_1): y_2(z, y_1) - y_2 == 0.0
INFO - 17:24:50:    over the design space:
INFO - 17:24:50:    +------+-------------+-------+-------------+-------+
INFO - 17:24:50:    | name | lower_bound | value | upper_bound | type  |
INFO - 17:24:50:    +------+-------------+-------+-------------+-------+
INFO - 17:24:50:    | x    |      0      |   1   |      10     | float |
INFO - 17:24:50:    | z |     -10     |   4   |      10     | float |
INFO - 17:24:50:    | z |      0      |   3   |      10     | float |
INFO - 17:24:50:    | y_1  |     -100    |   1   |     100     | float |
INFO - 17:24:50:    | y_2  |     -100    |   1   |     100     | float |
INFO - 17:24:50:    +------+-------------+-------+-------------+-------+
INFO - 17:24:50: Solving optimization problem with algorithm SLSQP:
INFO - 17:24:50: ...   0%|          | 0/10 [00:00<?, ?it]
INFO - 17:24:50: ...  10%|█         | 1/10 [00:00<00:00, 131.47 it/sec, obj=5.37]
INFO - 17:24:50: ...  20%|██        | 2/10 [00:00<00:00, 46.83 it/sec, obj=4.34]
INFO - 17:24:50: ...  30%|███       | 3/10 [00:00<00:00, 45.27 it/sec, obj=3.26]
INFO - 17:24:50: ...  40%|████      | 4/10 [00:00<00:00, 44.18 it/sec, obj=3.18]
INFO - 17:24:50: ...  50%|█████     | 5/10 [00:00<00:00, 43.69 it/sec, obj=3.18]
INFO - 17:24:50: ...  60%|██████    | 6/10 [00:00<00:00, 43.40 it/sec, obj=3.18]
INFO - 17:24:51: Optimization result:
INFO - 17:24:51:    Optimizer info:
INFO - 17:24:51:       Status: 8
INFO - 17:24:51:       Message: Positive directional derivative for linesearch
INFO - 17:24:51:       Number of calls to the objective function by the optimizer: 7
INFO - 17:24:51:    Solution:
INFO - 17:24:51:       The solution is feasible.
INFO - 17:24:51:       Objective: 3.1833939516400456
INFO - 17:24:51:       Standardized constraints:
INFO - 17:24:51:          c_1 = 5.764277943853813e-13
INFO - 17:24:51:          c_2 = -20.244722233074114
INFO - 17:24:51:          y_1 = [2.06834549e-15]
INFO - 17:24:51:          y_2 = [1.98063788e-15]
INFO - 17:24:51:       Design space:
INFO - 17:24:51:       +------+-------------+-------------------+-------------+-------+
INFO - 17:24:51:       | name | lower_bound |       value       | upper_bound | type  |
INFO - 17:24:51:       +------+-------------+-------------------+-------------+-------+
INFO - 17:24:51:       | x    |      0      |         0         |      10     | float |
INFO - 17:24:51:       | z |     -10     | 1.977638883463326 |      10     | float |
INFO - 17:24:51:       | z |      0      |         0         |      10     | float |
INFO - 17:24:51:       | y_1  |     -100    | 1.777638883462956 |     100     | float |
INFO - 17:24:51:       | y_2  |     -100    | 3.755277766925886 |     100     | float |
INFO - 17:24:51:       +------+-------------+-------------------+-------------+-------+
INFO - 17:24:51: *** End MDOScenario execution (time: 0:00:00.184817) ***

{'max_iter': 10, 'algo': 'SLSQP'}


### Post-process the scenario¶

Finally, we can generate plots of the optimization history:

scenario.post_process("OptHistoryView", save=False, show=True)

• • • • • • <gemseo.post.opt_history_view.OptHistoryView object at 0x7fccd19b9580>


Total running time of the script: ( 0 minutes 2.202 seconds)

Gallery generated by Sphinx-Gallery