OptimizationProblem crashes when the default value of a float design variable is an integer
Summary
OptimizationProblem.evaluate_functions
raises a NumPy exception when the design space contains only variables of type float
with default values passed as integers. If the design space contains a tvariable of type float
with a default value passed as an integer and a variable of type float
with a default value passed as a float, it works. An integer being a real, the user should be able to pass an integer as default value.
Gemseo version
develop
Platfom info
windows
Environment info
tox / dev-win
Steps to reproduce
from gemseo.algos.design_space import DesignSpace
from gemseo.algos.opt_problem import OptimizationProblem
from gemseo.core.mdofunctions.mdo_function import MDOFunction
ds = DesignSpace()
ds.add_variable("x", l_b=0.0, u_b=1.0, value=0)
problem = OptimizationProblem(ds)
problem.objective = MDOFunction(lambda x: x, "f")
problem.evaluate_functions()
What is the current bug behavior?
ds.__lower_bounds_array
is of type float
while norm_vect
is of type int
in ds.normalize_vect
, making the operation norm_vect-ds.__lower_bounds_array
crash.
Traceback (most recent call last):
File "C:/Users/matthias.delozzo/workspace/GEMSEO_fork/sandbox/current_value.py", line 11, in <module>
problem.evaluate_functions()
File "C:\Users\matthias.delozzo\workspace\GEMSEO_fork\gemseo\src\gemseo\algos\opt_problem.py", line 886, in evaluate_functions
x_vect = self.get_x0_normalized()
File "C:\Users\matthias.delozzo\workspace\GEMSEO_fork\gemseo\src\gemseo\algos\opt_problem.py", line 721, in get_x0_normalized
return dspace.normalize_vect(dspace.get_current_x())
File "C:\Users\matthias.delozzo\workspace\GEMSEO_fork\gemseo\src\gemseo\algos\design_space.py", line 1053, in normalize_vect
norm_vect[norm_inds] -= self.__lower_bounds_array[norm_inds]
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('int32') with casting rule 'same_kind'
What is the expected correct behavior?
OptimizationProblem.evaluate_functions
should work.
Possible fixes
If the dtype.kind
of the passed value
is "i"
while the var_type
is float
, then cast the integer to float.