celi
celi

Reputation: 13

do() missing 2 required positional arguments: 'n_select' and 'n_parents' during Pymoo optimization

I'm trying to find the answer to the multi-objective optimization problem, using the pymoo library. Objective is to find the set of Pareto Fronts with dominance condition, in the risk_cost function. There are also cost_alpha, risk_alpha function, so to unify the variable I just let made two composite functions. This is the code that I've tried:

`def risk_cost_alpha(x): 
return (somefunction(x))

def cost_risk_alpha(x):
    return (someotherfunction(x))

class MyProblem(Problem):
    def __init__(self):
    super().__init__(n_var = 1, 
                     n_obj = 2,
             xl=np.array([0]),
             xu = np.array([10]))
def _evaluate(self, risk_cost_alpha,cost_risk_alpha, out, *args, **kwargs): 
    f1 = risk_cost_alpha(x)
    f2 = cost_risk_alpha(x)
    out["F"] = np.column_stack([f1,f2])

algorithm = NSGA2(
             pop_size= 40,
                 sampling = RandomSelection(),
                 selection = TournamentSelection(func_comp=binary_tournament),
             crossover = SBX(prob = 0.9, eta = 15),
             mutation = PolynomialMutation(eta = 20),
                 output = MultiObjectiveOutput(),
                 eliminate_duplicates=True
                 )
problem = MyProblem()
res = minimize(problem, algorithm, ("n_gen", 100),seed = 1, verbose = True)`

I followed mostly the same codes for setting algorithms and res from this link: https://pymoo.org/algorithms/moo/nsga2.html#nb-nsga2, just to define the problem differently.

This is the error that I got: (just modified some names of the File path)

res = minimize(problem, algorithm, ("n_gen", 100),seed = 1, verbose = True) Traceback (most recent call last): File "", line 1, in File "C:\Users\Miniconda3\lib\site-packages\pymoo\optimize.py", line 67, in minimize res = algorithm.run() File "C:\Users\Miniconda3\lib\site-packages\pymoo\core\algorithm.py", line 141, in run self.next() File "C:\Users\Miniconda3\lib\site-packages\pymoo\core\algorithm.py", line 157, in next infills = self.infill() File "C:\Users\Miniconda3\lib\site-packages\pymoo\core\algorithm.py", line 189, in infill infills = self._initialize_infill() File "C:\Users\Miniconda3\lib\site-packages\pymoo\algorithms\base\genetic.py", line 75, in _initialize_infill pop = self.initialization.do(self.problem, self.pop_size, algorithm=self) File "C:\Users\Miniconda3\lib\site-packages\pymoo\core\initialization.py", line 32, in do pop = self.sampling(problem, n_samples, **kwargs) File "C:\Users\Miniconda3\lib\site-packages\pymoo\core\operator.py", line 27, in call out = self.do(problem, elem, *args, **kwargs) TypeError: do() missing 2 required positional arguments: 'n_select' and 'n_parents'

I tried to follow the errors link, to see there did do() come from, it first occured at:

`def _initialize_infill(self):
    pop = self.initialization.do(self.problem, self.pop_size, algorithm=self)
    return pop`

then

`def __call__(self, problem, elem, *args, to_numpy=False, **kwargs):
    out = self.do(problem, elem, *args, **kwargs)

    if self.vtype is not None:
        for ind in out:
            ind.X = ind.X.astype(self.vtype)

    # allow to have a built-in repair (can be useful to customize standard crossover)
    if self.repair is not None:
        self.repair.do(problem, out)

    if to_numpy:
        out = np.array([ind.X for ind in out])

    return out`

So according to this code, I thought if I add the variable n_select, and n_parents in the class where I defined Problem it would be okay, but it will still come up with the same error. I am quite sure there is some problem with how I defined the class Problem, but I am not sure which part that I should change to make this code work.

Upvotes: 0

Views: 171

Answers (2)

Bernard da Silva
Bernard da Silva

Reputation: 1

Here the sampling change worked as suggested.

Upvotes: 0

M_Heddar
M_Heddar

Reputation: 1

I faced similar problem like yours I think you need to change the RandomSelection() with a sampling operator like LHS()

from pymoo.operators.sampling.lhs import LHS

Upvotes: 0

Related Questions