def evaluate(model, candidates, coefficients, profiles, evaluators, **kwargs): if all(map(lambda v : v == 0, coefficients)): return Pareto([1000000] + [100000 for _ in profiles]) else: fitness_list = [f(model, coefficients, candidates, profile, **kwargs) for f, profile in zip(evaluators, profiles)] coefficient_sum = sum(coefficients) return Pareto([coefficient_sum] + fitness_list)
def __init__(self, pareto, args): """ edit this function to change the way that multiple objectives are combined into a single objective """ Pareto.__init__(self, pareto.values) if "fitness_weights" in args : weights = asarray(args["fitness_weights"]) else : weights = asarray([1 for _ in pareto.values]) self.fitness = sum(asarray(pareto.values) * weights)
def myevaluator(candidates, args): fitness = [] for i, cc in enumerate(candidates): rr = evaluate(cc, i) fitness.append(Pareto(rr)) update_graph() print(rr, ": ", cc) return fitness
def evaluator(self, candidates, args): fitness = [] self.generation += 1 for c in candidates: #print("c: ", c, "\n conversion: ", utils.vector_to_matrix(c, self.n_turbines, self.n_cities)) c_power = fit.wind_turbine_power_fitness( np.array( utils.vector_to_matrix(c, self.n_turbines, self.n_cities)), self.matrix_power) c_cost, cities_with = fit.wind_turbine_cost_fitness( np.array( utils.vector_to_matrix(c, self.n_turbines, self.n_cities)), self.wind_turbines_costs) #print(c_cost, cities_with) power_plant = fit.PowerPlant( utils.vector_to_matrix(c, self.n_turbines, self.n_cities), self.matrix_cities.tolist(), 10).run() ''' the cost of building transmission line for electrcity is 2.24 mil. Dollars per km Transporting actualy electrcity costs 3.61 dollars per km per kW. ''' c_cost_power_plant = power_plant.fitness * (3.61 * 10**(-6)) * ( c_power * 10**(6)) # 0.02 -> 2 $ per km #c_cost_power_plant = power_plant.fitness * 0.01 # 0.02 -> 2 $ per km total_cost = (c_cost + c_cost_power_plant) # 0: lat, 1: long #print("lat,lon") for i in range(self.n_powerplants): #print(power_plant.candidate[0+i*2],",",power_plant.candidate[1+i*2]) if self.best_fitness_power <= c_power and [ c_power, total_cost ] >= self.best_fitness: self.best_fitness = [c_power, total_cost] self.best_fitness_power = c_power self.powerplants.append([ power_plant.candidate[0 + i * 2], power_plant.candidate[1 + i * 2] ]) ''' penalty if (self.budget - total_cost) < 0: c_power = 0 ''' fitness.append(Pareto([c_power, total_cost], [True, False])) print("GENERATION: [", self.generation, "] | fitness of 3 individuals (not sorted): ", fitness[:3]) return fitness
def evaluate(self, solution): """Evaluates a single solution :param solution: The individual to be evaluated. :returns: A list with a fitness value or a Pareto object. """ p = self.problem.evaluate_solution(solution) # single objective if self.problem.number_of_objectives == 1: return p[0] # multi objective else: return Pareto(p)
def myevaluator(candidates, args): fitness = [] for cc in candidates: f1 = sum([ -10 * math.exp(-0.2 * math.sqrt(cc[i]**2 + cc[i + 1]**2)) for i in range(len(cc) - 1) ]) f2 = sum([math.pow(abs(x), 0.8) + 5 * math.sin(x)**3 for x in cc]) pp = Pareto([ f1, f2 ]) # Pareto is the special data type provided by inspyred libarary fitness.append(pp) # append it to the list called fitness return ( fitness ) # return fitness which is the set fitness values for all candidates.
def myevaluator(candidates, args): """given a set of candidates calculate objective 1 and 2 (if 2 objectives, more if you have more objectives). These are teh fitness values. Put each pair in Pareto object provided by inspyred. Return as a list (same order as candidates! ) """ fitness = [] for cc in candidates: f1 = sum([ -10 * math.exp(-0.2 * math.sqrt(cc[i]**2 + cc[i + 1]**2)) for i in range(len(cc) - 1) ]) f2 = sum([math.pow(abs(x), 0.8) + 5 * math.sin(x)**3 for x in cc]) pp = Pareto([ f1, f2 ]) # Pareto is the special data type provided by inspyred libarary fitness.append(pp) # append it to the list called fitness return ( fitness ) # return fitness which is the set fitness values for all candidates.
def __call__(self, model, solution, targets): return Pareto( values=[o(model, solution, targets) for o in self.objectives])
def worst_fitness(self, maximize=True): return Pareto( values=[o.worst_fitness(maximize) for o in self.objectives], maximize=maximize)