.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/post_process/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_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-45 .. code-block:: default 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 matplotlib import pyplot as plt from numpy import array configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 46-52 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 52-60 .. 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 61-62 This constraint will be used to set `obj1` to a target value for the lower-level scenario. .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: default expr_cstr_obj1_target = {"cstr3": "obj1 - obj1_target"} .. GENERATED FROM PYTHON SOURCE LINES 66-70 Instantiation of the disciplines -------------------------------- Here, we create the disciplines from their expressions. .. GENERATED FROM PYTHON SOURCE LINES 70-78 .. 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 79-81 Definition of the lower-level design space ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 81-91 .. 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 92-95 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 95-105 .. 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 106-107 We add the Binh and Korn problem constraints. .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: default scenario.add_constraint("cstr1", "ineq") scenario.add_constraint("cstr2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 111-112 We add a constraint to force the value of `obj1` to `obj1_target`. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default scenario.add_constraint("cstr3", "eq") .. GENERATED FROM PYTHON SOURCE LINES 115-123 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 123-132 .. 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 133-139 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 139-150 .. 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 151-154 Run the scenario ---------------- Finally, we run a full-factorial DOE using 100 samples and we run the post-processing. .. GENERATED FROM PYTHON SOURCE LINES 154-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=False ) plt.show() .. image-sg:: /examples/post_process/images/sphx_glr_plot_pareto_front_binhkorn_bilevel_001.png :alt: Pareto front :srcset: /examples/post_process/images/sphx_glr_plot_pareto_front_binhkorn_bilevel_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 07:14:58: INFO - 07:14:58: *** Start DOEScenario execution *** INFO - 07:14:58: DOEScenario INFO - 07:14:58: Disciplines: MDOScenario_adapter INFO - 07:14:58: MDO formulation: DisciplinaryOpt INFO - 07:14:58: Optimization problem: INFO - 07:14:58: minimize obj1(obj1_target) INFO - 07:14:58: with respect to obj1_target INFO - 07:14:58: subject to constraints: INFO - 07:14:58: cstr3(obj1_target) == 0.0 INFO - 07:14:58: over the design space: INFO - 07:14:58: +-------------+-------------+-------+-------------+-------+ INFO - 07:14:58: | name | lower_bound | value | upper_bound | type | INFO - 07:14:58: +-------------+-------------+-------+-------------+-------+ INFO - 07:14:58: | obj1_target | 0.1 | 1 | 100 | float | INFO - 07:14:58: +-------------+-------------+-------+-------------+-------+ INFO - 07:14:58: Solving optimization problem with algorithm fullfact: INFO - 07:14:58: Full factorial design required. Number of samples along each direction for a design vector of size 1 with 50 samples: 50 INFO - 07:14:58: Final number of samples for DOE = 50 vs 50 requested INFO - 07:14:58: ... 0%| | 0/50 [00:00` .. 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 `_