Note
Click here to download the full example code
DesignSpace creation and manipulation¶
In this example, we will see how to create and how to manipulate an instance of
DesignSpace
.
from __future__ import annotations
from gemseo.api import configure_logger
from gemseo.api import create_design_space
from numpy import array
configure_logger()
<RootLogger root (INFO)>
Create a design space¶
The user can create an instance of the DesignSpace
using the API
and the create_design_space()
function.
design_space = create_design_space()
Add design variables¶
The user can add new design variables using the DesignSpace.add_variable()
. In
the following example, we add the x variable in the design space. We also
define the lower and upper bound of the variable.
It is then possible to plot the DesignSpace
instance either using a
print statement or by using the logger.
design_space.add_variable("x", l_b=array([-2.0]), u_b=array([2.0]), value=array([0.0]))
print(design_space)
Design space:
+------+-------------+-------+-------------+-------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+-------+
| x | -2 | 0 | 2 | float |
+------+-------------+-------+-------------+-------+
The user can also add design variables with dimension greater than one. To do that, the user can use the size keyword:
design_space.add_variable(
"y", l_b=array([-2.0, -1.0]), u_b=array([2.0, 1.0]), value=array([0.0, 0.0]), size=2
)
print(design_space)
Design space:
+------+-------------+-------+-------------+-------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+-------+
| x | -2 | 0 | 2 | float |
| y[0] | -2 | 0 | 2 | float |
| y[1] | -1 | 0 | 1 | float |
+------+-------------+-------+-------------+-------+
By default, each variable infers its type from the given values. One may also specify it with the var_type keyword
design_space.add_variable(
"z",
l_b=array([0, -1]),
u_b=array([3, 1]),
value=array([0, 1]),
size=2,
var_type="integer",
)
design_space.add_variable(
"w",
l_b=array([-2, -5]),
u_b=array([3, 1]),
value=array([2, -2]),
size=2,
var_type=["integer", "integer"],
)
print(design_space)
Design space:
+------+-------------+-------+-------------+---------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+---------+
| x | -2 | 0 | 2 | float |
| y[0] | -2 | 0 | 2 | float |
| y[1] | -1 | 0 | 1 | float |
| z[0] | 0 | 0 | 3 | integer |
| z[1] | -1 | 1 | 1 | integer |
| w[0] | -2 | 2 | 3 | integer |
| w[1] | -5 | -2 | 1 | integer |
+------+-------------+-------+-------------+---------+
Note
Some optimization algorithms may not handle integer variables properly. For updated information about the optimization algorithms that handle integer variables, refer to Optimization algorithms.
For additional information on how GEMSEO handles integer variables, refer to How to deal with design spaces.
Remove design variables¶
The user can also remove a variable in the design space using the
DesignSpace.remove_variable()
method:
design_space.remove_variable("x")
print(design_space)
Design space:
+------+-------------+-------+-------------+---------+
| name | lower_bound | value | upper_bound | type |
+------+-------------+-------+-------------+---------+
| y[0] | -2 | 0 | 2 | float |
| y[1] | -1 | 0 | 1 | float |
| z[0] | 0 | 0 | 3 | integer |
| z[1] | -1 | 1 | 1 | integer |
| w[0] | -2 | 2 | 3 | integer |
| w[1] | -5 | -2 | 1 | integer |
+------+-------------+-------+-------------+---------+
Total running time of the script: ( 0 minutes 0.017 seconds)