def evaluator(self, candidates, args): fitness = [] g = lambda x: 100 * (len(x) + sum([(a - 0.5)**2 - math.cos(20 * math.pi * (a - 0.5)) for a in x])) for c in candidates: gval = g(c[self.objectives-1:]) fit = [0.5 * reduce(lambda x,y: x*y, c[:self.objectives-1]) * (1 + gval)] for m in reversed(range(1, self.objectives)): fit.append(0.5 * reduce(lambda x,y: x*y, c[:m-1], 1) * (1 - c[m-1]) * (1 + gval)) fitness.append(emo.Pareto(fit)) return fitness
def evaluator(self, candidates, args): fitness = [] g = lambda x: 1 + 9.0 / len(x) * sum([a for a in x]) for c in candidates: gval = g(c[self.objectives-1:]) fit = [] for m in range(self.objectives-1): fit.append(c[m]) fit.append((1 + gval) * (self.objectives - sum([a / (1.0 + gval) * (1 + math.sin(3 * math.pi * a)) for a in c[:self.objectives-1]]))) fitness.append(emo.Pareto(fit)) return fitness
def evaluator_internal(self, candidate, dataset, per_time_step=False): """ Take a candidate (i.e. a number of parameter settings for the dynamical systems model) and the dataset and evaluats how well it performs in terms of the mean squared error per eval_aspect. Return the fitness and the prediction. In case of per_time_step=True overwrite the predicted values for the previous time point with the real values. """ self.model.reset() y = [dataset.iloc[0, [dataset.columns.get_loc(x) for x in self.cleaned_eval_aspects]].values] # Go through the dataset, all but last as we need to evaluate our prediction with the next time point. for step in range(0, len(dataset.index) - 1): state_values = [] # Get the relevant values for each of the states in our model. for col in self.model.state_names: # Overwrite the values we predicted previously for the evaluation states if we do it per time step or # if we do not have any prediction yet. if per_time_step or (step == 0): state_values.append(dataset.iloc[step, dataset.columns.get_loc(col[len(self.default_start):])]) # Only overwrite values for the non eval states if we do not do it per time step and use our # predicted value for the eval aspects. else: if col in self.eval_aspects: state_values.append(pred_values[self.eval_aspects.index(col)]) else: state_values.append(dataset.iloc[step, dataset.columns.get_loc(col[len(self.default_start):])]) # Set the state values, parameter values, and execute the model. self.model.set_state_values(state_values) self.model.set_parameter_values(candidate) self.model.execute_steps(1) evals = [] pred_values = [] # Determine the error for the evaluation aspects. for aspect in self.eval_aspects: pred_value = self.model.get_values(aspect)[-1] pred_values.append(pred_value) mse = mean_squared_error([pred_value], [ dataset.iloc[step + 1, dataset.columns.get_loc(col[len(self.default_start):])]]) evals.append(mse) # Store the fitness for all aspects. fitness = emo.Pareto(evals) # Store the predicted values. y.append(pred_values) # And return the fitness and the predicted values. y_frame = pd.DataFrame(y, columns=self.cleaned_eval_aspects) return fitness, y_frame
def evaluator_internal(self, candidate, dataset, per_time_step=False): self.model.reset() y = [] y.append(dataset.ix[0, self.cleaned_eval_aspects].values) # Go through the dataset, all but last as we need to evaluate our # prediction with the next time point. for step in range(0, len(dataset.index) - 1): state_values = [] # Get the relevant values for each of the states # in our model. for col in self.model.state_names: # Overwrite the values we predicted previously for the evaluation states # if we do it per time step or if we do not have any prediction yet. if per_time_step or (step == 0): state_values.append( dataset.ix[step, col[len(self.default_start):]]) # Only overwrite values for the non eval states if we do not do it # per time step and use our predicted value for the eval aspects. else: if col in self.eval_aspects: state_values.append( pred_values[self.eval_aspects.index(col)]) else: state_values.append( dataset.ix[step, col[len(self.default_start):]]) # Set the state values, parameter values, and execute the model. self.model.set_state_values(state_values) self.model.set_parameter_values(candidate) self.model.execute_steps(1) evals = [] pred_values = [] # Determine the error for the evaluation aspects. for eval in self.eval_aspects: pred_value = self.model.get_values(eval)[-1] pred_values.append(pred_value) mse = mean_squared_error( [pred_value], [dataset.ix[step + 1, col[len(self.default_start):]]]) evals.append(mse) # Store the fitness for all aspects. fitness = emo.Pareto(evals) # Store the predicted values. y.append(pred_values) # And return the fitness and the predicted values. y_frame = pd.DataFrame(y, columns=self.cleaned_eval_aspects) return fitness, y_frame
def evaluator(self, candidates, args): fitness = [] numTarget = len(self.minA) for c in candidates: f = [] f1 = 0.00 # for contiguity for i in range(numTarget + 1): f.append(0) count = 0 tempSumArea = [] for i in range(0, 7): tempSumArea.append(0) sumIndArea = self.sumIndArea for i in range(len(c)): #print "i",i, "idx2id:", self.idx2id[i],"c[i]", c[i] if self.indxMap[i][0] == 0: sumIndArea[self.copyLandType[i]] = sumIndArea[ self.copyLandType[i]] - self.areaMap[i] sumIndArea[c[i]] = sumIndArea[c[i]] + self.areaMap[i] count += 1 """" OTHER WAY OF DOING IT""" #tempSumArea[self.landType[i]] = tempSumArea[self.landType[i]] + self.areaMap[i] for val in self.adjList[i]: if self.landType[i] == self.landType[val]: f1 += 10 print count for ltidx in range(1, numTarget + 1): #print ltidx if (sumIndArea[ltidx] - self.minA[ltidx] < 0): f[ltidx] = -(sumIndArea[ltidx] - self.minA[ltidx]) elif (sumIndArea[ltidx] - self.maxA[ltidx] > 0): f[ltidx] = (sumIndArea[ltidx] - self.maxA[ltidx]) else: f[ltidx] = 0 #print "contiguity:", f1, "other fitness", f self.landType = self.copyLandType self.sumIndArea = self.copySumIndArea f[0] = f1 #flist.append(-f) fitness.append(emo.Pareto(f)) return fitness
def evaluator(self, candidates, args): fitness = [] g = lambda x: sum([(a - 0.5)**2 for a in x]) for c in candidates: gval = g(c[self.objectives-1:]) fit = [(1 + gval) * reduce(lambda x,y: x*y, [math.cos(a**self.alpha * math.pi / 2.0) for a in c[:self.objectives-1]])] for m in reversed(range(1, self.objectives)): fit.append((1 + gval) * reduce(lambda x,y: x*y, [math.cos(a**self.alpha * math.pi / 2.0) for a in c[:m-1]], 1) * math.sin(c[m-1]**self.alpha * math.pi / 2.0)) fitness.append(emo.Pareto(fit)) return fitness
def evaluator(self, candidates, args): fitness = [] g = lambda x: sum([a**0.1 for a in x]) for c in candidates: gval = g(c[self.objectives-1:]) theta = lambda x: math.pi / (4.0 * (1 + gval)) * (1 + 2 * gval * x) fit = [(1 + gval) * math.cos(math.pi / 2.0 * c[0]) * reduce(lambda x,y: x*y, [math.cos(theta(a)) for a in c[1:self.objectives-1]])] for m in reversed(range(1, self.objectives)): if m == 1: fit.append((1 + gval) * math.sin(math.pi / 2.0 * c[0])) else: fit.append((1 + gval) * math.cos(math.pi / 2.0 * c[0]) * reduce(lambda x,y: x*y, [math.cos(theta(a)) for a in c[1:m-1]], 1) * math.sin(theta(c[m-1]))) fitness.append(emo.Pareto(fit)) return fitness
def insp_evaluator(self, candidates, args): # check constraint bounds before evaluating [ub,lb] = self.bounds self.storecands.append(candidates) self.evalct = self.evalct + 1 fitness = [] # if failed constraint, penalize the candidate for X in candidates: failed = self.check_penalty(X) # failed = False f1 = self.function_1(X) f2 = self.function_2(X) if f1 <-26 or f1 >= 0:# f1 > 0 for neg lift/drag, # f1 < 0 for drag/lift # 60 l/d is only going to happen with ridiculous glider, but is limit # print 'is failed?' # initial optimisations, -60, for this case we know -27 is too big failed = True if f2 <= 0 or f2 > 500: # print 'is failed2' failed = True if failed: # if failed, penalise both functions # quit() f1 = f1 + 200 # order of magnitude larger than expected lift/drag f2 = f2 + 1000 # half an order of magnitude larger than expected mass else: fitness.append(emo.Pareto([f1, f2])) # for X in candidates: # test case # f1 = X[0]**2 + sum([X[i]**2 for i in range(1,len(X))]) # f2 = (X[0]-1)**2 + sum([X[i]**2 for i in range(1,len(X))]) # fitness.append(emo.Pareto([f1, f2])) self.storegens.append(fitness) return fitness
def evaluator(self, candidates, args): fitness = [] for c_binary in candidates: #print("binary solution:", c_binary) #shuffle net work net = add_solution(self.decoy_net, c_binary, self.info, self.decoy_list) newnet = add_attacker(net) # print("Add attacker:") # printNet(newnet) harm = constructHARM(newnet) f1 = decoyPath(harm) f3 = solutionCost(c_binary, self.info) f2 = 0.0 for i in range(0, self.sim_num): f2 += computeMTTSF(harm, self.net, self.info["threshold"]) print(f1, f2, f3) fitness.append(emo.Pareto([f1, float(f2 / self.sim_num), f3 ])) # a Pareto multi-objective solution return fitness
def evaluatorSol(self, c): # TOtal number of targets equals the land types numTarget = len(self.minA) f=[] # List of all the objective value for each solution f1 = 0.00 # Objective for contiguity objDir=[] # Boolean parameter for anyobjective to maximize or minimize: FALSE=> Minimize that objective # initiating the parameter for all the objectives for i in range(numTarget+1): f.append(0) objDir.append(False) count = 0 tempSumArea = [] for i in range(0,25): tempSumArea.append(0) sumIndArea = copy.deepcopy(self.sumIndArea) p = range(len(c)) for i in p: # if there is change in landtype with respect to basecase for that cell, recalculate the area for the landtypes (1-25) involved # We let the changes happen only for the cells for which there was change wrt base case if self.indxMap[i][0] == 0: # Because the land types have changed, reevaluate the area for that cell # it is: Get landtype of that cell, update the change in area of that landtype because of change in landtype of this cell sumIndArea[self.copyLandType[i]] = sumIndArea[self.copyLandType[i]] - self.areaMap[i] sumIndArea[c[i]] = sumIndArea[c[i]] + self.areaMap[i] if c[i] == self.indxMap[i][4]: count +=1 if count == 6570: print "these are X:", sumIndArea # We need to maximize this object: => more similar adjacent cells => more contiguity for val in self.adjList[i]: if c[i] == c[val]: f1 += 0.01 #print count #""" for ltidx in range(1,numTarget+1): #print ltidx objDir[ltidx]=False # minimizing objective which goes out of boundary if (sumIndArea[ltidx] - self.minA[ltidx] < 0 ): f[ltidx] = -(sumIndArea[ltidx] - self.minA[ltidx]) elif (sumIndArea[ltidx] - self.maxA[ltidx] > 0 ): f[ltidx] = (sumIndArea[ltidx] - self.maxA[ltidx]) else: f[ltidx] = 0 # f[ltidx] = (self.maxA[ltidx] + self.minA[ltidx])/2 - abs(sumIndArea[ltidx] - (self.maxA[ltidx] + self.minA[ltidx])/2 ) self.landType = self.copyLandType # just in case to maintain the original values of the landtypes and sumIndexArea self.sumIndArea = self.copySumIndArea #print f #flist = [-i for i in f] #flist.append(-f1) f[0]=f1 objDir[0]=True # maximizing the contiguity objective f1 f = [f1, sum(f[1:])] objDir = [True, False] #f[1] = f2 #flist.append(-f) #print f return emo.Pareto(f,objDir)
def evaluatorSol(self, c): numTarget = len(self.minA) f = [] f1 = 0.00 # for contiguity objDir = [] for i in range(numTarget + 1): f.append(0) objDir.append(False) count = 0 tempSumArea = [] for i in range(0, 25): tempSumArea.append(0) sumIndArea = copy.deepcopy(self.sumIndArea) p = range(len(c)) for i in p: #print "i",i, "idx2id:", self.idx2id[i],"c[i]", c[i] if self.indxMap[i][0] == 0: sumIndArea[self.copyLandType[i]] = sumIndArea[ self.copyLandType[i]] - self.areaMap[i] sumIndArea[c[i]] = sumIndArea[c[i]] + self.areaMap[i] if c[i] == self.indxMap[i][4]: count += 1 if count == 6570: print "these are X:", sumIndArea for val in self.adjList[i]: if c[i] == c[val]: f1 += 0.01 #print count #""" for ltidx in range(1, numTarget + 1): #print ltidx objDir[ltidx] = False if (sumIndArea[ltidx] - self.minA[ltidx] < 0): f[ltidx] = -(sumIndArea[ltidx] - self.minA[ltidx]) elif (sumIndArea[ltidx] - self.maxA[ltidx] > 0): f[ltidx] = (sumIndArea[ltidx] - self.maxA[ltidx]) else: f[ltidx] = 0 # f[ltidx] = (self.maxA[ltidx] + self.minA[ltidx])/2 - abs(sumIndArea[ltidx] - (self.maxA[ltidx] + self.minA[ltidx])/2 ) self.landType = self.copyLandType self.sumIndArea = self.copySumIndArea #print f #flist = [-i for i in f] #flist.append(-f1) f[0] = f1 objDir[0] = True f = [f1, sum(f[1:])] objDir = [True, False] #f[1] = f2 #flist.append(-f) #print f return emo.Pareto(f, objDir)