.. 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-33 .. 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.algos.opt.nlopt.settings.nlopt_mma_settings import NLOPT_MMA_Settings 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 34-35 Number of constraints .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python N = 100 .. GENERATED FROM PYTHON SOURCE LINES 38-39 Build the discipline .. GENERATED FROM PYTHON SOURCE LINES 39-49 .. 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 50-51 Build the design space .. GENERATED FROM PYTHON SOURCE LINES 51-68 .. code-block:: Python ds = DesignSpace() ds.add_variable( "x", lower_bound=0.0, upper_bound=1, value=1.0 / N / 2.0, type_=DesignSpace.DesignVariableType.FLOAT, ) ds.add_variable( "y", lower_bound=0.0, upper_bound=1, value=1, type_=DesignSpace.DesignVariableType.FLOAT, ) ds_new = deepcopy(ds) .. GENERATED FROM PYTHON SOURCE LINES 69-70 Build the optimization solver settings .. GENERATED FROM PYTHON SOURCE LINES 70-81 .. code-block:: Python mma_settings = NLOPT_MMA_Settings( ineq_tolerance=1e-5, eq_tolerance=1e-5, xtol_rel=1e-8, xtol_abs=1e-8, ftol_rel=1e-8, ftol_abs=1e-8, normalize_design_space=True, max_iter=1000, ) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Build the optimization scenario .. GENERATED FROM PYTHON SOURCE LINES 83-96 .. code-block:: Python original_scenario = create_scenario( [disc, concat], "o", ds, maximize_objective=False, formulation_name="DisciplinaryOpt", ) original_scenario.add_constraint("g", constraint_type="ineq") original_scenario.execute(mma_settings) # 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 - 08:35:32: INFO - 08:35:32: *** Start MDOScenario execution *** INFO - 08:35:32: MDOScenario INFO - 08:35:32: Disciplines: Concatenater function INFO - 08:35:32: MDO formulation: DisciplinaryOpt INFO - 08:35:32: Optimization problem: INFO - 08:35:32: minimize o(x, y) INFO - 08:35:32: with respect to x, y INFO - 08:35:32: subject to constraints: INFO - 08:35:32: g(x, y) <= 0 INFO - 08:35:32: over the design space: INFO - 08:35:32: +------+-------------+-------+-------------+-------+ INFO - 08:35:32: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:35:32: +------+-------------+-------+-------------+-------+ INFO - 08:35:32: | x | 0 | 0.005 | 1 | float | INFO - 08:35:32: | y | 0 | 1 | 1 | float | INFO - 08:35:32: +------+-------------+-------+-------------+-------+ INFO - 08:35:32: Solving optimization problem with algorithm NLOPT_MMA: INFO - 08:35:32: 1%| | 6/1000 [00:00<00:51, 19.41 it/sec, obj=0.00931] INFO - 08:35:32: 1%| | 7/1000 [00:00<00:49, 20.25 it/sec, obj=8.28e-5] INFO - 08:35:32: 1%| | 8/1000 [00:00<00:47, 21.00 it/sec, obj=5.2e-9] INFO - 08:35:45: 1%| | 9/1000 [00:12<23:50, 41.57 it/min, obj=0] INFO - 08:35:45: Optimization result: INFO - 08:35:45: Optimizer info: INFO - 08:35:45: Status: 5 INFO - 08:35:45: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached INFO - 08:35:45: Number of calls to the objective function by the optimizer: 1501 INFO - 08:35:45: Solution: INFO - 08:35:45: The solution is feasible. INFO - 08:35:45: Objective: 0.0 INFO - 08:35:45: Standardized constraints: INFO - 08:35:45: 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 - 08:35:45: 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 - 08:35:45: 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 - 08:35:45: 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 - 08:35:45: 0. 0. 0. 0.] INFO - 08:35:45: Design space: INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | x | 0 | 0 | 1 | float | INFO - 08:35:45: | y | 0 | 0 | 1 | float | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: *** End MDOScenario execution (time: 0:00:12.997006) *** .. GENERATED FROM PYTHON SOURCE LINES 97-98 exploiting constraint aggregation on the same scenario: .. GENERATED FROM PYTHON SOURCE LINES 98-107 .. code-block:: Python new_scenario = create_scenario( [disc, concat], "o", ds_new, maximize_objective=False, formulation_name="DisciplinaryOpt", ) new_scenario.add_constraint("g", constraint_type="ineq") .. GENERATED FROM PYTHON SOURCE LINES 108-109 This method aggregates the constraints using the lower bound KS function .. GENERATED FROM PYTHON SOURCE LINES 109-114 .. code-block:: Python new_scenario.formulation.optimization_problem.constraints.aggregate( 0, method="lower_bound_KS", rho=10.0 ) new_scenario.execute(mma_settings) .. rst-class:: sphx-glr-script-out .. code-block:: none INFO - 08:35:45: INFO - 08:35:45: *** Start MDOScenario execution *** INFO - 08:35:45: MDOScenario INFO - 08:35:45: Disciplines: Concatenater function INFO - 08:35:45: MDO formulation: DisciplinaryOpt INFO - 08:35:45: Optimization problem: INFO - 08:35:45: minimize o(x, y) INFO - 08:35:45: with respect to x, y INFO - 08:35:45: subject to constraints: INFO - 08:35:45: lower_bound_KS() <= 0.0 INFO - 08:35:45: over the design space: INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | x | 0 | 0.005 | 1 | float | INFO - 08:35:45: | y | 0 | 1 | 1 | float | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: Solving optimization problem with algorithm NLOPT_MMA: INFO - 08:35:45: 1%| | 6/1000 [00:00<00:31, 31.63 it/sec, obj=0.00773] INFO - 08:35:45: 1%| | 7/1000 [00:00<00:30, 32.29 it/sec, obj=5.72e-5] INFO - 08:35:45: 1%| | 8/1000 [00:00<00:30, 32.84 it/sec, obj=2.63e-9] INFO - 08:35:45: 1%| | 9/1000 [00:00<00:53, 18.44 it/sec, obj=0] INFO - 08:35:45: Optimization result: INFO - 08:35:45: Optimizer info: INFO - 08:35:45: Status: 5 INFO - 08:35:45: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached INFO - 08:35:45: Number of calls to the objective function by the optimizer: 1501 INFO - 08:35:45: Solution: INFO - 08:35:45: The solution is feasible. INFO - 08:35:45: Objective: 0.0 INFO - 08:35:45: Standardized constraints: INFO - 08:35:45: lower_bound_KS(g) = 2.7755575615628914e-16 INFO - 08:35:45: Design space: INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | Name | Lower bound | Value | Upper bound | Type | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: | x | 0 | 0 | 1 | float | INFO - 08:35:45: | y | 0 | 0 | 1 | float | INFO - 08:35:45: +------+-------------+-------+-------------+-------+ INFO - 08:35:45: *** End MDOScenario execution (time: 0:00:00.491943) *** .. GENERATED FROM PYTHON SOURCE LINES 115-116 with constraint aggregation the last iteration is faster. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 14.851 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 ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_constraint_aggregation_example.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_