def Crossover(): global funEval for i in range(0,popSize): if (random.random() <= CR): # Choose two random indices i1,i2=random.sample(range(0,popSize), 2) # Parents p1=deepcopy(pop[i1]) p2=deepcopy(pop[i2]) # Choose a crossover point pt = 0 # Generate new childs c1=p1.chromosome[0:pt] + p2.chromosome[pt:] c2=p2.chromosome[0:pt] + p1.chromosome[pt:] # Get the fitness of childs c1Fitness=ff.FitnessFunction(c1) funEval = funEval + 1 c2Fitness=ff.FitnessFunction(c2) funEval = funEval + 1 # Select between parent and child if c1Fitness < p1.fitness: pop[i1].fitness=c1Fitness pop[i1].chromosome=c1 if c2Fitness < p2.fitness: pop[i2].fitness=c2Fitness pop[i2].chromosome=c2
def Mutation(): global UB, LB, funEval for i in range(0,popSize): if (random.random() <= MR): # Choose random index r=random.randint(0,popSize-1) # Choose a parent p=deepcopy(pop[r]) # Choose mutation point pt = random.randint(0,ff.D-1) # Generate new childs c=deepcopy(p.chromosome) # Mutation c[pt] = round(random.uniform(ff.LB,ff.UB),2) #Get the fitness of childs cFitness=ff.FitnessFunction(c) funEval = funEval + 1 # Select between parent and child if cFitness < p.fitness: pop[r].fitness=cFitness pop[r].chromosome=c
def DEOperation(): global funEval for i in range(0,popSize): # Choose three random indices i1,i2,i3=random.sample(range(0,popSize), 3) # Iterate for every Dimension newChild=[] for j in range(ff.D): if (random.random() <= CR): k = pop[i1].chromosome[j] + \ F * (pop[i2].chromosome[j] - pop[i3].chromosome[j]) # If new dimention cross LB if k < ff.LB: k = random.uniform(ff.LB,ff.UB) # If new dimention cross LB if k > ff.UB: k = random.uniform(ff.LB,ff.UB) newChild.append(round(k,2)) else: newChild.append(pop[i].chromosome[j]) # Child Fitness newChildFitness=ff.FitnessFunction(newChild) funEval = funEval + 1 # Select between parent and child if newChildFitness < pop[i].fitness: pop[i].fitness=newChildFitness pop[i].chromosome=newChild
def PSOOperation(): for i in range(0, popSize): for j in range(0, ff.D): # Choose two random numbers r1 = random.random() r2 = random.random() # Velocity update pop[i].velocity[j] = w * pop[i].velocity[j] + \ c1 * r1 * (pop[i].pBest[j] - pop[i].particle[j]) + \ c2 * r2 * (gBest[j] - pop[i].particle[j]) if pop[i].velocity[j] < vLB: pop[i].velocity[j] = random.uniform(vLB, vUB) if pop[i].velocity[j] > vUB: pop[i].velocity[j] = random.uniform(vLB, vUB) # Particle update pop[i].particle[j] = round(pop[i].particle[j] + pop[i].velocity[j], 2) if pop[i].particle[j] < ff.LB: pop[i].particle[j] = round(random.uniform(ff.LB, ff.UB), 2) if pop[i].particle[j] > ff.UB: pop[i].particle[j] = round(random.uniform(ff.LB, ff.UB), 2) pop[i].particleFitness = round(ff.FitnessFunction(pop[i].particle), 2) # Select between particle and pBest if pop[i].particleFitness <= pop[i].pBestFitness: pop[i].pBest = pop[i].particle pop[i].pBestFitness = pop[i].particleFitness
def Init(): for i in range(0, popSize): chromosome = [] for j in range(0, ff.D): chromosome.append(round(random.uniform(ff.LB, ff.UB), 2)) fitness = ff.FitnessFunction(chromosome) newIndividual = Individual(chromosome, fitness) pop.append(newIndividual)
def Init(): global funEval for i in range(0, popSize): chromosome = [] chromosome.append(random.uniform(ff.LB, 0)) chromosome.append(random.uniform(0, ff.UB)) fitness = ff.FitnessFunction(chromosome) funEval = funEval + 1 newIndividual = Individual(chromosome, fitness) pop.append(newIndividual)
def Init(): for i in range(0, popSize): particle = [] velocity = [] for j in range(0, ff.D): particle.append(round(random.uniform(ff.LB, ff.UB), 2)) velocity.append(round(random.uniform(vLB, vUB), 2)) particleFitness = round(ff.FitnessFunction(particle), 2) newIndividual = Individual(particle, particleFitness, velocity, deepcopy(particle), particleFitness) pop.append(newIndividual)
def PSOOperation(pop,funEval,popSize,w,c1,c2,vLB,vUB,gBest,gBestFitness): ##global funEval #print("h=",gBestFitness) for i in range(0,popSize): for j in range(0,ff.D): # Choose two random numbers r1=random.random() r2=random.random() ##if (i%10==0,j==1): ##print("in pso : i ",i," j ",j) # Velocity update pop[i].velocity[j] = w * pop[i].velocity[j] + \ c1 * r1 * (pop[i].pBest[j] - pop[i].particle[j]) + \ c2 * r2 * (gBest[j] - pop[i].particle[j]) if pop[i].velocity[j] < vLB: pop[i].velocity[j] = random.uniform(vLB, vUB) if pop[i].velocity[j] > vUB: pop[i].velocity[j] = random.uniform(vLB, vUB) # Particle update pop[i].particle[j] = round(pop[i].particle[j] + pop[i].velocity[j],2) if pop[i].particle[j] < ff.LB: pop[i].particle[j] = round(random.uniform(ff.LB, ff.UB),2) if pop[i].particle[j] > ff.UB: pop[i].particle[j] = round(random.uniform(ff.LB, ff.UB),2) pop[i].particleFitness = round(ff.FitnessFunction(pop[i].particle),2) funEval = funEval + 1 # Select between particle and pBest if pop[i].particleFitness <= pop[i].pBestFitness: pop[i].pBest=pop[i].particle pop[i].pBestFitness=pop[i].particleFitness for p in pop: #print(p.pBestFitness) if p.pBestFitness < gBestFitness: gBest = deepcopy(p.pBest) gBestFitness=p.pBestFitness #print("m = ",gBestFitness) return gBest,gBestFitness
#------------------------------------------------------------- # Step 5: Start Program #------------------------------------------------------------- # Saving Result fp=open(AlgoName+"Result.csv","w") fp.write("Iteration,Fitness,Chromosome\n") for i in range(0,Iterations): Chromosome = random.sample(,ff.D) Fitness = ff.FitnessFunction(Chromosome) if Fitness < BestFitness: BestFitness=Fitness BestChromosome=Chromosome if i%10==0: print ("I:",i,"\t Fitness:",BestFitness) fp.write(str(i) + "," + str(BestFitness)+ "," + str(BestChromosome) + "\n") fp.close() print ("Done") print ("\nBest Chromosome:", BestChromosome, "\tFitness:", BestFitness) print ("Result is saved in", AlgoName+"Result.csv") print ("Total Time Taken: ", round(time.time() - startTime,2), " sec\n")