.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/post_process/algorithms/plot_gradient_sensitivity.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_post_process_algorithms_plot_gradient_sensitivity.py: Gradient Sensitivity ==================== In this example, we illustrate the use of the :class:`.GradientSensitivity` post-processing on the Sobieski's SSBJ problem. The :class:`.GradientSensitivity` post-processor plots histograms of the objective and the constraints derivatives. By default, the sensitivities are calculated either at the optimum, or when the result is not feasible, at the least-non feasible point. The iteration where the sensitivities are computed can be modified via the `iteration` setting. .. note:: In some cases, the iteration used to compute the sensitivities corresponds to a point for which the algorithm did not request the evaluation of the gradients. In this case, a `ValueError` is raised by :class:`.GradientSensitivity`. To overcome this issue, one can set the `compute_missing_gradients` setting to True. This way, |g| will compute the gradients for the iterations where it is lacking. This can be done only if the underlying disciplines are available, explaing why why, unlike the other post-processing examples, we need to **post-process directly from the MDO scenario**. .. warning:: Please note that this extra computation may be expensive depending on the :class:`.OptimizationProblem` defined by the user. Additionally, keep in mind that |g| cannot compute missing gradients for an :class:`.OptimizationProblem` that was imported from an HDF5 file. .. GENERATED FROM PYTHON SOURCE LINES 53-57 MDO scenario ------------ Let us first create and execute the MDF scenario on the Sobieski's SSBJ problem. .. GENERATED FROM PYTHON SOURCE LINES 57-98 .. code-block:: Python from __future__ import annotations from gemseo import create_discipline from gemseo import create_scenario from gemseo.algos.opt.scipy_local.settings.slsqp import SLSQP_Settings from gemseo.formulations.mdf_settings import MDF_Settings from gemseo.mda.gauss_seidel_settings import MDAGaussSeidel_Settings from gemseo.problems.mdo.sobieski.core.design_space import SobieskiDesignSpace from gemseo.settings.post import GradientSensitivity_Settings disciplines = create_discipline([ "SobieskiPropulsion", "SobieskiAerodynamics", "SobieskiMission", "SobieskiStructure", ]) formulation_settings = MDF_Settings( main_mda_name="MDAGaussSeidel", main_mda_settings=MDAGaussSeidel_Settings( max_mda_iter=30, tolerance=1e-10, warm_start=True, use_lu_fact=True, ), ) scenario = create_scenario( disciplines, "y_4", design_space=SobieskiDesignSpace(), maximize_objective=True, formulation_settings_model=formulation_settings, ) for name in ["g_1", "g_2", "g_3"]: scenario.add_constraint(name, constraint_type="ineq") scenario.execute(SLSQP_Settings(max_iter=20)) .. GENERATED FROM PYTHON SOURCE LINES 99-102 Post-processing --------------- Let us now post-process the scenario by means of the :class:`.GradientSensitivity`. .. GENERATED FROM PYTHON SOURCE LINES 102-110 .. code-block:: Python scenario.post_process( settings_model=GradientSensitivity_Settings( compute_missing_gradients=True, save=False, show=True, ), ) .. image-sg:: /examples/post_process/algorithms/images/sphx_glr_plot_gradient_sensitivity_001.png :alt: Derivatives of objective and constraints with respect to design variables, -y_4, g_1[0], g_1[1], g_1[2], g_1[3], g_1[4], g_1[5], g_1[6], g_2, g_3[0], g_3[1], g_3[2], g_3[3] :srcset: /examples/post_process/algorithms/images/sphx_glr_plot_gradient_sensitivity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.861 seconds) .. _sphx_glr_download_examples_post_process_algorithms_plot_gradient_sensitivity.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_gradient_sensitivity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_gradient_sensitivity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_gradient_sensitivity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_