Skip to content

[BUG] Completely broken on numpy 2.x (wrong vector sent to fitness()) #177

@samimia-swks

Description

@samimia-swks

With numpy 2.x, the fitness function is called with the wrong values in the decision vector x.

import pygmo as pg
class sphere_function:
    def fitness(self, x):
        cost = sum(x * x)
        print(f"FITNESS EVAL: x = {x}, cost = {cost}")
        return [cost]

    def get_bounds(self):
        return ([-1] * 3, [1] * 3)
prob = pg.problem(sphere_function())
pop = pg.population(prob, 5)
print(pop)

The example above results in the following. Notice how the population member #0 is [-0.504554, 0.87294, 0.639481] but the fitness function gets [-0.50455375 -0.50455375 -0.50455375].

FITNESS EVAL: x = [-0.50455375 -0.50455375 -0.50455375], cost = 0.7637234652148235
FITNESS EVAL: x = [0.4426457 0.4426457 0.4426457], cost = 0.5878056348538054
FITNESS EVAL: x = [-0.47532954 -0.47532954 -0.47532954], cost = 0.6778145209225833
FITNESS EVAL: x = [-0.29921424 -0.29921424 -0.29921424], cost = 0.2685874763420541
FITNESS EVAL: x = [0.63765334 0.63765334 0.63765334], cost = 1.219805338218631
Problem name: <class '__main__.sphere_function'>
        C++ class name: pybind11::object

        Global dimension:                       3
        Integer dimension:                      0
        Fitness dimension:                      1
        Number of objectives:                   1
        Equality constraints dimension:         0
        Inequality constraints dimension:       0
        Lower bounds: [-1, -1, -1]
        Upper bounds: [1, 1, 1]
        Has batch fitness evaluation: false

        Has gradient: false
        User implemented gradient sparsity: false
        Has hessians: false
        User implemented hessians sparsity: false

        Fitness evaluations: 5

        Thread safety: none

Population size: 5

List of individuals: 
#0:
        ID:                     14351618211829196000
        Decision vector:        [-0.504554, 0.87294, 0.639481]
        Fitness vector:         [0.763723]
#1:
        ID:                     4233680892357765143
        Decision vector:        [0.442646, 0.462569, -0.970743]
        Fitness vector:         [0.587806]
#2:
        ID:                     457977541108690035
        Decision vector:        [-0.47533, 0.97677, -0.805264]
        Fitness vector:         [0.677815]
#3:
        ID:                     3780462174932513958
        Decision vector:        [-0.299214, -0.134968, -0.699614]
        Fitness vector:         [0.268587]
#4:
        ID:                     12888162120884326344
        Decision vector:        [0.637653, 0.840588, -0.0665339]
        Fitness vector:         [1.21981]

Champion decision vector: [-0.299214, -0.134968, -0.699614]
Champion fitness: [0.268587]

On numpy 1.x, we get :

FITNESS EVAL: x = [-0.38570461 -0.2763677  -0.19182114], cost = 0.2619425019598244
FITNESS EVAL: x = [-0.20674582 -0.56690414 -0.68899929], cost = 0.8388441636353365
FITNESS EVAL: x = [-0.57724158  0.56498671  0.34808134], cost = 0.77357844517818
FITNESS EVAL: x = [-0.13960828  0.79237103  0.04465593], cost = 0.6493364798439734
FITNESS EVAL: x = [-0.19441917  0.14853349  0.61230586], cost = 0.4347794724943631

Population size: 5

List of individuals: 
#0:
        ID:                     4942601127234171947
        Decision vector:        [-0.385705, -0.276368, -0.191821]
        Fitness vector:         [0.261943]
#1:
        ID:                     11313699314120481030
        Decision vector:        [-0.206746, -0.566904, -0.688999]
        Fitness vector:         [0.838844]
#2:
        ID:                     8331669631110857304
        Decision vector:        [-0.577242, 0.564987, 0.348081]
        Fitness vector:         [0.773578]
#3:
        ID:                     3796521727361963477
        Decision vector:        [-0.139608, 0.792371, 0.0446559]
        Fitness vector:         [0.649336]
#4:
        ID:                     18100786683321109976
        Decision vector:        [-0.194419, 0.148533, 0.612306]
        Fitness vector:         [0.434779]
  • OS: ubuntu
  • Installation method: uv
  • Version: v2.19.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions