Skip to content

Add test #8

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

Merged
merged 4 commits into from
Mar 12, 2022
Merged

Add test #8

merged 4 commits into from
Mar 12, 2022

Conversation

firefly-cpp
Copy link
Owner

  • new test,
  • FA moved to the class,
  • README.rst removed,
  • updated example in README

@firefly-cpp
Copy link
Owner Author

@zStupan, I can not assign you a reviewer role for this PR.

@zStupan
Copy link
Collaborator

zStupan commented Mar 12, 2022

@firefly-cpp I think I have to be added as a collaborator for that, I'm not sure though.

As for the review, I'd rename move() to run(), move the func, dim, lb, ub and max_evals parameters to run and have the rng as an attribute of the class. Something like this:

import numpy as np
from numpy.random import default_rng


class FireflyAlgorithm:
    def __init__(self, pop_size=20, alpha=1.0, betamin=1.0, gamma=0.01, seed=None):
        self.pop_size = pop_size
        self.alpha = alpha
        self.betamin = betamin
        self.gamma = gamma
        self.rng = default_rng(seed)
    
    def run(self, function, dim, lb, ub, max_evals):
        fireflies = self.rng.uniform(lb, ub, (self.pop_size, dim))
        intensity = np.apply_along_axis(function, 1, fireflies)
        best = np.min(intensity)

        evaluations = self.pop_size
        new_alpha = self.alpha
        search_range = ub - lb

        while evaluations <= self.max_evals:
            new_alpha *= 0.97
            for i in range(self.pop_size):
                for j in range(self.pop_size):
                    if intensity[i] >= intensity[j]:
                        r = np.sum(np.square(fireflies[i] - fireflies[j]), axis=-1)
                        beta = self.betamin * np.exp(-self.gamma * r)
                        steps = new_alpha * (self.rng.random(self.dim) - 0.5) * search_range
                        fireflies[i] += beta * (fireflies[j] - fireflies[i]) + steps
                        fireflies[i] = np.clip(fireflies[i], lb, ub)
                        intensity[i] = function(fireflies[i])
                        evaluations += 1
                        best = min(intensity[i], best)
        return best

@firefly-cpp
Copy link
Owner Author

@zStupan, I thought you are already in this group.

Let's try now.

@firefly-cpp firefly-cpp requested a review from zStupan March 12, 2022 13:16
@firefly-cpp
Copy link
Owner Author

Try to merge this PR (when you confirm).

Copy link
Collaborator

@zStupan zStupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I've said in the other comment, I'd change the class interface slightly, but everything else looks good.

@firefly-cpp firefly-cpp merged commit 6ae0981 into master Mar 12, 2022
@firefly-cpp firefly-cpp deleted the modifications branch October 31, 2022 20:59
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 this pull request may close these issues.

2 participants