def move_to_food(self, foods): """ Selection of Food to move towards, and ant movement :param foods: (list) Food objects in noticeable objects :return: (array) new ant position """ # Go directly to food if there is only one source if len(foods) == 1: return self.move_to(foods[0].position) # Compare food sources in terms of size and distance to nest else: sub_food = [] for i, obj in enumerate(foods): # If food size equal to zero, we ignore the Food object if obj.size == 0: pass # If distance is equal to zero, we ignore the Food object elif distance(obj.position - self.home.position) == 0: pass else: sub_food.append(obj) if sub_food: # Getting features of food objects data = zeros((len(sub_food), 2)) for i, obj in enumerate(sub_food): # Food size data[i, 0] = obj.size # Distance to nest data[i, 1] = distance(obj.position - self.home.position) # Rescaling each feature to have values bounded by 1 data /= np.max(data, axis=0) # Calculating probability distribution probs = (data[:, 0] ** self.foodiness) * (data[:, 1] ** self.explorativeness) probs /= np.sum(probs) # Drawing an object from the prob distribution index = np.random.choice(len(sub_food), p=probs) # index = np.argmax(probs) return self.move_to(sub_food[index].position) else: return None
def move_to_pheromone(self, pheromones): """ Selection of Pheromone to move towards, and ant movement :param pheromones: (list) Pheromone objects in noticeable objects :return: (array) new ant position """ # Go directly to scent if there is only one source if len(pheromones) == 1: return self.move_to(pheromones[0].position) # Compare pheromone sources else: # Getting features of pheromone objects data = zeros((len(pheromones), 3)) for i, obj in enumerate(pheromones): # Intensity data[i, 0] = obj.strength # Distance to nest data[i, 1] = distance(obj.position - self.home.position) # Difference in momentum # TODO define difference in momentum data[i, 2] = 1 # Rescaling each feature to have values bounded by 1 data /= np.max(data, axis=0) # Calculating probability distribution probs = (data[:, 0] ** self.inscentiveness) * (data[:, 1] ** self.explorativeness) probs *= (data[:, 2] ** self.directionism) probs /= np.sum(probs) # Draw an object from the prob distribution index = np.random.choice(len(pheromones), p=probs) # index = np.argmax(probs) return self.move_to(pheromones[index].position)
def reset_parameters(self): glorot(self.weight) zeros(self.bias)