Skip to content

Commit

Permalink
Fix handlers (#286)
Browse files Browse the repository at this point in the history
This fixes a bug where the BoundaryHandler would not handle the positions
correctly because its memory wasn't populated.
  • Loading branch information
whzup authored and ljvmiranda921 committed Feb 14, 2019
1 parent 45880d0 commit e87a451
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pyswarms/backend/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ def reflective(self, position, bounds, **kwargs):
:nowrap:
\begin{gather*}
\text{while } x_{i, t, d} \not\in \left[lb_d,\,ub_d\right] \text{ do the
following:}
\text{while } x_{i, t, d} \not\in \left[lb_d,\,ub_d\right] \\
\text{ do the following:}\\
\\
x_{i, t, d} = \begin{cases}
2\cdot lb_d - x_{i, t, d} & \quad \text{if } x_{i,
t, d} < lb_d
t, d} < lb_d \\
2\cdot ub_d - x_{i, t, d} & \quad \text{if } x_{i,
t, d} > ub_d
t, d} > ub_d \\
x_{i, t, d} & \quad \text{otherwise}
\end{cases}
\end{gather*}
Expand Down
2 changes: 2 additions & 0 deletions pyswarms/discrete/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
"Optimize for {} iters with {}".format(iters, self.options),
lvl=logging.INFO,
)
# Populate memory of the handlers
self.vh.memory = self.swarm.position

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
Expand Down
5 changes: 5 additions & 0 deletions pyswarms/single/general_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
"Optimize for {} iters with {}".format(iters, self.options),
lvl=logging.INFO,
)

# Populate memory of the handlers
self.bh.memory = self.swarm.position
self.vh.memory = self.swarm.position

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
Expand Down
3 changes: 3 additions & 0 deletions pyswarms/single/global_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
"Optimize for {} iters with {}".format(iters, self.options),
lvl=logging.INFO,
)
# Populate memory of the handlers
self.bh.memory = self.swarm.position
self.vh.memory = self.swarm.position

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
Expand Down
3 changes: 3 additions & 0 deletions pyswarms/single/local_best.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ def optimize(self, objective_func, iters, fast=False, **kwargs):
"Optimize for {} iters with {}".format(iters, self.options),
lvl=logging.INFO,
)
# Populate memory of the handlers
self.bh.memory = self.swarm.position
self.vh.memory = self.swarm.position

self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
for i in self.rep.pbar(iters, self.name):
Expand Down

0 comments on commit e87a451

Please sign in to comment.