Note
Go to the end to download the full example code.
Analytical test case # 2¶
In this example, we consider a simple optimization problem to illustrate algorithms interfaces and optimization libraries integration.
Imports¶
from __future__ import annotations
from numpy import cos
from numpy import exp
from numpy import sin
from gemseo import configure_logger
from gemseo import execute_algo
from gemseo import execute_post
from gemseo import get_available_opt_algorithms
from gemseo.algos.design_space import DesignSpace
from gemseo.algos.optimization_problem import OptimizationProblem
from gemseo.core.mdo_functions.mdo_function import MDOFunction
configure_logger()
<RootLogger root (INFO)>
Define the objective function¶
We define the objective function \(f(x)=\sin(x)-\exp(x)\)
using an MDOFunction
defined by the sum of MDOFunction
objects.
f_1 = MDOFunction(sin, name="f_1", jac=cos, expr="sin(x)")
f_2 = MDOFunction(exp, name="f_2", jac=exp, expr="exp(x)")
objective = f_1 - f_2
See also
The following operators are implemented: addition, subtraction and multiplication. The minus operator is also defined.
Define the design space¶
Then, we define the DesignSpace
with GEMSEO.
design_space = DesignSpace()
design_space.add_variable("x", l_b=-2.0, u_b=2.0, value=-0.5)
Define the optimization problem¶
Then, we define the OptimizationProblem
with GEMSEO.
problem = OptimizationProblem(design_space)
problem.objective = objective
Solve the optimization problem using an optimization algorithm¶
Finally, we solve the optimization problems with GEMSEO interface.
Solve the problem¶
optimization_result = execute_algo(problem, "L-BFGS-B")
optimization_result
INFO - 10:39:40: Optimization problem:
INFO - 10:39:40: minimize [f_1-f_2] = sin(x)-exp(x)
INFO - 10:39:40: with respect to x
INFO - 10:39:40: over the design space:
INFO - 10:39:40: +------+-------------+-------+-------------+-------+
INFO - 10:39:40: | Name | Lower bound | Value | Upper bound | Type |
INFO - 10:39:40: +------+-------------+-------+-------------+-------+
INFO - 10:39:40: | x | -2 | -0.5 | 2 | float |
INFO - 10:39:40: +------+-------------+-------+-------------+-------+
INFO - 10:39:40: Solving optimization problem with algorithm L-BFGS-B:
INFO - 10:39:41: 1%| | 5/999 [00:00<00:00, 1343.04 it/sec, obj=-1.24]
INFO - 10:39:41: 1%| | 6/999 [00:00<00:00, 1246.51 it/sec, obj=-1.24]
INFO - 10:39:41: 1%| | 7/999 [00:00<00:00, 1196.91 it/sec, obj=-1.24]
INFO - 10:39:41: Optimization result:
INFO - 10:39:41: Optimizer info:
INFO - 10:39:41: Status: 0
INFO - 10:39:41: Message: CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL
INFO - 10:39:41: Number of calls to the objective function by the optimizer: 8
INFO - 10:39:41: Solution:
INFO - 10:39:41: Objective: -1.2361083418592416
INFO - 10:39:41: Design space:
INFO - 10:39:41: +------+-------------+--------------------+-------------+-------+
INFO - 10:39:41: | Name | Lower bound | Value | Upper bound | Type |
INFO - 10:39:41: +------+-------------+--------------------+-------------+-------+
INFO - 10:39:41: | x | -2 | -1.292695718944152 | 2 | float |
INFO - 10:39:41: +------+-------------+--------------------+-------------+-------+
Note that you can get all the optimization algorithms names:
get_available_opt_algorithms()
['Augmented_Lagrangian_order_0', 'Augmented_Lagrangian_order_1', 'MNBI', 'MultiStart', 'NLOPT_MMA', 'NLOPT_COBYLA', 'NLOPT_SLSQP', 'NLOPT_BOBYQA', 'NLOPT_BFGS', 'NLOPT_NEWUOA', 'DUAL_ANNEALING', 'SHGO', 'DIFFERENTIAL_EVOLUTION', 'LINEAR_INTERIOR_POINT', 'REVISED_SIMPLEX', 'SIMPLEX', 'HIGHS_INTERIOR_POINT', 'HIGHS_DUAL_SIMPLEX', 'HIGHS', 'Scipy_MILP', 'SLSQP', 'L-BFGS-B', 'TNC', 'NELDER-MEAD']
Save the optimization results¶
We can serialize the results for further exploitation.
problem.to_hdf("my_optim.hdf5")
INFO - 10:39:41: Exporting the optimization problem to the file my_optim.hdf5 at node
Post-process the results¶
execute_post(problem, "OptHistoryView", show=True, save=False)
<gemseo.post.opt_history_view.OptHistoryView object at 0x7f134fcd9640>
Note
We can also save this plot using the arguments save=False
and file_path='file_path'
.
Solve the optimization problem using a DOE algorithm¶
We can also see this optimization problem as a trade-off problem and solve it by means of a design of experiments (DOE).
problem.reset()
optimization_result = execute_algo(problem, "lhs", n_samples=10, algo_type="doe")
optimization_result
INFO - 10:39:41: Optimization problem:
INFO - 10:39:41: minimize [f_1-f_2] = sin(x)-exp(x)
INFO - 10:39:41: with respect to x
INFO - 10:39:41: over the design space:
INFO - 10:39:41: +------+-------------+-------+-------------+-------+
INFO - 10:39:41: | Name | Lower bound | Value | Upper bound | Type |
INFO - 10:39:41: +------+-------------+-------+-------------+-------+
INFO - 10:39:41: | x | -2 | -0.5 | 2 | float |
INFO - 10:39:41: +------+-------------+-------+-------------+-------+
INFO - 10:39:41: Solving optimization problem with algorithm lhs:
INFO - 10:39:41: 10%|█ | 1/10 [00:00<00:00, 4809.98 it/sec, obj=-5.17]
INFO - 10:39:41: 20%|██ | 2/10 [00:00<00:00, 3818.21 it/sec, obj=-1.15]
INFO - 10:39:41: 30%|███ | 3/10 [00:00<00:00, 3880.02 it/sec, obj=-1.24]
INFO - 10:39:41: 40%|████ | 4/10 [00:00<00:00, 3933.70 it/sec, obj=-1.13]
INFO - 10:39:41: 50%|█████ | 5/10 [00:00<00:00, 3986.22 it/sec, obj=-2.91]
INFO - 10:39:41: 60%|██████ | 6/10 [00:00<00:00, 4019.46 it/sec, obj=-1.75]
INFO - 10:39:41: 70%|███████ | 7/10 [00:00<00:00, 4053.03 it/sec, obj=-1.14]
INFO - 10:39:41: 80%|████████ | 8/10 [00:00<00:00, 4078.08 it/sec, obj=-1.05]
INFO - 10:39:41: 90%|█████████ | 9/10 [00:00<00:00, 4104.91 it/sec, obj=-1.23]
INFO - 10:39:41: 100%|██████████| 10/10 [00:00<00:00, 4124.60 it/sec, obj=-1]
INFO - 10:39:41: Optimization result:
INFO - 10:39:41: Optimizer info:
INFO - 10:39:41: Status: None
INFO - 10:39:41: Message: None
INFO - 10:39:41: Number of calls to the objective function by the optimizer: 10
INFO - 10:39:41: Solution:
INFO - 10:39:41: Objective: -5.174108803965849
INFO - 10:39:41: Design space:
INFO - 10:39:41: +------+-------------+-------------------+-------------+-------+
INFO - 10:39:41: | Name | Lower bound | Value | Upper bound | Type |
INFO - 10:39:41: +------+-------------+-------------------+-------------+-------+
INFO - 10:39:41: | x | -2 | 1.815526693601343 | 2 | float |
INFO - 10:39:41: +------+-------------+-------------------+-------------+-------+
Total running time of the script: (0 minutes 0.835 seconds)