Use a design of experiments from an array

from __future__ import annotations

import numpy as np
from gemseo.api import create_design_space
from gemseo.api import create_discipline
from gemseo.api 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.INTEGER, 1, 10)
design_space.add_variable("b", 1, design_space.INTEGER, 1, 10)

We want to evaluate this discipline over this design space by using the following input samples:

sample_1 = [1.0, 2.0]
sample_2 = [2.0, 3.0]
samples = np.array([sample_1, sample_2])

For that, we can create a scenario and execute it with a CustomDOE with the option “samples”:

scenario = create_scenario(
    [discipline], "DisciplinaryOpt", "y", design_space, scenario_type="DOE"
)
scenario.execute({"algo": "CustomDOE", "algo_options": {"samples": samples}})
{'eval_jac': False, 'algo': 'CustomDOE', 'algo_options': {'samples': array([[1., 2.],
       [2., 3.]])}}

Then, we can display the content of the database as a dataframe and check the values of the output, which should be the product of \(a\) and \(b\):

opt_problem = scenario.formulation.opt_problem
dataset = opt_problem.export_to_dataset(name="custom_sampling", opt_naming=False)
print(dataset.export_to_dataframe())
  inputs      outputs
       a    b       y
       0    0       0
0    1.0  2.0     2.0
1    2.0  3.0     6.0

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

Gallery generated by Sphinx-Gallery