Analytical test case # 3

In this example, we consider a simple optimization problem to illustrate algorithms interfaces and DOE libraries integration. Integer variables are used

Imports

from gemseo.algos.design_space import DesignSpace
from gemseo.algos.doe.doe_factory import DOEFactory
from gemseo.algos.opt_problem import OptimizationProblem
from gemseo.api import configure_logger
from gemseo.api import execute_post
from gemseo.core.mdofunctions.mdo_function import MDOFunction
from matplotlib import pyplot as plt
from numpy import sum as np_sum

LOGGER = configure_logger()

Define the objective function

We define the objective function \(f(x)=\sum_{i=1}^dx_i\) using a MDOFunction.

objective = MDOFunction(np_sum, name="f", expr="sum(x)")

Define the design space

Then, we define the DesignSpace with GEMSEO.

design_space = DesignSpace()
design_space.add_variable("x", 2, l_b=-5, u_b=5, var_type="integer")

Define the optimization problem

Then, we define the OptimizationProblem with GEMSEO.

problem = OptimizationProblem(design_space)
problem.objective = objective

Solve the optimization problem using a DOE algorithm

We can see this optimization problem as a trade-off and solve it by means of a design of experiments (DOE), e.g. full factorial design

DOEFactory().execute(problem, "fullfact", n_samples=11**2)

Out:

    INFO - 15:04:26: Optimization problem:
    INFO - 15:04:26:    minimize f = sum(x)
    INFO - 15:04:26:    with respect to x
    INFO - 15:04:26:    over the design space:
    INFO - 15:04:26:    +------+-------------+-------+-------------+---------+
    INFO - 15:04:26:    | name | lower_bound | value | upper_bound | type    |
    INFO - 15:04:26:    +------+-------------+-------+-------------+---------+
    INFO - 15:04:26:    | x    |      -5     |  None |      5      | integer |
    INFO - 15:04:26:    | x    |      -5     |  None |      5      | integer |
    INFO - 15:04:26:    +------+-------------+-------+-------------+---------+
    INFO - 15:04:26: Solving optimization problem with algorithm fullfact:
    INFO - 15:04:26: Full factorial design required. Number of samples along each direction for a design vector of size 2 with 121 samples: 11
    INFO - 15:04:26: Final number of samples for DOE = 121 vs 121 requested
    INFO - 15:04:26: ...   0%|          | 0/121 [00:00<?, ?it]
    INFO - 15:04:26: ... 100%|██████████| 121/121 [00:00<00:00, 10906.70 it/sec, obj=10]
    INFO - 15:04:26: Optimization result:
    INFO - 15:04:26:    Optimizer info:
    INFO - 15:04:26:       Status: None
    INFO - 15:04:26:       Message: None
    INFO - 15:04:26:       Number of calls to the objective function by the optimizer: 121
    INFO - 15:04:26:    Solution:
    INFO - 15:04:26:       Objective: -10.0
    INFO - 15:04:26:       Design space:
    INFO - 15:04:26:       +------+-------------+-------+-------------+---------+
    INFO - 15:04:26:       | name | lower_bound | value | upper_bound | type    |
    INFO - 15:04:26:       +------+-------------+-------+-------------+---------+
    INFO - 15:04:26:       | x    |      -5     |   -5  |      5      | integer |
    INFO - 15:04:26:       | x    |      -5     |   -5  |      5      | integer |
    INFO - 15:04:26:       +------+-------------+-------+-------------+---------+

Optimization result:
   Design variables: [-5. -5.]
   Objective function: -10.0
   Feasible solution: True

Post-process the results

execute_post(
    problem, "ScatterPlotMatrix", variable_names=["x", "f"], save=False, show=False
)
# Workaround for HTML rendering, instead of ``show=True``
plt.show()
plot simple opt 3

Note that you can get all the optimization algorithms names:

algo_list = DOEFactory().algorithms
print("Available algorithms ", algo_list)

Out:

Available algorithms  ['CustomDOE', 'DiagonalDOE', 'OT_SOBOL', 'OT_RANDOM', 'OT_HASELGROVE', 'OT_REVERSE_HALTON', 'OT_HALTON', 'OT_FAURE', 'OT_MONTE_CARLO', 'OT_FACTORIAL', 'OT_COMPOSITE', 'OT_AXIAL', 'OT_OPT_LHS', 'OT_LHS', 'OT_LHSC', 'OT_FULLFACT', 'OT_SOBOL_INDICES', 'fullfact', 'ff2n', 'pbdesign', 'bbdesign', 'ccdesign', 'lhs']

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

Gallery generated by Sphinx-Gallery