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] ''' ind.fitness = lcs(ind.genome, best.genome)
def fitness(self, ind, user_feedback): ''' Compute fitness. user_feedback is [best] ''' best = user_feedback[0] widgetNum = self.widgetNum subj = 0 subj += lcs(ind.genome['grid'], best.genome['grid']) subj += lcs(ind.genome['style'], best.genome['style']) #-------------------------------------------------------------# # BEGIN OBJECTIVE EVALUATION #-------------------------------------------------------------# grid, myValues = self.decode(ind) red = green = blue = 0 # and the background color # A high value is good # window color is stored at pos 0 red = myValues['window_red'] - myValues['widget_red'] green = myValues['window_green'] - myValues['widget_green'] blue = myValues['window_blue'] - myValues['widget_blue'] distance = (red*red + green*green + blue*blue)**0.5 obj = distance / self.maxDist * 100. grid_max = self.gridLen - 1. ui = self.uiWidgets layout_score = 0. num_binds = 0 for id in grid: if id: if ui[id]['bind']: other = grid.index(ui[id]['bind']) me = grid.index(id) diff = abs(other - me) if diff < 3: layout_score += 1. else: layout_score += (grid_max-diff)/grid_max num_binds += 1. if num_binds: val = layout_score/num_binds * 100. obj += val else: obj += 100. #-------------------------------------------------------------# # 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 ind.objFitness = objFitness ind.subjFitness = subjFitness ind.fitness = [self.objScaleMax-objFitness, self.subjScaleMax-subjFitness]