def add_preys(self, num): self.increase_preys += num ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) for i in range(num): agent = Agent() health = np.random.uniform(self.min_health, self.max_health) agent.health = health agent.birth_time = self.timestep agent.predator = False agent.life = np.random.normal(500, 100) agent.attack = np.random.uniform(self.min_attack, self.max_attack) agent.resilience = np.random.uniform(self.min_resilience, self.max_resilience) agent.gene_attack = agent.attack agent.gene_resilience = agent.resilience agent.speed = np.random.randint(self.min_speed, self.max_speed) agent.gene_speed = agent.speed agent.id = self.max_id self.max_id += 1 agent.property = [self._gen_power(agent.id), [1, 0, 0]] x = ind[0][perm[i]] y = ind[1][perm[i]] if self.map[x][y] == 0: self.map[x][y] = agent.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map.shape[1]] = agent.id agent.pos = (x, y) self.preys[agent.id] = agent
def add_predators(self, num): ''' Add predators Args: num: Number of predator this function generates ''' self.increase_predators += num ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) if self.experiment_type == 'variation': total = len(self.random_predators) + len( self.trained_predators) + len(self.training_predators) p = [ len(self.random_predators) / total, len(self.trained_predators) / total, len(self.training_predators) / total ] for i in range(num): agent = Agent() agent.health = 1 agent.original_health = 1 agent.birth_time = self.timestep agent.predator = True agent.id = self.max_id self.max_id += 1 agent.speed = 1 agent.life = np.random.normal(500, 100) agent.hunt_square = self.max_hunt_square agent.property = [self._gen_power(agent.id), [0, 0, 1]] x = ind[0][perm[i]] y = ind[1][perm[i]] if self.map[x][y] == 0: self.map[x][y] = agent.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map.shape[1]] = agent.id agent.pos = (x, y) self.predator_num += 1 if self.experiment_type == 'variation': exp_type = np.random.choice(3, p=p) if exp_type == 0: agent.policy_type = 'random' self.random_predators[agent.id] = agent elif exp_type == 1: agent.policy_type = 'trained' self.trained_predators[agent.id] = agent else: agent.policy_type = 'training' self.training_predators[agent.id] = agent else: self.predators[agent.id] = agent
def increase_predator(self, prob): ''' Generates new predators Args: prob: Ratio against the population which determins how many new agents generated. ''' num = max(1, int(self.predator_num * prob)) self.increase_predators = num ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) if self.experiment_type == 'variation': total = len(self.random_predators) + len( self.trained_predators) + len(self.training_predators) p = [ len(self.random_predators) / total, len(self.trained_predators) / total, len(self.training_predators) / total ] for i in range(num): agent = Agent() agent.health = np.random.uniform(self.min_health, self.max_health) agent.original_health = agent.health agent.birth_time = self.timestep agent.predator = True agent.id = self.max_id self.max_id += 1 agent.speed = 1 agent.hunt_square = self.max_hunt_square agent.property = [self._gen_power(agent.id), [0, 0, 1]] x = ind[0][perm[i]] y = ind[1][perm[i]] if self.map[x][y] == 0: self.map[x][y] = agent.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map.shape[1]] = agent.id agent.pos = (x, y) if self.experiment_type == 'variation': exp_type = np.random.choice(3, p=p) if exp_type == 0: agent.policy_type = 'random' self.random_predators[agent.id] = agent elif exp_type == 1: agent.policy_type = 'trained' self.trained_predators[agent.id] = agent else: agent.policy_type = 'training' self.training_predators[agent.id] = agent else: self.predators[agent.id] = agent self.predator_num += 1
def crossover_prey(self, crossover_scope=3, crossover_rate=0.001): ''' Mating function which generates new agents with the given probability if agents are within a certain square Args: crossover_scope: Scope of crossover. If two agents are within this scope, then those agents are candidates for the crossover crossover_rate: The probability that two agents successfully reproduce a new agent ''' ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) index = 0 for prey in list(self.prey_agents.values()): x, y = prey.pos local_map = self.large_map[(self.w + x - crossover_scope // 2):( self.w + x - crossover_scope // 2 + crossover_scope), (self.h + y - crossover_scope // 2):( self.h + y - crossover_scope // 2 + crossover_scope)] agent_indices = np.where(local_map > 0) flag = True if len(agent_indices[0]) == 0 or prey.crossover: continue for candidate_x, candidate_y in zip(agent_indices[0], agent_indices[1]): candidate_id = local_map[candidate_x, candidate_y] candidate_agent = self.agents[candidate_id] prey.checked.append(candidate_agent.id) if (not candidate_agent.predator and not candidate_agent.crossover and candidate_agent.id != prey.id and \ prey.id not in candidate_agent.checked and prey.age > self.args.min_crossover_age and candidate_agent.age > self.args.min_crossover_age and len(self.prey_agents) <= self.args.prey_capacity): candidate_agent.get_closer = True if np.random.rand() < crossover_rate and flag: for i in range( np.random.randint( self.args.max_prey_offsprings) + 1): candidate_agent.crossover = True prey.crossover = True child = Agent() child.id = self.max_id self.max_id += 1 child.predator = False child.life = np.random.normal(500, scale=100) child.health = 1 new_embedding = np.random.normal( size=[self.agent_emb_dim]) self.agent_embeddings[child.id] = new_embedding child.hunt_square = self.max_hunt_square child.property = [ self._gen_power(child.id), [1, 0, 0] ] new_pos_indices = np.where(local_map == 0) x = ind[0][perm[index]] y = ind[1][perm[index]] index += 1 self.map[x][y] = child.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map. shape[1]] = child.id child.pos = (x, y) self.prey_num += 1 if self.experiment_type == 'variation': rand = np.random.rand() if rand < 0.5: child.policy_type = prey.policy_type else: child.policy_type = candidate_agent.policy_type if child.policy_type == 'random': self.random_preys[child.id] = child elif child.policy_type == 'trained': self.trained_preys[child.id] = child else: self.training_preys[child.id] = child else: self.preys[child.id] = child #candidate_agent.health -= 0.1 #prey.health -= 0.1 self.increase_preys += 1 flag = False
def crossover_prey(self, crossover_scope=3, crossover_rate=0.001, mutation_prob=0.001): ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) index = 0 for prey in list(self.preys.values()): x, y = prey.pos local_map = self.large_map[(self.w + x - crossover_scope // 2):( self.w + x - crossover_scope // 2 + crossover_scope), (self.h + y - crossover_scope // 2):( self.h + y - crossover_scope // 2 + crossover_scope)] agent_indices = np.where(local_map > 0) flag = True if len(agent_indices[0]) == 0 or prey.crossover: continue for candidate_x, candidate_y in zip(agent_indices[0], agent_indices[1]): candidate_id = local_map[candidate_x, candidate_y] candidate_agent = self.agents[candidate_id] #prey.checked.append(candidate_agent.id) if (not candidate_agent.predator and not candidate_agent.crossover and candidate_agent.id != prey.id and \ prey.id not in candidate_agent.checked and prey.age > self.args.min_crossover_age and candidate_agent.age > self.args.min_crossover_age \ and len(self.preys) <= self.args.prey_capacity): candidate_agent.get_closer = True if np.random.rand() < crossover_rate and flag: #for i in range(np.random.randint(self.args.max_prey_offsprings)+1): child = Agent() child.id = self.max_id self.max_id += 1 child.predator = False child.life = np.random.normal(500, scale=100) child.health = 1 rate = np.random.rand() if np.random.rand() < mutation_prob: child.attack = ( rate * prey.gene_attack + (1 - rate) * candidate_agent.gene_attack ) + np.random.normal() else: child.attack = ( rate * prey.gene_attack + (1 - rate) * candidate_agent.gene_attack) rate = np.random.rand() if np.random.rand() < mutation_prob: child.resilience = ( rate * prey.gene_resilience + (1 - rate) * candidate_agent.gene_resilience ) + np.random.normal() else: child.resilience = ( rate * prey.gene_resilience + (1 - rate) * candidate_agent.gene_resilience) rate = np.random.rand() if np.random.rand() < mutation_prob: speed = (rate * prey.gene_speed + (1 - rate) * candidate_agent.gene_speed ) + np.random.normal() speed = np.clip(speed, self.min_speed, self.max_speed) child.speed = int(speed) else: child.speed = int( np.round(rate * prey.gene_speed + (1 - rate) * candidate_agent.gene_speed)) child.gene_attack = child.attack child.gene_resilience = child.resilience child.gene_speed = child.speed prey.reward = child.gene_attack + child.gene_resilience candidate_agent.reward = child.gene_attack + child.gene_resilience new_embedding = np.random.normal( size=[self.agent_emb_dim]) self.agent_embeddings[child.id] = new_embedding child.hunt_square = self.max_hunt_square child.property = [self._gen_power(child.id), [1, 0, 0]] x = ind[0][perm[index]] y = ind[1][perm[index]] index += 1 self.map[x][y] = child.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map. shape[1]] = child.id child.pos = (x, y) self.preys[child.id] = child self.prey_num += 1 #candidate_agent.health -= 0.1 #prey.health -= 0.1 self.increase_preys += 1 flag = False candidate_agent.crossover = True prey.crossover = True
def crossover_prey(self, prob, mutation_prob=0.001): ''' Crossover function for preys Args: prob: Ratio against the population. This determins how many agents are chosen for the crossover mutation_prob: Mutation probability ''' num = max(1, int(self.prey_num * prob)) self.increase_preys = num ind = np.where(self.map == 0) perm = np.random.permutation(np.arange(len(ind[0]))) index = 0 preys = list(self.preys.values()) np.random.shuffle(preys) for i in range(num): prey = preys[i * 2] candidate_agent = preys[i * 2 + 1] child = Agent() child.id = self.max_id self.max_id += 1 child.predator = False child.life = np.random.normal(500, scale=100) child.health = 1 child.attack = 1 rate = np.random.rand() if np.random.rand() < mutation_prob: child.resilience = ( rate * prey.gene_resilience + (1 - rate) * candidate_agent.gene_resilience) + np.random.normal() else: child.resilience = ( rate * prey.gene_resilience + (1 - rate) * candidate_agent.gene_resilience) rate = np.random.rand() if np.random.rand() < mutation_prob: speed = (rate * prey.gene_speed + (1 - rate) * candidate_agent.gene_speed) + np.random.normal() speed = np.clip(speed, self.min_speed, self.max_speed) child.speed = int(speed) else: child.speed = int( np.round(rate * prey.gene_speed + (1 - rate) * candidate_agent.gene_speed)) child.gene_speed = child.speed child.gene_attack = child.attack child.gene_resilience = child.resilience child.gene_speed = child.speed prey.reward = child.gene_attack + child.gene_resilience candidate_agent.reward = child.gene_attack + child.gene_resilience new_embedding = np.random.normal(size=[self.agent_emb_dim]) self.agent_embeddings[child.id] = new_embedding child.hunt_square = self.max_hunt_square child.property = [self._gen_power(child.id), [1, 0, 0]] x = ind[0][perm[i]] y = ind[1][perm[i]] index += 1 self.map[x][y] = child.id self.large_map[ x:self.large_map.shape[0]:self.map.shape[0], y:self.large_map.shape[1]:self.map.shape[1]] = child.id child.pos = (x, y) self.preys[child.id] = child self.prey_num += 1