Note
Click here to download the full example code
Parameter space¶
In this example, we will see the basics of ParameterSpace
.
from __future__ import division, unicode_literals
from gemseo.algos.parameter_space import ParameterSpace
from gemseo.api import configure_logger, create_discipline, create_scenario
configure_logger()
Out:
<RootLogger root (INFO)>
Firstly, a ParameterSpace
does not require any mandatory argument.
Create a parameter space¶
parameter_space = ParameterSpace()
Then, we can add either deterministic variables from their lower and upper
bounds (use DesignSpace.add_variable()
)
or uncertain variables from their distribution names and parameters
(use add_random_variable()
)
parameter_space.add_variable("x", l_b=-2.0, u_b=2.0)
parameter_space.add_random_variable("y", "SPNormalDistribution", mu=0.0, sigma=1.0)
print(parameter_space)
Out:
+----------------------------------------------------------------------------+
| Parameter space |
+------+-------------+-------+-------------+-------+-------------------------+
| name | lower_bound | value | upper_bound | type | Initial distribution |
+------+-------------+-------+-------------+-------+-------------------------+
| x | -2 | None | 2 | float | |
| y | -inf | 0 | inf | float | norm(mu=0.0, sigma=1.0) |
+------+-------------+-------+-------------+-------+-------------------------+
We can check that the deterministic and uncertain variables are implemented as deterministic and deterministic variables respectively:
print("x is deterministic: ", parameter_space.is_deterministic("x"))
print("y is deterministic: ", parameter_space.is_deterministic("y"))
print("x is uncertain: ", parameter_space.is_uncertain("x"))
print("y is uncertain: ", parameter_space.is_uncertain("y"))
Out:
x is deterministic: True
y is deterministic: False
x is uncertain: False
y is uncertain: True
Sample from the parameter space¶
We can sample the uncertain variables from the ParameterSpace
and
get values either as an array (default value) or as a dictionary:
sample = parameter_space.compute_samples(n_samples=2, as_dict=True)
print(sample)
sample = parameter_space.compute_samples(n_samples=4)
print(sample)
Out:
[{'y': array([-2.24477858])}, {'y': array([1.31078112])}]
[[-0.37021933]
[-2.19431686]
[ 1.66306461]
[ 0.58473134]]
Sample a discipline over the parameter space¶
We can also sample a discipline over the parameter space. For simplicity,
we instantiate an AnalyticDiscipline
from a dictionary of
expressions and update the cache policy # so as to cache all data in memory.
discipline = create_discipline("AnalyticDiscipline", expressions_dict={"z": "x+y"})
discipline.set_cache_policy(discipline.MEMORY_FULL_CACHE)
From these parameter space and discipline, we build a DOEScenario
and execute it with a Latin Hypercube Sampling algorithm and 100 samples.
Warning
A Scenario
deals with all variables available in the
DesignSpace
. By inheritance, a DOEScenario
deals
with all variables available in the ParameterSpace
.
Thus, if we do not filter the uncertain variables, the
DOEScenario
will consider all variables. In particular, the
deterministic variables will be consider as uniformly distributed.
scenario = create_scenario(
[discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE"
)
scenario.execute({"algo": "lhs", "n_samples": 100})
Out:
INFO - 21:49:56:
INFO - 21:49:56: *** Start DOE Scenario execution ***
INFO - 21:49:56: DOEScenario
INFO - 21:49:56: Disciplines: AnalyticDiscipline
INFO - 21:49:56: MDOFormulation: DisciplinaryOpt
INFO - 21:49:56: Algorithm: lhs
INFO - 21:49:56: Optimization problem:
INFO - 21:49:56: Minimize: z(x, y)
INFO - 21:49:56: With respect to: x, y
INFO - 21:49:56: DOE sampling: 0%| | 0/100 [00:00<?, ?it]
INFO - 21:49:56: DOE sampling: 74%|███████▍ | 74/100 [00:00<00:00, 987.00 it/sec, obj=2.3]
INFO - 21:49:56: DOE sampling: 100%|██████████| 100/100 [00:00<00:00, 735.15 it/sec, obj=-1.22]
INFO - 21:49:56: Optimization result:
INFO - 21:49:56: Objective value = -1.8744085267228487
INFO - 21:49:56: The result is feasible.
INFO - 21:49:56: Status: None
INFO - 21:49:56: Optimizer message: None
INFO - 21:49:56: Number of calls to the objective function by the optimizer: 100
INFO - 21:49:56: +------------------------------------------------------------------------------------------+
INFO - 21:49:56: | Parameter space |
INFO - 21:49:56: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:56: | name | lower_bound | value | upper_bound | type | Initial distribution |
INFO - 21:49:56: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:56: | x | -2 | -1.959995425007306 | 2 | float | |
INFO - 21:49:56: | y | -inf | 0.08558689828445752 | inf | float | norm(mu=0.0, sigma=1.0) |
INFO - 21:49:56: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:56: *** DOE Scenario run terminated ***
{'eval_jac': False, 'algo': 'lhs', 'n_samples': 100}
We can visualize the result by encapsulating the disciplinary cache in
a Dataset
:
dataset = discipline.cache.export_to_dataset()
This visualization can be tabular for example:
print(dataset.export_to_dataframe())
Out:
inputs outputs
x y z
0 0 0
0 1.869403 0.893701 2.763103
1 -1.567970 0.999490 -0.568480
2 0.282640 0.459495 0.742135
3 1.916313 0.967722 2.884034
4 1.562653 0.721075 2.283728
.. ... ... ...
95 0.120633 0.371654 0.492286
96 -0.999225 0.928048 -0.071178
97 -1.396066 0.165332 -1.230734
98 1.090093 0.589230 1.679323
99 -1.433207 0.217893 -1.215314
[100 rows x 3 columns]
or graphical by means of a scatter plot matrix for example:
dataset.plot("ScatterMatrix")
Out:
<gemseo.post.dataset.scatter_plot_matrix.ScatterMatrix object at 0x7f61948533d0>
Sample a discipline over the uncertain space¶
If we want to sample a discipline over the uncertain space, we need to filter the uncertain variables:
parameter_space.filter(parameter_space.uncertain_variables)
Out:
<gemseo.algos.parameter_space.ParameterSpace object at 0x7f61999a0430>
Then, we clear the cache, create a new scenario from this parameter space containing only the uncertain variables and execute it.
discipline.cache.clear()
scenario = create_scenario(
[discipline], "DisciplinaryOpt", "z", parameter_space, scenario_type="DOE"
)
scenario.execute({"algo": "lhs", "n_samples": 100})
Out:
INFO - 21:49:57:
INFO - 21:49:57: *** Start DOE Scenario execution ***
INFO - 21:49:57: DOEScenario
INFO - 21:49:57: Disciplines: AnalyticDiscipline
INFO - 21:49:57: MDOFormulation: DisciplinaryOpt
INFO - 21:49:57: Algorithm: lhs
INFO - 21:49:57: Optimization problem:
INFO - 21:49:57: Minimize: z(y)
INFO - 21:49:57: With respect to: y
INFO - 21:49:57: DOE sampling: 0%| | 0/100 [00:00<?, ?it]
INFO - 21:49:57: DOE sampling: 76%|███████▌ | 76/100 [00:00<00:00, 994.08 it/sec, obj=0.667]
INFO - 21:49:57: DOE sampling: 100%|██████████| 100/100 [00:00<00:00, 752.95 it/sec, obj=0.208]
INFO - 21:49:57: Optimization result:
INFO - 21:49:57: Objective value = 0.00417022004702574
INFO - 21:49:57: The result is feasible.
INFO - 21:49:57: Status: None
INFO - 21:49:57: Optimizer message: None
INFO - 21:49:57: Number of calls to the objective function by the optimizer: 100
INFO - 21:49:57: +------------------------------------------------------------------------------------------+
INFO - 21:49:57: | Parameter space |
INFO - 21:49:57: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:57: | name | lower_bound | value | upper_bound | type | Initial distribution |
INFO - 21:49:57: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:57: | y | -inf | 0.00417022004702574 | inf | float | norm(mu=0.0, sigma=1.0) |
INFO - 21:49:57: +------+-------------+---------------------+-------------+-------+-------------------------+
INFO - 21:49:57: *** DOE Scenario run terminated ***
{'eval_jac': False, 'algo': 'lhs', 'n_samples': 100}
Finally, we build a dataset from the disciplinary cache and visualize it. We can see that the deterministic variable ‘x’ is set to its default value for all evaluations, contrary to the previous case where we were considering the whole parameter space.
dataset = discipline.cache.export_to_dataset()
print(dataset.export_to_dataframe())
Out:
inputs outputs
x y z
0 0 0
0 0.0 0.260850 0.260850
1 0.0 0.346919 0.346919
2 0.0 0.709034 0.709034
3 0.0 0.827509 0.827509
4 0.0 0.017203 0.017203
.. ... ... ...
95 0.0 0.532655 0.532655
96 0.0 0.138781 0.138781
97 0.0 0.223134 0.223134
98 0.0 0.482878 0.482878
99 0.0 0.208007 0.208007
[100 rows x 3 columns]
Total running time of the script: ( 0 minutes 1.227 seconds)