def constriction_factor_particle_swarm_test(Tx, Ty): particles = [] pbestvector = [] lenTx = len(Tx) lenTy = len(Ty) # global objfitness n = random.randint(0, 10) for i in range(20): Sx = gd.get_xdata(0, 500, lenTx - 2) Sy = gd.get_ydata(0, 500, lenTx - 2) Xs = reduce((lambda x, y: x + y), Sx) // len(Sx) Ys = reduce((lambda x, y: x + y), Sy) // len(Sy) particle = Particle(Sx, Sy, 0, Xs, Ys) particles.append(particle) pbestvector.append(math.inf) for i in range(20): # objfitness = [] for j in range(len(particles)): mst = get_fitness(Tx, Ty, particles[j]) if mst < pbestvector[j]: pbestvector[j] = mst Rx = Tx[0:len(Tx) - lenTx] Ry = Ty[0:len(Tx) - lenTy] Tx = Tx[0:len(Tx) - len(Rx)] Ty = Ty[0:len(Ty) - len(Ry)] best_obj_fitness = min(pbestvector) for j in range(len(particles)): constriction_factor_particle_swarm_optimization(best_obj_fitness, particles[j], pbestvector[j]) particle_best = particles[pbestvector.index(min(pbestvector))] return particle_best
def create_particles(Tx, Ty, len_S, particles): global particle_no # Global Variables declared at the begening of the program # Creating and initiating the Particles for _ in range(particle_no): Sx = gd.get_xdata(0, 500, len_S) Sy = gd.get_ydata(0, 500, len_S) particle = Particle(Sx, Sy) particle.fitness = get_fitness(np.copy(Tx), np.copy(Ty), particle) particles.append(particle)
def iwo_test(Tx, Ty): lenTx = len(Tx) lenTy = len(Ty) weedlist = [] for i in range(10): Wx = gd.get_xdata(0, 500, 8) Wy = gd.get_ydata(0, 500, 8) Xp = reduce((lambda x, y: x + y), Wx) // len(Wx) Yp = reduce((lambda x, y: x + y), Wy) // len(Wy) weed = Weed(Wx, Wy, Xp, Yp, 0) weedlist.append(weed) # iwo_algorithm(Tx,Ty,weedlist,itermax,pmax,Smax,Smin,sigmafinal,sigmainit,n) wlist = iwo_algorithm(Tx, Ty, weedlist, 5, 150, 10, 1, 0.01, 1, 3) fmax = get_max_fitness(wlist) for weed in wlist: if fmax == weed.fitness: bestweed = weed break Rx = Tx[0:len(Tx) - lenTx] Ry = Ty[0:len(Ty) - lenTy] Tx = Tx[0:len(Tx) - len(Rx)] Ty = Ty[0:len(Ty) - len(Ry)] print("X-Coordinates of best weed", list(map(lambda x: math.ceil(x), bestweed.Wx))) print("Y-Coordinates of best weed", list(map(lambda y: math.ceil(y), bestweed.Wy))) count = 0 bestweed.Wx = list(map(lambda x: math.ceil(x), bestweed.Wx)) bestweed.Wy = list(map(lambda y: math.ceil(y), bestweed.Wy)) for i in range(len(bestweed.Wx)): if bestweed.Wx[i] < min(Tx) or bestweed.Wx[i] > max(Tx): continue if bestweed.Wy[i] < min(Ty) or bestweed.Wy[i] > max(Ty): continue Tx.append(bestweed.Wx[i]) Ty.append(bestweed.Wy[i]) count = count + 1 print("Updated X Coordinates", Tx) print("Updated Y Coordinates", Ty) distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) print("Size of Steiner Tree for IWO", mst_size) pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy) Tx = Tx[0:len(Tx) - count] Ty = Ty[0:len(Ty) - count] print("Restored X Coordinates=", Tx) print("Restored Y Coordinates=", Ty) print("End of IWO")
def pigeon_test(Tx, Ty): pigeons = [] global objfitness #n = random.randint(0, 10) lenTx = len(Tx) lenTy = len(Ty) for i in range(150): # No. of Pigeons Sx = gd.get_xdata(0, 500, lenTx - 2) Sy = gd.get_ydata(0, 500, lenTy - 2) Xs = reduce((lambda x, y: x + y), Sx) // len(Sx) Ys = reduce((lambda x, y: x + y), Sy) // len(Sy) pigeon = Pigeon(Sx, Sy, 0, Xs, Ys, 0) pigeons.append(pigeon) for i in range(10): for j in range(len(pigeons)): #Txnew = Tx[:] #Tynew = Ty[:] mst = get_fitness(Tx, Ty, pigeons[j]) pigeons[j].fitness = 1 / mst Rx = Tx[0:len(Tx) - lenTx] Ry = Ty[0:len(Tx) - lenTy] Tx = Tx[0:len(Tx) - len(Rx)] Ty = Ty[0:len(Ty) - len(Ry)] #get_original_list(Tx,Ty,lenTx,lenTy) # Tx = Tx[0:len(Tx) - len(pigeons[j].Sx)] # print(Tx) # Ty = Ty[0:len(Ty) - len(pigeons[j].Sy)] # print(Ty) best_fitness = max(pigeons, key=lambda pigeon: pigeon.fitness).fitness for j in range(len(pigeons)): pigeon_optimization_map_and_compass(best_fitness, pigeons[j], i) pigeon_optimization_landmark(Tx, Ty, lenTx, lenTy, pigeons) pigeon_best = max(pigeons, key=lambda pigeon: pigeon.fitness) print(pigeon_best.Sx) print(pigeon_best.Sy) return pigeon_best
def pigeon_test(Tx, Ty): pigeons = [] #global objfitness #n = random.randint(0, 10) lenTx = len(Tx) lenTy = len(Ty) for i in range(150): Sx = gd.get_xdata(0, 500, lenTx - 2) Sy = gd.get_ydata(0, 500, lenTy - 2) len_S = Sx.size #Xs = reduce((lambda x, y: x + y), Sx) // len(Sx) #Ys = reduce((lambda x, y: x + y), Sy) // len(Sy) Xs = np.sum(Sx) // len_S Ys = np.sum(Sy) // len_S pigeon = Pigeon(Sx, Sy, 0, Xs, Ys,0) pigeons.append(pigeon) for i in range(15): for j in range(len(pigeons)): mst = get_fitness(Tx, Ty, pigeons[j]) pigeons[j].fitness = 1/mst temp_len = Tx.size Rx = Tx[0:temp_len - lenTx] len_Rx = Rx.size Tx = Tx[0:temp_len - len_Rx] Ty = Ty[0:temp_len - len_Rx] # Tx = Tx[0:len(Tx) - len(pigeons[j].Sx)] # print(Tx) # Ty = Ty[0:len(Ty) - len(pigeons[j].Sy)] # print(Ty) best_fitness = max(pigeons, key = lambda pigeon: pigeon.fitness).fitness for j in range(len(pigeons)): pigeon_optimization_map_and_compass(best_fitness, pigeons[j], i) pigeon_optimization_landmark(Tx,Ty,lenTx,lenTy,pigeons) pigeon_best = max(pigeons, key = lambda pigeon: pigeon.fitness) #print(pigeon_best.Sx) #print(pigeon_best.Sy) return pigeon_best
def particle_swarm_test(Tx, Ty): particles = [] pbestvector = [] lenTx = Tx.size lenTy = lenTx for i in range(150): len_S = lenTx - 2 Sx = gd.get_xdata(0, 500, len_S) Sy = gd.get_ydata(0, 500, len_S) #Xs = reduce((lambda x, y: x+y), Sx) // len(Sx) #Ys = reduce((lambda x, y: x+y), Sy) // len(Sy) Xs = np.sum(Sx) // len_S Ys = np.sum(Sy) // len_S particle = Particle(Sx, Sy, 0, Xs, Ys) particles.append(particle) pbestvector.append(math.inf) for i in range(15): #objfitness = [] for j in range(len(particles)): mst = get_fitness(Tx, Ty, particles[j]) if mst < pbestvector[j]: pbestvector[j] = mst temp_len = Tx.size Rx = Tx[0:temp_len - lenTx] Ry = Ty[0:temp_len - lenTy] len_Rx = Rx.size Tx = Tx[0:temp_len - len_Rx] Ty = Ty[0:temp_len - len_Rx] best_obj_fitness = min(pbestvector) for j in range(len(particles)): particle_swarm_optimization(best_obj_fitness, particles[j], pbestvector[j]) #print("X Coordinates of particle",particles[j].Sx) #print("Y Coordinates of particle",particles[j].Sy) particle_best = particles[pbestvector.index(min(pbestvector))] return particle_best
import prim_algorithm as pa import gridgraphdemo as gd import pigeon_optimization_algorithm as poa import particle_swarm_optimization as pso import constriction_factor_pso as cpso Tx = gd.get_xdata(0, 500, 20) #Change values over here and test Ty = gd.get_ydata(0, 500, 20) #Change values over here and test lenTx = len(Tx) lenTy = len(Ty) print("X Coordinates", Tx) print("Y Coordinates", Ty) distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) print("Size of the MST = ", mst_size) pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy) poa.call_methods(Tx, Ty, lenTx, lenTy) pso.call_methods(Tx, Ty, lenTx, lenTy) cpso.call_methods(Tx, Ty, lenTx, lenTy)
Ty.append(bestweed.Wy[i]) count = count + 1 print("Updated X Coordinates", Tx) print("Updated Y Coordinates", Ty) distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) print("Size of Steiner Tree for IWO", mst_size) pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy) Tx = Tx[0:len(Tx) - count] Ty = Ty[0:len(Ty) - count] print("Restored X Coordinates=", Tx) print("Restored Y Coordinates=", Ty) print("End of IWO") Tx = gd.get_xdata(0, 500, 10) Ty = gd.get_ydata(0, 500, 10) print(Tx) print(Ty) lenTx = len(Tx) lenTy = len(Ty) print("X Coordinates", Tx) print("Y Coordinates", Ty) distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) print("Size of the MST = ", mst_size) pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy) iwo_test(Tx, Ty)
import gridgraphdemo as gd import numpy as np dim = int(input('Enter the dimension of the search space : ')) limit = int(input('Enter the limit of terminal points : ')) for i in range(10, limit + 10, 10): Tx = gd.get_xdata(0, dim, i) Ty = gd.get_ydata(0, dim, i) filename = 'terminal_point_{0}_{1}'.format(str(dim), str(i)) np.save(filename, np.array([Tx, Ty]))