Particle.gbest=np.copy(temp2) gbest_fit=fit_val2 if flag==0: Particle.gbest=np.copy(base) print gbest_fit #iterations of hybrid algo starts here gbest_fit=fit.calculate_fitness(Particle.gbest,cost) temp_fit=gbest_fit flag=0 for k in range(0,iterations): for i in range(0,no_of_particles): child=ero.cross_over(pop[i].pos,pop[i].pbest) fit_val=fit.calculate_fitness(child,cost) #if fitness of child is higher than pbest,update pbest if fit_val<pop[i].pbest_fit: pop[i].pbest=np.copy(child) pop[i].pbest_fit=fit_val #if fitness of child is higher than gbest,update gbest #if fit_val<gbest_fit: # Particle.gbest=np.copy(child) # gbest_fit=fit_val # flag=0 child=ero.cross_over(pop[i].pos,Particle.gbest) fit_val=fit.calculate_fitness(child,cost)
fit_val2=fit.calculate_fitness(pop[indx2],cost) if fit_val1 <= fit_val2: new_pop[i]=pop[indx1] else: new_pop[i]=pop[indx2] #print new_pop #perform cross over b/w randomly selected parents for i in range(0,pop_size): randno=random.uniform(0,1) indx1=random.randrange(0,pop_size) indx2=random.randrange(0,pop_size) if(randno>=0.5): #print new_pop[indx1] #print new_pop[indx2] new_pop[i]=ero.cross_over(new_pop[indx1],new_pop[indx2]) #print 'after cross over' #print new_pop #perform mutation #mutation probability = 0.1 #mutation uses swap operator if(k%10==0): for i in range(0,pop_size): randno=random.uniform(0,1) if(randno>=0.5): pos1=random.randrange(0,cities) pos2=random.randrange(0,cities) temp=new_pop[i][pos1] new_pop[i][pos1]=new_pop[i][pos2] new_pop[i][pos2]=temp
Particle.gbest = np.copy(temp2) gbest_fit = fit_val2 if flag == 0: Particle.gbest = np.copy(base) print gbest_fit #iterations of hybrid algo starts here gbest_fit = fit.calculate_fitness(Particle.gbest, cost) temp_fit = gbest_fit flag = 0 for k in range(0, iterations): for i in range(0, no_of_particles): child = ero.cross_over(pop[i].pos, pop[i].pbest) fit_val = fit.calculate_fitness(child, cost) #if fitness of child is higher than pbest,update pbest if fit_val < pop[i].pbest_fit: pop[i].pbest = np.copy(child) pop[i].pbest_fit = fit_val #if fitness of child is higher than gbest,update gbest #if fit_val<gbest_fit: # Particle.gbest=np.copy(child) # gbest_fit=fit_val # flag=0 child = ero.cross_over(pop[i].pos, Particle.gbest) fit_val = fit.calculate_fitness(child, cost)
fit_val2 = fit.calculate_fitness(pop[indx2], cost) if fit_val1 <= fit_val2: new_pop[i] = pop[indx1] else: new_pop[i] = pop[indx2] #print new_pop #perform cross over b/w randomly selected parents for i in range(0, pop_size): randno = random.uniform(0, 1) indx1 = random.randrange(0, pop_size) indx2 = random.randrange(0, pop_size) if (randno >= 0.5): #print new_pop[indx1] #print new_pop[indx2] new_pop[i] = ero.cross_over(new_pop[indx1], new_pop[indx2]) #print 'after cross over' #print new_pop #perform mutation #mutation probability = 0.1 #mutation uses swap operator if (k % 10 == 0): for i in range(0, pop_size): randno = random.uniform(0, 1) if (randno >= 0.5): pos1 = random.randrange(0, cities) pos2 = random.randrange(0, cities) temp = new_pop[i][pos1] new_pop[i][pos1] = new_pop[i][pos2] new_pop[i][pos2] = temp