Note
Click here to download the full example code
Design space¶
In this example, we will discover the different functions of the API related to design space, which is a key element to represent the space of parameters on which a scenario will evaluate a list of disciplines.
from __future__ import annotations
from gemseo.api import configure_logger
from gemseo.api import create_design_space
from gemseo.api import export_design_space
from gemseo.api import read_design_space
from numpy import array
configure_logger()
<RootLogger root (INFO)>
Create a design space¶
To create a standard DesignSpace
,
the API function create_design_space()
can be used.
This function does not take any argument.
This function returns an empty instance of
DesignSpace
.
design_space = create_design_space()
print(design_space)
Design space:
+------+-------------+-------+-------------+------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+------+
+------+-------------+-------+-------------+------+
Once built, we can add variables. E.g.
design_space.add_variable(
"x", 2, l_b=array([0.0] * 2), u_b=array([1.0] * 2), value=array([0.5] * 2)
)
print(design_space)
Design space:
+------+-------------+-------+-------------+-------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+-------+
| x[0] | 0 | 0.5 | 1 | float |
| x[1] | 0 | 0.5 | 1 | float |
+------+-------------+-------+-------------+-------+
Read a design space¶
In presence of a design space specified in a CSV file,
the API function read_design_space()
can be used.
Its first argument is the file path of the design space. Its second argument is the list of fields available in the file and is optional.
By default, the design space reads these information from the file.
This function returns an instance of
DesignSpace
.
design_space.export_to_txt("saved_design_space.txt")
loaded_design_space = read_design_space("saved_design_space.txt")
Write a design space¶
To export an instance of DesignSpace
into an hdf or txt file,
the export_design_space()
API function can be used:
loaded_design_space.add_variable("y", l_b=-1, u_b=3, value=0.0)
export_design_space(loaded_design_space, "saved_design_space.txt")
print(read_design_space("saved_design_space.txt"))
Design space:
+------+-------------+-------+-------------+-------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+-------+
| x[0] | 0 | 0.5 | 1 | float |
| x[1] | 0 | 0.5 | 1 | float |
| y | -1 | 0 | 3 | float |
+------+-------------+-------+-------------+-------+
Total running time of the script: ( 0 minutes 0.016 seconds)