Note
Click here to download the full example code
Examples for constraint aggregation¶
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()
<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=DesignVariableType.FLOAT
)
ds.add_variable("y", l_b=0.0, u_b=1, value=1, var_type=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 - 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<?, ?it]
INFO - 17:00:15: ... 0%| | 1/1000 [00:00<01:43, 9.65 it/sec, obj=1]
INFO - 17:00:15: ... 0%| | 2/1000 [00:00<01:22, 12.04 it/sec, obj=0.866]
INFO - 17:00:15: ... 0%| | 3/1000 [00:00<01:04, 15.56 it/sec, obj=0.616]
INFO - 17:00:15: ... 0%| | 4/1000 [00:00<00:54, 18.26 it/sec, obj=0.312]
INFO - 17:00:15: ... 0%| | 5/1000 [00:00<00:48, 20.36 it/sec, obj=0.0944]
INFO - 17:00:15: ... 1%| | 6/1000 [00:00<00:45, 21.99 it/sec, obj=0.00931]
INFO - 17:00:16: ... 1%| | 7/1000 [00:00<00:42, 23.33 it/sec, obj=8.28e-5]
INFO - 17:00:16: ... 1%| | 8/1000 [00:00<00:40, 24.47 it/sec, obj=5.2e-9]
INFO - 17:00:32: ... 1%| | 9/1000 [00:17<31:23, 31.57 it/min, obj=0]
INFO - 17:00:32: Optimization result:
INFO - 17:00:32: Optimizer info:
INFO - 17:00:32: Status: 5
INFO - 17:00:32: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 17:00:32: Number of calls to the objective function by the optimizer: 1501
INFO - 17:00:32: Solution:
INFO - 17:00:32: The solution is feasible.
INFO - 17:00:32: Objective: 0.0
INFO - 17:00:32: Standardized constraints:
INFO - 17:00:32: 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 - 17:00:32: 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 - 17:00:32: 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 - 17:00:32: 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 - 17:00:32: 0. 0. 0. 0.]
INFO - 17:00:32: Design space:
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: | name | lower_bound | value | upper_bound | type |
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: | x | 0 | 0 | 1 | float |
INFO - 17:00:32: | y | 0 | 0 | 1 | float |
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: *** End MDOScenario execution (time: 0:00:17.120245) ***
{'max_iter': 1000, 'algo': 'NLOPT_MMA', '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}}
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(
method="KS", rho=10.0, constr_id=0
)
new_scenario.execute(algo_options)
INFO - 17:00:32:
INFO - 17:00:32: *** Start MDOScenario execution ***
INFO - 17:00:32: MDOScenario
INFO - 17:00:32: Disciplines: Concatenater function
INFO - 17:00:32: MDO formulation: DisciplinaryOpt
INFO - 17:00:32: Optimization problem:
INFO - 17:00:32: minimize o(x, y)
INFO - 17:00:32: with respect to x, y
INFO - 17:00:32: subject to constraints:
INFO - 17:00:32: KS() <= 0.0
INFO - 17:00:32: over the design space:
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: | name | lower_bound | value | upper_bound | type |
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: | x | 0 | 0.005 | 1 | float |
INFO - 17:00:32: | y | 0 | 1 | 1 | float |
INFO - 17:00:32: +------+-------------+-------+-------------+-------+
INFO - 17:00:32: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 17:00:32: ... 0%| | 0/1000 [00:00<?, ?it]
INFO - 17:00:32: ... 0%| | 1/1000 [00:00<00:40, 24.86 it/sec, obj=1]
INFO - 17:00:32: ... 0%| | 2/1000 [00:00<00:40, 24.54 it/sec, obj=0.866]
INFO - 17:00:32: ... 0%| | 3/1000 [00:00<00:32, 30.45 it/sec, obj=0.592]
INFO - 17:00:32: ... 0%| | 4/1000 [00:00<00:28, 34.57 it/sec, obj=0.295]
INFO - 17:00:32: ... 0%| | 5/1000 [00:00<00:26, 37.64 it/sec, obj=0.0857]
INFO - 17:00:32: ... 1%| | 6/1000 [00:00<00:24, 40.07 it/sec, obj=0.00773]
INFO - 17:00:33: ... 1%| | 7/1000 [00:00<00:23, 41.97 it/sec, obj=5.72e-5]
INFO - 17:00:33: ... 1%| | 8/1000 [00:00<00:22, 43.39 it/sec, obj=2.62e-9]
INFO - 17:00:33: ... 1%| | 9/1000 [00:00<00:52, 18.70 it/sec, obj=0]
INFO - 17:00:33: Optimization result:
INFO - 17:00:33: Optimizer info:
INFO - 17:00:33: Status: 5
INFO - 17:00:33: Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 17:00:33: Number of calls to the objective function by the optimizer: 1501
INFO - 17:00:33: Solution:
INFO - 17:00:33: The solution is feasible.
INFO - 17:00:33: Objective: 0.0
INFO - 17:00:33: Standardized constraints:
INFO - 17:00:33: KS(g) = 4.440892098500626e-16
INFO - 17:00:33: Design space:
INFO - 17:00:33: +------+-------------+-------+-------------+-------+
INFO - 17:00:33: | name | lower_bound | value | upper_bound | type |
INFO - 17:00:33: +------+-------------+-------+-------------+-------+
INFO - 17:00:33: | x | 0 | 0 | 1 | float |
INFO - 17:00:33: | y | 0 | 0 | 1 | float |
INFO - 17:00:33: +------+-------------+-------+-------------+-------+
INFO - 17:00:33: *** End MDOScenario execution (time: 0:00:00.493595) ***
{'max_iter': 1000, 'algo': 'NLOPT_MMA', '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}}
with constraint aggregation the last iteration is faster.
Total running time of the script: ( 0 minutes 19.398 seconds)