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

Re-fix duplicate cost function calls in optimize() #290

Merged
merged 1 commit into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,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
2 changes: 1 addition & 1 deletion pyswarms/single/general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
"Optimize for {} iters with {}".format(iters, self.options),
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
2 changes: 1 addition & 1 deletion pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,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
4 changes: 1 addition & 3 deletions pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,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