Skip to content

Commit

Permalink
Numpy 2.0.0 breaks pymoo 0.6.1.1 #606
Browse files Browse the repository at this point in the history
  • Loading branch information
blankjul committed Jul 28, 2024
1 parent dca843a commit a51cb9c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pymoo/algorithms/moo/age.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def survival_score(self, front, ideal_point):
p = self.compute_geometry(front, extreme, n)

nn = np.linalg.norm(front, p, axis=1)
distances = self.pairwise_distances(front, p) / nn[:, None]
distances = self.pairwise_distances(front, p) / (nn[:, None])

neighbors = 2
remaining = np.arange(m)
Expand Down
4 changes: 3 additions & 1 deletion pymoo/gradient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

GRADIENT_MODULE = "pymoo.gradient.toolbox"

TOOLBOX = "autograd.numpy"
TOOLBOX = "numpy"


def activate(name):
Expand All @@ -17,3 +17,5 @@ def activate(name):

def deactivate():
activate("numpy")


32 changes: 28 additions & 4 deletions pymoo/gradient/grad_autograd.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import warnings

import autograd.numpy as anp
import numpy as np
from autograd.core import VJPNode, vspace, backward_pass
from autograd.tracer import new_box, isbox

try:
import autograd.numpy as anp
from autograd import value_and_grad
from autograd.core import VJPNode, backward_pass
from autograd.tracer import new_box, isbox
except:
print("autograd only supports numpy < 2.0.0 versions.")


def value_and_grad(*args, **kwargs):
return value_and_grad(*args, **kwargs)


def log(*args, **kwargs):
return anp.log(*args, **kwargs)


def sqrt(*args, **kwargs):
return anp.sqrt(*args, **kwargs)


def row_stack(*args, **kwargs):
return anp.row_stack(*args, **kwargs)


def triu_indices(*args, **kwargs):
return anp.triu_indices(*args, **kwargs)


def run_and_trace(f, x):
Expand Down
9 changes: 4 additions & 5 deletions pymoo/util/ref_dirs/energy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pymoo.gradient.toolbox as anp


from pymoo.gradient.grad_autograd import triu_indices, sqrt, log
from pymoo.util.ref_dirs.construction import ConstructionBasedReferenceDirectionFactory
from pymoo.util.ref_dirs.misc import project_onto_sum_equals_zero_plane, project_onto_unit_simplex_recursive
from pymoo.util.ref_dirs.optimizer import Adam
Expand Down Expand Up @@ -280,9 +279,9 @@ def squared_dist(A, B):


def calc_potential_energy(A, d):
i, j = anp.triu_indices(len(A), 1)
D = anp.sqrt(squared_dist(A, A)[i, j])
energy = anp.log((1 / D ** d).mean())
i, j = triu_indices(len(A), 1)
D = sqrt(squared_dist(A, A)[i, j])
energy = log((1 / D ** d).mean())
return energy


Expand Down
9 changes: 5 additions & 4 deletions pymoo/util/ref_dirs/energy_layer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import autograd.numpy as anp


import numpy as np
from autograd import value_and_grad

from pymoo.gradient.grad_autograd import value_and_grad, triu_indices, row_stack
from pymoo.util.normalization import normalize
from pymoo.util.ref_dirs.energy import squared_dist
from pymoo.util.ref_dirs.optimizer import Adam
Expand Down Expand Up @@ -102,14 +103,14 @@ def get_points(X, scalings):
vals = []
for i in range(len(X)):
vals.append(scale_reference_directions(X[i], scalings[i]))
X = anp.row_stack(vals)
X = row_stack(vals)
return X


def calc_potential_energy(scalings, X):
X = get_points(X, scalings)

i, j = anp.triu_indices(len(X), 1)
i, j = triu_indices(len(X), 1)
D = squared_dist(X, X)[i, j]

if np.any(D < 1e-12):
Expand Down

0 comments on commit a51cb9c

Please sign in to comment.