def generate_particle_test(): dimension = 4 inf_limit = [] sup_limit = [] for i in range(1, dimension + 1): inf_limit.append(-i) sup_limit.append(i) particle = pso.Particle(dimension) particle.generate_random_particle(inf_limit, sup_limit) particle.print()
def set_parallel_fitness(self, fitness, skip_test=False): if skip_test: self.FITNESS = fitness self.ParallelFitness = True return np = pso.Particle() np.X = [0] * self.dimensions fitness([np.X] * self.numberofparticles) self.FITNESS = fitness self.ParallelFitness = True """
def set_parallel_fitness(self, fitness, arguments=None, skip_test=False): if skip_test: self.FITNESS = fitness self._FITNESS_ARGS = arguments self.ParallelFitness = True return np = pso.Particle() np.X = [0] * self.dimensions self.FITNESS = fitness self._FITNESS_ARGS = arguments try: self.call_fitness([np.X] * self.numberofparticles, arguments) except: print( "ERROR: the specified function does not seem to implement a correct fitness function" ) exit(-2) self.ParallelFitness = True print(" * Test successful")
def test_particle_str_erm(self): p = pso.Particle(0, "Alice") start = "Alice::x:0, best:0, velocity:" self.assertEqual(str(p)[:len(start)], start)
def minimize(bounds, file, optimize, num_particles=32, gen=50, topology='star', verbose=False, seed=19520109, num_func=1, num_dimensions=2): err_best_g = -1 # best error for group pos_best_g = [] # best position for group # establish the swarm swarm = [] #alibotto = pso.Particle(3,4,(1,2)) #print(type(pso.Particle)) for i in range(0, num_particles): swarm.append(pso.Particle(num_dimensions, num_func, bounds, optimize)) # begin optimization loop i = 0 while i < gen: if verbose: print(f'iter: {i:>4d}, best solution: {err_best_g:10.6f}') # cycle through particles in swarm and evaluate fitness for j in swarm: j.evaluate(1) # determine if current particle is the best (globally) #print(err_best_g == -1,) #print(swarm[j].position_i, swarm[j].err_i) if abs(j.err_i - optimize) < abs(err_best_g - optimize - optimize) or err_best_g == -1: pos_best_g = list(j.position_i) err_best_g = j.err_i #print(err_best_g) #xit() #if i == 0: for j in swarm: #swarm[j].pos_best_neighbor_i = swarm[j].position_i.copy() #swarm[j].err_best_neighbor_i = swarm[j].err_best_i #print(swarm.index(j)) index = swarm.index(j) if topology == 'ring': j.update_neighbor_position(swarm[(index - 1 + num_particles) % num_particles]) j.update_neighbor_position(swarm[(index + 1) % num_particles]) j.update_neighbor_position(j) else: for z in swarm: j.update_neighbor_position(z) #exit() # cycle through swarm and update velocities and position for j in swarm: j.update_velocity() for j in swarm: j.update_position(bounds) for j in swarm: j.evaluate(0) # for j in range(0,num_particles): # #swarm[j].pos_best_neighbor_i = swarm[j].position_i.copy() # if topology == 'ring': # swarm[j].update_neighbor_position(swarm[(j-1 + num_particles )%num_particles]) # swarm[j].update_neighbor_position(swarm[(j+1)%num_particles]) # swarm[j].update_neighbor_position(swarm[j]) # else: # for z in swarm: # swarm[j].update_neighbor_position(z) #print(pso.Particle.count) if pso.Particle.count > 1e6: break i += 1 #for i in range(num_particles): # print(swarm[i].err_best_neighbor_i) #exit() # print final results #print('\nFINAL SOLUTION:') #print(f' > {pos_best_g}') #print(f' > {err_best_g}\n') file.write("-Random seed: " + str(seed + 19520109) + "\n") file.write(" +objective value: " + str(err_best_g) + "\n") file.write(" +best solution: ") for i in pos_best_g: file.write(str(i) + ' ') file.write('\n') return err_best_g
import pso alibotto = pso.Particle(3, 4, (1, 2))