示例#1
0
 def __init__(self, pygame_screen):
     self.ticks = 0
     self.num_sweepers = NUM_SWEEPERS
     self.num_mines = NUM_MINES
     self.screen = pygame_screen
     self.generations = 0
     self.sweepers = [MineSweeper() for i in xrange(self.num_sweepers)]
     self.num_weights = self.sweepers[0].get_num_weights()
     self.GA = GenAlg(NUM_SWEEPERS,
                      MUTATION_RATE,
                      CROSSOVER_RATE,
                      self.num_weights)
     
     self.population = self.GA.get_population()
     for i in xrange(self.num_sweepers):
         self.sweepers[i].put_weights(self.population[i].weights)
         
     self.mines = []
     for i in xrange(self.num_mines):
         self.mines.append(Mine(random() * WINDOW_WIDTH, 
                                random() * WINDOW_HEIGHT))
示例#2
0
class Controller(object):
    '''
    The controller class.
    This class would rather be a module with the related functions.
    Controller syncs the `MineSweeper`s and the `GenAlg` objects to produce
    actual meaningful simulations.
    '''
    def __init__(self, pygame_screen):
        self.ticks = 0
        self.num_sweepers = NUM_SWEEPERS
        self.num_mines = NUM_MINES
        self.screen = pygame_screen
        self.generations = 0
        self.sweepers = [MineSweeper() for i in xrange(self.num_sweepers)]
        self.num_weights = self.sweepers[0].get_num_weights()
        self.GA = GenAlg(NUM_SWEEPERS,
                         MUTATION_RATE,
                         CROSSOVER_RATE,
                         self.num_weights)
        
        self.population = self.GA.get_population()
        for i in xrange(self.num_sweepers):
            self.sweepers[i].put_weights(self.population[i].weights)
            
        self.mines = []
        for i in xrange(self.num_mines):
            self.mines.append(Mine(random() * WINDOW_WIDTH, 
                                   random() * WINDOW_HEIGHT))
        
    
    def update(self):
        '''
        Walks through the list of sweepers, checking whether a mine has been
        swept. If yes, update the fitness.
        '''
        if self.ticks < NUM_TICKS:
            self.ticks += 1
            for i in xrange(self.num_sweepers):
                self.sweepers[i].update(self.mines)
                
                grab_hit = self.sweepers[i].check_for_mine(self.mines, 
                                                           MINE_SCALE)
                if grab_hit >= 0:
                    self.sweepers[i].inc_fitness()
                    self.mines[grab_hit].position = Vector2D(
                                                        random() * WINDOW_WIDTH,
                                                        random() * WINDOW_HEIGHT
                                                        )
                    self.population[i].fitness = self.sweepers[i].fitness
                
        else:
            self.generations += 1
            self.ticks = 0
            self.population = self.GA.epoch()
            
            for i in xrange(self.num_sweepers):
                self.sweepers[i].put_weights(self.population[i].weights)
                self.sweepers[i].reset()

    def render(self):
        '''
        The main rendering method.
        '''
        f_sweepers = []
        i = 0
        for sweeper in sorted(self.sweepers, 
                              key=lambda x: x.fitness, 
                              reverse=True):
            # Paint the fittest sweepers green.
            color = (0,255,0) if i < NUM_ELITE else None
            f_sweepers.append(MineSweeperFigure(sweeper, color))
            i += 1
         
        f_mines = map(MineFigure, self.mines)
        self.screen.fill(BACKGROUND_COLOUR)
        map(lambda x: x.draw(self.screen), f_sweepers)
        map(lambda x: x.draw(self.screen), f_mines)
        pygame.display.flip()