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 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()
<RootLogger root (INFO)>
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)),
)
# 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",
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)
Build the optimization solver options
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,
},
}
Build the optimization scenario
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.
INFO - 08:23:09:
INFO - 08:23:09: *** Start MDOScenario execution ***
INFO - 08:23:09: MDOScenario
INFO - 08:23:09: Disciplines: Concatenater function
INFO - 08:23:09: MDO formulation: DisciplinaryOpt
INFO - 08:23:09: Optimization problem:
INFO - 08:23:09: minimize o(x, y)
INFO - 08:23:09: with respect to x, y
INFO - 08:23:09: subject to constraints:
INFO - 08:23:09: g(x, y) <= 0.0
INFO - 08:23:09: over the design space:
INFO - 08:23:09: +------+-------------+-------+-------------+-------+
INFO - 08:23:09: | name | lower_bound | value | upper_bound | type |
INFO - 08:23:09: +------+-------------+-------+-------------+-------+
INFO - 08:23:09: | x | 0 | 0.005 | 1 | float |
INFO - 08:23:09: | y | 0 | 1 | 1 | float |
INFO - 08:23:09: +------+-------------+-------+-------------+-------+
INFO - 08:23:09: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 08:23:09: ... 0%| | 0/1000 [00:00<?, ?it]
INFO - 08:23:09: ... 0%| | 1/1000 [00:00<01:45, 9.45 it/sec, obj=1]
INFO - 08:23:10: ... 0%| | 2/1000 [00:00<02:12, 7.51 it/sec, obj=0.866]
INFO - 08:23:10: ... 0%| | 3/1000 [00:00<01:47, 9.24 it/sec, obj=0.616]
INFO - 08:23:10: ... 0%| | 4/1000 [00:00<01:35, 10.44 it/sec, obj=0.312]
INFO - 08:23:10: ... 0%| | 5/1000 [00:00<01:27, 11.34 it/sec, obj=0.0944]
INFO - 08:23:10: ... 1%| | 6/1000 [00:00<01:22, 12.02 it/sec, obj=0.00931]
INFO - 08:23:10: ... 1%| | 7/1000 [00:00<01:19, 12.53 it/sec, obj=8.28e-5]
INFO - 08:23:10: ... 1%| | 8/1000 [00:00<01:16, 12.95 it/sec, obj=5.2e-9]
INFO - 08:23:26: ... 1%| | 9/1000 [00:17<31:13, 31.73 it/min, obj=0]
INFO - 08:23:26: Optimization result:
INFO - 08:23:26: Optimizer info:
INFO - 08:23:26: Status: 5
INFO - 08:23:26: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 08:23:26: Number of calls to the objective function by the optimizer: 1501
INFO - 08:23:26: Solution:
INFO - 08:23:26: The solution is feasible.
INFO - 08:23:26: Objective: 0.0
INFO - 08:23:26: Standardized constraints:
INFO - 08:23:26: 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:23:26: 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:23:26: 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:23:26: 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:23:26: 0. 0. 0. 0.]
INFO - 08:23:26: Design space:
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: | name | lower_bound | value | upper_bound | type |
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: | x | 0 | 0 | 1 | float |
INFO - 08:23:26: | y | 0 | 0 | 1 | float |
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: *** End MDOScenario execution (time: 0:00:17.033640) ***
{'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'}
exploiting constraint aggregation on the same scenario:
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")
This method aggregates the constraints using the KS function
new_scenario.formulation.opt_problem.aggregate_constraint(0, method="KS", rho=10.0)
new_scenario.execute(algo_options)
INFO - 08:23:26:
INFO - 08:23:26: *** Start MDOScenario execution ***
INFO - 08:23:26: MDOScenario
INFO - 08:23:26: Disciplines: Concatenater function
INFO - 08:23:26: MDO formulation: DisciplinaryOpt
INFO - 08:23:26: Optimization problem:
INFO - 08:23:26: minimize o(x, y)
INFO - 08:23:26: with respect to x, y
INFO - 08:23:26: subject to constraints:
INFO - 08:23:26: KS() <= 0.0
INFO - 08:23:26: over the design space:
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: | name | lower_bound | value | upper_bound | type |
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: | x | 0 | 0.005 | 1 | float |
INFO - 08:23:26: | y | 0 | 1 | 1 | float |
INFO - 08:23:26: +------+-------------+-------+-------------+-------+
INFO - 08:23:26: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 08:23:26: ... 0%| | 0/1000 [00:00<?, ?it]
INFO - 08:23:26: ... 0%| | 1/1000 [00:00<00:38, 25.72 it/sec, obj=1]
INFO - 08:23:27: ... 0%| | 2/1000 [00:00<01:26, 11.53 it/sec, obj=0.866]
INFO - 08:23:27: ... 0%| | 3/1000 [00:00<01:13, 13.58 it/sec, obj=0.592]
INFO - 08:23:27: ... 0%| | 4/1000 [00:00<01:06, 14.89 it/sec, obj=0.295]
INFO - 08:23:27: ... 0%| | 5/1000 [00:00<01:02, 15.79 it/sec, obj=0.0857]
INFO - 08:23:27: ... 1%| | 6/1000 [00:00<01:00, 16.46 it/sec, obj=0.00773]
INFO - 08:23:27: ... 1%| | 7/1000 [00:00<00:58, 17.00 it/sec, obj=5.72e-5]
INFO - 08:23:27: ... 1%| | 8/1000 [00:00<00:56, 17.41 it/sec, obj=2.62e-9]
INFO - 08:23:27: ... 1%| | 9/1000 [00:00<01:25, 11.61 it/sec, obj=0]
INFO - 08:23:27: Optimization result:
INFO - 08:23:27: Optimizer info:
INFO - 08:23:27: Status: 5
INFO - 08:23:27: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 08:23:27: Number of calls to the objective function by the optimizer: 1501
INFO - 08:23:27: Solution:
INFO - 08:23:27: The solution is feasible.
INFO - 08:23:27: Objective: 0.0
INFO - 08:23:27: Standardized constraints:
INFO - 08:23:27: KS(g) = 4.440892098500626e-16
INFO - 08:23:27: Design space:
INFO - 08:23:27: +------+-------------+-------+-------------+-------+
INFO - 08:23:27: | name | lower_bound | value | upper_bound | type |
INFO - 08:23:27: +------+-------------+-------+-------------+-------+
INFO - 08:23:27: | x | 0 | 0 | 1 | float |
INFO - 08:23:27: | y | 0 | 0 | 1 | float |
INFO - 08:23:27: +------+-------------+-------+-------------+-------+
INFO - 08:23:27: *** End MDOScenario execution (time: 0:00:00.787316) ***
{'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'}
with constraint aggregation the last iteration is faster.
Total running time of the script: (0 minutes 19.355 seconds)