Skip to content

Commit

Permalink
[WIP] Update operators
Browse files Browse the repository at this point in the history
  • Loading branch information
ljvmiranda921 committed May 20, 2018
1 parent d253076 commit 359131c
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pyswarms/backend/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,29 @@ def update_velocity(velocity, clamp, pos, pbest_pos, best_pos, c1, c2, w):
temp_velocity <= max_velocity)
updated_velocity = np.where(~mask, velocity, temp_velocity)

return updated_velocity
return updated_velocity

def update_position(velocity, position, bounds):
"""Updates the position matrix
Parameters
----------
velocity : numpy.ndarray
velocity-matrix of shape (n_samples, dimensions)
position : numpy.ndarray
position-matrix of shape (n_samples, dimensions)
"""
temp_position = position.copy()
temp_position += velocity

if bounds is not None:
lb, ub = bounds
min_bounds = np.repeat(np.array(lb)[np.newaxis, :], n_particles, axis=0)
max_bounds = np.repeat(np.array(ub)[np.newaxis, :], n_particles, axis=0)
mask = (np.all(min_bounds <= temp_position, axis=1)
* np.all(temp_position <= max_bounds, axis=1))
mask = np.repeat(mask[:, np.newaxis], position.shape[1], axis=1)
temp_position = np.where(~mask, position, temp_position)
position = temp_position
return position

0 comments on commit 359131c

Please sign in to comment.