.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/formulations/plot_sobieski_bilevel_example.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_formulations_plot_sobieski_bilevel_example.py: BiLevel-based MDO on the Sobieski SSBJ test case ================================================ .. GENERATED FROM PYTHON SOURCE LINES 24-36 .. code-block:: default from __future__ import annotations from copy import deepcopy from gemseo.api import configure_logger from gemseo.api import create_discipline from gemseo.api import create_scenario from gemseo.api import execute_post from gemseo.problems.sobieski.core.problem import SobieskiProblem configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 37-44 Instantiate the disciplines ---------------------------- First, we instantiate the four disciplines of the use case: :class:`~gemseo.problems.sobieski.disciplines.SobieskiPropulsion`, :class:`~gemseo.problems.sobieski.disciplines.SobieskiAerodynamics`, :class:`~gemseo.problems.sobieski.disciplines.SobieskiMission` and :class:`~gemseo.problems.sobieski.disciplines.SobieskiStructure`. .. GENERATED FROM PYTHON SOURCE LINES 44-53 .. code-block:: default propu, aero, mission, struct = create_discipline( [ "SobieskiPropulsion", "SobieskiAerodynamics", "SobieskiMission", "SobieskiStructure", ] ) .. GENERATED FROM PYTHON SOURCE LINES 54-62 Build, execute and post-process the scenario -------------------------------------------- Then, we build the scenario which links the disciplines with the formulation and the optimization algorithm. Here, we use the :class:`.BiLevel` formulation. We tell the scenario to minimize -y_4 instead of minimizing y_4 (range), which is the default option. We need to define the design space. .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: default design_space = SobieskiProblem().design_space .. GENERATED FROM PYTHON SOURCE LINES 65-68 Then, we build a sub-scenario for each strongly coupled disciplines, using the following algorithm, maximum number of iterations and algorithm options: .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: default algo_options = { "xtol_rel": 1e-7, "xtol_abs": 1e-7, "ftol_rel": 1e-7, "ftol_abs": 1e-7, "ineq_tolerance": 1e-4, } sub_sc_opts = {"max_iter": 30, "algo": "SLSQP", "algo_options": algo_options} .. GENERATED FROM PYTHON SOURCE LINES 77-80 Build a sub-scenario for Propulsion ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize SFC. .. GENERATED FROM PYTHON SOURCE LINES 80-90 .. code-block:: default sc_prop = create_scenario( propu, "DisciplinaryOpt", "y_34", design_space=deepcopy(design_space).filter("x_3"), name="PropulsionScenario", ) sc_prop.default_inputs = sub_sc_opts sc_prop.add_constraint("g_3", constraint_type="ineq") .. GENERATED FROM PYTHON SOURCE LINES 91-94 Build a sub-scenario for Aerodynamics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize L/D. .. GENERATED FROM PYTHON SOURCE LINES 94-105 .. code-block:: default sc_aero = create_scenario( aero, "DisciplinaryOpt", "y_24", deepcopy(design_space).filter("x_2"), name="AerodynamicsScenario", maximize_objective=True, ) sc_aero.default_inputs = sub_sc_opts sc_aero.add_constraint("g_2", constraint_type="ineq") .. GENERATED FROM PYTHON SOURCE LINES 106-110 Build a sub-scenario for Structure ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will maximize log(aircraft total weight / (aircraft total weight - fuel weight)). .. GENERATED FROM PYTHON SOURCE LINES 110-121 .. code-block:: default sc_str = create_scenario( struct, "DisciplinaryOpt", "y_11", deepcopy(design_space).filter("x_1"), name="StructureScenario", maximize_objective=True, ) sc_str.add_constraint("g_1", constraint_type="ineq") sc_str.default_inputs = sub_sc_opts .. GENERATED FROM PYTHON SOURCE LINES 122-126 Build a scenario for Mission ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This scenario is based on the three previous sub-scenarios and on the Mission and aims to maximize the range (Breguet). .. GENERATED FROM PYTHON SOURCE LINES 126-147 .. code-block:: default sub_disciplines = [sc_prop, sc_aero, sc_str] + [mission] design_space = deepcopy(design_space).filter("x_shared") system_scenario = create_scenario( sub_disciplines, "BiLevel", "y_4", design_space, apply_cstr_tosub_scenarios=False, parallel_scenarios=False, multithread_scenarios=True, tolerance=1e-14, max_mda_iter=30, maximize_objective=True, ) system_scenario.add_constraint(["g_1", "g_2", "g_3"], "ineq") # system_scenario.xdsmize(open_browser=True) system_scenario.execute( {"max_iter": 50, "algo": "NLOPT_COBYLA", "algo_options": algo_options} ) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:58:10: INFO - 16:58:10: *** Start MDOScenario execution *** INFO - 16:58:10: MDOScenario INFO - 16:58:10: Disciplines: AerodynamicsScenario PropulsionScenario SobieskiMission StructureScenario INFO - 16:58:10: MDO formulation: BiLevel INFO - 16:58:10: Optimization problem: INFO - 16:58:10: minimize -y_4(x_shared) = -y_4(x_shared) INFO - 16:58:10: with respect to x_shared INFO - 16:58:10: subject to constraints: INFO - 16:58:10: g_1_g_2_g_3(x_shared) <= 0.0 INFO - 16:58:10: over the design space: INFO - 16:58:10: +-------------+-------------+-------+-------------+-------+ INFO - 16:58:10: | name | lower_bound | value | upper_bound | type | INFO - 16:58:10: +-------------+-------------+-------+-------------+-------+ INFO - 16:58:10: | x_shared[0] | 0.01 | 0.05 | 0.09 | float | INFO - 16:58:10: | x_shared[1] | 30000 | 45000 | 60000 | float | INFO - 16:58:10: | x_shared[2] | 1.4 | 1.6 | 1.8 | float | INFO - 16:58:10: | x_shared[3] | 2.5 | 5.5 | 8.5 | float | INFO - 16:58:10: | x_shared[4] | 40 | 55 | 70 | float | INFO - 16:58:10: | x_shared[5] | 500 | 1000 | 1500 | float | INFO - 16:58:10: +-------------+-------------+-------+-------------+-------+ INFO - 16:58:10: Solving optimization problem with algorithm NLOPT_COBYLA: INFO - 16:58:10: ... 0%| | 0/50 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 161-163 Plot the structure optimization histories of the 2 first iterations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 163-172 .. code-block:: default struct_databases = system_scenario.formulation.scenario_adapters[2].databases for database in struct_databases[:2]: opt_problem = deepcopy(sc_str.formulation.opt_problem) opt_problem.database = database execute_post(opt_problem, "OptHistoryView", save=False, show=True) for disc in [propu, aero, mission, struct]: print(f"{disc.name}: {disc.n_calls} calls.") .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_007.png :alt: Evolution of the optimization variables :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_007.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_008.png :alt: Evolution of the objective value :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_008.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_009.png :alt: Distance to the optimum :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_009.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_010.png :alt: Hessian diagonal approximation :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_010.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_011.png :alt: Evolution of the inequality constraints :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_011.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_012.png :alt: Evolution of the optimization variables :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_012.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_013.png :alt: Evolution of the objective value :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_013.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_014.png :alt: Distance to the optimum :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_014.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_015.png :alt: Hessian diagonal approximation :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_015.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_016.png :alt: Evolution of the inequality constraints :srcset: /examples/formulations/images/sphx_glr_plot_sobieski_bilevel_example_016.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none SobieskiPropulsion: 1326 calls. SobieskiAerodynamics: 1442 calls. SobieskiMission: 50 calls. SobieskiStructure: 1571 calls. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 13.606 seconds) .. _sphx_glr_download_examples_formulations_plot_sobieski_bilevel_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_sobieski_bilevel_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_sobieski_bilevel_example.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_