You need to sign in or sign up before continuing.
Bug: MDOFunction string representation from OptimizationProblem.import_hdf
Summary
After importing an optimization problem from a .h5 file and trying to get the string representation of the objective or the constraints, the following error raises: TypeError: sequence item 0: expected str instance, bytes found. A similar issue can be found on Stack Overflow.
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
design_space = DesignSpace()
design_space.add_variable("x0", l_b=0.0, u_b=1.0, value=0.5)
design_space.add_variable("x1", l_b=0.0, u_b=1.0, value=0.5)
problem = OptimizationProblem(design_space)
problem.objective = MDOFunction(
lambda x: x[0] + x[1], "f", args=["x0", "x1"], outvars=["z"]
)
problem.evaluate_functions()
problem.export_hdf("file.hdf5")
new_problem = OptimizationProblem.import_hdf("file.hdf5")
print(new_problem.objective)
What is the current bug behavior?
Traceback (most recent call last):
File "C:/Users/matthias.delozzo/workspace/sandbox/function_bytes.py", line 16, in <module>
print(new_problem.objective)
File "C:\Users\matthias.delozzo\workspace\GEMSEO_fork\gemseo\src\gemseo\core\mdofunctions\mdo_function.py", line 484, in __repr__
return self.special_repr or self.default_repr
File "C:\Users\matthias.delozzo\workspace\GEMSEO_fork\gemseo\src\gemseo\core\mdofunctions\mdo_function.py", line 491, in default_repr
arguments = ", ".join(self.args)
TypeError: sequence item 0: expected str instance, bytes found
What is the expected correct behavior?
f(x0, x1)
Possible fixes
In OptimizationProblem.__h5_group_to_dict
, we could replace:
if isinstance(value, bytes):
value = value.decode()
by:
if isinstance(value, bytes):
value = value.decode()
elif isinstance(value, List):
value = [
sub_value.decode() if isinstance(sub_value, bytes) else sub_value
for sub_value in value
]