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 - 10:51:03:
    INFO - 10:51:03: *** Start MDOScenario execution ***
    INFO - 10:51:03: MDOScenario
    INFO - 10:51:03:    Disciplines: Concatenater function
    INFO - 10:51:03:    MDO formulation: DisciplinaryOpt
    INFO - 10:51:03: Optimization problem:
    INFO - 10:51:03:    minimize o(x, y)
    INFO - 10:51:03:    with respect to x, y
    INFO - 10:51:03:    subject to constraints:
    INFO - 10:51:03:       g(x, y) <= 0.0
    INFO - 10:51:03:    over the design space:
    INFO - 10:51:03:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:03:       | Name | Lower bound | Value | Upper bound | Type  |
    INFO - 10:51:03:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:03:       | x    |      0      | 0.005 |      1      | float |
    INFO - 10:51:03:       | y    |      0      |   1   |      1      | float |
    INFO - 10:51:03:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:03: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 10:51:04:      1%|          | 6/1000 [00:00<01:24, 11.70 it/sec, obj=0.00931]
    INFO - 10:51:04:      1%|          | 7/1000 [00:00<01:21, 12.22 it/sec, obj=8.28e-5]
    INFO - 10:51:04:      1%|          | 8/1000 [00:00<01:18, 12.68 it/sec, obj=5.2e-9]
    INFO - 10:51:18:      1%|          | 9/1000 [00:15<27:39, 35.83 it/min, obj=0]
    INFO - 10:51:18: Optimization result:
    INFO - 10:51:18:    Optimizer info:
    INFO - 10:51:18:       Status: 5
    INFO - 10:51:18:       Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
    INFO - 10:51:18:       Number of calls to the objective function by the optimizer: 1501
    INFO - 10:51:18:    Solution:
    INFO - 10:51:18:       The solution is feasible.
    INFO - 10:51:18:       Objective: 0.0
    INFO - 10:51:18:       Standardized constraints:
    INFO - 10:51:18:          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:51:18:  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:51:18:  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:51:18:  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:51:18:  0. 0. 0. 0.]
    INFO - 10:51:18:       Design space:
    INFO - 10:51:18:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:18:          | Name | Lower bound | Value | Upper bound | Type  |
    INFO - 10:51:18:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:18:          | x    |      0      |   0   |      1      | float |
    INFO - 10:51:18:          | y    |      0      |   0   |      1      | float |
    INFO - 10:51:18:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:18: *** End MDOScenario execution (time: 0:00:15.088424) ***

{'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 lower bound KS function

new_scenario.formulation.opt_problem.aggregate_constraint(
    0, method="lower_bound_KS", rho=10.0
)
new_scenario.execute(algo_options)
    INFO - 10:51:18:
    INFO - 10:51:18: *** Start MDOScenario execution ***
    INFO - 10:51:18: MDOScenario
    INFO - 10:51:18:    Disciplines: Concatenater function
    INFO - 10:51:18:    MDO formulation: DisciplinaryOpt
    INFO - 10:51:19: Optimization problem:
    INFO - 10:51:19:    minimize o(x, y)
    INFO - 10:51:19:    with respect to x, y
    INFO - 10:51:19:    subject to constraints:
    INFO - 10:51:19:       lower_bound_KS() <= 0.0
    INFO - 10:51:19:    over the design space:
    INFO - 10:51:19:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:19:       | Name | Lower bound | Value | Upper bound | Type  |
    INFO - 10:51:19:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:19:       | x    |      0      | 0.005 |      1      | float |
    INFO - 10:51:19:       | y    |      0      |   1   |      1      | float |
    INFO - 10:51:19:       +------+-------------+-------+-------------+-------+
    INFO - 10:51:19: Solving optimization problem with algorithm NLOPT_MMA:
    INFO - 10:51:19:      1%|          | 6/1000 [00:00<01:02, 15.89 it/sec, obj=0.00773]
    INFO - 10:51:19:      1%|          | 7/1000 [00:00<01:00, 16.38 it/sec, obj=5.72e-5]
    INFO - 10:51:19:      1%|          | 8/1000 [00:00<00:59, 16.78 it/sec, obj=2.63e-9]
    INFO - 10:51:19:      1%|          | 9/1000 [00:00<01:23, 11.91 it/sec, obj=0]
    INFO - 10:51:19: Optimization result:
    INFO - 10:51:19:    Optimizer info:
    INFO - 10:51:19:       Status: 5
    INFO - 10:51:19:       Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
    INFO - 10:51:19:       Number of calls to the objective function by the optimizer: 1501
    INFO - 10:51:19:    Solution:
    INFO - 10:51:19:       The solution is feasible.
    INFO - 10:51:19:       Objective: 0.0
    INFO - 10:51:19:       Standardized constraints:
    INFO - 10:51:19:          lower_bound_KS(g) = 2.7755575615628914e-16
    INFO - 10:51:19:       Design space:
    INFO - 10:51:19:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:19:          | Name | Lower bound | Value | Upper bound | Type  |
    INFO - 10:51:19:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:19:          | x    |      0      |   0   |      1      | float |
    INFO - 10:51:19:          | y    |      0      |   0   |      1      | float |
    INFO - 10:51:19:          +------+-------------+-------+-------------+-------+
    INFO - 10:51:19: *** End MDOScenario execution (time: 0:00:00.769804) ***

{'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 17.431 seconds)

Gallery generated by Sphinx-Gallery