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.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()
<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",
    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 - 11:43:33: *** Start MDOScenario execution ***
INFO - 11:43:33: MDOScenario
INFO - 11:43:33:    Disciplines: Concatenater function
INFO - 11:43:33:    MDO formulation: DisciplinaryOpt
INFO - 11:43:33: Optimization problem:
INFO - 11:43:33:    minimize o(x, y)
INFO - 11:43:33:    with respect to x, y
INFO - 11:43:33:    subject to constraints:
INFO - 11:43:33:       g(x, y) <= 0
INFO - 11:43:33:    over the design space:
INFO - 11:43:33:       +------+-------------+-------+-------------+-------+
INFO - 11:43:33:       | Name | Lower bound | Value | Upper bound | Type  |
INFO - 11:43:33:       +------+-------------+-------+-------------+-------+
INFO - 11:43:33:       | x    |      0      | 0.005 |      1      | float |
INFO - 11:43:33:       | y    |      0      |   1   |      1      | float |
INFO - 11:43:33:       +------+-------------+-------+-------------+-------+
INFO - 11:43:33: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 11:43:34:      1%|          | 6/1000 [00:00<00:41, 23.92 it/sec, obj=0.00931]
INFO - 11:43:34:      1%|          | 7/1000 [00:00<00:39, 24.99 it/sec, obj=8.28e-5]
INFO - 11:43:34:      1%|          | 8/1000 [00:00<00:38, 25.85 it/sec, obj=5.2e-9]
INFO - 11:43:43:      1%|          | 9/1000 [00:09<18:00, 55.03 it/min, obj=0]
INFO - 11:43:43: Optimization result:
INFO - 11:43:43:    Optimizer info:
INFO - 11:43:43:       Status: 5
INFO - 11:43:43:       Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 11:43:43:       Number of calls to the objective function by the optimizer: 1501
INFO - 11:43:43:    Solution:
INFO - 11:43:43:       The solution is feasible.
INFO - 11:43:43:       Objective: 0.0
INFO - 11:43:43:       Standardized constraints:
INFO - 11:43:43:          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 - 11:43:43:  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 - 11:43:43:  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 - 11:43:43:  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 - 11:43:43:  0. 0. 0. 0.]
INFO - 11:43:43:       Design space:
INFO - 11:43:43:          +------+-------------+-------+-------------+-------+
INFO - 11:43:43:          | Name | Lower bound | Value | Upper bound | Type  |
INFO - 11:43:43:          +------+-------------+-------+-------------+-------+
INFO - 11:43:43:          | x    |      0      |   0   |      1      | float |
INFO - 11:43:43:          | y    |      0      |   0   |      1      | float |
INFO - 11:43:43:          +------+-------------+-------+-------------+-------+
INFO - 11:43:43: *** End MDOScenario execution (time: 0:00:09.816728) ***

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 - 11:43:43: *** Start MDOScenario execution ***
INFO - 11:43:43: MDOScenario
INFO - 11:43:43:    Disciplines: Concatenater function
INFO - 11:43:43:    MDO formulation: DisciplinaryOpt
INFO - 11:43:43: Optimization problem:
INFO - 11:43:43:    minimize o(x, y)
INFO - 11:43:43:    with respect to x, y
INFO - 11:43:43:    subject to constraints:
INFO - 11:43:43:       lower_bound_KS() <= 0.0
INFO - 11:43:43:    over the design space:
INFO - 11:43:43:       +------+-------------+-------+-------------+-------+
INFO - 11:43:43:       | Name | Lower bound | Value | Upper bound | Type  |
INFO - 11:43:43:       +------+-------------+-------+-------------+-------+
INFO - 11:43:43:       | x    |      0      | 0.005 |      1      | float |
INFO - 11:43:43:       | y    |      0      |   1   |      1      | float |
INFO - 11:43:43:       +------+-------------+-------+-------------+-------+
INFO - 11:43:43: Solving optimization problem with algorithm NLOPT_MMA:
INFO - 11:43:43:      1%|          | 6/1000 [00:00<00:24, 40.35 it/sec, obj=0.00773]
INFO - 11:43:43:      1%|          | 7/1000 [00:00<00:24, 41.15 it/sec, obj=5.72e-5]
INFO - 11:43:43:      1%|          | 8/1000 [00:00<00:23, 41.70 it/sec, obj=2.63e-9]
INFO - 11:43:44:      1%|          | 9/1000 [00:00<00:41, 23.92 it/sec, obj=0]
INFO - 11:43:44: Optimization result:
INFO - 11:43:44:    Optimizer info:
INFO - 11:43:44:       Status: 5
INFO - 11:43:44:       Message: NLOPT_MAXEVAL_REACHED: Optimization stopped because maxeval (above) was reached
INFO - 11:43:44:       Number of calls to the objective function by the optimizer: 1501
INFO - 11:43:44:    Solution:
INFO - 11:43:44:       The solution is feasible.
INFO - 11:43:44:       Objective: 0.0
INFO - 11:43:44:       Standardized constraints:
INFO - 11:43:44:          lower_bound_KS(g) = 2.7755575615628914e-16
INFO - 11:43:44:       Design space:
INFO - 11:43:44:          +------+-------------+-------+-------------+-------+
INFO - 11:43:44:          | Name | Lower bound | Value | Upper bound | Type  |
INFO - 11:43:44:          +------+-------------+-------+-------------+-------+
INFO - 11:43:44:          | x    |      0      |   0   |      1      | float |
INFO - 11:43:44:          | y    |      0      |   0   |      1      | float |
INFO - 11:43:44:          +------+-------------+-------+-------------+-------+
INFO - 11:43:44: *** End MDOScenario execution (time: 0:00:00.378530) ***

with constraint aggregation the last iteration is faster.

Total running time of the script: (0 minutes 11.219 seconds)

Gallery generated by Sphinx-Gallery