.. 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 :ref:`Go to the end ` 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:: Python from __future__ import annotations from copy import deepcopy from os import name as os_name from gemseo import create_discipline from gemseo import create_scenario from gemseo import execute_post from gemseo.problems.mdo.sobieski.core.design_space import SobieskiDesignSpace .. GENERATED FROM PYTHON SOURCE LINES 36-43 Instantiate the disciplines ---------------------------- First, we instantiate the four disciplines of the use case: :class:`.SobieskiPropulsion`, :class:`.SobieskiAerodynamics`, :class:`.SobieskiMission` and :class:`.SobieskiStructure`. .. GENERATED FROM PYTHON SOURCE LINES 43-50 .. code-block:: Python propu, aero, mission, struct = create_discipline([ "SobieskiPropulsion", "SobieskiAerodynamics", "SobieskiMission", "SobieskiStructure", ]) .. GENERATED FROM PYTHON SOURCE LINES 51-59 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 59-61 .. code-block:: Python design_space = SobieskiDesignSpace() .. GENERATED FROM PYTHON SOURCE LINES 62-63 Then, we build a sub-scenario for each strongly coupled disciplines. .. GENERATED FROM PYTHON SOURCE LINES 65-68 Build a sub-scenario for Propulsion ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize SFC. .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: Python sc_prop = create_scenario( propu, "y_34", design_space.filter("x_3", copy=True), name="PropulsionScenario", formulation_name="DisciplinaryOpt", ) .. GENERATED FROM PYTHON SOURCE LINES 77-80 Build a sub-scenario for Aerodynamics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize L/D. .. GENERATED FROM PYTHON SOURCE LINES 80-89 .. code-block:: Python sc_aero = create_scenario( aero, "y_24", design_space.filter("x_2", copy=True), name="AerodynamicsScenario", maximize_objective=True, formulation_name="DisciplinaryOpt", ) .. GENERATED FROM PYTHON SOURCE LINES 90-94 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 94-103 .. code-block:: Python sc_str = create_scenario( struct, "y_11", deepcopy(design_space).filter("x_1"), name="StructureScenario", maximize_objective=True, formulation_name="DisciplinaryOpt", ) .. GENERATED FROM PYTHON SOURCE LINES 104-108 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 108-121 .. code-block:: Python sub_disciplines = [sc_prop, sc_aero, sc_str, mission] system_scenario = create_scenario( sub_disciplines, "y_4", design_space.filter("x_shared", copy=True), parallel_scenarios=False, reset_x0_before_opt=True, scenario_type="DOE", formulation_name="BiLevel", save_opt_history="True", naming="UUID", ) .. GENERATED FROM PYTHON SOURCE LINES 122-126 .. note:: Setting ``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 128-143 .. tip:: When running BiLevel scenarios, it is interesting to access the optimization history of the sub-scenarios for each system iteration. By default, the setting ``keep_opt_history`` is set to ``True``. This allows you to store in memory the databases of the sub-scenarios (see the last section of this example for more details). In some cases, storing the databases in memory can take up too much space and cause performance issues. In these cases, set ``keep_opt_history=False`` and save the databases to the disk using ``save_opt_history=True``. If your sub-scenarios are running in parallel, and you are saving the optimization histories to the disk, set the ``naming`` setting to ``"UUID"``, which is multiprocessing-safe. The setting ``keep_opt_history`` will not work if the sub-scenarios are running in parallel because the databases are not copied from the sub-processes to the main process. In this case you shall always save the optimization history to the disk. .. GENERATED FROM PYTHON SOURCE LINES 143-147 .. code-block:: Python system_scenario.formulation.mda1.warm_start = False system_scenario.formulation.mda2.warm_start = False .. GENERATED FROM PYTHON SOURCE LINES 148-153 .. 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 153-157 .. code-block:: Python for sub_sc in sub_disciplines[0:3]: sub_sc.set_algorithm(algo_name="L-BFGS-B", max_iter=20) .. GENERATED FROM PYTHON SOURCE LINES 158-165 Visualize the XDSM ^^^^^^^^^^^^^^^^^^ Generate the XDSM on the fly: - ``log_workflow_status=True`` will log the status of the workflow in the console, - ``save_html`` (default ``True``) will generate a self-contained HTML file, that can be automatically opened using ``show_html=True``. .. GENERATED FROM PYTHON SOURCE LINES 165-167 .. code-block:: Python system_scenario.xdsmize(save_html=False) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 168-173 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 175-182 .. warning:: The multiprocessing option has some limitations on Windows. Due to problems with sphinx, we disable it in this example. 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 182-187 .. code-block:: Python n_processes = 1 if os_name == "nt" else 4 system_scenario.execute(algo_name="PYDOE_LHS", n_samples=30, n_processes=n_processes) system_scenario.print_execution_metrics() .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:24:06: *** Start DOEScenario execution *** INFO - 16:24:06: DOEScenario INFO - 16:24:06: Disciplines: AerodynamicsScenario PropulsionScenario SobieskiMission StructureScenario INFO - 16:24:06: MDO formulation: BiLevel INFO - 16:24:06: Optimization problem: INFO - 16:24:06: minimize y_4(x_shared) INFO - 16:24:06: with respect to x_shared INFO - 16:24:06: over the design space: INFO - 16:24:06: +-------------+-------------+-------+-------------+-------+ INFO - 16:24:06: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:06: +-------------+-------------+-------+-------------+-------+ INFO - 16:24:06: | x_shared[0] | 0.01 | 0.05 | 0.09 | float | INFO - 16:24:06: | x_shared[1] | 30000 | 45000 | 60000 | float | INFO - 16:24:06: | x_shared[2] | 1.4 | 1.6 | 1.8 | float | INFO - 16:24:06: | x_shared[3] | 2.5 | 5.5 | 8.5 | float | INFO - 16:24:06: | x_shared[4] | 40 | 55 | 70 | float | INFO - 16:24:06: | x_shared[5] | 500 | 1000 | 1500 | float | INFO - 16:24:06: +-------------+-------------+-------+-------------+-------+ INFO - 16:24:06: Solving optimization problem with algorithm PYDOE_LHS: INFO - 16:24:06: Running DOE in parallel on n_processes = 4 INFO - 16:24:06: 3%|▎ | 1/30 [00:00<00:06, 4.78 it/sec, feas=True, obj=247] INFO - 16:24:06: 7%|▋ | 2/30 [00:00<00:03, 7.83 it/sec, feas=True, obj=485] INFO - 16:24:06: 10%|█ | 3/30 [00:00<00:02, 10.51 it/sec, feas=True, obj=388] INFO - 16:24:06: 13%|█▎ | 4/30 [00:00<00:01, 13.33 it/sec, feas=True, obj=350] INFO - 16:24:06: 17%|█▋ | 5/30 [00:00<00:01, 14.83 it/sec, feas=True, obj=621] INFO - 16:24:06: 20%|██ | 6/30 [00:00<00:01, 14.68 it/sec, feas=True, obj=458] INFO - 16:24:06: 23%|██▎ | 7/30 [00:00<00:01, 16.47 it/sec, feas=True, obj=495] INFO - 16:24:07: 27%|██▋ | 8/30 [00:00<00:01, 16.52 it/sec, feas=True, obj=549] INFO - 16:24:07: 30%|███ | 9/30 [00:00<00:01, 18.15 it/sec, feas=True, obj=367] INFO - 16:24:07: 33%|███▎ | 10/30 [00:00<00:01, 17.67 it/sec, feas=True, obj=892] INFO - 16:24:07: 37%|███▋ | 11/30 [00:00<00:00, 19.26 it/sec, feas=True, obj=918] INFO - 16:24:07: 40%|████ | 12/30 [00:00<00:00, 18.89 it/sec, feas=True, obj=1.27e+3] INFO - 16:24:07: 43%|████▎ | 13/30 [00:00<00:00, 19.35 it/sec, feas=True, obj=354] INFO - 16:24:07: 47%|████▋ | 14/30 [00:00<00:00, 20.20 it/sec, feas=True, obj=415] INFO - 16:24:07: 50%|█████ | 15/30 [00:00<00:00, 19.91 it/sec, feas=True, obj=337] INFO - 16:24:07: 53%|█████▎ | 16/30 [00:00<00:00, 20.14 it/sec, feas=True, obj=2.27e+3] INFO - 16:24:07: 57%|█████▋ | 17/30 [00:00<00:00, 21.31 it/sec, feas=True, obj=1.2e+3] INFO - 16:24:07: 60%|██████ | 18/30 [00:00<00:00, 20.85 it/sec, feas=True, obj=380] INFO - 16:24:07: 63%|██████▎ | 19/30 [00:00<00:00, 21.21 it/sec, feas=True, obj=394] INFO - 16:24:07: 67%|██████▋ | 20/30 [00:00<00:00, 21.82 it/sec, feas=True, obj=829] INFO - 16:24:07: 70%|███████ | 21/30 [00:00<00:00, 22.14 it/sec, feas=True, obj=832] INFO - 16:24:07: 73%|███████▎ | 22/30 [00:01<00:00, 21.80 it/sec, feas=True, obj=1.04e+3] INFO - 16:24:07: 77%|███████▋ | 23/30 [00:01<00:00, 22.48 it/sec, feas=True, obj=1.21e+3] INFO - 16:24:07: 80%|████████ | 24/30 [00:01<00:00, 22.56 it/sec, feas=True, obj=640] INFO - 16:24:07: 83%|████████▎ | 25/30 [00:01<00:00, 21.95 it/sec, feas=True, obj=1.19e+3] INFO - 16:24:07: 87%|████████▋ | 26/30 [00:01<00:00, 21.96 it/sec, feas=True, obj=470] INFO - 16:24:07: 90%|█████████ | 27/30 [00:01<00:00, 22.58 it/sec, feas=True, obj=293] INFO - 16:24:07: 93%|█████████▎| 28/30 [00:01<00:00, 23.39 it/sec, feas=True, obj=484] INFO - 16:24:07: 97%|█████████▋| 29/30 [00:01<00:00, 23.48 it/sec, feas=True, obj=647] INFO - 16:24:07: 100%|██████████| 30/30 [00:01<00:00, 23.69 it/sec, feas=True, obj=952] INFO - 16:24:07: Optimization result: INFO - 16:24:07: Optimizer info: INFO - 16:24:07: Status: None INFO - 16:24:07: Message: None INFO - 16:24:07: Solution: INFO - 16:24:07: Objective: 246.89549262432172 INFO - 16:24:07: Design space: INFO - 16:24:07: +-------------+-------------+---------------------+-------------+-------+ INFO - 16:24:07: | Name | Lower bound | Value | Upper bound | Type | INFO - 16:24:07: +-------------+-------------+---------------------+-------------+-------+ INFO - 16:24:07: | x_shared[0] | 0.01 | 0.01316336056367379 | 0.09 | float | INFO - 16:24:07: | x_shared[1] | 30000 | 39053.36254511708 | 60000 | float | INFO - 16:24:07: | x_shared[2] | 1.4 | 1.759600266521177 | 1.8 | float | INFO - 16:24:07: | x_shared[3] | 2.5 | 8.352983911532561 | 8.5 | float | INFO - 16:24:07: | x_shared[4] | 40 | 66.23984775914758 | 70 | float | INFO - 16:24:07: | x_shared[5] | 500 | 1232.319858277323 | 1500 | float | INFO - 16:24:07: +-------------+-------------+---------------------+-------------+-------+ INFO - 16:24:07: *** End DOEScenario execution *** INFO - 16:24:07: The discipline counters are disabled. .. GENERATED FROM PYTHON SOURCE LINES 188-194 .. warning:: On Windows, the progress bar may show duplicated instances during the initialization of each subprocess. In some cases it may also print the conclusion of an iteration ahead of another one that was concluded first. This is a consequence of the pickling process and does not affect the computations of the scenario. .. GENERATED FROM PYTHON SOURCE LINES 196-201 Exporting the problem data. ^^^^^^^^^^^^^^^^^^^^^^^^^^^ After the execution of the scenario, you may want to export your data to use it elsewhere. The method :meth:`.Scenario.to_dataset` will allow you to export your results to a :class:`.Dataset`, the basic |g| class to store data. .. GENERATED FROM PYTHON SOURCE LINES 201-203 .. code-block:: Python dataset = system_scenario.to_dataset("a_name_for_my_dataset") .. GENERATED FROM PYTHON SOURCE LINES 204-206 Plot the optimization history view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 206-208 .. code-block:: Python system_scenario.post_process(post_name="OptHistoryView", save=False, show=True) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_001.png :alt: Evolution of the optimization variables :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_001.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_002.png :alt: Evolution of the objective value :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_002.png :class: sphx-glr-multi-img * .. image-sg:: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_003.png :alt: Evolution of the distance to the optimum :srcset: /examples/formulations/images/sphx_glr_plot_doe_sobieski_bilevel_example_003.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 209-211 Plot the scatter matrix ^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 211-218 .. code-block:: Python system_scenario.post_process( post_name="ScatterPlotMatrix", variable_names=["y_4", "x_shared"], save=False, show=True, ) .. 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 .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 219-221 Plot parallel coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 221-223 .. code-block:: Python system_scenario.post_process(post_name="ParallelCoordinates", save=False, show=True) .. 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 .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 224-226 Plot correlations ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 226-228 .. code-block:: Python system_scenario.post_process(post_name="Correlations", save=False, show=True) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:24:09: Detected 0 correlations > 0.95 .. GENERATED FROM PYTHON SOURCE LINES 229-237 Plot the structure optimization histories of the 2 first iterations ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The code below will not work if you ran the system scenario with ``n_processes`` > 1. Indeed, parallel execution of sub-scenarios prevents us to save the databases from each sub-process to the main process. If you ran the system scenario with many processes, you can still save the databases to the disk with ``save_opt_history=True`` and ``naming="UUID"``. Refer to the formulation settings for more information. .. GENERATED FROM PYTHON SOURCE LINES 237-242 .. code-block:: Python struct_databases = system_scenario.formulation.scenario_adapters[2].databases for database in struct_databases[:2]: opt_problem = deepcopy(sc_str.formulation.optimization_problem) opt_problem.database = database execute_post(opt_problem, post_name="OptHistoryView", save=False, show=True) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.759 seconds) .. _sphx_glr_download_examples_formulations_plot_doe_sobieski_bilevel_example.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_doe_sobieski_bilevel_example.ipynb ` .. 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-zip :download:`Download zipped: plot_doe_sobieski_bilevel_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_