def evolution_search(f, para_b): begin_time = datetime.now() Timestamps_list = [] Target_list = [] Parameters_list = [] keys = list(para_b.keys()) dim = len(keys) plog = PrintLog(keys) min = np.ones(dim) max = np.ones(dim) value_list = list(parameters.values()) for i_v in range(dim): min[i_v] = value_list[i_v][0] max[i_v] = value_list[i_v][1] bounds = (min, max) plog.print_header(initialization=True) my_topology = Star() my_options ={'c1': 0.6, 'c2': 0.3, 'w': 0.4} my_swarm = P.create_swarm(n_particles=20, dimensions=dim, options=my_options, bounds=bounds) # The Swarm Class iterations = 30 # Set 100 iterations for i in range(iterations): # Part 1: Update personal best # for evaluated_result in map(evaluate, my_swarm.position): # my_swarm.current_cost = np.append(evaluated_result) # for best_personal_result in map(evaluate, my_swarm.pbest_pos): # Compute personal best pos # my_swarm.pbest_cost = np.append(my_swarm.pbest_cost, best_personal_result) my_swarm.current_cost = np.array(list(map(evaluate, my_swarm.position))) #print(my_swarm.current_cost) my_swarm.pbest_cost = np.array(list(map(evaluate, my_swarm.pbest_pos))) my_swarm.pbest_pos, my_swarm.pbest_cost = P.compute_pbest(my_swarm) # Update and store # Part 2: Update global best # Note that gbest computation is dependent on your topology if np.min(my_swarm.pbest_cost) < my_swarm.best_cost: my_swarm.best_pos, my_swarm.best_cost = my_topology.compute_gbest(my_swarm) # Let's print our output #if i % 2 == 0: # print('Iteration: {} | my_swarm.best_cost: {:.4f}'.format(i + 1, my_swarm.best_cost)) # Part 3: Update position and velocity matrices # Note that position and velocity updates are dependent on your topology my_swarm.velocity = my_topology.compute_velocity(my_swarm) my_swarm.position = my_topology.compute_position(my_swarm) Parameters_list.append(my_swarm.best_pos.tolist()) Target_list.append(1-my_swarm.best_cost) elapse_time = (datetime.now() - begin_time).total_seconds() Timestamps_list.append(elapse_time) # print("The best candidate: ", my_swarm.best_pos) # print("The best result: ", res[1]) plog.print_step(my_swarm.best_pos, 1 - my_swarm.best_cost) if i == 0: plog.print_header(initialization=False) return Timestamps_list, Target_list, Parameters_list
async def attack(self, phys_swarm): if phys_swarm.amount > 5: orders = [] # reinitialize swarm if needed if phys_swarm.amount != self.swarm_size: self.swarm_size = phys_swarm.amount self.phys_swarm_pos = [] for unit in phys_swarm: self.phys_swarm_pos.append( [unit.position.x, unit.position.y]) self.phys_swarm_pos = np.array(self.phys_swarm_pos) self.logical_swarm = P.create_swarm( n_particles=phys_swarm.amount, dimensions=2, options=self.my_options, bounds=([0, 0], [ self.game_info.map_size[0], self.game_info.map_size[1] ]), init_pos=self.phys_swarm_pos, clamp=(0, 5.6)) self.logical_swarm.current_cost = self.fitness( self.logical_swarm.position, phys_swarm) self.logical_swarm.pbest_cost = self.fitness( self.logical_swarm.pbest_pos, phys_swarm) self.logical_swarm.pbest_pos, self.logical_swarm.pbest_cost = P.compute_pbest( self.logical_swarm) if np.min(self.logical_swarm.pbest_cost ) < self.logical_swarm.best_cost: self.logical_swarm.best_pos, self.logical_swarm.best_cost = self.my_topology.compute_gbest( self.logical_swarm) self.logical_swarm.velocity = self.my_topology.compute_velocity( self.logical_swarm) self.logical_swarm.position = self.my_topology.compute_position( self.logical_swarm) # Extract positions from above and issue movement/attack orders # loop through np array compiling positions and appending them to orders list. # The mutas are still ignoring nearby enemies. for row, unit in zip(self.logical_swarm.position, phys_swarm): if self.known_enemy_units.closer_than(unit.radar_range, unit.position).exists: orders.append( unit.attack( self.known_enemy_units.closest_to(unit.position))) else: orders.append( unit.move(Point2(Pointlike((row[0], row[1]))))) await self.do_actions(orders)
def __init__(self, objective_func, iters=30, num_particles=30, num_features=14): self.swarm_size = (num_particles, num_features) self.swarm = create_swarm(n_particles=num_particles, dimensions=num_features, discrete=True, binary=True) self.food_fitness = -np.inf self.enemy_fitness = +np.inf self.food_pos = np.zeros(num_features) self.enemy_pos = np.zeros(num_features) self.num_particles = num_particles self.objective_func = objective_func self.iters = iters
async def attack(self, phys_swarm, iteration): if phys_swarm.amount > 10: orders = [] # reinitialize swarm if needed if phys_swarm.amount > self.swarm_size + 3 or iteration > self.iter_of_last_update + 75: self.swarm_size = phys_swarm.amount self.phys_swarm_pos = [] for unit in phys_swarm: self.phys_swarm_pos.append([unit.position.x, unit.position.y]) self.phys_swarm_pos = np.array(self.phys_swarm_pos) self.logical_swarm = P.create_swarm(n_particles=phys_swarm.amount, dimensions=2, options=self.my_options, bounds=([0,0], [self.game_info.map_size[0], self.game_info.map_size[1]]) , init_pos=self.phys_swarm_pos, clamp=(0,4.0)) self.iter_of_last_update = iteration self.logical_swarm.current_cost = self.fitness(self.logical_swarm.position, phys_swarm) self.logical_swarm.pbest_cost = self.fitness(self.logical_swarm.pbest_pos, phys_swarm) self.logical_swarm.pbest_pos, self.logical_swarm.pbest_cost = P.compute_pbest(self.logical_swarm) if np.min(self.logical_swarm.pbest_cost) < self.logical_swarm.best_cost: self.logical_swarm.best_pos, self.logical_swarm.best_cost = self.my_topology.compute_gbest(self.logical_swarm) self.logical_swarm.velocity = self.my_topology.compute_velocity(self.logical_swarm) self.logical_swarm.position = self.my_topology.compute_position(self.logical_swarm) # Extract positions from above and issue movement/attack orders # loop through np array compiling positions and appending them to orders list. wounded_units = phys_swarm.filter(lambda u: u.health_percentage <= .7) phys_swarm = phys_swarm.filter(lambda u: u.health_percentage > .7) for unit in wounded_units: unit.move(self.townhalls.first.position) # The mutas are still ignoring nearby enemies. for row, unit in zip(self.logical_swarm.position, phys_swarm): if self.known_enemy_units.closer_than(unit.radar_range, unit.position).exists: orders.append(unit.stop()) orders.append(unit.attack(self.known_enemy_units.closest_to(unit.position).position)) elif self.known_enemy_units.exists: orders.append(unit.attack(self.known_enemy_units.closest_to(Point2(Pointlike((row[0], row[1])))))) else: orders.append(unit.move(Point2(Pointlike((row[0], row[1]))))) await self.do_actions(orders)
def __init__(self): self.swarm_size = 0 self.my_topology = Star() # connect to n nearest neighbors -> Ring() #options should be parameterized... self.my_options = {'c1': 4, 'c2': 1, 'w': 0.3} # might need dummy values for the following and wait until phys swarm exists to continue... self.phys_swarm_pos = [] self.log_swarm = \ P.create_swarm(n_particles=0, dimensions=2, options=self.my_options, bounds=([0,0], [self.game_info.map_size[0], self.game_info.map_size[1]]), init_pos=phys_swarm_pos, clamp=(0,10)) self.iter_of_last_update = 0 self.num_overlords = 0 #load in cnn model self.brain = model.load("brain")
def __fitNBeta(self, dim, n_particles, itera, options, objetive_function, BetaChange, bound): my_topology = Ring() my_swarm = P.create_swarm(n_particles=n_particles, dimensions=dim, options=options, bounds=bound) my_swarm.pbest_cost = np.full(n_particles, np.inf) my_swarm.best_cost = np.inf for i in range(itera): for a in range(n_particles): my_swarm.position[a][0:BetaChange] = sorted( my_swarm.position[a][0:BetaChange]) for c in range(1, self.BetaChange): if my_swarm.position[a][c - 1] + 5 >= my_swarm.position[a][c]: my_swarm.position[a][c] = my_swarm.position[a][c] + 5 my_swarm.current_cost = objetive_function(my_swarm.position) my_swarm.pbest_pos, my_swarm.pbest_cost = P.operators.compute_pbest( my_swarm) #my_swarm.current_cost[np.isnan(my_swarm.current_cost)]=np.nanmax(my_swarm.current_cost) #my_swarm.pbest_cost = objetive_function(my_swarm.pbest_pos) my_swarm.best_pos, my_swarm.best_cost = my_topology.compute_gbest( my_swarm, options['p'], options['k']) if i % 20 == 0: print( 'Iteration: {} | my_swarm.best_cost: {:.4f} | days: {}'. format( i + 1, my_swarm.best_cost, str(my_swarm.pbest_pos[my_swarm.pbest_cost.argmin()]))) my_swarm.velocity = my_topology.compute_velocity(my_swarm, bounds=bound) my_swarm.position = my_topology.compute_position(my_swarm, bounds=bound) final_best_cost = my_swarm.best_cost.copy() final_best_pos = my_swarm.pbest_pos[ my_swarm.pbest_cost.argmin()].copy() return final_best_pos, final_best_cost
cop = C11() N = 150 dim = 5 l_lim = cop.l_lim u_lim = cop.u_lim if l_lim == None or u_lim == None: bounds = None else: l_lims = np.asarray([l_lim] * dim) u_lims = np.asarray([u_lim] * dim) bounds = (l_lims, u_lims) my_topology = AdaptiveRing() my_options = {'c1': 0.6, 'c2': 0.3, 'w': 0.4, 'feasibility': np.zeros(N, dtype = bool), 'best_position': None} my_swarm = P.create_swarm(n_particles = N, dimensions=dim, options=my_options, bounds = bounds) iterations = 300 my_swarm.options['feasibility'] = cop.constraints(my_swarm.position) my_swarm.current_cost = cop.sum_violations(my_swarm.position) my_swarm.pbest_pos = my_swarm.position my_swarm.pbest_cost = my_swarm.current_cost for i in range(iterations): if np.all(my_swarm.options['feasibility'] == False) == False: break my_swarm.best_cost = min(x for x in my_swarm.pbest_cost if x is not None) min_pos_id = np.where(my_swarm.pbest_cost == my_swarm.best_cost)[0][0] my_swarm.options['best_position'] = my_swarm.pbest_pos[min_pos_id]
def optimize(self): results_2k = 0 results_10k = 0 results_20k = 0 fes = 0 # Function evaluations l_lim = self.cop.l_lim u_lim = self.cop.u_lim if l_lim == None or u_lim == None: bounds = None else: l_lims = np.asarray([l_lim] * self.dim) u_lims = np.asarray([u_lim] * self.dim) bounds = (l_lims, u_lims) my_options = { 'c1': self.c1, 'c2': self.c2, 'w': self.w, 'feasibility': np.zeros(self.N, dtype=bool), 'best_position': None } my_swarm = P.create_swarm(n_particles=self.N, dimensions=self.dim, options=my_options, bounds=bounds) my_swarm.pbest_pos = np.asarray([None] * self.N) my_swarm.pbest_cost = np.asarray([None] * self.N) k_delta = math.ceil(self.N / self.iterations) # additional neighbors each gen k = k_delta # Check feasibility and update personal best only if feasible my_swarm.options['feasibility'] = self.cop.constraints( my_swarm.position) my_swarm.current_cost = self.cop.objective(my_swarm.position) fes += self.N for particle_id in range(self.N): if my_swarm.options['feasibility'][particle_id] == True: my_swarm.pbest_pos[particle_id] = my_swarm.position[ particle_id] my_swarm.pbest_cost[particle_id] = my_swarm.current_cost[ particle_id] for i in range(self.iterations): if np.all(my_swarm.pbest_cost == None) == False: my_swarm.best_cost = min(x for x in my_swarm.pbest_cost if x is not None) min_pos_id = np.where( my_swarm.pbest_cost == my_swarm.best_cost)[0][0] my_swarm.options['best_position'] = my_swarm.pbest_pos[ min_pos_id] if k < self.N: my_swarm.best_pos = self.my_topology.compute_gbest(my_swarm, p=2, k=k) else: my_swarm.best_pos = self.my_topology.compute_gbest(my_swarm, p=2, k=self.N) k += k_delta if fes == 2000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format( fes, my_swarm.best_cost)) results_2k = my_swarm.best_cost elif fes == 10000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format( fes, my_swarm.best_cost)) results_10k = my_swarm.best_cost elif fes == 20000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format( fes, my_swarm.best_cost)) results_20k = my_swarm.best_cost break my_swarm.velocity = self.my_topology.compute_constrained_velocity( my_swarm) my_swarm.position = self.my_topology.compute_position(my_swarm) my_swarm.options['feasibility'] = self.cop.constraints( my_swarm.position) my_swarm.current_cost = self.cop.objective(my_swarm.position) fes += self.N my_swarm.pbest_pos, my_swarm.pbest_cost = P.compute_constrained_pbest( my_swarm) if self.verbose == True: print('The best cost found by our swarm is: {:.4f}'.format( my_swarm.best_cost)) print('The best position found by our swarm is: {}'.format( my_swarm.options['best_position'])) return (results_2k, results_10k, results_20k)
def PSO_grad_optimize(n_particles, dimensions, bounds, init_pose, select_num, iteration, lr, init_state, init_act): #Input: # n_particles:num of particles #dimension: dim of varibales #bounds: boundary of actions #init_pose: initial pose #select_num: num of particle kept #iteration: number of iterations #lr: learning rate of apply gradient #init_state: initial state input for gradient compute #init_act: initial action input for gradient compute #output: #the best pose #a list of selected pos #define partcicle my_topology = Star() # The Topology Class my_options = { 'c1': 0.1, 'c2': 0.000, 'w': 0.000 } # arbitrarily set #0.01,0.01 my_swarm = P.create_swarm(n_particles=n_particles, dimensions=dimensions, options=my_options, bounds=bounds, init_pos=init_pose) # The Swarm Class for i in range(iteration): # Part 1: Update personal best step_num = int(dimensions / actor_critic.act_dim) my_swarm.current_cost = f(my_swarm.position) cur_action = my_swarm.position.reshape( (n_particles, step_num, actor_critic.act_dim)) gradients = actor_critic.compute_gradient(step_num, init_act, init_state, cur_action) my_swarm.pbest_pos = cur_action.reshape( (-1, dimensions)) + gradients[0].reshape((-1, dimensions)) * lr my_swarm.pbest_pos = np.clip(my_swarm.pbest_pos, -0.015, 0.015) my_swarm.pbest_cost = f(my_swarm.pbest_pos) print(my_swarm.pbest_cost) # Part 2: Update global best # Note that gbest computation is dependent on your topology if np.min(my_swarm.pbest_cost) < my_swarm.best_cost: my_swarm.best_pos, my_swarm.best_cost = my_topology.compute_gbest( my_swarm) # Let's print our output print('Iteration: {} | my_swarm.best_cost: {:.4f}'.format( i + 1, my_swarm.best_cost)) # Part 3: Update position and velocity matrices # Note that position and velocity updates are dependent on your topology my_swarm.velocity = my_topology.compute_velocity(my_swarm) my_swarm.position = my_topology.compute_position(my_swarm) my_swarm.position = np.clip(my_swarm.position, -0.015, 0.015) best_index = my_swarm.pbest_cost.argsort()[:select_num] print(best_index) good_actions = my_swarm.pbest_pos[best_index].tolist() good_actions.append(my_swarm.best_pos) return np.clip(my_swarm.best_pos, -0.015, 0.015), np.asarray(good_actions)
def optimize(self): results_2k = 0 results_10k = 0 results_20k = 0 fes = 0 # Function evaluations l_lim = self.cop.l_lim u_lim = self.cop.u_lim if l_lim == None or u_lim == None: bounds = None else: l_lims = np.asarray([l_lim] * self.dim) u_lims = np.asarray([u_lim] * self.dim) bounds = (l_lims, u_lims) my_options = {'c1': self.c1, 'c2': self.c2, 'w': self.w, 'feasibility': np.zeros(self.N, dtype = bool), 'best_position': None} my_swarm = P.create_swarm(n_particles = self.N, dimensions = self.dim, options = my_options, bounds = bounds) my_swarm.options['feasibility'] = self.cop.constraints(my_swarm.position) my_swarm.current_cost = self.cop.sum_violations(my_swarm.position) fes += self.N my_swarm.pbest_pos = my_swarm.position my_swarm.pbest_cost = my_swarm.current_cost for i in range(self.iterations): if np.all(my_swarm.options['feasibility'] == False) == False: if i == 0: my_swarm.best_cost = 0 break my_swarm.best_cost = min(x for x in my_swarm.pbest_cost if x is not None) min_pos_id = np.where(my_swarm.pbest_cost == my_swarm.best_cost)[0][0] my_swarm.options['best_position'] = my_swarm.pbest_pos[min_pos_id] my_swarm.best_pos = self.my_topology.compute_gbest(my_swarm, p = 2, k = self.N) if fes == 2000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format(fes, my_swarm.best_cost)) results_2k = my_swarm.best_cost elif fes == 10000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format(fes, my_swarm.best_cost)) results_10k = my_swarm.best_cost elif fes == 20000: if self.verbose == True: print('FES: {} | my_swarm.best_cost: {:.4f}'.format(fes, my_swarm.best_cost)) results_20k = my_swarm.best_cost break new_best_pos = np.empty([self.N, self.dim]) for n in range(self.N): for d in range(self.dim): new_best_pos[n][d] = my_swarm.best_pos[n][d] my_swarm.best_pos = new_best_pos my_swarm.velocity = self.my_topology.compute_velocity(my_swarm) my_swarm.position = self.my_topology.compute_position(my_swarm) my_swarm.options['feasibility'] = self.cop.constraints(my_swarm.position) my_swarm.current_cost = self.cop.sum_violations(my_swarm.position) fes += self.N my_swarm.pbest_pos, my_swarm.pbest_cost = P.compute_pbest(my_swarm) if i%50==0: if self.verbose == True: print('Iteration: {} | my_swarm.best_cost: {:.4f}'.format(i+1, my_swarm.best_cost)) if np.all(my_swarm.options['feasibility'] == False) == False: self.success = True if self.verbose == True: print('The feasible region was found!') print('The following are all the known feasible points:') print(my_swarm.position[my_swarm.options['feasibility'] == True]) else: if self.verbose == True: print('The feasible region was not found :(') print('The best sum of violations found by our swarm is: {:.4f}'.format(my_swarm.best_cost)) print('The best position found by our swarm is: {}'.format(my_swarm.options['best_position'])) return (my_swarm.best_cost, results_2k, results_10k, results_20k, self.success)