.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/constraint_aggregation/constraint_aggregation_example.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_constraint_aggregation_constraint_aggregation_example.py: Examples for constraint aggregation =================================== .. GENERATED FROM PYTHON SOURCE LINES 20-32 .. code-block:: default from __future__ import annotations from copy import deepcopy from gemseo.algos.design_space import DesignSpace from gemseo.algos.design_space import DesignVariableType from gemseo.api import configure_logger from gemseo.api import create_scenario from gemseo.disciplines.analytic import AnalyticDiscipline from gemseo.disciplines.concatenater import Concatenater configure_logger() .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 33-34 Number of constraints .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: default N = 100 .. GENERATED FROM PYTHON SOURCE LINES 37-38 Build the discipline .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: default constraint_names = [f"g_{k + 1}" for k in range(N)] function_names = ["o"] + constraint_names function_expressions = ["y"] + [f"{k + 1}*x*exp(1-{k + 1}*x)-y" for k in range(N)] disc = AnalyticDiscipline( name="function", expressions=dict(zip(function_names, function_expressions)), ) # This step is required to put all constraints needed for aggregation in one variable. concat = Concatenater(constraint_names, "g") .. GENERATED FROM PYTHON SOURCE LINES 49-50 Build the design space .. GENERATED FROM PYTHON SOURCE LINES 50-57 .. code-block:: default ds = DesignSpace() ds.add_variable( "x", l_b=0.0, u_b=1, value=1.0 / N / 2.0, var_type=DesignVariableType.FLOAT ) ds.add_variable("y", l_b=0.0, u_b=1, value=1, var_type=DesignVariableType.FLOAT) ds_new = deepcopy(ds) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Build the optimization solver options .. GENERATED FROM PYTHON SOURCE LINES 59-78 .. code-block:: default max_iter = 1000 ineq_tol = 1e-5 convergence_tol = 1e-8 normalize = True algo_options = { "algo": "NLOPT_MMA", "max_iter": max_iter, "algo_options": { "ineq_tolerance": ineq_tol, "eq_tolerance": ineq_tol, "xtol_rel": convergence_tol, "xtol_abs": convergence_tol, "ftol_rel": convergence_tol, "ftol_abs": convergence_tol, "ctol_abs": convergence_tol, "normalize_design_space": normalize, }, } .. GENERATED FROM PYTHON SOURCE LINES 79-80 Build the optimization scenario .. GENERATED FROM PYTHON SOURCE LINES 80-93 .. code-block:: default original_scenario = create_scenario( disciplines=[disc, concat], formulation="DisciplinaryOpt", objective_name="o", design_space=ds, maximize_objective=False, ) original_scenario.add_constraint("g", "ineq") original_scenario.execute(algo_options) # Without constraint aggregation MMA iterations become more expensive, when a # large number of constraints are activated. .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 17:00:15: INFO - 17:00:15: *** Start MDOScenario execution *** INFO - 17:00:15: MDOScenario INFO - 17:00:15: Disciplines: Concatenater function INFO - 17:00:15: MDO formulation: DisciplinaryOpt INFO - 17:00:15: Optimization problem: INFO - 17:00:15: minimize o(x, y) INFO - 17:00:15: with respect to x, y INFO - 17:00:15: subject to constraints: INFO - 17:00:15: g(x, y) <= 0.0 INFO - 17:00:15: over the design space: INFO - 17:00:15: +------+-------------+-------+-------------+-------+ INFO - 17:00:15: | name | lower_bound | value | upper_bound | type | INFO - 17:00:15: +------+-------------+-------+-------------+-------+ INFO - 17:00:15: | x | 0 | 0.005 | 1 | float | INFO - 17:00:15: | y | 0 | 1 | 1 | float | INFO - 17:00:15: +------+-------------+-------+-------------+-------+ INFO - 17:00:15: Solving optimization problem with algorithm NLOPT_MMA: INFO - 17:00:15: ... 0%| | 0/1000 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: constraint_aggregation_example.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_