Note

Click here to download the full example code

# Gradient Sensitivity¶

In this example, we illustrate the use of the `GradientSensitivity`

plot on the Sobieski’s SSBJ problem.

```
from gemseo.api import configure_logger
from gemseo.api import create_discipline
from gemseo.api import create_scenario
from gemseo.problems.sobieski.core.problem import SobieskiProblem
from matplotlib import pyplot as plt
```

## Import¶

The first step is to import some functions from the API and a method to get the design space.

```
configure_logger()
```

Out:

```
<RootLogger root (INFO)>
```

## Description¶

The **GradientSensitivity** post-processing
builds histograms of derivatives of the objective and the constraints.

## Create disciplines¶

At this point, we instantiate the disciplines of Sobieski’s SSBJ problem: Propulsion, Aerodynamics, Structure and Mission

```
disciplines = create_discipline(
[
"SobieskiPropulsion",
"SobieskiAerodynamics",
"SobieskiStructure",
"SobieskiMission",
]
)
```

## Create design space¶

We also read the design space from the `SobieskiProblem`

.

```
design_space = SobieskiProblem().design_space
```

## Create and execute scenario¶

The next step is to build an MDO scenario in order to maximize the range, encoded ‘y_4’, with respect to the design parameters, while satisfying the inequality constraints ‘g_1’, ‘g_2’ and ‘g_3’. We can use the MDF formulation, the SLSQP optimization algorithm and a maximum number of iterations equal to 100.

```
scenario = create_scenario(
disciplines,
formulation="MDF",
objective_name="y_4",
maximize_objective=True,
design_space=design_space,
)
scenario.set_differentiation_method("finite_differences")
for constraint in ["g_1", "g_2", "g_3"]:
scenario.add_constraint(constraint, "ineq")
scenario.execute({"algo": "SLSQP", "max_iter": 10})
```

Out:

```
INFO - 07:14:30:
INFO - 07:14:30: *** Start MDOScenario execution ***
INFO - 07:14:30: MDOScenario
INFO - 07:14:30: Disciplines: SobieskiPropulsion SobieskiAerodynamics SobieskiStructure SobieskiMission
INFO - 07:14:30: MDO formulation: MDF
INFO - 07:14:30: Optimization problem:
INFO - 07:14:30: minimize -y_4(x_shared, x_1, x_2, x_3)
INFO - 07:14:30: with respect to x_1, x_2, x_3, x_shared
INFO - 07:14:30: subject to constraints:
INFO - 07:14:30: g_1(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 07:14:30: g_2(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 07:14:30: g_3(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 07:14:30: over the design space:
INFO - 07:14:30: +----------+-------------+-------+-------------+-------+
INFO - 07:14:30: | name | lower_bound | value | upper_bound | type |
INFO - 07:14:30: +----------+-------------+-------+-------------+-------+
INFO - 07:14:30: | x_shared | 0.01 | 0.05 | 0.09 | float |
INFO - 07:14:30: | x_shared | 30000 | 45000 | 60000 | float |
INFO - 07:14:30: | x_shared | 1.4 | 1.6 | 1.8 | float |
INFO - 07:14:30: | x_shared | 2.5 | 5.5 | 8.5 | float |
INFO - 07:14:30: | x_shared | 40 | 55 | 70 | float |
INFO - 07:14:30: | x_shared | 500 | 1000 | 1500 | float |
INFO - 07:14:30: | x_1 | 0.1 | 0.25 | 0.4 | float |
INFO - 07:14:30: | x_1 | 0.75 | 1 | 1.25 | float |
INFO - 07:14:30: | x_2 | 0.75 | 1 | 1.25 | float |
INFO - 07:14:30: | x_3 | 0.1 | 0.5 | 1 | float |
INFO - 07:14:30: +----------+-------------+-------+-------------+-------+
INFO - 07:14:30: Solving optimization problem with algorithm SLSQP:
INFO - 07:14:30: ... 0%| | 0/10 [00:00<?, ?it]
INFO - 07:14:32: ... 20%|██ | 2/10 [00:02<00:01, 4.74 it/sec, obj=-2.12e+3]
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067738728359277e-06 is still above the tolerance 1e-06.
INFO - 07:14:35: ... 30%|███ | 3/10 [00:04<00:03, 2.00 it/sec, obj=-3.81e+3]
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067522741880028e-06 is still above the tolerance 1e-06.
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.106813064067522e-06 is still above the tolerance 1e-06.
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067846748313078e-06 is still above the tolerance 1e-06.
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068165770949126e-06 is still above the tolerance 1e-06.
WARNING - 07:14:35: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068099188494698e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067815296068116e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067873198565832e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068260032962304e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068104192521803e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1069168537347672e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067738728359277e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067522741880028e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.106813064067522e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067846748313078e-06 is still above the tolerance 1e-06.
WARNING - 07:14:36: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068165770949126e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068099188494698e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067815296068116e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067873198565832e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068260032962304e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068104192521803e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1069168537347672e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067738728359277e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067522741880028e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.106813064067522e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067846748313078e-06 is still above the tolerance 1e-06.
WARNING - 07:14:37: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068165770949126e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068099188494698e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067815296068116e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067873198565832e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068260032962304e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068104192521803e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1069168537347672e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067738728359277e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067522741880028e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.106813064067522e-06 is still above the tolerance 1e-06.
WARNING - 07:14:38: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067846748313078e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068165770949126e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068099188494698e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067815296068116e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1067873198565832e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068260032962304e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1068104192521803e-06 is still above the tolerance 1e-06.
WARNING - 07:14:39: MDAJacobi has reached its maximum number of iterations but the normed residual 1.1069168537347672e-06 is still above the tolerance 1e-06.
INFO - 07:14:39: ... 40%|████ | 4/10 [00:09<00:05, 1.08 it/sec, obj=-4.01e+3]
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377945654084145e-06 is still above the tolerance 1e-06.
INFO - 07:14:43: ... 50%|█████ | 5/10 [00:12<00:00, 47.76 it/min, obj=-4.51e+3]
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023366e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238928e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779485439447035e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827516e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129487e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377742640146233e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377997922984005e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3782574002965124e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619637e-06 is still above the tolerance 1e-06.
WARNING - 07:14:43: MDAJacobi has reached its maximum number of iterations but the normed residual 1.376763131250477e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377945654084145e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023366e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238928e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779485439447035e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827516e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129487e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377742640146233e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377997922984005e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3782574002965124e-06 is still above the tolerance 1e-06.
WARNING - 07:14:44: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619637e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.376763131250477e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377945654084145e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023366e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238928e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779485439447035e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827516e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129487e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377742640146233e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377997922984005e-06 is still above the tolerance 1e-06.
WARNING - 07:14:45: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3782574002965124e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619637e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.376763131250477e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377945654084145e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023366e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238928e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779485439447035e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827516e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129487e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377742640146233e-06 is still above the tolerance 1e-06.
WARNING - 07:14:46: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377997922984005e-06 is still above the tolerance 1e-06.
WARNING - 07:14:47: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3782574002965124e-06 is still above the tolerance 1e-06.
WARNING - 07:14:47: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619637e-06 is still above the tolerance 1e-06.
WARNING - 07:14:47: MDAJacobi has reached its maximum number of iterations but the normed residual 1.376763131250477e-06 is still above the tolerance 1e-06.
INFO - 07:14:47: ... 60%|██████ | 6/10 [00:16<00:00, 35.62 it/min, obj=-3.74e+3]
INFO - 07:14:47: ... 80%|████████ | 8/10 [00:17<00:00, 35.28 it/min, obj=-4.76e+3]
INFO - 07:14:47: ... 100%|██████████| 10/10 [00:17<00:00, 34.94 it/min, obj=-4.57e+3]
WARNING - 07:14:47: Optimization found no feasible point ! The least infeasible point is selected.
INFO - 07:14:47: ... 100%|██████████| 10/10 [00:17<00:00, 34.93 it/min, obj=-4.57e+3]
INFO - 07:14:47: Optimization result:
INFO - 07:14:47: Optimizer info:
INFO - 07:14:47: Status: None
INFO - 07:14:47: Message: Maximum number of iterations reached. GEMSEO Stopped the driver
INFO - 07:14:47: Number of calls to the objective function by the optimizer: 12
INFO - 07:14:47: Solution:
WARNING - 07:14:47: The solution is not feasible.
INFO - 07:14:47: Objective: -3805.1978443187672
INFO - 07:14:47: Standardized constraints:
INFO - 07:14:47: g_1 = [-0.01890636 -0.03395136 -0.04471869 -0.05221743 -0.05764924 -0.13711592
INFO - 07:14:47: -0.10288408]
INFO - 07:14:47: g_2 = 0.00024768913891715094
INFO - 07:14:47: g_3 = [-0.63817382 -0.36182618 -0.13441339 -0.1831937 ]
INFO - 07:14:47: Design space:
INFO - 07:14:47: +----------+-------------+---------------------+-------------+-------+
INFO - 07:14:47: | name | lower_bound | value | upper_bound | type |
INFO - 07:14:47: +----------+-------------+---------------------+-------------+-------+
INFO - 07:14:47: | x_shared | 0.01 | 0.06006192228472926 | 0.09 | float |
INFO - 07:14:47: | x_shared | 30000 | 60000 | 60000 | float |
INFO - 07:14:47: | x_shared | 1.4 | 1.400471025224229 | 1.8 | float |
INFO - 07:14:47: | x_shared | 2.5 | 2.5 | 8.5 | float |
INFO - 07:14:47: | x_shared | 40 | 70 | 70 | float |
INFO - 07:14:47: | x_shared | 500 | 1500 | 1500 | float |
INFO - 07:14:47: | x_1 | 0.1 | 0.3994580569121137 | 0.4 | float |
INFO - 07:14:47: | x_1 | 0.75 | 0.75 | 1.25 | float |
INFO - 07:14:47: | x_2 | 0.75 | 0.75 | 1.25 | float |
INFO - 07:14:47: | x_3 | 0.1 | 0.1352994187733632 | 1 | float |
INFO - 07:14:47: +----------+-------------+---------------------+-------------+-------+
INFO - 07:14:47: *** End MDOScenario execution (time: 0:00:17.190094) ***
{'max_iter': 10, 'algo': 'SLSQP'}
```

## Post-process scenario¶

Lastly, we post-process the scenario by means of the `GradientSensitivity`

plot which builds histograms of derivatives of objective and constraints.

Tip

Each post-processing method requires different inputs and offers a variety
of customization options. Use the API function
`get_post_processing_options_schema()`

to print a table with
the options for any post-processing algorithm.
Or refer to our dedicated page:
Post-processing algorithms.

```
scenario.post_process("GradientSensitivity", save=False, show=False)
# Workaround for HTML rendering, instead of ``show=True``
plt.show()
```

Out:

```
/home/docs/checkouts/readthedocs.org/user_builds/gemseo/envs/4.0.0/lib/python3.9/site-packages/gemseo/post/gradient_sensitivity.py:159: UserWarning: FixedFormatter should only be used together with FixedLocator
axe.set_xticklabels(design_names, fontsize=font_size, rotation=rotation)
```

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