Skip to content

Commit

Permalink
Fix duplicate cost function calls in optimize() implementations (#266)
Browse files Browse the repository at this point in the history
Resolves #257
  • Loading branch information
danielcorreia96 authored and ljvmiranda921 committed Feb 4, 2019
1 parent 52d69e4 commit 0adef1a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 26 deletions.
4 changes: 1 addition & 3 deletions pyswarms/backend/topology/ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def compute_gbest(self, swarm, p, k, **kwargs):
if (self.static and self.neighbor_idx is None) or not self.static:
# Obtain the nearest-neighbors for each particle
tree = cKDTree(swarm.position)
_, self.neighbor_idx = tree.query(
swarm.position, p=p, k=k
)
_, self.neighbor_idx = tree.query(swarm.position, p=p, k=k)

# Map the computed costs to the neighbour indices and take the
# argmin. If k-neighbors is equal to 1, then the swarm acts
Expand Down
4 changes: 1 addition & 3 deletions pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
lvl=logging.INFO,
)

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
if not fast:
sleep(0.01)
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(
self.swarm.position, **kwargs
)
self.swarm.pbest_cost = objective_func(
self.swarm.pbest_pos, **kwargs
)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
self.swarm
)
Expand Down
6 changes: 4 additions & 2 deletions pyswarms/single/general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
lvl=logging.INFO,
)

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
# Compute cost for current position and personal best
# fmt: off
self.swarm.current_cost = objective_func(self.swarm.position, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
best_cost_yet_found = self.swarm.best_cost
# fmt: on
Expand Down Expand Up @@ -241,7 +241,9 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.position[self.swarm.pbest_cost.argmin()].copy()
final_best_pos = self.swarm.position[
self.swarm.pbest_cost.argmin()
].copy()
# Write report in log and return final cost and position
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
Expand Down
2 changes: 1 addition & 1 deletion pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
lvl=logging.INFO,
)

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
if not fast:
sleep(0.01)
# Compute cost for current position and personal best
# fmt: off
self.swarm.current_cost = objective_func(self.swarm.position, **kwargs)
self.swarm.pbest_cost = objective_func(self.swarm.pbest_pos, **kwargs)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
# Set best_cost_yet_found for ftol
best_cost_yet_found = self.swarm.best_cost
Expand Down
8 changes: 4 additions & 4 deletions pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,14 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
lvl=logging.INFO,
)

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
if not fast:
sleep(0.01)
# Compute cost for current position and personal best
self.swarm.current_cost = objective_func(
self.swarm.position, **kwargs
)
self.swarm.pbest_cost = objective_func(
self.swarm.pbest_pos, **kwargs
)
self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
self.swarm
)
Expand Down Expand Up @@ -230,7 +228,9 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
)
# Obtain the final best_cost and the final best_position
final_best_cost = self.swarm.best_cost.copy()
final_best_pos = self.swarm.position[self.swarm.pbest_cost.argmin(axis=0)].copy()
final_best_pos = self.swarm.position[
self.swarm.pbest_cost.argmin(axis=0)
].copy()
# Write report in log and return final cost and position
self.rep.log(
"Optimization finished | best cost: {}, best pos: {}".format(
Expand Down
4 changes: 3 additions & 1 deletion tests/backend/topology/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def test_compute_gbest_return_values(
"""Test if compute_gbest() gives the expected return values"""
topo = topology(static=static)
expected_cost = 1.0002528364353296
expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05])
expected_pos = np.array(
[9.90438476e-01, 2.50379538e-03, 1.87405987e-05]
)
pos, cost = topo.compute_gbest(swarm, **options)
assert cost == pytest.approx(expected_cost)
assert pos[np.argmin(cost)] == pytest.approx(expected_pos)
13 changes: 9 additions & 4 deletions tests/backend/topology/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ def test_compute_gbest_return_values(
"""Test if update_gbest_neighborhood gives the expected return values"""
topo = topology(static=static)
expected_cost = 1.0002528364353296
expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05])
expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03])
expected_pos = np.array(
[9.90438476e-01, 2.50379538e-03, 1.87405987e-05]
)
expected_pos_2 = np.array(
[9.98033031e-01, 4.97392619e-03, 3.07726256e-03]
)
pos, cost = topo.compute_gbest(swarm, **options)
assert cost == pytest.approx(expected_cost)
assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or
(pos[np.argmin(cost)] == pytest.approx(expected_pos_2)))
assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or (
pos[np.argmin(cost)] == pytest.approx(expected_pos_2)
)

@pytest.mark.parametrize("static", [True, False])
@pytest.mark.parametrize("k", [1, 2])
Expand Down
13 changes: 9 additions & 4 deletions tests/backend/topology/test_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ def test_compute_gbest_return_values(self, swarm, topology, p, k, static):
topo = topology(static=static)
pos, cost = topo.compute_gbest(swarm, p=p, k=k)
expected_cost = 1.0002528364353296
expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05])
expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03])
expected_pos = np.array(
[9.90438476e-01, 2.50379538e-03, 1.87405987e-05]
)
expected_pos_2 = np.array(
[9.98033031e-01, 4.97392619e-03, 3.07726256e-03]
)
assert cost == pytest.approx(expected_cost)
assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or
(pos[np.argmin(cost)] == pytest.approx(expected_pos_2)))
assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or (
pos[np.argmin(cost)] == pytest.approx(expected_pos_2)
)
13 changes: 9 additions & 4 deletions tests/backend/topology/test_von_neumann.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ def test_update_gbest_neighborhood(self, swarm, topology, p, r):
"""Test if update_gbest_neighborhood gives the expected return values"""
topo = topology()
pos, cost = topo.compute_gbest(swarm, p=p, r=r)
expected_pos = np.array([9.90438476e-01, 2.50379538e-03, 1.87405987e-05])
expected_pos_2 = np.array([9.98033031e-01, 4.97392619e-03, 3.07726256e-03])
expected_pos = np.array(
[9.90438476e-01, 2.50379538e-03, 1.87405987e-05]
)
expected_pos_2 = np.array(
[9.98033031e-01, 4.97392619e-03, 3.07726256e-03]
)
expected_cost = 1.0002528364353296
assert cost == pytest.approx(expected_cost)
assert ((pos[np.argmin(cost)] == pytest.approx(expected_pos)) or
(pos[np.argmin(cost)] == pytest.approx(expected_pos_2)))
assert (pos[np.argmin(cost)] == pytest.approx(expected_pos)) or (
pos[np.argmin(cost)] == pytest.approx(expected_pos_2)
)

@pytest.mark.parametrize("m", [i for i in range(3)])
@pytest.mark.parametrize("n", [i for i in range(3)])
Expand Down

0 comments on commit 0adef1a

Please sign in to comment.