def test_compute_position_return_values(swarm, bounds, static): """Test if compute_position() gives the expected shape and range""" topology = Ring(static=static) p = topology.compute_position(swarm, bounds) assert p.shape == swarm.velocity.shape if bounds is not None: assert (bounds[0] <= p).all() and (bounds[1] >= p).all()
def __fitNBeta(self, dim, n_particles, itera, options, objetive_function, BetaChange, bound): my_topology = Ring() my_swarm = P.create_swarm(n_particles=n_particles, dimensions=dim, options=options, bounds=bound) my_swarm.pbest_cost = np.full(n_particles, np.inf) my_swarm.best_cost = np.inf for i in range(itera): for a in range(n_particles): my_swarm.position[a][0:BetaChange] = sorted( my_swarm.position[a][0:BetaChange]) for c in range(1, self.BetaChange): if my_swarm.position[a][c - 1] + 5 >= my_swarm.position[a][c]: my_swarm.position[a][c] = my_swarm.position[a][c] + 5 my_swarm.current_cost = objetive_function(my_swarm.position) my_swarm.pbest_pos, my_swarm.pbest_cost = P.operators.compute_pbest( my_swarm) #my_swarm.current_cost[np.isnan(my_swarm.current_cost)]=np.nanmax(my_swarm.current_cost) #my_swarm.pbest_cost = objetive_function(my_swarm.pbest_pos) my_swarm.best_pos, my_swarm.best_cost = my_topology.compute_gbest( my_swarm, options['p'], options['k']) if i % 20 == 0: print( 'Iteration: {} | my_swarm.best_cost: {:.4f} | days: {}'. format( i + 1, my_swarm.best_cost, str(my_swarm.pbest_pos[my_swarm.pbest_cost.argmin()]))) my_swarm.velocity = my_topology.compute_velocity(my_swarm, bounds=bound) my_swarm.position = my_topology.compute_position(my_swarm, bounds=bound) final_best_cost = my_swarm.best_cost.copy() final_best_pos = my_swarm.pbest_pos[ my_swarm.pbest_cost.argmin()].copy() return final_best_pos, final_best_cost