Skip to content

[BUG] Error with random_state.random in custom SBX going from 0.6.1.3 to 0.6.1.6 #772

@th0ben

Description

@th0ben

Checklist

  • I searched existing issues
  • I'm using the latest pymoo version

Bug Description

Hey everyone,

I started working on a project last year using version 0.6.1.3. I created a customized version of the _do-Method in SBX with a very simple change. I recently updated to version 0.6.1.6 and suddenly got the error:
'NoneType' object has no attribute 'random'
I could pin down the error to the line
rand = random_state.random((len(prob_bin), 1))
in the SBX module.

I then created the attached MWE built from the "MyProblem" example from your website to reproduce the error. Here, I simply defined a CustomSBX class based on the regular SBX and defined the _do-Method to be identical to the original _do-Method with the super() function. Using this CustomSBX produces the error while using the regular SBX works fine on version 0.6.1.6. On version 0.6.1.3, both ways are working without problems.
(I did not test version 0.6.1.5 because it says I would need Microsoft Visual C++ 14.0 or greater in the error message of the installation process, which is not installed on my machine.)

I can continue working on version 0.6.1.3, so it's no deal breaker for me personally, but maybe this error is reproducible and could be fixed in future releases.

Kind regards and thanks for this awesome package,
Niklas

Minimal Code to Reproduce

import numpy as np

from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import ElementwiseProblem
from pymoo.optimize import minimize


class MyProblem(ElementwiseProblem):

    def __init__(self):
        super().__init__(n_var=2,
                         n_obj=2,
                         n_ieq_constr=2,
                         xl=np.array([-2, -2]),
                         xu=np.array([2, 2]))

    def _evaluate(self, x, out, *args, **kwargs):
        f1 = 100 * (x[0] ** 2 + x[1] ** 2)
        f2 = (x[0] - 1) ** 2 + x[1] ** 2

        g1 = 2 * (x[0] - 0.1) * (x[0] - 0.9) / 0.18
        g2 = - 20 * (x[0] - 0.4) * (x[0] - 0.6) / 4.8

        out["F"] = [f1, f2]
        out["G"] = [g1, g2]

from pymoo.operators.crossover.sbx import SBX

class CustomSBX(SBX):

    def _do(self, problem, X, *args, random_state=None, **kwargs):
        Q = super()._do(problem, X, *args, random_state=None, **kwargs)

        return Q


problem = MyProblem()

algorithm = NSGA2(pop_size=100,
                  crossover=CustomSBX(),
                  #crossover=SBX(),
                  )

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

Error Message

Exception has occurred: AttributeError
'NoneType' object has no attribute 'random'
  File "C:\my_path\main.py", line 32, in _do
    Q = super()._do(problem, X, *args, random_state=None, **kwargs)
  File "C:\my_path\main.py", line 44, in <module>
    algorithm,
    ^^^^^^^^^^
    ...<3 lines>...
    verbose=True,
    ^^^^^^^
AttributeError: 'NoneType' object has no attribute 'random'

PyMoo & Python Version

pymoo 0.6.1.6, Python 3.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions