-
Notifications
You must be signed in to change notification settings - Fork 335
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
Ring topology does not return the best cost #170
Comments
If there is a nice algorithm to make a path graph of the points this might be an easy solution. |
A pathfinding algorithm might suffice. |
Before we jump into adding a new algorithm in My suggestion is to check the traditional lbest algorithm, compare it with our implementation, and see if there are any discrepancies. My reference is this textbook-algorithm from A. Engelbrecht, Computational Intelligence 2nd Ed., p.291. Where a neighbor is defined as My guess is that we are failing to assign the personal best as global best on the whole iteration. Also @whzup, when does this problem occur? Whenever we have odd number of neighbors? |
test.txt |
Ok understood. How would a shortest-path algorithm solve this? |
Hmm, maybe the solution with the shortest-path algorithm isn't what we want. if k == 1:
# The minimum index is itself, no mapping needed.
best_neighbor = swarm.pbest_cost[self.neighbor_idx][:, np.newaxis].argmin(
axis=1
) I find it suspicious that there is no comparison in the |
I switched the best position and the best cost of the swarm fixture in the ...
"pbest_cost": np.array([1, 2, 3]),
"pbest_pos": np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
... My change: ...
"pbest_cost": np.array([2, 1, 3]),
"pbest_pos": np.array([[4, 5, 6], [1, 2, 3], [7, 8, 9]]),
... Apparently, the # Doesn't work
best_pos = swarm.pbest_pos[
np.argmin(swarm.pbest_cost[best_neighbor])
] But this returns the index of the particle that has the neighbour with the best cost (one long relationship 😄 give it a thought) not the index of the particle that has the best cost which we are looking for. Funny enough. By chance, this always returned the index 0 which was actually the best particle in the tests we were using until now. That's why the tests passed all the time. I changed it to the following: # Does work
best_pos = swarm.pbest_pos[
best_neighbor[np.argmin(swarm.pbest_cost[best_neighbor])]
] Now it returns the right result every time I run the tests. It also resolved some of the test failures of the # Doesn't work
best_neighbor = swarm.pbest_cost[self.neighbor_idx][:, np.newaxis].argmin(axis=1) To that: # Does work
best_neighbor = swarm.pbest_cost[self.neighbor_idx][:, np.newaxis].argmin(axis=0) The first one creates a column vector and searches for the minimum in each row so it just returns an array of zeros because in every row there is only a single entry (which is on index 0). |
I'd like to test it with the test script I used to detect this issue. Do you know a way to do this? |
Hey Aaron,
Great analysis! So the culprit was how the best_pos is being assigned
right? Instead of getting the index of the actual particle with the best
fitness, what we’re getting is the particle that neighbors it?
Let me reply to you properly once I go to school. Great job! I’ll check on
how we can run the tests similar to what you have done :)
|
Hi, Lester 😄. Yes, exactly. And the wrong axis was used to determine the best neighbours. |
Yo @whzup ! Will a test asserting A short integration test like the one you posted in pastebin is also a nice idea. We conjure up a small swarm in conftest, set the random seed, and check if the value will always be the case. I believe in pytest or numpy there is an Also, I might be quite inactive for the next two weeks until August 4 (next two weeks: conference and moving out). I suggest we do a feature lock in the development branch (no new features, just bug fixes if possible) so that I can keep up with my backlog in #157 and prepare for v.0.3.0 release by next month. What do you think? |
Resolves #170 Fixed a bug in the topology class where not the best values were used as result when optimizing with the LocalBest class. Made the tests for the topologies less trivial by changing the index of the best cost and best position in the fixture swarm. Added a bigger fixture swarm to test the topology classes. Additionally, I fixed a little bug in the Random topology where the elements were compared to 0 instead of np.inf. Deleted the fixture for the neighbour number and replaced it with a pytest.mark.parametrize(). Signed-off-by: Lester James V. Miranda <ljvmiranda@gmail.com> Committed-with: Aaron (@whzup)
Describe the bug
Running the
LocalBest
optimizer does not return the best cost of the optimization process. If you run the script below the follwing output is given:To Reproduce
Run the following script:
The
GlobalBest
class returns the best cost it found while theLocalBest
class does not.Expected behaviour
The
LocalBest
class should return the best cost it found as well.Environment (please complete the following information):
Additional context
![skizze](https://user-images.githubusercontent.com/39431903/42836323-00b52150-89fb-11e8-83db-b7193d0fe354.png)
My guess is that something like this happens. The blue circles are particles and the arrow denote the "is a neighbour of" relationship. When this happens the
compute_gbest
does not work properly, I guess. I'm not entirely sure though.The text was updated successfully, but these errors were encountered: