Note
Go to the end to download the full example code.
Scenario#
from __future__ import annotations
from gemseo import configure_logger
from gemseo import create_design_space
from gemseo import create_discipline
from gemseo import create_scenario
from gemseo import get_available_scenario_types
from gemseo import get_scenario_differentiation_modes
from gemseo import get_scenario_inputs_schema
from gemseo import get_scenario_options_schema
from gemseo import monitor_scenario
configure_logger()
<RootLogger root (INFO)>
In this example, we will discover the different functions of the API to
related to scenarios, which are the GEMSEO' objects
dedicated to the resolution of a problem, e.g. optimization or trade-off,
associated with a list of disciplines and a design space. All classes
implementing scenarios inherit from Scenario which is an
abstract class. Classical concrete classes are MDOScenario and
DOEScenario, respectively dedicated to optimization and trade-off
problems.
Get available scenario type#
The high-level function get_available_scenario_types() can be used
to get the available scenario types (MDOScenario and
DOEScenario).
get_available_scenario_types()
['MDO', 'DOE']
Get scenario options schema#
The get_scenario_options_schema() function can be used
to get the options of a given scenario type:
get_scenario_options_schema("MDO")
{'$schema': 'http://json-schema.org/schema#', 'type': 'object', 'properties': {'name': {'description': 'The name to be given to this scenario. If empty, use the name of the class.', 'type': 'string'}, 'maximize_objective': {'description': 'Whether to maximize the objective.', 'type': 'boolean'}, 'formulation_settings_model': {'description': 'The formulation settings as a Pydantic model. If ``None``, use ``**settings``.', 'type': 'null'}}}
Create a scenario#
The API function create_scenario() can be used
to create a scenario:
The four first arguments are mandatory:
disciplines: the list ofDiscipline(or possibly, a singleDiscipline),objective_name: the name of the objective function (one of the discipline outputs),design_space: theDesignSpaceor the file path of the design space,either a
formulation_namefollowed by itsformulation_settings`; ora
formulation_settings_model(see Formulation Settings).
The other arguments are optional:
name: scenario name,scenario_type: type of scenario, either "MDO" (default) or "DOE" ,**formulation_settings: settings passed to the formulation as keyword arguments when theformulation_settings_modelwas not provided.
This function returns an instance of
MDOScenarioorDOEScenario.
discipline = create_discipline("AnalyticDiscipline", expressions={"y": "x1+x2"})
design_space = create_design_space()
design_space.add_variable("x1", 1, "float", 0.0, 1.0)
design_space.add_variable("x2", 1, "float", 0.0, 1.0)
scenario = create_scenario(
discipline,
"y",
design_space,
scenario_type="DOE",
formulation_name="DisciplinaryOpt",
)
scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=25)
scenario.post_process(
post_name="ScatterPlotMatrix",
variable_names=["x1", "x2", "y"],
save=False,
show=True,
)

INFO - 08:36:14:
INFO - 08:36:14: *** Start DOEScenario execution ***
INFO - 08:36:14: DOEScenario
INFO - 08:36:14: Disciplines: AnalyticDiscipline
INFO - 08:36:14: MDO formulation: DisciplinaryOpt
INFO - 08:36:14: Optimization problem:
INFO - 08:36:14: minimize y(x1, x2)
INFO - 08:36:14: with respect to x1, x2
INFO - 08:36:14: over the design space:
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: | x1 | 0 | None | 1 | float |
INFO - 08:36:14: | x2 | 0 | None | 1 | float |
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: Solving optimization problem with algorithm PYDOE_FULLFACT:
INFO - 08:36:14: 4%|▍ | 1/25 [00:00<00:00, 456.70 it/sec, obj=0]
INFO - 08:36:14: 8%|▊ | 2/25 [00:00<00:00, 750.86 it/sec, obj=0.25]
INFO - 08:36:14: 12%|█▏ | 3/25 [00:00<00:00, 949.44 it/sec, obj=0.5]
INFO - 08:36:14: 16%|█▌ | 4/25 [00:00<00:00, 1114.25 it/sec, obj=0.75]
INFO - 08:36:14: 20%|██ | 5/25 [00:00<00:00, 1251.28 it/sec, obj=1]
INFO - 08:36:14: 24%|██▍ | 6/25 [00:00<00:00, 1355.11 it/sec, obj=0.25]
INFO - 08:36:14: 28%|██▊ | 7/25 [00:00<00:00, 1447.60 it/sec, obj=0.5]
INFO - 08:36:14: 32%|███▏ | 8/25 [00:00<00:00, 1519.12 it/sec, obj=0.75]
INFO - 08:36:14: 36%|███▌ | 9/25 [00:00<00:00, 1588.08 it/sec, obj=1]
INFO - 08:36:14: 40%|████ | 10/25 [00:00<00:00, 1644.37 it/sec, obj=1.25]
INFO - 08:36:14: 44%|████▍ | 11/25 [00:00<00:00, 1696.41 it/sec, obj=0.5]
INFO - 08:36:14: 48%|████▊ | 12/25 [00:00<00:00, 1733.18 it/sec, obj=0.75]
INFO - 08:36:14: 52%|█████▏ | 13/25 [00:00<00:00, 1766.82 it/sec, obj=1]
INFO - 08:36:14: 56%|█████▌ | 14/25 [00:00<00:00, 1802.62 it/sec, obj=1.25]
INFO - 08:36:14: 60%|██████ | 15/25 [00:00<00:00, 1836.17 it/sec, obj=1.5]
INFO - 08:36:14: 64%|██████▍ | 16/25 [00:00<00:00, 1861.50 it/sec, obj=0.75]
INFO - 08:36:14: 68%|██████▊ | 17/25 [00:00<00:00, 1888.43 it/sec, obj=1]
INFO - 08:36:14: 72%|███████▏ | 18/25 [00:00<00:00, 1913.90 it/sec, obj=1.25]
INFO - 08:36:14: 76%|███████▌ | 19/25 [00:00<00:00, 1935.35 it/sec, obj=1.5]
INFO - 08:36:14: 80%|████████ | 20/25 [00:00<00:00, 1954.75 it/sec, obj=1.75]
INFO - 08:36:14: 84%|████████▍ | 21/25 [00:00<00:00, 1975.07 it/sec, obj=1]
INFO - 08:36:14: 88%|████████▊ | 22/25 [00:00<00:00, 1986.45 it/sec, obj=1.25]
INFO - 08:36:14: 92%|█████████▏| 23/25 [00:00<00:00, 2002.26 it/sec, obj=1.5]
INFO - 08:36:14: 96%|█████████▌| 24/25 [00:00<00:00, 2018.47 it/sec, obj=1.75]
INFO - 08:36:14: 100%|██████████| 25/25 [00:00<00:00, 2034.21 it/sec, obj=2]
INFO - 08:36:14: Optimization result:
INFO - 08:36:14: Optimizer info:
INFO - 08:36:14: Status: None
INFO - 08:36:14: Message: None
INFO - 08:36:14: Number of calls to the objective function by the optimizer: 25
INFO - 08:36:14: Solution:
INFO - 08:36:14: Objective: 0.0
INFO - 08:36:14: Design space:
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: | x1 | 0 | 0 | 1 | float |
INFO - 08:36:14: | x2 | 0 | 0 | 1 | float |
INFO - 08:36:14: +------+-------------+-------+-------------+-------+
INFO - 08:36:14: *** End DOEScenario execution (time: 0:00:00.016556) ***
<gemseo.post.scatter_plot_matrix.ScatterPlotMatrix object at 0x7f6dc8f6bb50>
The
get_scenario_inputs_schema()function can be used to get the inputs of a scenario:
get_scenario_inputs_schema(scenario)
{'$defs': {'_algo_enum': {'enum': ['CustomDOE', 'DiagonalDOE', 'MorrisDOE', 'OATDOE', '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', 'PYDOE_BBDESIGN', 'PYDOE_CCDESIGN', 'PYDOE_FF2N', 'PYDOE_FULLFACT', 'PYDOE_LHS', 'PYDOE_PBDESIGN', 'Halton', 'LHS', 'MC', 'PoissonDisk', 'Sobol'], 'title': 'algo_enum', 'type': 'string'}}, 'properties': {'algo_name': {'$ref': '#/$defs/_algo_enum', 'description': 'The name of the algorithm.'}, 'algo_settings': {'description': 'The settings for the algorithm.', 'title': 'Algo Settings', 'type': 'object'}}, 'required': ['algo_name'], 'title': 'Settings', 'type': 'object'}
Get scenario differentiation modes#
The get_scenario_differentiation_modes() can be used to
get the available differentiation modes of a scenario:
get_scenario_differentiation_modes()
(<DifferentiationMethod.COMPLEX_STEP: 'complex_step'>, <DifferentiationMethod.FINITE_DIFFERENCES: 'finite_differences'>, <DifferentiationMethod.CENTERED_DIFFERENCES: 'centered_differences'>, <DifferentiationMethod.USER_GRAD: 'user'>, <DifferentiationMethod.NO_DERIVATIVE: 'no_derivative'>)
Monitor a scenario#
To monitor a scenario execution programmatically,
ie get a notification when a discipline status is changed,
use monitor_scenario().
The first argument is the scenario to monitor, and the second is an
observer object, that is notified by its update(atom) method, which takes an
ExecutionSequence as argument.
This method will be called every time a discipline status changes.
The atom represents a discipline's position in the process. One discipline
can have multiple atoms, since one discipline can be used in multiple
positions in the MDO formulation.
class Observer:
"""Observer."""
def update(self, atom) -> None:
"""Update method.
:param ExecutionSequence atom: atomic execution sequence.
"""
print(atom)
scenario = create_scenario(
discipline,
"y",
design_space,
scenario_type="DOE",
formulation_name="DisciplinaryOpt",
)
monitor_scenario(scenario, Observer())
scenario.execute(algo_name="PYDOE_FULLFACT", n_samples=25)
INFO - 08:36:15:
INFO - 08:36:15: *** Start DOEScenario execution ***
INFO - 08:36:15: DOEScenario
INFO - 08:36:15: Disciplines: AnalyticDiscipline
INFO - 08:36:15: MDO formulation: DisciplinaryOpt
DOEScenario(RUNNING)
INFO - 08:36:15: Optimization problem:
INFO - 08:36:15: minimize y(x1, x2)
INFO - 08:36:15: with respect to x1, x2
INFO - 08:36:15: over the design space:
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
INFO - 08:36:15: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
INFO - 08:36:15: | x1 | 0 | 0 | 1 | float |
INFO - 08:36:15: | x2 | 0 | 0 | 1 | float |
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
INFO - 08:36:15: Solving optimization problem with algorithm PYDOE_FULLFACT:
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 4%|▍ | 1/25 [00:00<00:00, 1707.78 it/sec, obj=0]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 8%|▊ | 2/25 [00:00<00:00, 1771.99 it/sec, obj=0.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 12%|█▏ | 3/25 [00:00<00:00, 1806.07 it/sec, obj=0.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 16%|█▌ | 4/25 [00:00<00:00, 1831.77 it/sec, obj=0.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 20%|██ | 5/25 [00:00<00:00, 1854.25 it/sec, obj=1]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 24%|██▍ | 6/25 [00:00<00:00, 1867.73 it/sec, obj=0.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 28%|██▊ | 7/25 [00:00<00:00, 1881.46 it/sec, obj=0.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 32%|███▏ | 8/25 [00:00<00:00, 1863.62 it/sec, obj=0.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 36%|███▌ | 9/25 [00:00<00:00, 1869.58 it/sec, obj=1]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 40%|████ | 10/25 [00:00<00:00, 1877.74 it/sec, obj=1.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 44%|████▍ | 11/25 [00:00<00:00, 1879.55 it/sec, obj=0.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 48%|████▊ | 12/25 [00:00<00:00, 1877.07 it/sec, obj=0.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 52%|█████▏ | 13/25 [00:00<00:00, 1878.33 it/sec, obj=1]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 56%|█████▌ | 14/25 [00:00<00:00, 1883.39 it/sec, obj=1.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 60%|██████ | 15/25 [00:00<00:00, 1888.87 it/sec, obj=1.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 64%|██████▍ | 16/25 [00:00<00:00, 1886.25 it/sec, obj=0.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 68%|██████▊ | 17/25 [00:00<00:00, 1889.58 it/sec, obj=1]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 72%|███████▏ | 18/25 [00:00<00:00, 1890.79 it/sec, obj=1.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 76%|███████▌ | 19/25 [00:00<00:00, 1890.09 it/sec, obj=1.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 80%|████████ | 20/25 [00:00<00:00, 1892.91 it/sec, obj=1.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 84%|████████▍ | 21/25 [00:00<00:00, 1893.43 it/sec, obj=1]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 88%|████████▊ | 22/25 [00:00<00:00, 1895.46 it/sec, obj=1.25]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 92%|█████████▏| 23/25 [00:00<00:00, 1894.45 it/sec, obj=1.5]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 96%|█████████▌| 24/25 [00:00<00:00, 1893.27 it/sec, obj=1.75]
AnalyticDiscipline(RUNNING)
AnalyticDiscipline(DONE)
INFO - 08:36:15: 100%|██████████| 25/25 [00:00<00:00, 1895.47 it/sec, obj=2]
INFO - 08:36:15: Optimization result:
INFO - 08:36:15: Optimizer info:
INFO - 08:36:15: Status: None
INFO - 08:36:15: Message: None
INFO - 08:36:15: Number of calls to the objective function by the optimizer: 25
INFO - 08:36:15: Solution:
INFO - 08:36:15: Objective: 0.0
INFO - 08:36:15: Design space:
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
INFO - 08:36:15: | Name | Lower bound | Value | Upper bound | Type |
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
INFO - 08:36:15: | x1 | 0 | 0 | 1 | float |
INFO - 08:36:15: | x2 | 0 | 0 | 1 | float |
INFO - 08:36:15: +------+-------------+-------+-------------+-------+
DOEScenario(DONE)
INFO - 08:36:15: *** End DOEScenario execution (time: 0:00:00.017261) ***
Total running time of the script: (0 minutes 0.683 seconds)