Note
Go to the end to download the full example code.
Examples for constraint aggregation#
from __future__ import annotations
from copy import deepcopy
from gemseo import create_scenario
from gemseo.algos.design_space import DesignSpace
from gemseo.disciplines.analytic import AnalyticDiscipline
from gemseo.disciplines.concatenater import Concatenater
from gemseo.settings.opt import NLOPT_MMA_Settings
Number of constraints
N = 100
Build the discipline
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, strict=False)),
)
# This step is required to put all constraints needed for aggregation in one variable.
concat = Concatenater(constraint_names, "g")
Build the design space
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)
Build the optimization solver settings
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,
)
Build the optimization scenario
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.
INFO - 16:21:13: *** Start MDOScenario execution ***
INFO - 16:21:13: MDOScenario
INFO - 16:21:13: Disciplines: Concatenater function
INFO - 16:21:13: MDO formulation: DisciplinaryOpt
INFO - 16:21:13: Optimization problem:
INFO - 16:21:13: minimize o(x, y)
INFO - 16:21:13: with respect to x, y
INFO - 16:21:13: under the inequality constraints
INFO - 16:21:13: g(x, y) <= 0
INFO - 16:21:13: over the design space:
INFO - 16:21:13: +------+-------------+-------+-------------+-------+
INFO - 16:21:13: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:21:13: +------+-------------+-------+-------------+-------+
INFO - 16:21:13: | x | 0 | 0.005 | 1 | float |
INFO - 16:21:13: | y | 0 | 1 | 1 | float |
INFO - 16:21:13: +------+-------------+-------+-------------+-------+
INFO - 16:21:13: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 16:21:14: 1%| | 6/1000 [00:00<00:28, 35.22 it/sec, feas=True, obj=0.00931]
INFO - 16:21:14: 1%| | 7/1000 [00:00<00:27, 36.72 it/sec, feas=True, obj=8.28e-5]
INFO - 16:21:14: 1%| | 8/1000 [00:00<00:26, 38.11 it/sec, feas=True, obj=5.51e-9]
INFO - 16:21:20: 1%| | 9/1000 [00:06<12:28, 1.32 it/sec, feas=True, obj=0]
INFO - 16:21:20: Optimization result:
INFO - 16:21:20: Optimizer info:
INFO - 16:21:20: Status: 5
INFO - 16:21:20: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 16:21:20: Solution:
INFO - 16:21:20: The solution is feasible.
INFO - 16:21:20: Objective: 0.0
INFO - 16:21:20: Standardized constraints:
INFO - 16:21:20: 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 - 16:21:20: 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 - 16:21:20: 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 - 16:21:20: 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 - 16:21:20: 0. 0. 0. 0.]
INFO - 16:21:20: Design space:
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: | x | 0 | 0 | 1 | float |
INFO - 16:21:20: | y | 0 | 0 | 1 | float |
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: *** End MDOScenario execution ***
exploiting constraint aggregation on the same scenario:
new_scenario = create_scenario(
[disc, concat],
"o",
ds_new,
maximize_objective=False,
formulation_name="DisciplinaryOpt",
)
new_scenario.add_constraint("g", constraint_type="ineq")
This method aggregates the constraints using the lower bound KS function
new_scenario.formulation.optimization_problem.constraints.aggregate(
0, method="lower_bound_KS", rho=10.0
)
new_scenario.execute(mma_settings)
INFO - 16:21:20: *** Start MDOScenario execution ***
INFO - 16:21:20: MDOScenario
INFO - 16:21:20: Disciplines: Concatenater function
INFO - 16:21:20: MDO formulation: DisciplinaryOpt
INFO - 16:21:20: Optimization problem:
INFO - 16:21:20: minimize o(x, y)
INFO - 16:21:20: with respect to x, y
INFO - 16:21:20: under the inequality constraints
INFO - 16:21:20: lower_bound_KS() <= 0.0
INFO - 16:21:20: over the design space:
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: | x | 0 | 0.005 | 1 | float |
INFO - 16:21:20: | y | 0 | 1 | 1 | float |
INFO - 16:21:20: +------+-------------+-------+-------------+-------+
INFO - 16:21:20: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 16:21:20: 1%| | 6/1000 [00:00<00:14, 67.74 it/sec, feas=True, obj=0.00773]
INFO - 16:21:20: 1%| | 7/1000 [00:00<00:14, 67.66 it/sec, feas=True, obj=5.72e-5]
INFO - 16:21:20: 1%| | 8/1000 [00:00<00:14, 67.68 it/sec, feas=True, obj=2.6e-9]
INFO - 16:21:21: 1%| | 9/1000 [00:00<00:28, 35.16 it/sec, feas=True, obj=0]
INFO - 16:21:21: Optimization result:
INFO - 16:21:21: Optimizer info:
INFO - 16:21:21: Status: 5
INFO - 16:21:21: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 16:21:21: Solution:
INFO - 16:21:21: The solution is feasible.
INFO - 16:21:21: Objective: 0.0
INFO - 16:21:21: Standardized constraints:
INFO - 16:21:21: lower_bound_KS(g) = 2.7755575615628914e-16
INFO - 16:21:21: Design space:
INFO - 16:21:21: +------+-------------+-------+-------------+-------+
INFO - 16:21:21: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:21:21: +------+-------------+-------+-------------+-------+
INFO - 16:21:21: | x | 0 | 0 | 1 | float |
INFO - 16:21:21: | y | 0 | 0 | 1 | float |
INFO - 16:21:21: +------+-------------+-------+-------------+-------+
INFO - 16:21:21: *** End MDOScenario execution ***
with constraint aggregation the last iteration is faster.
Total running time of the script: (0 minutes 7.863 seconds)