def observe(self, state): """Generate a particulat observation for the current state""" # Compile a distribution over observations in current state obs_dist = {obs: self.pe(state, obs) for obs in self.get_observations()} # Sample from the distribution return weighted_random_choice(obs_dist)
def selection(chromosomes, fitnesses, method): """ Implements selection amongst generations :param chromosomes: population :param fitnesses: fitness values :param method: Choice between different selection strategies :return: new population """ if method is None: method = 'fitness_proportionate' dict_fitnesses = fitnesses.to_dict() n_population = chromosomes.shape[0] selected_individuals = pd.DataFrame(data=None, columns=chromosomes.columns) # Or use df.sample(weights='a') if method == 'fitness_proportionate': for _ in range(n_population): loc = utils.weighted_random_choice(dict_fitnesses) selected_individuals = selected_individuals.append(chromosomes.iloc[loc]) else: selected_individuals = chromosomes.copy() return selected_individuals.reset_index(drop=True)
def step(self, state): """Make a time step: generate a particular next state for the current state""" # Compile a distribution over next states next_dist = {next_state: self.pt(state, next_state) for next_state in self.get_targets(state)} # Sample from the distribution return weighted_random_choice(next_dist)
def next_move_dir(self): """Return the direction of next move""" return weighted_random_choice(self.move_probs)
def read(self): """Return a single sensor reading depending on robot position""" p = self.get_value_probabilities() return weighted_random_choice(p)