예제 #1
0
    def createIndividualFixedPartition( self ):
        # This individual will have as many points as we have defined.
        points = []
        interval = (self.hEnd[0] - self.hBegin[0]) / ((self.noPoints + 1) * 1.0)

        #Appends the initial point.
        points.extend([self.hBegin[0], self.hBegin[1]])
                      
        for i in xrange(self.noPoints):
            # Each position will hold an array with the y position.
            # The x position is given by the number of the point.
            # We won't accept values lower than 0, at least, in the beginning.
            points.extend([interval * (i+1), self.rIP.random()*self.hBegin[1]])

        #Appends the final point.
        points.extend([self.hEnd[0], self.hEnd[1]])
                
        individual = Individual(points,0)
        individual.setFitness(calcBrachTime(individual.points))
        
        return individual
예제 #2
0
    def bestCurve(self):

        # If the starting point is at a lower point than the end point,
        # it's useless to start the algorithm because the ball won't ever
        # finish the trail.
        if self.hBegin[1] < self.hEnd[1]:
            print "The answer is obvious. The ball can't get there..."
            return;

        self.createPopulation()
        self.plotMin=[]
        self.plotMax=[]
        self.plotMed=[]
        
        self.retMin = []
        self.retMax = []
        self.retMed = []
        self.retStDev = []
        
        # Starts the algorithm itself for a given number of generations.
        for i in xrange(self.numberGenerations):
            self.currentGeneration = i
            
            # If we are using cross over, we will produce two individuals at a time.
            if self.useCrossover:
                limit = self.sizePopulation / 2
            else:
                limit = self.sizePopulation

            # Now, it's time to produce the next generation.
            newGen = []

            # We will need these values for the roulette selection.
            if (self.useSelectionParents == 2):
                #Minimization Problem: 1 / (1 + fitness)
                totalFitness = 0
                   
                # First, we find the total fitness.
                for member in self.population:
                    totalFitness += (1/(1 + member.fitness))

                # Then, we divide th fitness of each individual by the total fitness.
                for member in self.population:
                    member.probFitness = (1/(1 + member.fitness)) / totalFitness
            
                
            for _ in xrange(limit):
                # We have to select two parents to the crossover.
                parentOne = None
                parentTwo = None
                                
                if(self.useSelectionParents == 1):
                    # Select parents by tournament selection.
                    parentOne = self.tournamentSelection()
                    parentTwo = self.tournamentSelection()
                elif (self.useSelectionParents == 2):
                    # Select parents by roulette selection.
                    parentOne = self.rouletteWheelSelection()
                    parentTwo = self.rouletteWheelSelection()
                    
                else:
                    pass

                sonOne = self.cloneParent(parentOne);
                sonTwo = self.cloneParent(parentTwo);
                    
                #crossover
                if( self.useCrossover ):
                    if self.rCO.random() < self.getCrossOverProb():
                        self.crossOver(sonOne , sonTwo)
                            
                if ( self.useMutation ):
                    self.mutation(sonOne)
                    self.mutation(sonTwo)

                newGen.extend([sonOne, sonTwo]);            
            
            # evaluate the newGeneration
            for i in newGen:
                i.fitness=calcBrachTime(i.points)

            newGen.sort(key = attrgetter('fitness'))
            self.population.sort(key = attrgetter('fitness'))

            # We select the individual that will survive to the next generation.
            if( self.useElitism ):
                self.elitism(self.population, newGen)
                            
            self.plotMed.append(self.currentGeneration)
            avg=self.avg()
            self.plotMed.append(avg)
            self.retMed.append(avg)
            
            min,max=self.findMinAndMaxFit()
            
            self.plotMin.append(self.currentGeneration)
            self.plotMin.append(min)
            
            self.plotMax.append(self.currentGeneration)
            self.plotMax.append(max)
            
            self.retMin.append(min)
            self.retMax.append(max)
            self.retStDev.append(self.stDev(avg))
            
            if self.plot and self.currentGeneration%self.plotOn==0:
                drawGraph(self)
            
        if (self.printPopulation):
            #self.printPopulation()
            print "\nBEST INDIVIDUAL:"
            self.printIndividual(self.population[0])        
        
        
        return [self.population[0].fitness,self.population[0].points,self.retMin,self.retMed,self.retMax,self.retStDev]