Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError in problem.py: unsupported operand type(s) for *: 'float' and 'NoneType' #405

Open
fracerma opened this issue Mar 24, 2023 · 2 comments
Assignees

Comments

@fracerma
Copy link

Description

I get this error frompymoo/core/problem.py when instantiating a subclass of Problem.
In my case I pass vars as a dictionary: vars = { 'x1': Choice(), 'x2' Choice(), 'x3': Integer(lb=1, ub=10) leaving xl and xu as None.

I think there is a problem in this part:

...
self.xl, self.xu = xl, xu

# if the variables are provided in their explicit form
if vars is not None:
    self.vars = vars
    self.n_var = len(vars)
    if self.xl is None:
        self.xl = {name: var.lb if hasattr(var, "lb") else None for name, var in vars.items()}
    if self.xu is None:
        self.xu = {name: var.ub if hasattr(var, "ub") else None for name, var in vars.items()}

...

# if it is a problem with an actual number of variables - make sure xl and xu are numpy arrays
if n_var > 0:

    if self.xl is not None:
        if not isinstance(self.xl, np.ndarray):
            self.xl = np.ones(n_var) * xl
        self.xl = self.xl.astype(float)

    if self.xu is not None:
        if not isinstance(self.xu, np.ndarray):
            self.xu = np.ones(n_var) * xu
        self.xu = self.xu.astype(float)

xl and xu are not checked to be not None and I'd changed it as:

if n_var > 0:

            if xl is not None:
                if not isinstance(self.xl, np.ndarray):
                    self.xl = np.ones(n_var) * xl
                self.xl = self.xl.astype(float)

            if xu is not None:
                if not isinstance(self.xu, np.ndarray):
                    self.xu = np.ones(n_var) * xu
                self.xu = self.xu.astype(float)

self.xl/xu should be a numpy array or a dictionary?
I also don't understand when this values are used, since the upper and lower bound are taken directly from each different variable type, right?

Environment

  • OS: Ubuntu 20.04
  • Python Version: 3.7
  • Package Versions:
    • numpy: 1.21.6
    • pymoo: 0.6.0.1

Error Traceback

...
  File "/home/user/miniconda3/envs/fatai/lib/python3.7/site-packages/pymoo/core/problem.py", line 158, in __init__
    self.xl = np.ones(n_var) * xl
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
@blankjul
Copy link
Collaborator

Can you please provide an example to reproduce the error?

The Integer variable takes bounds as an input. See the following example below:

from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.mixed import MixedVariableSampling, MixedVariableMating, \
    MixedVariableDuplicateElimination
from pymoo.core.problem import ElementwiseProblem
from pymoo.core.variable import Choice, Integer
from pymoo.optimize import minimize


class MultiObjectiveMixedVariableProblem(ElementwiseProblem):

    def __init__(self, **kwargs):
        vars = {'x1': Choice(options=[0,1]), 'x2': Choice(options=[0,1]), 'x3': Integer(bounds=(1, 10))}
        super().__init__(vars=vars, n_obj=2, n_ieq_constr=0, **kwargs)

    def _evaluate(self, X, out, *args, **kwargs):
        out["F"] = [X["x1"], X["x2"] * X["x3"]]


problem = MultiObjectiveMixedVariableProblem()

algorithm = NSGA2(pop_size=50,
                  sampling=MixedVariableSampling(),
                  mating=MixedVariableMating(eliminate_duplicates=MixedVariableDuplicateElimination()),
                  eliminate_duplicates=MixedVariableDuplicateElimination(),
                  )


res = minimize(problem,
               algorithm,
               seed=1,
               verbose=True)

@blankjul blankjul self-assigned this Mar 25, 2023
@fracerma
Copy link
Author

Hello,
the same error happens when using the Problem class and inititializing with n_var setted:

from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.mixed import MixedVariableSampling, MixedVariableMating, \
    MixedVariableDuplicateElimination
from pymoo.core.problem import Problem
from pymoo.core.variable import Choice, Integer
from pymoo.optimize import minimize


class MultiObjectiveMixedVariableProblem(Problem):

    def __init__(self, **kwargs):
        vars = {'x1': Choice(options=[0,1]), 'x2': Choice(options=[0,1]), 'x3': Integer(bounds=(1, 10))}
        super().__init__(vars=vars, n_obj=2, n_ieq_constr=0, n_var=3, **kwargs)

    def _evaluate(self, X, out, *args, **kwargs):
        out["F"] = [[x["x1"], x["x2"] * x["x3"]] for x in X]


problem = MultiObjectiveMixedVariableProblem()

algorithm = NSGA2(pop_size=50,
                  sampling=MixedVariableSampling(),
                  mating=MixedVariableMating(eliminate_duplicates=MixedVariableDuplicateElimination()),
                  eliminate_duplicates=MixedVariableDuplicateElimination(),
                  )


res = minimize(problem,
               algorithm,
               seed=1,
               verbose=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants