def call_methods(Tx, Ty, lenT, func): # Setting the method into a method variable for dynamic linking global get_velocity_position get_velocity_position = func # Calling the method that controls the main optimisation algorithm bestparticle = particle_swarm_test(np.copy(Tx), np.copy(Ty), lenT) # Extracting the position of the Steiner Points from the best particle for i in range(bestparticle.Sx.size): if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx): continue if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty): continue Tx = np.append(Tx, bestparticle.Sx[i]) Ty = np.append(Ty, bestparticle.Sy[i]) # Finding the MST of the final RSMT distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) # Building the final return tuple for the use in the main program return_set = (mst_size, Tx, Ty) # Ploting the RSMT pa.draw_gridgraph(Tx, Ty, mst, lenT, lenT) return return_set
def call_methods(Tx, Ty, lenTx, lenTy): 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] #print("X coordinates =", Tx) #print("Y coordinates =", Ty) bestparticle = particle_swarm_test(Tx, Ty) 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] #print("Updated X coordinates", Tx) #print("Updated Y coordinates", Ty) '''print("X coordinates of best particle", bestparticle.Sx) print("Y coordinates of best particle", bestparticle.Sy) print("Updated X coordinates", Tx) print("Updated Y Coordinates", Ty)''' count = 0 for i in range(len(bestparticle.Sx)): if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx): continue if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty): continue #Tx.append(bestparticle.Sx[i]) #Ty.append(bestparticle.Sy[i]) Tx = np.append(Tx, bestparticle.Sx[i]) Ty = np.append(Ty, bestparticle.Sy[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 PSO", mst_size) return_set = (mst_size, Tx, Ty) #pa.draw_gridgraph(Tx, Ty, mst,lenTx,lenTx) temp_len = Tx.size Tx = Tx[0:temp_len - count] Ty = Ty[0:temp_len - count] '''print("Restored X Coordinates=", Tx) print("Restored Y Coordinates=", Ty) print("End of PSO")''' return return_set
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 call_methods(Tx, Ty, lenTx, lenTy): # Tx = gd.get_xdata(0, 500, 10) # Ty = gd.get_ydata(0, 500, 10) ''' lenTx = len(Tx) lenTy = len(Ty) ''' # Calculating mST for PIO #print("X coordinates =", Tx) #print("Y coordinates =", Ty) bestpigeon = pigeon_test(Tx, Ty) 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] # print("Updated X coordinates", Tx) # print("Updated Y coordinates", Ty) '''print("X coordinates of best pigeon", bestpigeon.Sx) print("Y coordinates of best pigeon", bestpigeon.Sy) print("Updated X coordinates", Tx) print("Updated Y Coordinates", Ty)''' count = 0 for i in range(len(bestpigeon.Sx)): if bestpigeon.Sx[i] < min(Tx) or bestpigeon.Sx[i] > max(Tx): continue if bestpigeon.Sy[i] < min(Ty) or bestpigeon.Sy[i] > max(Ty): continue #Tx.append(math.floor(bestpigeon.Sx[i])) #Ty.append(math.floor(bestpigeon.Sy[i])) Tx = np.append(Tx, np.floor(bestpigeon.Sx[i])) Ty = np.append(Ty, np.floor(bestpigeon.Sy[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 PIO", mst_size) #pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTy) return_set = (mst_size, Tx, Ty) 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 PIO")''' return return_set
def call_methods(Tx, Ty, lenTx, lenTy): ''' lenTx = len(Tx) lenTy = len(Ty) ''' 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)] print("X coordinates =", Tx) print("Y coordinates =", Ty) bestparticle = particle_swarm_test(Tx, Ty) 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("Updated X coordinates", Tx) #print("Updated Y coordinates", Ty) print("X coordinates of best particle", bestparticle.Sx) print("Y coordinates of best particle", bestparticle.Sy) print("Updated X coordinates", Tx) print("Updated Y Coordinates", Ty) count = 0 for i in range(len(bestparticle.Sx)): if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx): continue if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty): continue Tx.append(bestparticle.Sx[i]) Ty.append(bestparticle.Sy[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 PSO", mst_size) pa.draw_gridgraph(Tx, Ty, mst, lenTx, lenTx) 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 PSO")
def call_methods(Tx, Ty, lenTx, lenTy): # Tx = gd.get_xdata(0, 500, 10) # Ty = gd.get_ydata(0, 500, 10) ''' lenTx = len(Tx) lenTy = len(Ty) ''' # Calculating mST for PIO print("X coordinates =", Tx) print("Y coordinates =", Ty) bestpigeon = pigeon_test(Tx, Ty) 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("Updated X coordinates", Tx) # print("Updated Y coordinates", Ty) print("X coordinates of best pigeon", bestpigeon.Sx) print("Y coordinates of best pigeon", bestpigeon.Sy) print("Updated X coordinates", Tx) print("Updated Y Coordinates", Ty) count = 0 for i in range(len(bestpigeon.Sx)): if bestpigeon.Sx[i] < min(Tx) or bestpigeon.Sx[i] > max(Tx): continue if bestpigeon.Sy[i] < min(Ty) or bestpigeon.Sy[i] > max(Ty): continue Tx.append(math.floor(bestpigeon.Sx[i])) Ty.append(math.floor(bestpigeon.Sy[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 PIO", 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 PIO")
def call_methods(Tx, Ty, lenTx, lenTy): # Tx = gd.get_xdata(0, 500, 100) # Ty = gd.get_ydata(0, 500, 100) lenTx = Tx.size lenTy = Ty.size # Calculating mST for CPSO 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)] #print("X coordinates =", Tx) #print("Y coordinates =", Ty) bestparticle = constriction_factor_particle_swarm_test(Tx, Ty) 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("Updated X coordinates", Tx) # print("Updated Y coordinates", Ty) '''print("X coordinates of best particle", bestparticle.Sx) print("Y coordinates of best particle", bestparticle.Sy) print("Updated X coordinates", Tx) print("Updated Y Coordinates", Ty)''' count = 0 for i in range(len(bestparticle.Sx)): if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx): continue if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty): continue Tx = np.append(Tx, math.ceil(bestparticle.Sx[i])) Ty = np.append(Ty, math.ceil(bestparticle.Sy[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 Constricted-PSO", mst_size) return_set = (mst_size, Tx, Ty) #pa.draw_gridgraph(Tx, Ty, mst,lenTx,lenTy) return return_set
def call_methods(Tx, Ty, lenT, func): # Performance controler of the PSO global cost_vect global px_vect global py_vect # Setting the method into a method variable for dynamic linking global get_velocity_position get_velocity_position = func bestparticle = Particle(fitness=math.inf) # Running the GPSO Iterations for i in range(gpso_max_iter): # Running PSO algorithm bestparticle_local = particle_swarm_test(np.copy(Tx), np.copy(Ty), lenT) if bestparticle_local.fitness < bestparticle.fitness: bestparticle = bestparticle_local # Running Gradient Cycles px = np.mean(bestparticle_local.Sx) py = np.mean(bestparticle_local.Sy) for _ in range(grad_max_iter): cost = search_function(px, py) cost_vect.append(cost) px_vect.append(px) py_vect.append(py) grad = gradient(px, py) px, py = update_params(px, py, grad[0], grad[1]) new_Sx = bestparticle_local.Sx + round(px) new_Sy = bestparticle_local.Sy + round(py) new_fitness = get_fitness(np.copy(Tx), np.copy(Ty), Particle(new_Sx, new_Sy)) if new_fitness < bestparticle.fitness: bestparticle.fitness = new_fitness bestparticle.Sx = new_Sx bestparticle.Sy = new_Sy # Extracting the position of the Steiner Points from the best particle for i in range(bestparticle.Sx.size): if bestparticle.Sx[i] < min(Tx) or bestparticle.Sx[i] > max(Tx): continue if bestparticle.Sy[i] < min(Ty) or bestparticle.Sy[i] > max(Ty): continue Tx = np.append(Tx, bestparticle.Sx[i]) Ty = np.append(Ty, bestparticle.Sy[i]) # Finding the MST of the final RSMT distancevector = gd.get_distancevector(Tx, Ty) mst = pa.mst_prim(distancevector) mst_size = pa.get_tree(distancevector) # Building the final return tuple for the use in the main program return_set = (mst_size, Tx, Ty, mst) return return_set
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)