Python pymoo - Passing independent variables as arguments

656 Views Asked by At

I am using pymoo package to do multi-objective optimization, and I am having trouble setting up my model, because I get errors when trying to pass as arguments other independent variables (apart from the parameters that are being optimized). I tried following the getting_started example (https://pymoo.org/getting_started.html) for both OOP and functional programming. My objective functions have independent variables t, total and G, where t and total are arrays and G is a scalar. I try to pass them like so:

class MyProblem(Problem): 
    
    def __init__(self):
        super().__init__(n_var = 3, 
                         n_obj = 2, 
                         n_constr = 0, 
                         xl = np.array([0.0.0,0.0, -0.5]), 
                         xu = np.array([0.8, 10.0, 0.9]),
                         elementwise_evaluation = True)   
    
    def _evaluate(self, p, out, total, G, t):                         # *args = [total, G, t]
        f1 = 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
        f2 = 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
        
        out["F"] = np.column_stack([f1, f2])

elementwise_problem = MyProblem()

problem = elementwise_problem

resulting in:

TypeError: _evaluate() got an unexpected keyword argument 'algorithm'

p is my list of three parameters to be optimized.

Using functional programming I couldn't find where the args can be passed in the FunctionalProblem object, so I just did:

objs = [
        lambda p, total, t: 1/3*total*(1+2*((p[0]-p[2])*np.exp(-t/p[1]) + p[2])), 
        lambda p, total, t, G: 1/3*total*G*(1-((p[0]-p[2])*np.exp(-t/p[1]) + p[2]))
        ]
    
constr_ieq = []
 
functional_problem = FunctionalProblem(3,  
                                    objs,      
                                    constr_ieq = constr_ieq,    
                                    xl = np.array([0.0, 0.01, -0.1]),   
                                    xu = np.array([0.8, 50.0, 0.8]))   
    
problem = functional_problem

which results in:

TypeError: () missing 2 required positional arguments: 'total' and 't'

The rest of the code (algorithm and termination objects etc) are the same as in the Getting_started example, since I am just trying to get it running now..

Has anyone tried passing arguments using pymoo and knows how to do it properly?

1

There are 1 best solutions below

3
On BEST ANSWER

You may define your independent variables inside MyProblem class and then

def _evaluate(self, p, out):                         
        f1 = 1/3*self.total*(1+2*((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))
        f2 = 1/3*self.total*self.G*(1-((p[0]-p[2])*np.exp(-self.t/p[1]) + p[2]))