.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/post_process/algorithms/plot_pareto_front_binhkorn_bilevel.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_post_process_algorithms_plot_pareto_front_binhkorn_bilevel.py: Pareto front on Binh and Korn problem using a BiLevel formulation ================================================================= In this example, we illustrate the computation of a Pareto front plot for the Binh and Korn problem. We use a BiLevel formulation in order to only compute the Pareto-optimal points. .. GENERATED FROM PYTHON SOURCE LINES 30-34 Import ------ The first step is to import some functions from the API, and to configure the logger. .. GENERATED FROM PYTHON SOURCE LINES 34-46 .. code-block:: default from __future__ import annotations from gemseo.api import configure_logger from gemseo.api import create_design_space from gemseo.api import create_discipline from gemseo.api import create_scenario from gemseo.disciplines.scenario_adapter import MDOScenarioAdapter from numpy import array configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 47-53 Definition of the disciplines ----------------------------- In this example, we create the Binh and Korn disciplines from scratch by declaring their expressions and using the :class:`.AnalyticDiscipline`. .. GENERATED FROM PYTHON SOURCE LINES 53-61 .. code-block:: default expr_binh_korn = { "obj1": "4*x1**2 + 4*x2**2", "obj2": "(x1-5.)**2 + (x2-5.)**2", "cstr1": "(x1-5.)**2 + x2**2 - 25.", "cstr2": "-(x1-8.)**2 - (x2+3)**2 + 7.7", } .. GENERATED FROM PYTHON SOURCE LINES 62-63 This constraint will be used to set `obj1` to a target value for the lower-level scenario. .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: default expr_cstr_obj1_target = {"cstr3": "obj1 - obj1_target"} .. GENERATED FROM PYTHON SOURCE LINES 67-71 Instantiation of the disciplines -------------------------------- Here, we create the disciplines from their expressions. .. GENERATED FROM PYTHON SOURCE LINES 71-79 .. code-block:: default discipline_binh_korn = create_discipline( "AnalyticDiscipline", expressions=expr_binh_korn ) discipline_cstr_obj1 = create_discipline( "AnalyticDiscipline", expressions=expr_cstr_obj1_target ) .. GENERATED FROM PYTHON SOURCE LINES 80-82 Definition of the lower-level design space ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 82-92 .. code-block:: default design_space = create_design_space() design_space.add_variable("x1", l_b=array([0.0]), u_b=array([5.0]), value=array([2.0])) design_space.add_variable("x2", l_b=array([-5.0]), u_b=array([3.0]), value=array([2.0])) disciplines = [ discipline_binh_korn, discipline_cstr_obj1, ] .. GENERATED FROM PYTHON SOURCE LINES 93-96 Creation of the lower-level scenario ------------------------------------ This scenario aims at finding the `obj2` optimal value for a specific value of `obj1`. .. GENERATED FROM PYTHON SOURCE LINES 96-106 .. code-block:: default scenario = create_scenario( disciplines, "DisciplinaryOpt", design_space=design_space, objective_name="obj2", ) scenario.default_inputs = {"algo": "NLOPT_SLSQP", "max_iter": 100} .. GENERATED FROM PYTHON SOURCE LINES 107-108 We add the Binh and Korn problem constraints. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default scenario.add_constraint("cstr1", "ineq") scenario.add_constraint("cstr2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 112-113 We add a constraint to force the value of `obj1` to `obj1_target`. .. GENERATED FROM PYTHON SOURCE LINES 113-115 .. code-block:: default scenario.add_constraint("cstr3", "eq") .. GENERATED FROM PYTHON SOURCE LINES 116-124 Creation of an MDOScenarioAdapter --------------------------------- An :class:`.MDOScenarioAdapter` is created to use the lower-level scenario as a discipline. This newly created discipline takes as input a target `obj_1`, and returns `obj1`, `obj2` and `cstr3`. The latter variable is used by the upper level scenario to check if `obj1` = `obj1_target` at the end of the lower-lever scenario execution. .. GENERATED FROM PYTHON SOURCE LINES 124-133 .. code-block:: default scenario_adapter = MDOScenarioAdapter( scenario, ["obj1_target"], ["obj1", "obj2", "cstr3"] ) design_space_doe = create_design_space() design_space_doe.add_variable( "obj1_target", l_b=array([0.1]), u_b=array([100.0]), value=array([1.0]) ) .. GENERATED FROM PYTHON SOURCE LINES 134-140 Creation of a DOEScenario ------------------------- Create a DOE Scenario, which will take as input the scenario adapter. It will perform a DOE over the `obj1_target` variable. Note that `obj2` shall be added as an observable of `scenario_doe`, otherwise it cannot be used by the ParetoFront post-processing. .. GENERATED FROM PYTHON SOURCE LINES 140-151 .. code-block:: default scenario_doe = create_scenario( scenario_adapter, formulation="DisciplinaryOpt", objective_name="obj1", design_space=design_space_doe, scenario_type="DOE", ) scenario_doe.add_constraint("cstr3", "eq") scenario_doe.add_observable("obj2") .. GENERATED FROM PYTHON SOURCE LINES 152-155 Run the scenario ---------------- Finally, we run a full-factorial DOE using 100 samples and we run the post-processing. .. GENERATED FROM PYTHON SOURCE LINES 155-160 .. code-block:: default run_inputs = {"n_samples": 50, "algo": "fullfact"} scenario_doe.execute(run_inputs) scenario_doe.post_process( "ParetoFront", objectives=["obj1", "obj2"], save=False, show=True ) .. image-sg:: /examples/post_process/algorithms/images/sphx_glr_plot_pareto_front_binhkorn_bilevel_001.png :alt: Pareto front :srcset: /examples/post_process/algorithms/images/sphx_glr_plot_pareto_front_binhkorn_bilevel_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 16:55:31: INFO - 16:55:31: *** Start DOEScenario execution *** INFO - 16:55:31: DOEScenario INFO - 16:55:31: Disciplines: MDOScenario_adapter INFO - 16:55:31: MDO formulation: DisciplinaryOpt INFO - 16:55:31: Optimization problem: INFO - 16:55:31: minimize obj1(obj1_target) INFO - 16:55:31: with respect to obj1_target INFO - 16:55:31: subject to constraints: INFO - 16:55:31: cstr3(obj1_target) == 0.0 INFO - 16:55:31: over the design space: INFO - 16:55:31: +-------------+-------------+-------+-------------+-------+ INFO - 16:55:31: | name | lower_bound | value | upper_bound | type | INFO - 16:55:31: +-------------+-------------+-------+-------------+-------+ INFO - 16:55:31: | obj1_target | 0.1 | 1 | 100 | float | INFO - 16:55:31: +-------------+-------------+-------+-------------+-------+ INFO - 16:55:31: Solving optimization problem with algorithm fullfact: INFO - 16:55:31: ... 0%| | 0/50 [00:00 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 2.288 seconds) .. _sphx_glr_download_examples_post_process_algorithms_plot_pareto_front_binhkorn_bilevel.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_pareto_front_binhkorn_bilevel.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_pareto_front_binhkorn_bilevel.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_