Esempio n. 1
0
 def fitness(self, ind, user_feedback):
     '''
     Compute fitness.
     user_feedback is a list of the user's input,
     the list contents are ordered the same as the 
     feedback variable in the config yaml file.
     [best]
     '''
     best = user_feedback[0]
     ind.fitness = hamming(ind.genome, best.genome)
Esempio n. 2
0
    def fitness(self, ind, user_feedback):
        '''
        Compute fitness.
        user_feedback is [best]
        '''
        best = user_feedback[0]
        subj = 0
        if ind.genome == best.genome:
            subj = self.geneLen
        else:
            subj += hamming(ind, best)

        ind.scalefit = ind.fitness = subj
Esempio n. 3
0
    def fitness(self, ind, user_feedback):
        '''
        Compute fitness.
        user_feedback is a list of the user's input,
        the list contents are ordered the same as the 
        feedback variable in the config yaml file.
        [best]
        '''
        best = user_feedback[0]
        subj = 0
        if ind.genome == best.genome:
            subj = self.geneLen
        else:
            subj += hamming(ind, best)

        ind.scalefit = ind.fitness = subj
    def fitness(self, ind, user_feedback):
        '''
        Compute fitness.
        user_feedback is [best,worst]
        '''
        best, worst = user_feedback
        sumValue = 0
        if ind.genome == best.genome:
            sumValue = 2 * len(ind.genome)
        elif ind.genome == worst.genome:
            sumValue = 0
        else:
            #sumValue += lcs(ind.genome, best.genome)
            #sumValue += len(ind.genome)-lcs(ind.genome, worst.genome)
            sumValue += hamming(ind, best, worst)

        ind.scalefit = ind.fitness = sumValue
    def fitness(self, ind, user_feedback):
        '''
        Compute fitness.
        user_feedback is a list of the user's input,
        the list contents are ordered the same as the 
        feedback variable in the config yaml file.
        [best]
        '''
        best = user_feedback[0]
        if self.clear:
            self.decode(best)
            self.clear = False

        self.decode(ind)
        f1 = hamming(ind.bit_chrome, best.bit_chrome)
        f1 /= float(len(ind.bit_chrome))


        ind_root = ind.decoded_data
        ind_root.stats()

        best_root = best.decoded_data
        best_root.stats()

        
        f3 = compareTrees(ind_root, best_root)
        f3 /= float(max(ind_root.num_children, best_root.num_children)+1)


        f2 = 0

        #print 'comp: ', f3, f1
        #d = 0
        #printTree(d, ind_root)
        #printTree(d, best_root)


        ind.fitness = (f1*2 + f2 + f3) * 100
Esempio n. 6
0
    def fitness(self, ind, user_feedback):
        '''
        Compute fitness.
        user_feedback is [best,worst]
        '''
        best,worst = user_feedback
        subj = 0
        widgetNum = self.widgetNum
        if ind.genome == best.genome:
            subj = 2 * len(ind.genome['grid'])+len(ind.genome['style'])
        elif ind.genome == worst.genome:
            subj = 0
        else:
            #sum += lcs(ind.genome, best.genome)
            #sum += len(ind.genome)-lcs(ind.genome, worst.genome)
            subj += hamming(ind, best, worst)


        #-------------------------------------------------------------#
        # BEGIN OBJECTIVE EVALUATION
        #-------------------------------------------------------------#
        red = green = blue = 0
        colorFitness = 0
        myValues = self.decode(ind.genome['style'])
        # Compare widget colors, we want the colors to be similar, so a low value is good
        # start from 1, since at pos 0 we store window color
        for i in xrange(1, widgetNum - 1):
            for j in xrange(i + 1, widgetNum):
                red = abs(myValues[i][0] - myValues[j][0])
                green = abs(myValues[i][1] - myValues[j][1])
                blue = abs(myValues[i][2] - myValues[j][2])

                distance = (red*red + green*green + blue*blue)**0.5

                colorFitness += (self.maxDist-distance) / self.maxDist * 100.

        windColor = 0
        # compare all widgets to the window color, we want a high color diff between each widget
        # and the background color
        # A high value is good
        for i in xrange(1, widgetNum):
            # window color is stored at pos 0
            red = abs(myValues[i][0] - myValues[0][0])
            green = abs(myValues[i][1] - myValues[0][1])
            blue = abs(myValues[i][2] - myValues[0][2])

            distance = (red*red + green*green + blue*blue)**0.5
            windColor += distance / self.maxDist * 100.


        obj = colorFitness + windColor
        #-------------------------------------------------------------#
        # END OBJECTIVE EVALUATION
        #-------------------------------------------------------------#
        subj = float(subj)
        # Scale objective and subjective fitness components
        objFitness = (obj-self.objMin)/(self.objMax-self.objMin) * self.objScaleMax + self.objScaleMin
        subjFitness = (subj-self.subjMin)/(self.subjMax-self.subjMin) * self.subjScaleMax + self.subjScaleMin

        w1, w2 = self.params['weight1'], self.params['weight2']
        #ind.scalefit = ind.fitness = objFitness * w1 + subjFitness * w2
        ind.objFitness = objFitness
        ind.subjFitness = subjFitness
        ind.fitness = [objFitness, subjFitness]
    def report(self, pop, subset, gen):
        app.Application.report(self, pop, gen)
        maxFit = 0.0
        sumFit = 0.0
        minFit = 100000.0

        sumObjFit = 0.0
        sumSubjFit = 0.0
        
        best = None
        worst = None

        ## this can be replaced with a max and min function
        #for ind in pop:
        #    if ind.fitness > maxFit:
        #        maxFit = ind.fitness
        #        best = ind

        #    if ind.fitness < minFit:
        #        minFit = ind.fitness
        #        worst = ind
        #                
        #    sumFit += ind.fitness
        #    sumObjFit += ind.objFitness
        #    sumSubjFit += ind.subjFitness
        #    
        #popLen = len(pop)
        #print '%i) Best Fit: %.2f, Avg Fit: %.2f' % (gen, best.fitness, sumFit/popLen)


        ## Compute avg objective and subjective fitness
        #objFit = sumObjFit / popLen
        #subjFit = sumSubjFit / popLen

        ## gen max avg min opt
        #self.fout.write("%5d %10.2f %10.2f %10.2f " % (gen, 
        #            best.fitness, sumFit/len(pop), worst.fitness))

        ## obj_max obj_avg obj_min subj_max subj_avg subj_min
        #self.fout.write("%10.2f %10.2f %10.2f %10.2f %10.2f %10.2f\n" % (best.objFitness, objFit, 
        #            worst.objFitness, best.subjFitness, subjFit, worst.subjFitness))

        # minus fitness bias dimension
        #len_fit = len(pop[0].fitness) - 1
        #fit = [0.] * len_fit

        #for ind in pop:
        #    for i in xrange(len_fit):
        #        fit[i] += (ind.fitness[i] - self.limits['min'][i])/ self.limits['max'][i]

        #pop_len = len(pop)
        #self.fout.write('%d\t%.5f\t%.5f\n' % (gen, fit[0]/pop_len, fit[1]/pop_len))


        avg_hd = 0.
        pop_len = len(pop)
        for i in xrange(pop_len-1):
            for j in xrange(i+1, pop_len):
                avg_hd += hamming(pop[i].genome['grid']+pop[i].genome['style'],
                        pop[j].genome['grid']+pop[j].genome['style'])

        avg_hd /= float(pop_len)
        self.fout.write('%d\t%.5f\n' % (gen, avg_hd))

        f = open('data/xul_gen_%d' % gen, 'w')
        i = 0
        pop.sort(lambda a,b: cmp(a.rank, b.rank))
        for i in xrange(10):
            f.write('%.2f %.2f\n' % (pop[i].fitness[0], pop[i].fitness[1]))
        f.close()