Store observables#

Introduction#

In this example, we will learn how to store the history of state variables using the add_observable() method. This is useful in situations where we wish to access, post-process, or save the values of discipline outputs that are not design variables, constraints or objective functions.

The Sellar problem#

We will consider in this example the Sellar problem:

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

where the coupling variables are

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

and

\[\text{Discipline 2: }y_2 = |y_1| + x_{\text{shared},1} + x_{\text{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 numpy import array
from numpy import ones

from gemseo import configure_logger
from gemseo import create_discipline
from gemseo import create_scenario
from gemseo.algos.design_space import DesignSpace

configure_logger()
<RootLogger root (INFO)>

Create the problem disciplines#

In this section, we use the available classes Sellar1, Sellar2 and SellarSystem to define the disciplines of the problem. The create_discipline() API function allows us to carry out this task easily, as well as store the instances in a list to be used later on.

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

Create and execute the scenario#

Create the design space#

In this section, we define the design space which will be used for the creation of the MDOScenario.

design_space = DesignSpace()
design_space.add_variable("x_local", lower_bound=0.0, upper_bound=10.0, value=ones(1))
design_space.add_variable(
    "x_shared",
    2,
    lower_bound=(-10, 0.0),
    upper_bound=(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, "obj", design_space, formulation_name="MDF")
INFO - 08:36:22: Variable x_local was removed from the Design Space, it is not an input of any discipline.

Note that the formulation settings passed to create_scenario() can be provided via a Pydantic model. For more information, see Formulation Settings.

Add the constraints#

Then, we have to set the design constraints

scenario.add_constraint("c_1", constraint_type="ineq")
scenario.add_constraint("c_2", constraint_type="ineq")

Add the observables#

Only the design variables, objective function and constraints are stored by default. In order to be able to recover the data from the state variables, y1 and y2, we have to add them as observables. All we have to do is enter the variable name as a string to the add_observable() method. If more than one output name is provided (as a list of strings), the observable function returns a concatenated array of the output values.

scenario.add_observable("y_1")

It is also possible to add the observable with a custom name, using the option observable_name. Let us store the variable y_2 as y2.

scenario.add_observable("y_2", observable_name="y2")

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(algo_name="SLSQP", max_iter=10)
INFO - 08:36:22:
INFO - 08:36:22: *** Start MDOScenario execution ***
INFO - 08:36:22: MDOScenario
INFO - 08:36:22:    Disciplines: Sellar1 Sellar2 SellarSystem
INFO - 08:36:22:    MDO formulation: MDF
INFO - 08:36:22: Optimization problem:
INFO - 08:36:22:    minimize obj(x_shared)
INFO - 08:36:22:    with respect to x_shared
INFO - 08:36:22:    subject to constraints:
INFO - 08:36:22:       c_1(x_shared) <= 0
INFO - 08:36:22:       c_2(x_shared) <= 0
INFO - 08:36:22:    over the design space:
INFO - 08:36:22:       +-------------+-------------+-------+-------------+-------+
INFO - 08:36:22:       | Name        | Lower bound | Value | Upper bound | Type  |
INFO - 08:36:22:       +-------------+-------------+-------+-------------+-------+
INFO - 08:36:22:       | x_shared[0] |     -10     |   4   |      10     | float |
INFO - 08:36:22:       | x_shared[1] |      0      |   3   |      10     | float |
INFO - 08:36:22:       +-------------+-------------+-------+-------------+-------+
INFO - 08:36:22: Solving optimization problem with algorithm SLSQP:
INFO - 08:36:22:     10%|█         | 1/10 [00:00<00:00, 62.62 it/sec, obj=19.8]
INFO - 08:36:22:     20%|██        | 2/10 [00:00<00:00, 51.66 it/sec, obj=5.38]
INFO - 08:36:22:     30%|███       | 3/10 [00:00<00:00, 50.89 it/sec, obj=3.41]
INFO - 08:36:22:     40%|████      | 4/10 [00:00<00:00, 50.73 it/sec, obj=3.19]
INFO - 08:36:22:     50%|█████     | 5/10 [00:00<00:00, 50.51 it/sec, obj=3.18]
INFO - 08:36:22:     60%|██████    | 6/10 [00:00<00:00, 50.32 it/sec, obj=3.18]
INFO - 08:36:22:     70%|███████   | 7/10 [00:00<00:00, 50.19 it/sec, obj=3.18]
INFO - 08:36:22:     80%|████████  | 8/10 [00:00<00:00, 54.24 it/sec, obj=3.18]
INFO - 08:36:22: Optimization result:
INFO - 08:36:22:    Optimizer info:
INFO - 08:36:22:       Status: None
INFO - 08:36:22:       Message: Successive iterates of the objective function are closer than ftol_rel or ftol_abs. GEMSEO stopped the driver.
INFO - 08:36:22:       Number of calls to the objective function by the optimizer: 9
INFO - 08:36:22:    Solution:
INFO - 08:36:22:       The solution is feasible.
INFO - 08:36:22:       Objective: 3.183393960638473
INFO - 08:36:22:       Standardized constraints:
INFO - 08:36:22:          c_1 = 1.5724075375089797e-09
INFO - 08:36:22:          c_2 = -20.244722684911245
INFO - 08:36:22:       Design space:
INFO - 08:36:22:          +-------------+-------------+-------------------+-------------+-------+
INFO - 08:36:22:          | Name        | Lower bound |       Value       | Upper bound | Type  |
INFO - 08:36:22:          +-------------+-------------+-------------------+-------------+-------+
INFO - 08:36:22:          | x_shared[0] |     -10     | 1.977638860218251 |      10     | float |
INFO - 08:36:22:          | x_shared[1] |      0      |         0         |      10     | float |
INFO - 08:36:22:          +-------------+-------------+-------------------+-------------+-------+
INFO - 08:36:22: *** End MDOScenario execution (time: 0:00:00.153119) ***

Note that the algorithm settings passed to DriverLibrary.execute() can be provided via a Pydantic model. For more information, see Algorithm Settings.

Access the observable variables#

Retrieve observables from a dataset#

In order to create a dataset, we use the corresponding OptimizationProblem:

opt_problem = scenario.formulation.optimization_problem

We can easily build an OptimizationDataset from this OptimizationProblem: either by separating the design parameters from the functions (default option):

dataset = opt_problem.to_dataset("sellar_problem")
dataset
GROUP designs functions
VARIABLE x_shared c_1 c_2 obj y2 y_1
COMPONENT 0 1 0 0 0 0 0
1 4.000000 3.000000e+00 -1.362071e+01 -12.903573 19.780730 11.096427 4.096427
2 2.518818 0.000000e+00 -2.216919e+00 -19.162364 5.384844 4.837636 2.318818
3 2.040791 4.248151e-10 -2.285105e-01 -20.118419 3.409129 3.881581 1.840791
4 1.978722 1.358050e-12 -3.852514e-03 -20.242556 3.187196 3.757444 1.778722
5 1.977639 0.000000e+00 -1.172903e-06 -20.244722 3.183395 3.755278 1.777639
6 1.977639 8.487813e-14 1.061817e-12 -20.244723 3.183394 3.755277 1.777639
7 1.977639 0.000000e+00 1.572408e-09 -20.244723 3.183394 3.755277 1.777639
8 1.977639 4.245252e-14 NaN NaN 3.183394 3.755277 1.777639


or by considering all features as default parameters:

dataset = opt_problem.to_dataset("sellar_problem", categorize=False)
dataset
GROUP parameters
VARIABLE x_shared c_1 c_2 obj y2 y_1
COMPONENT 0 1 0 0 0 0 0
0 4.000000 3.000000e+00 -1.362071e+01 -12.903573 19.780730 11.096427 4.096427
1 2.518818 0.000000e+00 -2.216919e+00 -19.162364 5.384844 4.837636 2.318818
2 2.040791 4.248151e-10 -2.285105e-01 -20.118419 3.409129 3.881581 1.840791
3 1.978722 1.358050e-12 -3.852514e-03 -20.242556 3.187196 3.757444 1.778722
4 1.977639 0.000000e+00 -1.172903e-06 -20.244722 3.183395 3.755278 1.777639
5 1.977639 8.487813e-14 1.061817e-12 -20.244723 3.183394 3.755277 1.777639
6 1.977639 0.000000e+00 1.572408e-09 -20.244723 3.183394 3.755277 1.777639
7 1.977639 4.245252e-14 NaN NaN 3.183394 3.755277 1.777639


or by using an input-output naming rather than an optimization naming:

dataset = opt_problem.to_dataset("sellar_problem", opt_naming=False)
dataset
GROUP inputs outputs
VARIABLE x_shared c_1 c_2 obj y2 y_1
COMPONENT 0 1 0 0 0 0 0
0 4.000000 3.000000e+00 -1.362071e+01 -12.903573 19.780730 11.096427 4.096427
1 2.518818 0.000000e+00 -2.216919e+00 -19.162364 5.384844 4.837636 2.318818
2 2.040791 4.248151e-10 -2.285105e-01 -20.118419 3.409129 3.881581 1.840791
3 1.978722 1.358050e-12 -3.852514e-03 -20.242556 3.187196 3.757444 1.778722
4 1.977639 0.000000e+00 -1.172903e-06 -20.244722 3.183395 3.755278 1.777639
5 1.977639 8.487813e-14 1.061817e-12 -20.244723 3.183394 3.755277 1.777639
6 1.977639 0.000000e+00 1.572408e-09 -20.244723 3.183394 3.755277 1.777639
7 1.977639 4.245252e-14 NaN NaN 3.183394 3.755277 1.777639


Access observables by name#

We can get the observable data by variable names:

dataset.get_view(variable_names=["y_1", "y2"])
GROUP outputs
VARIABLE y_1 y2
COMPONENT 0 0
0 4.096427 11.096427
1 2.318818 4.837636
2 1.840791 3.881581
3 1.778722 3.757444
4 1.777639 3.755278
5 1.777639 3.755277
6 1.777639 3.755277
7 1.777639 3.755277


Use the observables in a post-processing method#

Finally, we can generate plots with the observable variables. Have a look at the Basic History plot and the Scatter Plot Matrix:

scenario.post_process(
    post_name="BasicHistory",
    variable_names=["obj", "y_1", "y2"],
    save=False,
    show=True,
)
History plot
<gemseo.post.basic_history.BasicHistory object at 0x7f25229f5c10>

Note that the post-processor settings passed to BaseScenario.post_process() can be provided via a Pydantic model. For more information, see Post-processor Settings. An example is shown below.

from gemseo.settings.post import ScatterPlotMatrix_Settings  # noqa: E402

scenario.post_process(
    ScatterPlotMatrix_Settings(
        variable_names=["obj", "c_1", "c_2", "y2", "y_1"],
        save=False,
        show=True,
    )
)
plot store observables
<gemseo.post.scatter_plot_matrix.ScatterPlotMatrix object at 0x7f2522a1ffd0>

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

Gallery generated by Sphinx-Gallery