.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/formulations/plot_doe_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_doe_sobieski_bilevel_example.py: BiLevel-based DOE on the Sobieski SSBJ test case ================================================ .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: default from copy import deepcopy from os import name as os_name 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 configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 36-43 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 43-52 .. code-block:: default propu, aero, mission, struct = create_discipline( [ "SobieskiPropulsion", "SobieskiAerodynamics", "SobieskiMission", "SobieskiStructure", ] ) .. GENERATED FROM PYTHON SOURCE LINES 53-61 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 61-63 .. code-block:: default design_space = SobieskiProblem().design_space .. GENERATED FROM PYTHON SOURCE LINES 64-67 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 67-75 .. 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 76-79 Build a sub-scenario for Propulsion ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize SFC. .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: default sc_prop = create_scenario( propu, "DisciplinaryOpt", "y_34", design_space=deepcopy(design_space).filter("x_3"), name="PropulsionScenario", ) .. GENERATED FROM PYTHON SOURCE LINES 88-91 Build a sub-scenario for Aerodynamics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize L/D. .. GENERATED FROM PYTHON SOURCE LINES 91-100 .. code-block:: default sc_aero = create_scenario( aero, "DisciplinaryOpt", "y_24", deepcopy(design_space).filter("x_2"), name="AerodynamicsScenario", maximize_objective=True, ) .. GENERATED FROM PYTHON SOURCE LINES 101-105 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 105-114 .. code-block:: default sc_str = create_scenario( struct, "DisciplinaryOpt", "y_11", deepcopy(design_space).filter("x_1"), name="StructureScenario", maximize_objective=True, ) .. GENERATED FROM PYTHON SOURCE LINES 115-119 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 119-131 .. 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, parallel_scenarios=False, reset_x0_before_opt=True, scenario_type="DOE", ) .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. note:: Setting :code:`reset_x0_before_opt=True` is mandatory when doing a DOE in parallel. If we want reproducible results, don't reuse previous xopt. .. GENERATED FROM PYTHON SOURCE LINES 136-140 .. code-block:: default system_scenario.formulation.mda1.warm_start = False system_scenario.formulation.mda2.warm_start = False .. GENERATED FROM PYTHON SOURCE LINES 141-146 .. note:: This is mandatory when doing a DOE in parallel if we want always exactly the same results, don't warm start mda1 to have exactly the same process whatever the execution order and process dispatch. .. GENERATED FROM PYTHON SOURCE LINES 146-150 .. code-block:: default for sub_sc in sub_disciplines[0:3]: sub_sc.default_inputs = {"max_iter": 20, "algo": "L-BFGS-B"} .. GENERATED FROM PYTHON SOURCE LINES 151-156 Multiprocessing ^^^^^^^^^^^^^^^ It is possible to run a DOE in parallel using multiprocessing, in order to do this, we specify the number of processes to be used for the computation of the samples. .. GENERATED FROM PYTHON SOURCE LINES 158-168 .. warning:: The multiprocessing option has some limitations on Windows. Due to problems with sphinx, we disable it in this example. For Python versions < 3.7 and Numpy < 1.20.0, subprocesses may get hung randomly during execution. It is strongly recommended to update your environment to avoid this problem. The features :class:`.MemoryFullCache` and :class:`.HDF5Cache` are not available for multiprocessing on Windows. As an alternative, we recommend the method :meth:`.DOEScenario.set_optimization_history_backup`. .. GENERATED FROM PYTHON SOURCE LINES 168-178 .. code-block:: default system_scenario.execute( { "n_samples": 30, "algo": "lhs", "algo_options": {"n_processes": 1 if os_name == "nt" else 4}, } ) system_scenario.print_execution_metrics() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 10:05:20: INFO - 10:05:20: *** Start DOEScenario execution *** INFO - 10:05:20: DOEScenario INFO - 10:05:20: Disciplines: PropulsionScenario AerodynamicsScenario StructureScenario SobieskiMission INFO - 10:05:20: MDO formulation: BiLevel INFO - 10:05:20: Optimization problem: INFO - 10:05:20: minimize y_4(x_shared) INFO - 10:05:20: with respect to x_shared INFO - 10:05:20: over the design space: INFO - 10:05:20: +----------+-------------+-------+-------------+-------+ INFO - 10:05:20: | name | lower_bound | value | upper_bound | type | INFO - 10:05:20: +----------+-------------+-------+-------------+-------+ INFO - 10:05:20: | x_shared | 0.01 | 0.05 | 0.09 | float | INFO - 10:05:20: | x_shared | 30000 | 45000 | 60000 | float | INFO - 10:05:20: | x_shared | 1.4 | 1.6 | 1.8 | float | INFO - 10:05:20: | x_shared | 2.5 | 5.5 | 8.5 | float | INFO - 10:05:20: | x_shared | 40 | 55 | 70 | float | INFO - 10:05:20: | x_shared | 500 | 1000 | 1500 | float | INFO - 10:05:20: +----------+-------------+-------+-------------+-------+ INFO - 10:05:20: Solving optimization problem with algorithm lhs: INFO - 10:05:20: ... 0%| | 0/30 [00:00 .. GENERATED FROM PYTHON SOURCE LINES 192-194 Plot the scatter matrix ^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 194-198 .. code-block:: default system_scenario.post_process( "ScatterPlotMatrix", show=False, save=False, variable_names=["y_4", "x_shared"] ) .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_004.png :alt: plot doe sobieski bilevel example :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 199-201 Plot parallel coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 201-203 .. code-block:: default system_scenario.post_process("ParallelCoordinates", show=False, save=False) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_005.png :alt: Design variables history colored by 'y_4' value :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_005.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_006.png :alt: Objective function and constraints history colored by 'y_4' value. :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 204-206 Plot correlations ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 206-209 .. code-block:: default system_scenario.post_process("Correlations", show=False, save=False) # Workaround for HTML rendering, instead of ``show=True`` plt.show() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 10:05:28: Detected 0 correlations > 0.95 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 7.529 seconds) .. _sphx_glr_download_examples_formulations_plot_doe_sobieski_bilevel_example.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_doe_sobieski_bilevel_example.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_doe_sobieski_bilevel_example.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_