コード例 #1
0
ファイル: gp_driver.py プロジェクト: wwlorey/pacman-gp
    def begin_run(self):
        """Initializes run variables and writes a run header
        to the log file. 

        This should be called before each run.
        """
        self.eval_count = 0
        self.local_best_score = -1
        self.avg_score = 0
        self.prev_avg_score = 0
        self.stale_score_count_termination = 0
        self.log.write_run_header(self.run_count)

        # Initialize the population
        self.population = []
        for _ in range(self.population_size):
            world = gpac_world_class.GPacWorld(self.config)
            game_state = game_state_class.GameState(
                world.pacman_coords, world.ghost_coords, world.pill_coords,
                self.get_num_adj_walls(world, world.pacman_coords[0]))
            pacman_cont = pacman_cont_class.PacmanController(self.config)
            ghosts_cont = ghosts_cont_class.GhostsController(self.config)
            game_state.update_walls(world.wall_coords)

            self.population.append(
                gpac_world_individual_class.GPacWorldIndividual(
                    world, game_state, pacman_cont, ghosts_cont))
コード例 #2
0
    def __init__(self, config):
        """Initializes the GPDriver class.
        
        Where config is a Config object.
        """
        self.config = config

        self.seed = seed_class.Seed(self.config)

        self.run_count = 1
        self.eval_count = 1
        self.local_best_score = -1

        self.log = log_class.Log(self.config, self.seed, overwrite=True)

        self.global_best_score = -1

        self.gpac_world = gpac_world_class.GPacWorld(self.config,
                                                     initial_instance=True)

        self.pacman_cont = pacman_cont_class.PacmanController()
        self.ghosts_cont = ghosts_cont_class.GhostsController()

        self.game_state = game_state_class.GameState(
            self.gpac_world.pacman_coord, self.gpac_world.ghost_coords,
            self.gpac_world.pill_coords)
コード例 #3
0
ファイル: gp_driver.py プロジェクト: wwlorey/pacman-gp
        def breed(parent_a, parent_b):
            """Performs sub-tree crossover on parent_a and parent_b returning the child tree."""
            def crossover_recursive(receiver_index, donator_index):
                if receiver_index < len(
                        child_pacman_cont.state_evaluator
                ) and donator_index < len(parent_pacman_cont.state_evaluator):
                    return

                child_pacman_cont.state_evaluator[
                    receiver_index] = tree.TreeNode(
                        receiver_index, parent_pacman_cont.
                        state_evaluator[donator_index].value)
                crossover_recursive(
                    child_pacman_cont.state_evaluator.get_left_child_index(
                        receiver_index),
                    parent_pacman_cont.state_evaluator.get_left_child_index(
                        donator_index))
                crossover_recursive(
                    child_pacman_cont.state_evaluator.get_right_child_index(
                        receiver_index),
                    parent_pacman_cont.state_evaluator.get_right_child_index(
                        donator_index))

            # Choose a random node (crossover point) from each state evaluator node list
            crossover_node_a = parent_a.pacman_cont.state_evaluator[
                random.choices([
                    n for n in parent_a.pacman_cont.state_evaluator if n.value
                ])[0].index]
            crossover_node_b = parent_b.pacman_cont.state_evaluator[
                random.choices([
                    n for n in parent_b.pacman_cont.state_evaluator if n.value
                ])[0].index]

            child_pacman_cont = copy.copy(parent_a.pacman_cont)
            parent_pacman_cont = parent_b.pacman_cont

            # Extend the child's state evaluator if necessary
            if len(child_pacman_cont.state_evaluator) < len(
                    parent_a.pacman_cont.state_evaluator):
                child_pacman_cont.state_evaluator = child_pacman_cont.state_evaluator + [
                    tree.TreeNode(index, None) for index in range(
                        len(child_pacman_cont.state_evaluator),
                        len(parent_a.pacman_cont.state_evaluator))
                ]

            # Perform sub-tree crossover
            crossover_recursive(crossover_node_a.index, crossover_node_b.index)

            # Finish generating the child
            world = gpac_world_class.GPacWorld(self.config)
            game_state = game_state_class.GameState(
                world.pacman_coords, world.ghost_coords, world.pill_coords,
                self.get_num_adj_walls(world, world.pacman_coords[0]))
            pacman_cont = child_pacman_cont
            ghosts_cont = parent_a.ghosts_cont
            game_state.update_walls(world.wall_coords)

            child = gpac_world_individual_class.GPacWorldIndividual(
                world, game_state, pacman_cont, ghosts_cont)
            return child
コード例 #4
0
    def begin_run(self):
        """Initializes run variables and writes a run header
        to the log file. 

        This should be called before each run.
        """
        self.eval_count = 0
        self.local_best_pacman_fitness = -1
        self.pacman_avg_fitness = 0
        self.prev_pacman_avg_fitness = 0
        self.stale_fitness_count_termination = 0
        self.log.write_run_header(self.run_count)

        # Initialize the populations
        self.gpac_world_population = []
        self.pacman_cont_population = []
        self.ghost_cont_population = []

        for _ in range(
                max(self.pacman_population_size, self.ghost_population_size,
                    self.pacman_child_population_size,
                    self.ghost_child_population_size)):
            world = gpac_world_class.GPacWorld(self.config)
            game_state = game_state_class.GameState(
                world.pacman_coords, world.ghost_coords, world.pill_coords, [
                    self.get_num_adj_walls(world, pacman_coord)
                    for pacman_coord in world.pacman_coords
                ])
            game_state.update_walls(world.wall_coords)

            self.gpac_world_population.append(
                gpac_world_individual_class.GPacWorldIndividual(
                    world, game_state))

        # BONUS2
        if self.use_single_pacman_cont:
            num_pacman_conts = 1

        else:
            num_pacman_conts = self.num_pacmen

        for _ in range(self.pacman_population_size):
            self.pacman_cont_population.append(
                cont_class.ControllerIndividual([
                    pacman_cont_class.PacmanController(self.config)
                    for _ in range(num_pacman_conts)
                ]))

        # BONUS1, BONUS2
        if self.use_single_ghost_cont:
            num_ghost_conts = 1

        else:
            num_ghost_conts = self.num_ghosts

        for _ in range(self.ghost_population_size):
            self.ghost_cont_population.append(
                cont_class.ControllerIndividual([
                    ghost_cont_class.GhostController(self.config)
                    for _ in range(num_ghost_conts)
                ]))
コード例 #5
0
 def begin_eval(self):
     """(Re)initializes the GPacWorld class member variable.
     
     This should be called prior to each evaluation.
     """
     self.gpac_world = gpac_world_class.GPacWorld(self.config)