pymoo icon indicating copy to clipboard operation
pymoo copied to clipboard

Numpy 2.0.0 breaks pymoo 0.6.1.1

Open jbuerman opened this issue 1 year ago • 1 comments

Installing pymoo from PyPI, i.e. 0.6.1.1, results in an AttributeError when importing ElementwiseProblem. The problem seems to be numpy version 2.0.0. Downgrading numpy to 1.26.4 resolves the issue.

MWE

  1. Create venv (python3 -m venv venv)
  2. Activate venv
  3. Install pymoo (python3 -m pip install pymoo)
  4. Run python
  5. import ElementwiseProblem (from pymoo.core.problem import ElementwiseProblem)

Error Message

Traceback (most recent call last): File "", line 1, in File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/pymoo/core/problem.py", line 5, in import pymoo.gradient.toolbox as anp File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/pymoo/gradient/toolbox/init.py", line 6, in sys.modules[name] = importlib.import_module(TOOLBOX) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.11/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/autograd/init.py", line 2, in from .differential_operators import ( File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/autograd/differential_operators.py", line 16, in import autograd.numpy as np File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/autograd/numpy/init.py", line 5, in from . import numpy_vjps File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/autograd/numpy/numpy_vjps.py", line 563, in defvjp(anp.msort, grad_sort) # Until multi-D is allowed, these are the same. ^^^^^^^^^ File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/autograd/tracer.py", line 48, in f_wrapped return f_raw(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/XXX/pymoo_msort/venv/lib/python3.11/site-packages/numpy/init.py", line 410, in getattr raise AttributeError("module {!r} has no attribute " AttributeError: module 'numpy' has no attribute 'msort'. Did you mean: 'sort'?

Additional Info

Tested with python version 3.11.6

Packages in venv

about-time 4.2.1 alive-progress 3.1.5 autograd 1.6.2 cma 3.2.2 contourpy 1.2.1 cycler 0.12.1 Deprecated 1.2.14 dill 0.3.8 fonttools 4.53.0 future 1.0.0 grapheme 0.6.0 kiwisolver 1.4.5 matplotlib 3.9.0 numpy 2.0.0 packaging 24.1 pillow 10.3.0 pip 23.2 pymoo 0.6.1.1 pyparsing 3.1.2 python-dateutil 2.9.0.post0 scipy 1.13.1 setuptools 68.1.2 six 1.16.0 wrapt 1.16.0

jbuerman avatar Jun 19 '24 14:06 jbuerman

Issue is autograd is no longer maintained and will not support numpy v2. This project should move to JAX.

In the meantime, I think you can try to monkeypatch it...

import jax.numpy as jnp

with unittest.mock.patch("pymoo.gradient.toolbox", new=jnp):
    import pymoo

What in black magic programming is happening with pymoo.toolbox module resolution?!

ketozhang avatar Jun 21 '24 20:06 ketozhang

I've also encountered this problem, and resolved it with monkeypatching the TOOLBOX string which checks for JAX / autograd:

import unittest.mock

with unittest.mock.patch("pymoo.gradient.TOOLBOX", new='jax.numpy'):
    import pymoo

matthew-epl avatar Jul 19 '24 11:07 matthew-epl

Sorry for the spam. :sweat_smile:

Until this is fixed, I propose pinning the numpy dependency in the setup.py file.

https://github.com/anyoptimization/pymoo/blob/dca843afaa8406829547bf117a72cf2b2a4ec84a/setup.py#L36

schmoelder avatar Jul 23 '24 13:07 schmoelder

I have released a new version and taken out the line using autograd by default.

Can someone check if this issue is fixed now?

blankjul avatar Jul 29 '24 00:07 blankjul

I have released a new version and taken out the line using autograd by default.

Can someone check if this issue is fixed now?

Hi @blankjul ,

thanks for the release. With the release 0.6.1.3, it's not crashing now and triggering the version warning printing.

airuchen avatar Jul 29 '24 08:07 airuchen

Great! I will close this issue then.

blankjul avatar Jul 31 '24 05:07 blankjul