.. 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 33-37 Import ------ The first step is to import some functions from the API, and to configure the logger. .. GENERATED FROM PYTHON SOURCE LINES 37-54 .. code-block:: default from __future__ import unicode_literals from matplotlib import pyplot as plt from numpy import array from gemseo.api import ( configure_logger, create_design_space, create_discipline, create_scenario, ) from gemseo.core.mdo_scenario import MDOScenarioAdapter configure_logger() .. rst-class:: sphx-glr-script-out Out: .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 55-61 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 61-69 .. 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 70-71 This constraint will be used to set `obj1` to a target value for the lower-level scenario. .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: default expr_cstr_obj1_target = {"cstr3": "obj1 - obj1_target"} .. GENERATED FROM PYTHON SOURCE LINES 75-79 Instantiation of the disciplines -------------------------------- Here, we create the disciplines from their expressions. .. GENERATED FROM PYTHON SOURCE LINES 79-87 .. code-block:: default discipline_binh_korn = create_discipline( "AnalyticDiscipline", expressions_dict=expr_binh_korn ) discipline_cstr_obj1 = create_discipline( "AnalyticDiscipline", expressions_dict=expr_cstr_obj1_target ) .. GENERATED FROM PYTHON SOURCE LINES 88-90 Definition of the lower-level design space ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 90-100 .. 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 101-104 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 104-114 .. 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 115-116 We add the Binh and Korn problem constraints. .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: default scenario.add_constraint("cstr1", "ineq") scenario.add_constraint("cstr2", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 120-121 We add a constraint to force the value of `obj1` to `obj1_target`. .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: default scenario.add_constraint("cstr3", "eq") .. GENERATED FROM PYTHON SOURCE LINES 124-132 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 132-141 .. 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 142-148 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 148-159 .. 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 160-163 Run the scenario ---------------- Finally, we run a full-factorial DOE using 100 samples and we run the post-processing. .. GENERATED FROM PYTHON SOURCE LINES 163-169 .. 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:: /examples/post_process/images/sphx_glr_plot_pareto_front_binhkorn_bilevel_001.png :alt: Pareto front :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none INFO - 21:51:33: INFO - 21:51:33: *** Start DOE Scenario execution *** INFO - 21:51:33: DOEScenario INFO - 21:51:33: Disciplines: MDOScenario_adapter INFO - 21:51:33: MDOFormulation: DisciplinaryOpt INFO - 21:51:33: Algorithm: fullfact INFO - 21:51:33: Optimization problem: INFO - 21:51:33: Minimize: obj1(obj1_target) INFO - 21:51:33: With respect to: obj1_target INFO - 21:51:33: Subject to constraints: INFO - 21:51:33: cstr3(obj1_target) == 0.0 INFO - 21:51:33: Full factorial design required. Number of samples along each direction for a design vector of size 1 with 50 samples: 50 INFO - 21:51:33: Final number of samples for DOE = 50 vs 50 requested INFO - 21:51:33: DOE sampling: 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 `_