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 __future__ import division, unicode_literals
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.

```
from gemseo.api import configure_logger, create_discipline, create_scenario
from gemseo.problems.sobieski.core import SobieskiProblem
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().read_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 - 21:51:08:
INFO - 21:51:08: *** Start MDO Scenario execution ***
INFO - 21:51:08: MDOScenario
INFO - 21:51:08: Disciplines: SobieskiPropulsion SobieskiAerodynamics SobieskiStructure SobieskiMission
INFO - 21:51:08: MDOFormulation: MDF
INFO - 21:51:08: Algorithm: SLSQP
INFO - 21:51:08: Optimization problem:
INFO - 21:51:08: Minimize: -y_4(x_shared, x_1, x_2, x_3)
INFO - 21:51:08: With respect to: x_shared, x_1, x_2, x_3
INFO - 21:51:08: Subject to constraints:
INFO - 21:51:08: g_1(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 21:51:08: g_2(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 21:51:08: g_3(x_shared, x_1, x_2, x_3) <= 0.0
INFO - 21:51:08: Design space:
INFO - 21:51:08: +----------+-------------+-------+-------------+-------+
INFO - 21:51:08: | name | lower_bound | value | upper_bound | type |
INFO - 21:51:08: +----------+-------------+-------+-------------+-------+
INFO - 21:51:08: | x_shared | 0.01 | 0.05 | 0.09 | float |
INFO - 21:51:08: | x_shared | 30000 | 45000 | 60000 | float |
INFO - 21:51:08: | x_shared | 1.4 | 1.6 | 1.8 | float |
INFO - 21:51:08: | x_shared | 2.5 | 5.5 | 8.5 | float |
INFO - 21:51:08: | x_shared | 40 | 55 | 70 | float |
INFO - 21:51:08: | x_shared | 500 | 1000 | 1500 | float |
INFO - 21:51:08: | x_1 | 0.1 | 0.25 | 0.4 | float |
INFO - 21:51:08: | x_1 | 0.75 | 1 | 1.25 | float |
INFO - 21:51:08: | x_2 | 0.75 | 1 | 1.25 | float |
INFO - 21:51:08: | x_3 | 0.1 | 0.5 | 1 | float |
INFO - 21:51:08: +----------+-------------+-------+-------------+-------+
INFO - 21:51:08: Optimization: 0%| | 0/10 [00:00<?, ?it]
INFO - 21:51:10: Optimization: 20%|██ | 2/10 [00:01<00:01, 5.35 it/sec, obj=2.12e+3]
INFO - 21:51:12: Optimization: 30%|███ | 3/10 [00:04<00:03, 2.24 it/sec, obj=3.79e+3]
INFO - 21:51:16: Optimization: 40%|████ | 4/10 [00:08<00:05, 1.20 it/sec, obj=4.01e+3]
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779456540841447e-06 is still above the tolerance 1e-06.
INFO - 21:51:19: Optimization: 50%|█████ | 5/10 [00:11<00:00, 52.99 it/min, obj=4.51e+3]
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023363e-06 is still above the tolerance 1e-06.
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238926e-06 is still above the tolerance 1e-06.
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377948543944703e-06 is still above the tolerance 1e-06.
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827514e-06 is still above the tolerance 1e-06.
WARNING - 21:51:19: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129485e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777426401462329e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779979229840046e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.378257400296512e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619635e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3767631312504768e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779456540841447e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023363e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238926e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377948543944703e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827514e-06 is still above the tolerance 1e-06.
WARNING - 21:51:20: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129485e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777426401462329e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779979229840046e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.378257400296512e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619635e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3767631312504768e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779456540841447e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023363e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238926e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377948543944703e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827514e-06 is still above the tolerance 1e-06.
WARNING - 21:51:21: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129485e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777426401462329e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779979229840046e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.378257400296512e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619635e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3767631312504768e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779456540841447e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777923572023363e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3780025526238926e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.377948543944703e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3776904668827514e-06 is still above the tolerance 1e-06.
WARNING - 21:51:22: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3781026103129485e-06 is still above the tolerance 1e-06.
WARNING - 21:51:23: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3777426401462329e-06 is still above the tolerance 1e-06.
WARNING - 21:51:23: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779979229840046e-06 is still above the tolerance 1e-06.
WARNING - 21:51:23: MDAJacobi has reached its maximum number of iterations but the normed residual 1.378257400296512e-06 is still above the tolerance 1e-06.
WARNING - 21:51:23: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3779467160619635e-06 is still above the tolerance 1e-06.
WARNING - 21:51:23: MDAJacobi has reached its maximum number of iterations but the normed residual 1.3767631312504768e-06 is still above the tolerance 1e-06.
INFO - 21:51:23: Optimization: 60%|██████ | 6/10 [00:15<00:00, 39.46 it/min, obj=3.74e+3]
INFO - 21:51:23: Optimization: 80%|████████ | 8/10 [00:15<00:00, 39.08 it/min, obj=4.76e+3]
INFO - 21:51:23: Optimization: 100%|██████████| 10/10 [00:15<00:00, 38.70 it/min, obj=4.57e+3]
WARNING - 21:51:23: Optimization found no feasible point ! The least infeasible point is selected.
INFO - 21:51:23: Optimization: 100%|██████████| 10/10 [00:15<00:00, 38.69 it/min, obj=4.57e+3]
INFO - 21:51:23: Optimization result:
INFO - 21:51:23: Objective value = 3789.3185446884972
INFO - 21:51:23: The result is not feasible.
INFO - 21:51:23: Status: None
INFO - 21:51:23: Optimizer message: Maximum number of iterations reached. GEMSEO Stopped the driver
INFO - 21:51:23: Number of calls to the objective function by the optimizer: 12
INFO - 21:51:23: Constraints values:
INFO - 21:51:23: g_1 = [-0.01968861 -0.03451033 -0.04515197 -0.0525708 -0.05794746 -0.13703141
INFO - 21:51:23: -0.10296859]
INFO - 21:51:23: g_2 = 0.0004732410401158127
INFO - 21:51:23: g_3 = [-0.62555576 -0.37444424 -0.14466936 -0.18313783]
INFO - 21:51:23: Design space:
INFO - 21:51:23: +----------+-------------+---------------------+-------------+-------+
INFO - 21:51:23: | name | lower_bound | value | upper_bound | type |
INFO - 21:51:23: +----------+-------------+---------------------+-------------+-------+
INFO - 21:51:23: | x_shared | 0.01 | 0.06011831026002896 | 0.09 | float |
INFO - 21:51:23: | x_shared | 30000 | 60000 | 60000 | float |
INFO - 21:51:23: | x_shared | 1.4 | 1.400900029003249 | 1.8 | float |
INFO - 21:51:23: | x_shared | 2.5 | 2.5 | 8.5 | float |
INFO - 21:51:23: | x_shared | 40 | 70 | 70 | float |
INFO - 21:51:23: | x_shared | 500 | 1500 | 1500 | float |
INFO - 21:51:23: | x_1 | 0.1 | 0.3989644630096731 | 0.4 | float |
INFO - 21:51:23: | x_1 | 0.75 | 0.75 | 1.25 | float |
INFO - 21:51:23: | x_2 | 0.75 | 0.75 | 1.25 | float |
INFO - 21:51:23: | x_3 | 0.1 | 0.1337468319957488 | 1 | float |
INFO - 21:51:23: +----------+-------------+---------------------+-------------+-------+
INFO - 21:51:23: *** MDO Scenario run terminated in 0:00:15.516184 ***
{'algo': 'SLSQP', 'max_iter': 10}
```

## 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:
Options for 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/conda/3.2.0/lib/python3.8/site-packages/gemseo/post/gradient_sensitivity.py:163: UserWarning: FixedFormatter should only be used together with FixedLocator
axe.set_xticklabels(x_labels, fontsize=12, rotation=90)
```

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