DOE from a file#

from __future__ import annotations

from pathlib import Path

from gemseo import create_design_space
from gemseo import create_discipline
from gemseo import create_scenario

Let us consider a discipline implementing the function \(y=a*b\)

discipline = create_discipline("AnalyticDiscipline", expressions={"y": "a*b"})

where \(a,b\in\{1,2,\ldots,10\}\):

design_space = create_design_space()
design_space.add_variable("a", 1, design_space.DesignVariableType.INTEGER, 1, 10)
design_space.add_variable("b", 1, design_space.DesignVariableType.INTEGER, 1, 10)

We want to evaluate this discipline over this design space by using the input samples defined in the file "doe.txt":

print(Path("doe.txt").read_text())
1,2
3,4
5,6

In this file, rows are points and columns are variables whose order must be consistent with that of the design space. In this example, we can see that the first input value is defined by \(a=1\) and \(b=2\).

For that, we can create a scenario and execute it with a CustomDOE, with the option "doe_file". We could also change the delimiter (default: ',') or skip the first rows in the file.

scenario = create_scenario(
    [discipline],
    "y",
    design_space,
    scenario_type="DOE",
    formulation_name="DisciplinaryOpt",
)
scenario.execute(algo_name="CustomDOE", doe_file="doe.txt")
INFO - 16:25:36: *** Start DOEScenario execution ***
INFO - 16:25:36: DOEScenario
INFO - 16:25:36:    Disciplines: AnalyticDiscipline
INFO - 16:25:36:    MDO formulation: DisciplinaryOpt
INFO - 16:25:36: Optimization problem:
INFO - 16:25:36:    minimize y(a, b)
INFO - 16:25:36:    with respect to a, b
INFO - 16:25:36:    over the design space:
INFO - 16:25:36:       +------+-------------+-------+-------------+---------+
INFO - 16:25:36:       | Name | Lower bound | Value | Upper bound | Type    |
INFO - 16:25:36:       +------+-------------+-------+-------------+---------+
INFO - 16:25:36:       | a    |      1      |  None |      10     | integer |
INFO - 16:25:36:       | b    |      1      |  None |      10     | integer |
INFO - 16:25:36:       +------+-------------+-------+-------------+---------+
INFO - 16:25:36: Solving optimization problem with algorithm CustomDOE:
INFO - 16:25:36:     33%|███▎      | 1/3 [00:00<00:00, 533.69 it/sec, feas=True, obj=2]
INFO - 16:25:36:     67%|██████▋   | 2/3 [00:00<00:00, 901.71 it/sec, feas=True, obj=12]
INFO - 16:25:36:    100%|██████████| 3/3 [00:00<00:00, 1177.95 it/sec, feas=True, obj=30]
INFO - 16:25:36: Optimization result:
INFO - 16:25:36:    Optimizer info:
INFO - 16:25:36:       Status: None
INFO - 16:25:36:       Message: None
INFO - 16:25:36:    Solution:
INFO - 16:25:36:       Objective: 2.0
INFO - 16:25:36:       Design space:
INFO - 16:25:36:          +------+-------------+-------+-------------+---------+
INFO - 16:25:36:          | Name | Lower bound | Value | Upper bound | Type    |
INFO - 16:25:36:          +------+-------------+-------+-------------+---------+
INFO - 16:25:36:          | a    |      1      |   1   |      10     | integer |
INFO - 16:25:36:          | b    |      1      |   2   |      10     | integer |
INFO - 16:25:36:          +------+-------------+-------+-------------+---------+
INFO - 16:25:36: *** End DOEScenario execution (time: 0:00:00.006156) ***

Note that both the formulation settings passed to create_scenario() and the algorithm settings passed to execute() can be provided via a Pydantic model. For more information, see Formulation Settings and Algorithm Settings.

We can display the content of the database as a Dataset and check the values of the output, which should be the product of \(a\) and \(b\).

opt_problem = scenario.formulation.optimization_problem
dataset = opt_problem.to_dataset(name="custom_sampling", opt_naming=False)
dataset
GROUP inputs outputs
VARIABLE a b y
COMPONENT 0 0 0
0 1 2 2.0
1 3 4 12.0
2 5 6 30.0


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

Gallery generated by Sphinx-Gallery