1

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?

ISquared
  • 364
  • 4
  • 22
  • Can somebody help me with this problem: https://stackoverflow.com/questions/75775606/constructing-a-custom-problem-in-pymoo-picking-a-subset-of-vehicles-from-a-bag – rockstone435 Mar 20 '23 at 06:01

1 Answers1

1

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]))
furious_bilbo
  • 176
  • 11
  • Thank you! I normally just do functional programming so OOP is new to me, sorry! However, even after I successfully add the other args, and even without additional args at all, I still get "TypeError: _evaluate() got an unexpected keyword argument 'algorithm'" ... I am trying to trace back from the source code to see what the root of this might be .. – ISquared Jul 14 '21 at 14:45
  • @Isquare1 it's not clear from your posted code, how do you get such TypeError. In pymoo 'algorithm' is usually optimizing method itself – furious_bilbo Jul 15 '21 at 06:22
  • Thank you so much for your input, I decided to make a new topic because the problem changed 100%: https://stackoverflow.com/questions/68391065/pymoo-python-typeerror-evaluate-got-an-unexpected-keyword-argument-algorit – ISquared Jul 15 '21 at 09:14