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([-1.12998636])}, {'y': array([0.73185832])}]
[[ 0.80627203]
 [-1.19246241]
 [ 0.66020314]
 [-1.00598885]]

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

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

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

Gallery generated by Sphinx-Gallery