optimisation.design_space.design_space module
Define an object to hold variables and constraints.
- class DesignSpace(variables: list[Variable], constraints: list[Constraint])
Bases:
object
Hold variables and constraints of an optimisation problem.
- __init__(variables: list[Variable], constraints: list[Constraint]) None
- __post_init__() None
Print out some info.
- _check_dimensions(parameters: list[Variable] | list[Constraint]) int
Ensure that all elements have the same number of var or const.
- _parameters_to_single_file_line(element_name: str, parameters: list[DesignSpaceParameter]) dict[str, float | None | tuple[float, float]]
Prepare a dict containing all info of a single element.
- Parameters:
element_name (str) – Name of the element, which will be inserted in the output dict.
parameters (list[DesignSpaceParameter]) – Parameters concerning the element, which
limits
(x_0
if appliable) will be inserted in the dict.
- Returns:
Contains all
Variable
orConstraint
information of the element.- Return type:
dict[str, float | None | tuple[float, float]]
- _str_constraints() list[str]
Generate information on the constraints that were created.
- _str_variables() list[str]
Generate information on the variables that were created.
- _to_file(parameters: list[DesignSpaceParameter], filepath: Path, delimiter: str = ',', **to_csv_kw: Any) None
Save all the design space parameters in a compact file.
- Parameters:
parameters (list[DesignSpaceParameter]) – All the defined parameters.
filepath (Path) – Where file will be stored.
delimiter (str) – Delimiter between two columns. The default is ‘,’.
to_csv_kw (dict[str, Any]) – Keyword arguments given to the pandas
to_csv
method.
- compute_constraints(simulation_output: SimulationOutput) ndarray
Compute constraint violation for
simulation_output
.
- constraints: list[Constraint]
- classmethod from_files(elements_names: Sequence[str], filepath_variables: Path, variables_names: Sequence[str], filepath_constraints: Path | None = None, constraints_names: Sequence[str] | None = None, delimiter: str = ',') Self
Generate design space from files.
- Parameters:
elements_names (Sequence[str]) – Name of the elements with variables and constraints.
filepath_variables (Path) – Path to the
variables.csv
file.variables_names (Sequence[str]) – Name of the variables to create.
filepath_constraints (Path | None, optional) – Path to the
constraints.csv
file. The default is None, in which case no constraint is created.constraints_names (Sequence[str] | None, optional) – Name of the constraints to create. The default is None, in which case no constraint is created.
delimiter (str, optional) – Delimiter in the files. The default is a commma.
- Return type:
cls
- to_files(basepath: Path, variables_filename: Path = PosixPath('variables'), constraints_filename: Path = PosixPath('constraints'), overwrite: bool = False, **to_csv_kw: Any) None
Save variables and constraints in files.
- Parameters:
basepath (Path) – Folder where the files will be stored.
variables_filename (Path) – Name of the output files without extension.
constraints_filename (Path) – Name of the output files without extension.
overwrite (bool, optional) – To overwrite an existing file with the same name or not. The default is False.
to_csv_kw (dict[str, Any]) – Keyword arguments given to the pandas
to_csv
method.
- to_pandas_dataframe() DataFrame
Convert list of variables to a pandas dataframe.
- _from_file(parameter_class: type[Variable], filepath: Path, elements_names: Sequence[str], parameters_names: Sequence[str], delimiter: str = ',') list[Variable]
- _from_file(parameter_class: type[Constraint], filepath: Path, elements_names: Sequence[str], parameters_names: Sequence[str], delimiter: str = ',') list[Constraint]
Generate list of variables or constraints from a given
.csv
.Todo
Add support for when all element do not have the same variables/constraints.
- Parameters:
parameter_class ({type[Variable], type[Constraint]}) – Object which
from_pd_series
method will be called.filepath (Path) – Path to the
.csv
.elements_names (Sequence[str]) – Name of the elements.
parameters_names (Sequence[str]) – Name of the parameters.
delimiter (str) – Delimiter in the
.csv
.
- Returns:
List of variables or constraints.
- Return type:
list[DesignSpaceParameter]
- _gather_dicts_by_key(parameters: list[DesignSpaceParameter], key: str) dict[str, list[DesignSpaceParameter]]
Gather parameters with the same
key
attribute value in lists.- Parameters:
parameters (list[DesignSpaceParameter]) – Objects to study.
key (str) – Name of the attribute against which
parameters
should be gathered.
- Returns:
Keys are all existing values of attribute
key
fromparameters
. Values are lists ofDesignSpaceParameter
withkey
attribute equaling the dict key.- Return type:
dict[Any, list[DesignSpaceParameter]]
- _merge(dicts: list[dict]) dict
Merge a list of dicts in a single dict.
- _parameters_to_dict(parameters: list[DesignSpaceParameter], to_get: Sequence[str]) list[dict]
Convert several design space parameters to dict.
We use the
prepend_parameter_name
argument to prepend the name of eachparameter.name
to the name of the valuesto_get
. This way, we avoid dictionaries sharing the same keys in the output list.- Parameters:
parameters (list[DesignSpaceParameter]) – Where
to_get
will be looked for.to_get (Sequence[str]) – Values to get.
- Returns:
Contains
to_get
values in dictionaries for every parameter.- Return type:
list[dict]