Example #1
0
 def update_choice(self):
     '''
     Updates every choice performed.
     '''
     global glb_num_population
     global glb_alg_choice
     
     # N of people      
     self.__number_population = glb_num_population 
     
     # Chosed algorithm
     if (glb_alg_choice == glb_key_random ): 
         # Random
         print("Chosed Random algorithm")
         params = Common_alg_params(self.__number_population)
         self.__fitness_alg = Random_alg(params)
         
     elif (glb_alg_choice == glb_key_pseudoME) :
         # Pseudo ME
         print("Chosed pseudoMacroevolutionary algorithm")
         params = Common_alg_params(self.__number_population)
         self.__fitness_alg = MA_alg(params)
         
     elif (glb_alg_choice == glb_key_ga):
         # Genetic Algorithm
         print("Chosed Genetic algorithm")
         params = Params_GA(self.__number_population)
         params.set_size_population(self.__number_population)
         self.__fitness_alg = GA_alg(params)
         
     elif (glb_alg_choice == glb_key_mixed):
         # Own algorithm
         print("Chosed own (mixed) algorithm")
         params = Common_alg_params(self.__number_population)
         self.__fitness_alg = Mixed_alg(params)
         
     # Pause worker and re-start
     self.init_population()
         
     print("The choice is : {0}".format(glb_alg_choice))
Example #2
0
class GraphicPlot(wx.Panel):
    def __init__(self,parent):
        '''
        Graphic plot conforms the zone to view and control the main flow of the  simulation.
        '''
        wx.Panel.__init__(self, parent,-1)
        
        # Draws the map
        self.draw_map()
        
        #Base parameters (from GUI).  
        self.update_choice()
        
        # Distributes the population among the map 
        self.init_population()
    
    def draw_map(self):
        '''
        It draws the landscape according to some function (to try to minimize)
        '''
        global glb_landscape_functions
        
        '''
        Base map contains the region to plot.
        By default an XY plane between (-10,-10) and (10,10)
        '''        
        self.upper_limit=10
        self.lower_limit=-10
        self.npts = 500
        self.function = glb_landscape_functions.get_function()
        self.points = []
        
        # Add Countor graph
        self.new_countour()
        
        
    def update_choice(self):
        '''
        Updates every choice performed.
        '''
        global glb_num_population
        global glb_alg_choice
        
        # N of people      
        self.__number_population = glb_num_population 
        
        # Chosed algorithm
        if (glb_alg_choice == glb_key_random ): 
            # Random
            print("Chosed Random algorithm")
            params = Common_alg_params(self.__number_population)
            self.__fitness_alg = Random_alg(params)
            
        elif (glb_alg_choice == glb_key_pseudoME) :
            # Pseudo ME
            print("Chosed pseudoMacroevolutionary algorithm")
            params = Common_alg_params(self.__number_population)
            self.__fitness_alg = MA_alg(params)
            
        elif (glb_alg_choice == glb_key_ga):
            # Genetic Algorithm
            print("Chosed Genetic algorithm")
            params = Params_GA(self.__number_population)
            params.set_size_population(self.__number_population)
            self.__fitness_alg = GA_alg(params)
            
        elif (glb_alg_choice == glb_key_mixed):
            # Own algorithm
            print("Chosed own (mixed) algorithm")
            params = Common_alg_params(self.__number_population)
            self.__fitness_alg = Mixed_alg(params)
            
        # Pause worker and re-start
        self.init_population()
            
        print("The choice is : {0}".format(glb_alg_choice))
            
    
    def init_population(self):
        '''
        Initializes a certain population (with his kind)
        '''
        self.__fitness_alg.new_population()
        
        
    def new_countour(self):
        '''
        It draws a new map and also inits new population
        '''
        # Once every time..
        self.xi = linspace(self.lower_limit-0.1, self.upper_limit+0.1, self.npts)
        self.yi = linspace(self.lower_limit-0.1, self.upper_limit+0.1, self.npts)

        self.x = []
        self.y = []
        self.z = []
        
        #seed(0)
        global glb_landscape_functions
        functionXY = glb_landscape_functions.get_function()
        self.x = uniform(self.lower_limit, self.upper_limit, self.npts)
        self.y = uniform(self.lower_limit, self.upper_limit, self.npts)
        for index in range(0,len(self.x)):
            self.z.append(functionXY(self.x[index],self.y[index]))             
    
        self.zgrid = griddata(self.x,self.y,self.z,self.xi,self.yi)
        
        # Distributes the population among the map 
        #self.init_population()
        
        self.draw_base_plot()
        
    def draw_base_plot(self):
        # Configure graph
        self.figure = matplotlib.figure.Figure()
        self.axes = self.figure.add_subplot(111)
        self.axes.cla()
        self.axes.contour(self.xi, self.yi, self.zgrid,linewidths=0.5,colors='k')
        self.axes.contourf(self.xi, self.yi, self.zgrid)
        self.canvas = FigureCanvas(self, -1, self.figure)


    def add(self, point):
        self.points.append(point)
        
    def clear(self):
        self.points = []

    def do_step(self):
        
        '''One step more, apply the algorithm'''
        self.draw_base_plot()
        
        self.__fitness_alg.update_population()
        
        #print("{0} individuals to draw !".format(len(self.__fitness_alg.get_individuals())))
        # Plot each individual
        for indy in self.__fitness_alg.get_individuals():
            self.axes.plot(indy.x,indy.y,'wo',ms=5)
            #print "pos ind {0},{1}".format(indy.x, indy.y)
        
        self.figure.canvas.draw()