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

Maximization doesn't work #328

Closed
zStupan opened this issue May 31, 2021 · 7 comments · Fixed by #329
Closed

Maximization doesn't work #328

zStupan opened this issue May 31, 2021 · 7 comments · Fixed by #329

Comments

@zStupan
Copy link
Contributor

zStupan commented May 31, 2021

Steps to reproduce

from niapy.task import OptimizationType, Task
from niapy.algorithms.basic import BatAlgorithm

task = Task(problem='sphere', dimension=10, max_iters=250, optimization_type=OptimizationType.MAXIMIZATION)
algorithm = BatAlgorithm()
print(algorithm.run(task))

Expected behavior
Should maximize the sphere function, i. e print solution full of ± 5.12 with a fitness of 262.144 or something very close to that.

Actual behavior
None and -inf get returned, which means the stopping condition evaluates to True on the first iteration.

I'm working on a fix, should be up in a few minutes.

@firefly-cpp
Copy link
Contributor

Thanks @zStupan. Please check maximization for the other algorithms too. I remember we had several problems with GWO algorithm in the past years.

@zStupan
Copy link
Contributor Author

zStupan commented May 31, 2021

Most Algorithms work fine for maximization. There are, however, some issues:

  • CrowdingDifferentialEvolution just doesn't work at all and doesn't have a test. I'll probably comment and add a TODO..
  • CMAES, works only sometimes. It appears that it only works when the number of evals is 10,000 or below, very weird.
  • KrillHerdV11, AnarchicSocietyOptimization and GlowwormSwarmOptimizationV1 all raise RuntimeWarning: invalid value encountered in double_scalars, no idea why or how to fix it. I think it's because there are inf fitness values in the population fitness.
  • If the stopping condition is true before the iterations begin, for example if there's an individual in the initial population, whose fitness is already less / greater than the cutoff value, there could be bugs, because 1 iteration will run regardless.

@GregaVrbancic
Copy link
Contributor

@zStupan thanks for pointing the algorithms which are not working properly. If we are unable to solve the problems with those algorithms I would suggest, we temporarily exclude them from release. Maybe leave them in a separate branch and fix them later? What do you think @firefly-cpp?

@firefly-cpp
Copy link
Contributor

@GregaVrbancic, I totally agree with you. We should put all immature implementations of algorithms in separate branches. We can fix them after the release of 2.0 version of NiaPy.

@firefly-cpp
Copy link
Contributor

@zStupan, I suggest that we include only one version of KrillHerd algorithm which is actually associated with original publication.

@zStupan
Copy link
Contributor Author

zStupan commented May 31, 2021

Crowding DE should definitely be excluded.

I figured out what's causing the warning in GSOv1:

def calculate_luciferin(self, luciferin, fitness):
return np.fmax(0.0, (1 - self.rho) * luciferin + self.gamma * fitness)

The function value of the sphere function is always greater than 0, and maximization is the minimization of -f(x), so our function values will always be negative. That means luciferin will always be all zeros, which causes division by zero in the probabilities function, making some probabilities infinite. I solved this by adding epsilon to the denominator in the probabilities function.

In ASO, I fixed it by changing:

x_best, x_best_fitness = self.update_personal_best(population, population_fitness,
np.zeros((self.population_size, task.dimension)),
np.full(self.population_size,
task.optimization_type.value * np.inf))

to:

x_best, x_best_fitness = self.update_personal_best(population, task.optimization_type.value * population_fitness,
                                                           np.zeros((self.population_size, task.dimension)),
                                                           np.full(self.population_size, np.inf))

Basically flipping the inequality operator if the optimization type is maximization.

I couldn't figure out KrillHerdV11 and CMAES.

@firefly-cpp
Copy link
Contributor

Awesome @zStupan.

KH and CMAES should go to the separate branches. We will fix them when time allows us to do it.

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

Successfully merging a pull request may close this issue.

3 participants