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

RuntimeWarning: invalid value encountered in cast multiarray.copyto(a, fill_value, casting='unsafe') #611

Open
alibaba613 opened this issue Jun 29, 2024 · 6 comments
Assignees

Comments

@alibaba613
Copy link

I keep getting this warning (it links to the numpy folder on my laptop). I was using an outdated version of numpy and updated it to 1.26.2, but the problem persisted for both. I'm avoiding updating to numpy 2.0 to avoid huge compatibility issues with other packages.
This is the test problem:

from pymoo.algorithms.moo.nsga3 import NSGA3
from pymoo.algorithms.moo.unsga3 import UNSGA3
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.optimize import minimize
from pymoo.problems import get_problem
from pymoo.util.ref_dirs import get_reference_directions
from pymoo.visualization.scatter import Scatter

# create the reference directions to be used for the optimization
ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=12)

# create the algorithm object
algorithm = NSGA3(pop_size=92,
                 ref_dirs=ref_dirs)

# execute the optimization
res = minimize(get_problem("dtlz1"),
              algorithm,
              seed=1,
              termination=('n_gen', 600))

Scatter().add(res.F).show()

The warning only appears when I use NSGA3 or UNSGA3, and not NSGA2. It should be noted that the warning also only appears on the first run, and not any other subsequent runs unless I restart the Jupyter Notebook kernel.
Considering the problem I'm using actually requires ~9 hours to run, I wanted to ask if it is necessary to run the quick dtlz1 beforehand just to fix the warning before running my main problem, or if it isn't something I should worry about.

@blankjul blankjul self-assigned this Jul 7, 2024
@blankjul
Copy link
Collaborator

blankjul commented Jul 7, 2024

Can you post the warning you are receiving here as well?

(+1 on the compatabilty issues. I have not looked into the numpy changelog yet and see what changes for pymoo)

@alibaba613
Copy link
Author

Hey Julian, I appreciate the response. The full warning is this:
[rest of filepath]\site-packages\numpy\core\numeric.py:330: RuntimeWarning: invalid value encountered in cast
multiarray.copyto(a, fill_value, casting='unsafe')
There's no more traceback or anything

@blankjul
Copy link
Collaborator

I am not able to reproduce this warning. What OS are you using?
(might be tricky to fix. Happy to look at a PR if you find where the warning occurs)

@mmun1
Copy link

mmun1 commented Oct 9, 2024

I am having the same warning as @alibaba613. Found out it comes from the line 56 in the file non_dominated_sorting.py:
rank = np.full(n, 1e16, dtype=int)
The problem here is that we are casting the float 1e16 into an int, leading to unexpected result with an array filled with negative values, which is not what we want I imagine.

An example with n=5 will give that:

>>> rank = np.full(n, 1e16, dtype=int)
>>> print(rank)                        
[-2147483648 -2147483648 -2147483648 -2147483648 -2147483648]

It could be easily fixed by replacing 1e16 by some high int value.

alibaba613 added a commit to alibaba613/pymoo that referenced this issue Oct 22, 2024
alibaba613 added a commit to alibaba613/pymoo that referenced this issue Oct 22, 2024
@syaykiran
Copy link

I am having the same warning as @alibaba613. Found out it comes from the line 56 in the file non_dominated_sorting.py: rank = np.full(n, 1e16, dtype=int) The problem here is that we are casting the float 1e16 into an int, leading to unexpected result with an array filled with negative values, which is not what we want I imagine.

An example with n=5 will give that:

>>> rank = np.full(n, 1e16, dtype=int)
>>> print(rank)                        
[-2147483648 -2147483648 -2147483648 -2147483648 -2147483648]

It could be easily fixed by replacing 1e16 by some high int value.

I created a subclass of the NonDominatedSorting class from the pymoo library and overrode the do() method to utilize your corrected rank_from_fronts function. Alternatively, I replaced the rank_from_fronts function in the pymoo library with your version. Unfortunately, neither approach has worked for me.

rank = np.full(n, 1000000, dtype=int) # Changed 1e16 to 1000000

@alibaba613
Copy link
Author

just to make sure, you're saying switching 1e16 to 10000000000000 didn't work? It worked perfectly for me. I don't know how sensitive rank is, but could you try using 10000000000000 instead of 1000000? or 16 zeroes instead. Maybe the actual order of magnitude makes a difference.

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

No branches or pull requests

4 participants