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([-0.06952932])}, {'y': array([-0.19329269])}]
[[ 0.60347518]
 [ 0.52930811]
 [-0.2089453 ]
 [-1.72762243]]

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 - 14:40:52:
    INFO - 14:40:52: *** Start DOE Scenario execution ***
    INFO - 14:40:52: DOEScenario
    INFO - 14:40:52:    Disciplines: AnalyticDiscipline
    INFO - 14:40:52:    MDOFormulation: DisciplinaryOpt
    INFO - 14:40:52:    Algorithm: lhs
    INFO - 14:40:52: Optimization problem:
    INFO - 14:40:52:    Minimize: z(x, y)
    INFO - 14:40:52:    With respect to: x, y
    INFO - 14:40:52: DOE sampling:   0%|          | 0/100 [00:00<?, ?it]
    INFO - 14:40:52: DOE sampling:  74%|███████▍  | 74/100 [00:00<00:00, 993.90 it/sec, obj=2.3]
    INFO - 14:40:52: DOE sampling: 100%|██████████| 100/100 [00:00<00:00, 735.90 it/sec, obj=-1.22]
    INFO - 14:40:52: Optimization result:
    INFO - 14:40:52: Objective value = -1.8744085267228487
    INFO - 14:40:52: The result is feasible.
    INFO - 14:40:52: Status: None
    INFO - 14:40:52: Optimizer message: None
    INFO - 14:40:52: Number of calls to the objective function by the optimizer: 100
    INFO - 14:40:52: +------------------------------------------------------------------------------------------+
    INFO - 14:40:52: |                                     Parameter space                                      |
    INFO - 14:40:52: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:52: | name | lower_bound |        value        | upper_bound | type  |   Initial distribution  |
    INFO - 14:40:52: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:52: | x    |      -2     |  -1.959995425007306 |      2      | float |                         |
    INFO - 14:40:52: | y    |     -inf    | 0.08558689828445752 |     inf     | float | norm(mu=0.0, sigma=1.0) |
    INFO - 14:40:52: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:52: *** 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 0x7fcaacb90640>

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 0x7fcab1cce760>

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 - 14:40:53:
    INFO - 14:40:53: *** Start DOE Scenario execution ***
    INFO - 14:40:53: DOEScenario
    INFO - 14:40:53:    Disciplines: AnalyticDiscipline
    INFO - 14:40:53:    MDOFormulation: DisciplinaryOpt
    INFO - 14:40:53:    Algorithm: lhs
    INFO - 14:40:53: Optimization problem:
    INFO - 14:40:53:    Minimize: z(y)
    INFO - 14:40:53:    With respect to: y
    INFO - 14:40:53: DOE sampling:   0%|          | 0/100 [00:00<?, ?it]
    INFO - 14:40:53: DOE sampling:  75%|███████▌  | 75/100 [00:00<00:00, 992.93 it/sec, obj=0.711]
    INFO - 14:40:53: DOE sampling: 100%|██████████| 100/100 [00:00<00:00, 740.09 it/sec, obj=0.208]
    INFO - 14:40:53: Optimization result:
    INFO - 14:40:53: Objective value = 0.00417022004702574
    INFO - 14:40:53: The result is feasible.
    INFO - 14:40:53: Status: None
    INFO - 14:40:53: Optimizer message: None
    INFO - 14:40:53: Number of calls to the objective function by the optimizer: 100
    INFO - 14:40:53: +------------------------------------------------------------------------------------------+
    INFO - 14:40:53: |                                     Parameter space                                      |
    INFO - 14:40:53: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:53: | name | lower_bound |        value        | upper_bound | type  |   Initial distribution  |
    INFO - 14:40:53: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:53: | y    |     -inf    | 0.00417022004702574 |     inf     | float | norm(mu=0.0, sigma=1.0) |
    INFO - 14:40:53: +------+-------------+---------------------+-------------+-------+-------------------------+
    INFO - 14:40:53: *** 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.164 seconds)

Gallery generated by Sphinx-Gallery