gugarosa / opytimizer

🐦 Opytimizer is a Python library consisting of meta-heuristic optimization algorithms.
https://opytimizer.readthedocs.io
Apache License 2.0
606 stars 41 forks source link

[BUG] EPO sometimes evaluates the objective with a (size, size) array. #40

Closed inikishev closed 3 months ago

inikishev commented 3 months ago

Hi!

Describe the bug If n_variables = 10, then the objective is evaluated with an ndarray of shape (10, 1). However population.EPO optimizer sometimes passes a (10, 10) ndarray.

To Reproduce

import numpy as np

from opytimizer import Opytimizer
from opytimizer.core import Function
from opytimizer.optimizers.population import EPO
from opytimizer.spaces import SearchSpace

def sphere(x):
    print(f"{x.shape = }")
    assert x.size == 10, f"{x.shape}"
    return np.sum(x ** 2)

n_agents = 20
n_variables = 10
lower_bound = [-10] * 10
upper_bound = [10] * 10

space = SearchSpace(n_agents, n_variables, lower_bound, upper_bound)
optimizer = EPO()
function = Function(sphere)

opt = Opytimizer(space, optimizer, function)
opt.start(n_iterations=1000)

Expected behavior If n_variables is 10, then I would expect that it always evaluates with an array of size 10.

Desktop (please complete the following information): Windows 11, conda, python 3.12, Opytimizer 3.1.2 (latest on pip).

gugarosa commented 3 months ago

Hello @inikishev!

Thanks for reporting out the bug. The C random number was indeed wrong and caused a reshape of the tensor. The https://github.com/gugarosa/opytimizer/commit/bee31efc64e618c7042d2929c5da86fc22d65484 commit should fix the issue.

Best regards, Gustavo.