-
Hi everyone, I develop an open source framework for modeling component based thermal conversion processes (https://github.com/oemof/tespy). One of the features the package had (before I did some refactoring) was that you could pass a model to a blackbox optimization tool, i.e. https://github.com/esa/pygmo2 and run multivariate optimization of your power plant/heat pump/... model in tespy. The reason it worked was, that the import pygmo as pg
import CoolProp as CP
fluid = CP.AbstractState("HEOS", "water")
class SampleProblem:
def __init__(self):
pass
def fitness(self, x):
# mock values for objective
fluid.update(CP.HmassP_INPUTS, 1e6, 1e5)
return [fluid.hmass()]
def get_bounds(self):
# lower and upper bound
return ([0], [10])
optimize = SampleProblem()
num_ind = 1
pop = pg.population(pg.problem(optimize), size=num_ind, seed=42) However now I changed the structure and I end up with something like here: import pygmo as pg
import CoolProp as CP
class SampleProblem:
def __init__(self):
self.fluid = CP.AbstractState("HEOS", "water")
def fitness(self, x):
# mock values for objective
self.fluid.update(CP.HmassP_INPUTS, 1e6, 1e5)
return [self.fluid.hmass()]
def get_bounds(self):
# lower and upper bound
return ([0], [10])
optimize = SampleProblem()
num_ind = 1
pop = pg.population(pg.problem(optimize), size=num_ind, seed=42) This produces an error ( Best and many thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Okay, found a solution on how to fix it: class SerializableAbstractState(CP.AbstractState):
def __init__(self, back_end, fluid_name):
self.back_end = back_end
self.fluid_name = fluid_name
def __reduce__(self):
return (self.__class__, (self.back_end, self.fluid_name)) Then you simply use CoolProp/wrappers/Python/CoolProp/AbstractState.pyx Lines 26 to 32 in 85f7684 Have a nice weekend! |
Beta Was this translation helpful? Give feedback.
-
I strongly advise against serializing the classes. It is better to serialize the inputs to the factory function yourself. There are a lot of possible wrinkles, including things like setting flags, environment variables, compositions for mixtures, etc. Also, testing is much easier when you don't serialize. |
Beta Was this translation helpful? Give feedback.
Okay, found a solution on how to fix it:
Then you simply use
SerializableAbstractState
as a wrapper for yourAbstractState
objects. Is adding the__reduce__
an idea worth adding to the CoolProp base-class? Let me know, if you think it is worth it, I'll make a PR then.CoolProp/wrappers/Python/CoolProp/AbstractState.pyx
Lines 26 to 32 in 85f7684