Note
Go to the end to download the full example code.
Convert a database to a dataset#
In this example,
we will see how to convert a Database to a Dataset.
from __future__ import annotations
from gemseo import execute_algo
from gemseo.problems.optimization.rosenbrock import Rosenbrock
Let us solve the Rosenbrock optimization problem
with the SLSQP algorithm and 10 iterations:
optimization_problem = Rosenbrock()
execute_algo(optimization_problem, algo_name="SLSQP", max_iter=10)
INFO - 16:24:39: Optimization problem:
INFO - 16:24:39: minimize rosen(x) = sum( 100*(x[1:] - x[:-1]**2)**2 + (1 - x[:-1])**2 )
INFO - 16:24:39: with respect to x
INFO - 16:24:39: over the design space:
INFO - 16:24:39: +------+-------------+-------+-------------+-------+
INFO - 16:24:39: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:24:39: +------+-------------+-------+-------------+-------+
INFO - 16:24:39: | x[0] | -2 | 0 | 2 | float |
INFO - 16:24:39: | x[1] | -2 | 0 | 2 | float |
INFO - 16:24:39: +------+-------------+-------+-------------+-------+
INFO - 16:24:39: Solving optimization problem with algorithm SLSQP:
INFO - 16:24:39: 10%|█ | 1/10 [00:00<00:00, 1045.44 it/sec, feas=True, obj=1]
INFO - 16:24:39: 20%|██ | 2/10 [00:00<00:00, 1559.80 it/sec, feas=True, obj=1.6e+3]
INFO - 16:24:39: 30%|███ | 3/10 [00:00<00:00, 1804.26 it/sec, feas=True, obj=0.8]
INFO - 16:24:39: 40%|████ | 4/10 [00:00<00:00, 1827.78 it/sec, feas=True, obj=962]
INFO - 16:24:39: 50%|█████ | 5/10 [00:00<00:00, 1814.30 it/sec, feas=True, obj=0.681]
INFO - 16:24:39: 60%|██████ | 6/10 [00:00<00:00, 1840.01 it/sec, feas=True, obj=8.3]
INFO - 16:24:39: 70%|███████ | 7/10 [00:00<00:00, 1885.69 it/sec, feas=True, obj=6.17]
INFO - 16:24:39: 80%|████████ | 8/10 [00:00<00:00, 1872.77 it/sec, feas=True, obj=0.398]
INFO - 16:24:39: 90%|█████████ | 9/10 [00:00<00:00, 1863.95 it/sec, feas=True, obj=0.221]
INFO - 16:24:39: 100%|██████████| 10/10 [00:00<00:00, 1856.71 it/sec, feas=True, obj=0.193]
INFO - 16:24:39: Optimization result:
INFO - 16:24:39: Optimizer info:
INFO - 16:24:39: Status: None
INFO - 16:24:39: Message: Maximum number of iterations reached. GEMSEO stopped the driver.
INFO - 16:24:39: Solution:
INFO - 16:24:39: Objective: 0.19349100516217954
INFO - 16:24:39: Design space:
INFO - 16:24:39: +------+-------------+--------------------+-------------+-------+
INFO - 16:24:39: | Name | Lower bound | Value | Upper bound | Type |
INFO - 16:24:39: +------+-------------+--------------------+-------------+-------+
INFO - 16:24:39: | x[0] | -2 | 0.5706658475298814 | 2 | float |
INFO - 16:24:39: | x[1] | -2 | 0.316087053903805 | 2 | float |
INFO - 16:24:39: +------+-------------+--------------------+-------------+-------+
Then,
the Database attached to this OptimizationProblem
can be converted to an OptimizationDataset
using its method to_dataset():
dataset = optimization_problem.to_dataset()
dataset
The design variables and output variables are in separate groups.
You can also use an IODataset instead of an OptimizationDataset:
dataset = optimization_problem.to_dataset(opt_naming=False)
dataset
or simply do not separate the variables
dataset = optimization_problem.to_dataset(categorize=False)
dataset
Note
Only design variables and functions (objective function, constraints) are
stored in the database. If you want to store state variables, you must add
them as observables before the problem is executed. Use the
add_observable() method.
Total running time of the script: (0 minutes 0.027 seconds)