def main(): prob = problem.Problem(dim=3, prangetup=(-3, 3)) popu = Population(prob, size=10) popu.randominit() print(popu.poparr) print(popu.avg_fitness()) print(popu.fitarr) print(popu.find_expecarr()) print("problem starts here") newsel = misc.Selection() newcros = misc.Crossover() newmuta = misc.Mutation() for i in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(i) child1 = newmuta.mutate( child1, prob.rangetup, switch=True, ) child2 = newmuta.mutate(child2, prob.rangetup) print(child1, child2)
def main(): mini=np.inf popsize=1100 prob=problem.Problem(fitness_func=problem.katsuura,dim=10,prangetup=(-100,100)) popu=population.Population(prob,size=popsize) popu.randominit() genlim=20000 print(popu.poparr) print("thing starts here") newsel=misc.Selection(3) newcros=misc.Crossover(rate=0.8) #newmuta=misc.Mutation(rate=0.1) mrate=0.2 newterm=misc.Termination(0) print(popu.avg_fitness()) for i in range(genlim): lis=[] if (i==200): print("here it is-----------------------------------------") if (i==5000): print("here it is 500 ------------------------------------") mrate=0.02 #newmuta=misc.Mutation(rate=0.01) popu.set_fitarr() minic=min(popu.fitarr) if mini>minic: mini=minic mininp=popu.poparr[list(popu.fitarr).index(minic)] for tup in newsel.select_parent(popu): child1,child2=newcros.do_crossover(tup) child1=smallstepchange(child1,fac=3,rate=mrate) child2=smallstepchange(child2,fac=3,rate=mrate) lis.append(child1) lis.append(child2) del(popu) arr=np.array(lis) popu=population.Population(prob,poparr=arr,size=popsize) popu.set_fitarr() if np.all(popu.poparr==popu.poparr[0] ): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr==popu.fitarr[0]): break print(popu.avg_fitness(),i) print(mini,list(mininp)) if newterm.terminate(popul=popu,generationnum=i,generationlim=genlim): print("breaking bad") break print(popu.poparr) print(popu.avg_fitness())
def main(): popsize = 1100 prob = problem.Problem(fitness_func=problem.katsuura, dim=5, prangetup=(-100, 100)) popu = population.Population(prob, size=popsize) popu.randominit() genlim = 2000 print(popu.poparr) print("thing starts here") newsel = misc.Selection(1) newcros = misc.Crossover(rate=0.9) newmuta = misc.Mutation(rate=0.1) newterm = misc.Termination(1) print(popu.avg_fitness()) for i in range(genlim): lis = [] if (i == 200): print("here it is-----------------------------------------") if (i == 500): print("here it is 500 ------------------------------------") for tup in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(tup) child1 = newmuta.mutate(child1, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) child2 = newmuta.mutate(child2, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) lis.append(child1) lis.append(child2) del (popu) arr = np.array(lis) popu = population.Population(prob, poparr=arr, size=popsize) popu.set_fitarr() if np.all(popu.poparr == popu.poparr[0]): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr == popu.fitarr[0]): break print(popu.avg_fitness()) if newterm.terminate(popul=popu, iteri=i, lim=500): print("breaking bad") break print(popu.poparr) print(popu.avg_fitness())
def main(): prob = problem.Problem(prob_func=po.problem1_func, randomvec_func=po.problem1_randomvec_func, constraints=po.constraints, dim=5, prangetup=(-100, 100)) popu = Population(prob, size=10) popu.randominit() print("problem starts here") newsel = misc.Selection(1) newcros = misc.Crossover() newmuta = misc.Mutation() """par1,par2=popu.poparr[0],popu.poparr[1] print(par1,par2) for i in range(100): child1,child2=newcros.do_crossover((par1,par2)) child1=newmuta.mutate(child1,prob.constraints,switch=True,iteri=i) child2=newmuta.mutate(child2,prob.constraints,iteri=i) par1,par2=child1,child2 print(child1,child2) """ count = 0 for i in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(i) child1 = newmuta.mutate(child1, prob.constraints, switch=True, iteri=count) child2 = newmuta.mutate(child2, prob.constraints, iteri=count) print(child1, child2) count += 1
def main(): popsize = 1100 mrate = 0.1 minavg = np.inf prob = problem.Problem(fitness_func=problem.rastrigin, dim=10, prangetup=(-100, 100)) popu = population.Population(prob, size=popsize) popu.randominit() genlim = 20000 print(popu.poparr) print("thing starts here") newsel = misc.Selection(1) newcros = misc.Crossover(rate=0.9) newterm = misc.Termination(1) prevtoprevavg = 2 prevavg = 1 nowavg = 0 flag = 0 newmuta = misc.Mutation(rate=mrate) print(mrate, "starts") print(popu.avg_fitness()) for i in range(genlim): lis = [] if (i == 200): print("here it is-----------------------------------------") if (i == 500): print("here it is 500 ------------------------------------") tup = mutachange(nowavg, prevavg, prevtoprevavg, mrate, step=0.001) if tup: (newmuta, nowavg, prevavg, prevtoprevavg, mrate) = tup kickchange(popu, nowavg, prevavg, prevtoprevavg, kickrate=0.05, step=0.001) #this changes population array for tup in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(tup) child1 = newmuta.mutate(child1, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) child2 = newmuta.mutate(child2, prob.rangetup, switch=1, iteri=i, switchiter=100, factup=(100, 1000)) lis.append(child1) lis.append(child2) del (popu) arr = np.array(lis) popu = population.Population(prob, poparr=arr, size=popsize) popu.set_fitarr() if np.all(popu.poparr == popu.poparr[0]): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr == popu.fitarr[0]): break print(popu.avg_fitness(), mrate, i) if newterm.terminate(popul=popu, iteri=i, lim=500): print("breaking bad") print("for mrate ", mrate, " last gen avg is", popu.avg_fitness()) flag = 1 break prevtoprevavg = prevavg prevavg = nowavg nowavg = popu.avg_fitness() print(popu.poparr) print(popu.avg_fitness())
def main(): mini = np.inf popsize = 1100 prob = problem.Problem(prob_func=po.problem1_func, randomvec_func=po.problem1_randomvec_func, constraints=po.constraints, dim=3, prangetup=(-100, 100)) popu = population.Population(prob, size=popsize) popu.randominit() genlim = 200 #print(popu.poparr) #print("thing starts here") newsel = misc.Selection(1) newcros = misc.Crossover(rate=0.8) newmuta = misc.Mutation(rate=0.1) mrate = 0.2 newterm = misc.Termination(0) #print(popu.avg_fitness()) for i in range(genlim): lis = [] if (i == 200): print("here it is-----------------------------------------") if (i == 5000): print("here it is 500 ------------------------------------") mrate = 0.02 newmuta = misc.Mutation(rate=0.01) #popu.set_fitarr() #minic=min(popu.fitarr) for j in range(popu.size): if popu.poparr[j][0] < 0: print("reporting issue -ve") exit() for tup in newsel.select_parent(popu): child1, child2 = newcros.do_crossover(tup) if child1[0] < 0: print("anarth h here") child1 = newmuta.mutate(child1, prob.constraints) child2 = newmuta.mutate(child2, prob.constraints) for p in range(popu.size): if popu.poparr[p][0] < 0: print("reporting issue -ve") exit() lis.append(child1) lis.append(child2) del (popu) #print("below here") arr = np.array(lis) popu = population.Population(prob, poparr=arr, size=popsize) #print("below here") popu.set_fitarr() if np.all(popu.poparr == popu.poparr[0]): break #these two conditionals have pen-ultimate IMPORTANCE, as my normalization fails heavily if all are same if np.all(popu.fitarr == popu.fitarr[0]): break #print(popu.avg_fitness(),i) #print(mini,list(mininp)) print(i, genlim) if newterm.terminate(popul=popu, generationnum=i, generationlim=genlim): print("breaking bad") break print(popu.poparr)