.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/constraint_aggregation/plot_constraint_aggregation_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_constraint_aggregation_plot_constraint_aggregation_example.py: Examples for constraint aggregation =================================== .. GENERATED FROM PYTHON SOURCE LINES 20-32 .. code-block:: Python from __future__ import annotations from copy import deepcopy from gemseo import configure_logger from gemseo import create_scenario from gemseo.algos.design_space import DesignSpace 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:: Python N = 100 .. GENERATED FROM PYTHON SOURCE LINES 37-38 Build the discipline .. GENERATED FROM PYTHON SOURCE LINES 38-48 .. code-block:: Python 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-63 .. code-block:: Python ds = DesignSpace() ds.add_variable( "x", l_b=0.0, u_b=1, value=1.0 / N / 2.0, var_type=DesignSpace.DesignVariableType.FLOAT, ) ds.add_variable( "y", l_b=0.0, u_b=1, value=1, var_type=DesignSpace.DesignVariableType.FLOAT ) ds_new = deepcopy(ds) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Build the optimization solver options .. GENERATED FROM PYTHON SOURCE LINES 65-84 .. code-block:: Python 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 85-86 Build the optimization scenario .. GENERATED FROM PYTHON SOURCE LINES 86-99 .. code-block:: Python 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 - 10:53:07: INFO - 10:53:07: *** Start MDOScenario execution *** INFO - 10:53:07: MDOScenario INFO - 10:53:07: Disciplines: Concatenater function INFO - 10:53:07: MDO formulation: DisciplinaryOpt INFO - 10:53:07: Optimization problem: INFO - 10:53:07: minimize o(x, y) INFO - 10:53:07: with respect to x, y INFO - 10:53:07: subject to constraints: INFO - 10:53:07: g(x, y) <= 0.0 INFO - 10:53:07: over the design space: INFO - 10:53:07: +------+-------------+-------+-------------+-------+ INFO - 10:53:07: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:07: +------+-------------+-------+-------------+-------+ INFO - 10:53:07: | x | 0 | 0.005 | 1 | float | INFO - 10:53:07: | y | 0 | 1 | 1 | float | INFO - 10:53:07: +------+-------------+-------+-------------+-------+ INFO - 10:53:07: Solving optimization problem with algorithm NLOPT_MMA: INFO - 10:53:08: 1%| | 6/1000 [00:00<01:23, 11.94 it/sec, obj=0.00931] INFO - 10:53:08: 1%| | 7/1000 [00:00<01:19, 12.50 it/sec, obj=8.28e-5] INFO - 10:53:08: 1%| | 8/1000 [00:00<01:16, 12.99 it/sec, obj=5.2e-9] INFO - 10:53:23: 1%| | 9/1000 [00:16<29:42, 33.35 it/min, obj=0] INFO - 10:53:23: Optimization result: INFO - 10:53:23: Optimizer info: INFO - 10:53:23: Status: 5 INFO - 10:53:23: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached INFO - 10:53:23: Number of calls to the objective function by the optimizer: 1501 INFO - 10:53:23: Solution: INFO - 10:53:23: The solution is feasible. INFO - 10:53:23: Objective: 0.0 INFO - 10:53:23: Standardized constraints: INFO - 10:53:23: g = [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. INFO - 10:53:23: 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. INFO - 10:53:23: 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. INFO - 10:53:23: 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. INFO - 10:53:23: 0. 0. 0. 0.] INFO - 10:53:23: Design space: INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: | x | 0 | 0 | 1 | float | INFO - 10:53:23: | y | 0 | 0 | 1 | float | INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: *** End MDOScenario execution (time: 0:00:16.206413) *** {'max_iter': 1000, 'algo_options': {'ineq_tolerance': 1e-05, 'eq_tolerance': 1e-05, 'xtol_rel': 1e-08, 'xtol_abs': 1e-08, 'ftol_rel': 1e-08, 'ftol_abs': 1e-08, 'ctol_abs': 1e-08, 'normalize_design_space': True}, 'algo': 'NLOPT_MMA'} .. GENERATED FROM PYTHON SOURCE LINES 100-101 exploiting constraint aggregation on the same scenario: .. GENERATED FROM PYTHON SOURCE LINES 101-110 .. code-block:: Python new_scenario = create_scenario( disciplines=[disc, concat], formulation="DisciplinaryOpt", objective_name="o", design_space=ds_new, maximize_objective=False, ) new_scenario.add_constraint("g", "ineq") .. GENERATED FROM PYTHON SOURCE LINES 111-112 This method aggregates the constraints using the lower bound KS function .. GENERATED FROM PYTHON SOURCE LINES 112-117 .. code-block:: Python new_scenario.formulation.opt_problem.aggregate_constraint( 0, method="lower_bound_KS", rho=10.0 ) new_scenario.execute(algo_options) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 10:53:23: INFO - 10:53:23: *** Start MDOScenario execution *** INFO - 10:53:23: MDOScenario INFO - 10:53:23: Disciplines: Concatenater function INFO - 10:53:23: MDO formulation: DisciplinaryOpt INFO - 10:53:23: Optimization problem: INFO - 10:53:23: minimize o(x, y) INFO - 10:53:23: with respect to x, y INFO - 10:53:23: subject to constraints: INFO - 10:53:23: lower_bound_KS() <= 0.0 INFO - 10:53:23: over the design space: INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: | x | 0 | 0.005 | 1 | float | INFO - 10:53:23: | y | 0 | 1 | 1 | float | INFO - 10:53:23: +------+-------------+-------+-------------+-------+ INFO - 10:53:23: Solving optimization problem with algorithm NLOPT_MMA: INFO - 10:53:24: 1%| | 6/1000 [00:00<01:00, 16.53 it/sec, obj=0.00773] INFO - 10:53:24: 1%| | 7/1000 [00:00<00:58, 17.08 it/sec, obj=5.72e-5] INFO - 10:53:24: 1%| | 8/1000 [00:00<00:56, 17.52 it/sec, obj=2.63e-9] INFO - 10:53:24: 1%| | 9/1000 [00:00<01:23, 11.89 it/sec, obj=0] INFO - 10:53:24: Optimization result: INFO - 10:53:24: Optimizer info: INFO - 10:53:24: Status: 5 INFO - 10:53:24: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached INFO - 10:53:24: Number of calls to the objective function by the optimizer: 1501 INFO - 10:53:24: Solution: INFO - 10:53:24: The solution is feasible. INFO - 10:53:24: Objective: 0.0 INFO - 10:53:24: Standardized constraints: INFO - 10:53:24: lower_bound_KS(g) = 2.7755575615628914e-16 INFO - 10:53:24: Design space: INFO - 10:53:24: +------+-------------+-------+-------------+-------+ INFO - 10:53:24: | Name | Lower bound | Value | Upper bound | Type | INFO - 10:53:24: +------+-------------+-------+-------------+-------+ INFO - 10:53:24: | x | 0 | 0 | 1 | float | INFO - 10:53:24: | y | 0 | 0 | 1 | float | INFO - 10:53:24: +------+-------------+-------+-------------+-------+ INFO - 10:53:24: *** End MDOScenario execution (time: 0:00:00.771464) *** {'max_iter': 1000, 'algo_options': {'ineq_tolerance': 1e-05, 'eq_tolerance': 1e-05, 'xtol_rel': 1e-08, 'xtol_abs': 1e-08, 'ftol_rel': 1e-08, 'ftol_abs': 1e-08, 'ctol_abs': 1e-08, 'normalize_design_space': True}, 'algo': 'NLOPT_MMA'} .. GENERATED FROM PYTHON SOURCE LINES 118-119 with constraint aggregation the last iteration is faster. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 18.618 seconds) .. _sphx_glr_download_examples_constraint_aggregation_plot_constraint_aggregation_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_constraint_aggregation_example.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_constraint_aggregation_example.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_