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

limit_repair method alters the input array #294

Closed
zStupan opened this issue Mar 26, 2021 · 2 comments
Closed

limit_repair method alters the input array #294

zStupan opened this issue Mar 26, 2021 · 2 comments

Comments

@zStupan
Copy link
Contributor

zStupan commented Mar 26, 2021

Steps to reproduce

import numpy as np
from NiaPy.task import StoppingTask

task = StoppingTask(D=2,  benchmark='sphere', nGEN=10)

x = np.array([5.5, 3.78], dtype=np.float64)
repaired_x = task.repair(x)

print(np.array_equal(x, repaired_x)) # will print 'True'

Expected behavior
I would expect the original x array to remain unchanged.

Actual behavior
The limit_repair function alters the input x array and returns it.

System configuration

  • OS: Kubuntu 20.10
  • Python version: 3.8.6

I believe this is undesirable behavior. Looking at limit_repair's signature (accepts an input array and returns the repaired array) it makes sense that a copy of the input array would be altered and returned, and the input array would remain unchanged.

I have found that if you use a custom function as the tasks frepair, a function that does not alter the input array, then the MTS algorithm and the HDE algorithms that use MTS search functions break (Returned best does not equal the global best stored in the task object).

Example:

from NiaPy.task import StoppingTask
from NiaPy.algorithms.other import MultipleTrajectorySearch
from NiaPy.benchmarks import Sphere
import numpy as np

def custom_frepair(x, l, u):
   return np.clip(x, l, u)

task = StoppingTask(D=10, nGEN=40, benchmark=Sphere(), frepair=custom_frepair)
algo = MultipleTrajectorySearch()

best_x, best_f = algo.run(task)
print(best_f, task.x_f)  # task.x_f will be smaller

This seems to only be an issue with MTS and related algorithms. @kb2623 Can you take a look at the MTS code? I can't figure out where exactly the issue is.

@sisco0
Copy link
Contributor

sisco0 commented Mar 28, 2021

I totally agree with the use of np.clip as the method for clipping data between two boundary values.

@zStupan zStupan mentioned this issue May 7, 2021
@firefly-cpp
Copy link
Contributor

Fixed in #309

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

3 participants