.. 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-37 .. code-block:: Python from __future__ import annotations from copy import deepcopy from os import name as os_name from gemseo import configure_logger from gemseo import create_discipline from gemseo import create_scenario from gemseo.problems.sobieski.core.design_space import SobieskiDesignSpace configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-45 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 45-52 .. code-block:: Python 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:: Python design_space = SobieskiDesignSpace() .. 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-76 .. code-block:: Python 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-88 .. code-block:: Python sc_prop = create_scenario( propu, "DisciplinaryOpt", "y_34", design_space.filter("x_3", copy=True), name="PropulsionScenario", ) .. GENERATED FROM PYTHON SOURCE LINES 89-92 Build a sub-scenario for Aerodynamics ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This sub-scenario will minimize L/D. .. GENERATED FROM PYTHON SOURCE LINES 92-101 .. code-block:: Python sc_aero = create_scenario( aero, "DisciplinaryOpt", "y_24", design_space.filter("x_2", copy=True), name="AerodynamicsScenario", maximize_objective=True, ) .. GENERATED FROM PYTHON SOURCE LINES 102-106 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 106-115 .. code-block:: Python sc_str = create_scenario( struct, "DisciplinaryOpt", "y_11", deepcopy(design_space).filter("x_1"), name="StructureScenario", maximize_objective=True, ) .. GENERATED FROM PYTHON SOURCE LINES 116-120 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 120-131 .. code-block:: Python sub_disciplines = [sc_prop, sc_aero, sc_str, mission] system_scenario = create_scenario( sub_disciplines, "BiLevel", "y_4", design_space.filter("x_shared", copy=True), parallel_scenarios=False, reset_x0_before_opt=True, scenario_type="DOE", ) .. GENERATED FROM PYTHON SOURCE LINES 132-136 .. 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 136-140 .. code-block:: Python 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:: Python 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-158 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 158-160 .. code-block:: Python system_scenario.xdsmize(save_html=False) .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 161-166 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 168-175 .. 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 175-183 .. code-block:: Python 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 .. code-block:: none INFO - 00:11:33: INFO - 00:11:33: *** Start DOEScenario execution *** INFO - 00:11:33: DOEScenario INFO - 00:11:33: Disciplines: AerodynamicsScenario PropulsionScenario SobieskiMission StructureScenario INFO - 00:11:33: MDO formulation: BiLevel INFO - 00:11:33: Optimization problem: INFO - 00:11:33: minimize y_4(x_shared) INFO - 00:11:33: with respect to x_shared INFO - 00:11:33: over the design space: INFO - 00:11:33: +-------------+-------------+-------+-------------+-------+ INFO - 00:11:33: | Name | Lower bound | Value | Upper bound | Type | INFO - 00:11:33: +-------------+-------------+-------+-------------+-------+ INFO - 00:11:33: | x_shared[0] | 0.01 | 0.05 | 0.09 | float | INFO - 00:11:33: | x_shared[1] | 30000 | 45000 | 60000 | float | INFO - 00:11:33: | x_shared[2] | 1.4 | 1.6 | 1.8 | float | INFO - 00:11:33: | x_shared[3] | 2.5 | 5.5 | 8.5 | float | INFO - 00:11:33: | x_shared[4] | 40 | 55 | 70 | float | INFO - 00:11:33: | x_shared[5] | 500 | 1000 | 1500 | float | INFO - 00:11:33: +-------------+-------------+-------+-------------+-------+ INFO - 00:11:33: Solving optimization problem with algorithm lhs: INFO - 00:11:33: Running DOE in parallel on n_processes = 4 INFO - 00:11:34: 3%|▎ | 1/30 [00:01<00:29, 58.66 it/min, obj=247] INFO - 00:11:34: 7%|▋ | 2/30 [00:01<00:17, 1.65 it/sec, obj=485] INFO - 00:11:34: 10%|█ | 3/30 [00:01<00:11, 2.31 it/sec, obj=388] INFO - 00:11:34: 13%|█▎ | 4/30 [00:01<00:10, 2.55 it/sec, obj=350] INFO - 00:11:34: 17%|█▋ | 5/30 [00:01<00:08, 3.07 it/sec, obj=621] INFO - 00:11:35: 20%|██ | 6/30 [00:02<00:08, 2.95 it/sec, obj=458] INFO - 00:11:35: 23%|██▎ | 7/30 [00:02<00:07, 3.24 it/sec, obj=367] INFO - 00:11:35: 27%|██▋ | 8/30 [00:02<00:05, 3.70 it/sec, obj=495] INFO - 00:11:35: 30%|███ | 9/30 [00:02<00:05, 3.73 it/sec, obj=549] INFO - 00:11:35: 33%|███▎ | 10/30 [00:02<00:05, 3.66 it/sec, obj=892] INFO - 00:11:36: 37%|███▋ | 11/30 [00:03<00:05, 3.64 it/sec, obj=1.27e+3] INFO - 00:11:36: 40%|████ | 12/30 [00:03<00:04, 3.93 it/sec, obj=918] INFO - 00:11:36: 43%|████▎ | 13/30 [00:03<00:04, 4.17 it/sec, obj=354] INFO - 00:11:36: 47%|████▋ | 14/30 [00:03<00:03, 4.19 it/sec, obj=415] INFO - 00:11:36: 50%|█████ | 15/30 [00:03<00:03, 4.12 it/sec, obj=337] INFO - 00:11:36: 53%|█████▎ | 16/30 [00:03<00:03, 4.37 it/sec, obj=2.27e+3] INFO - 00:11:36: 57%|█████▋ | 17/30 [00:03<00:02, 4.61 it/sec, obj=1.2e+3] INFO - 00:11:37: 60%|██████ | 18/30 [00:04<00:02, 4.23 it/sec, obj=380] INFO - 00:11:37: 63%|██████▎ | 19/30 [00:04<00:02, 4.44 it/sec, obj=394] INFO - 00:11:37: 67%|██████▋ | 20/30 [00:04<00:02, 4.56 it/sec, obj=832] INFO - 00:11:37: 70%|███████ | 21/30 [00:04<00:01, 4.74 it/sec, obj=829] INFO - 00:11:38: 73%|███████▎ | 22/30 [00:04<00:01, 4.47 it/sec, obj=1.21e+3] INFO - 00:11:38: 77%|███████▋ | 23/30 [00:04<00:01, 4.62 it/sec, obj=1.04e+3] INFO - 00:11:38: 80%|████████ | 24/30 [00:05<00:01, 4.72 it/sec, obj=640] INFO - 00:11:38: 83%|████████▎ | 25/30 [00:05<00:01, 4.79 it/sec, obj=1.19e+3] INFO - 00:11:38: 87%|████████▋ | 26/30 [00:05<00:00, 4.57 it/sec, obj=484] INFO - 00:11:38: 90%|█████████ | 27/30 [00:05<00:00, 4.71 it/sec, obj=470] INFO - 00:11:38: 93%|█████████▎| 28/30 [00:05<00:00, 4.84 it/sec, obj=647] INFO - 00:11:38: 97%|█████████▋| 29/30 [00:05<00:00, 4.99 it/sec, obj=293] INFO - 00:11:39: 100%|██████████| 30/30 [00:05<00:00, 5.05 it/sec, obj=952] INFO - 00:11:39: Optimization result: INFO - 00:11:39: Optimizer info: INFO - 00:11:39: Status: None INFO - 00:11:39: Message: None INFO - 00:11:39: Number of calls to the objective function by the optimizer: 30 INFO - 00:11:39: Solution: INFO - 00:11:39: Objective: 246.8954926013132 INFO - 00:11:39: Design space: INFO - 00:11:39: +-------------+-------------+---------------------+-------------+-------+ INFO - 00:11:39: | Name | Lower bound | Value | Upper bound | Type | INFO - 00:11:39: +-------------+-------------+---------------------+-------------+-------+ INFO - 00:11:39: | x_shared[0] | 0.01 | 0.01316336056367379 | 0.09 | float | INFO - 00:11:39: | x_shared[1] | 30000 | 39053.36254511708 | 60000 | float | INFO - 00:11:39: | x_shared[2] | 1.4 | 1.759600266521177 | 1.8 | float | INFO - 00:11:39: | x_shared[3] | 2.5 | 8.352983911532561 | 8.5 | float | INFO - 00:11:39: | x_shared[4] | 40 | 66.23984775914758 | 70 | float | INFO - 00:11:39: | x_shared[5] | 500 | 1232.319858277323 | 1500 | float | INFO - 00:11:39: +-------------+-------------+---------------------+-------------+-------+ INFO - 00:11:39: *** End DOEScenario execution (time: 0:00:06.007661) *** INFO - 00:11:39: Scenario Execution Statistics INFO - 00:11:39: Discipline: PropulsionScenario INFO - 00:11:39: Executions number: 30 INFO - 00:11:39: Execution time: 1.7776448440126842 s INFO - 00:11:39: Linearizations number: 0 INFO - 00:11:39: Discipline: AerodynamicsScenario INFO - 00:11:39: Executions number: 30 INFO - 00:11:39: Execution time: 1.9244248540126137 s INFO - 00:11:39: Linearizations number: 0 INFO - 00:11:39: Discipline: StructureScenario INFO - 00:11:39: Executions number: 30 INFO - 00:11:39: Execution time: 8.047441852009797 s INFO - 00:11:39: Linearizations number: 0 INFO - 00:11:39: Discipline: SobieskiMission INFO - 00:11:39: Executions number: 30 INFO - 00:11:39: Execution time: 0.0017869210132630542 s INFO - 00:11:39: Linearizations number: 0 INFO - 00:11:39: Total number of executions calls: 120 INFO - 00:11:39: Total number of linearizations: 0 .. GENERATED FROM PYTHON SOURCE LINES 184-190 .. 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 192-197 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 197-199 .. code-block:: Python dataset = system_scenario.to_dataset("a_name_for_my_dataset") .. GENERATED FROM PYTHON SOURCE LINES 200-202 Plot the optimization history view ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 202-204 .. code-block:: Python system_scenario.post_process("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: 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 205-207 Plot the scatter matrix ^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 207-214 .. code-block:: Python system_scenario.post_process( "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 215-217 Plot parallel coordinates ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 217-219 .. code-block:: Python system_scenario.post_process("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 220-222 Plot correlations ^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 222-223 .. code-block:: Python system_scenario.post_process("Correlations", save=False, show=True) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 00:11:42: Detected 0 correlations > 0.95 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 9.400 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_