def getBasicMeasures(pop, optimal_front): pop.sort(key=lambda x: x.fitness.values) convergenceValue = convergence(pop, optimal_front) diversityValue = diversity(pop, optimal_front[0], optimal_front[-1]) print("Convergence: ", convergenceValue) print("Diversity: ", diversityValue) return (convergenceValue, diversityValue)
def main(): # pop, stats = init(64) with open("pareto_front/zdt1_front.json") as optimal_front_data: optimal_front = json.load(optimal_front_data) # Use 500 of the 1000 points in the json file optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2)) pop, stats = init() pop.sort(key=lambda x: x.fitness.values) print(stats) print("Convergence: ", convergence(pop, optimal_front)) print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1])) # import matplotlib.pyplot as plt # import numpy front = np.array([ind.fitness.values for ind in pop]) optimal_front = np.array(optimal_front) plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r") plt.scatter(front[:,0], front[:,1], c="b") plt.axis("tight") plt.show()
def stat_basing_on_pop(pop, record_valid_only, optimal_in_theory=None): """ return some statstics basing on the populations :param pop: :param optimal_in_theory: :param record_valid_only: :return: * hyper_volume * spread * IGD * frontier_size * valid_rate """ if record_valid_only: vpop = filter(lambda p: p.fitness.correct, pop) if len(pop) == 0: return 0, 1, 1, 0, 0 if record_valid_only and len(vpop) == 0: return 0, 1, 1, 0, 0 front = _get_frontier(vpop) if record_valid_only else _get_frontier(pop) front_objs = [f.fitness.values for f in front] reference_point = [1] * len(front_objs[0]) hv = HyperVolume(reference_point).compute(front_objs) # did NOT use deap module calc # hv = -1 # TODO sort_front_by_obj0 = sorted(front, key=lambda f: f.fitness.values[1], reverse=True) first, last = sort_front_by_obj0[0], sort_front_by_obj0[-1] spread = diversity(front, first.fitness.values, last.fitness.values) # spread = -1 # TODO if optimal_in_theory is None: # not available!! IGD = -1 else: IGD = igd_metric(front, optimal_in_theory) frontier_size = len(front) if record_valid_only: valid_rate = len(vpop) / len(pop) else: valid_rate = -1 return round(hv, 3), round(spread, 3), IGD, frontier_size, valid_rate
def stat_basing_on_pop(pop, record_valid_only, optimal_in_theory=None): """ return some statstics basing on the populations :param pop: :param optimal_in_theory: :param record_valid_only: :return: * hyper_volume * spread * IGD * frontier_size * valid_rate """ vpop = filter(lambda p: p.fitness.correct, pop) if len(pop) == 0: return 0, 1, 1, 0, 0 if record_valid_only and len(vpop) == 0: return 0, 1, 1, 0, 0 front = _get_frontier(vpop) if record_valid_only else _get_frontier(pop) front_objs = [f.fitness.values for f in front] reference_point = [1] * len(front_objs[0]) hv = HyperVolume(reference_point).compute( front_objs) # did NOT use deap module calc sort_front_by_obj0 = sorted(front, key=lambda f: f.fitness.values[1], reverse=True) first, last = sort_front_by_obj0[0], sort_front_by_obj0[-1] spread = diversity(front, first, last) if optimal_in_theory is None: # not available!! IGD = -1 else: IGD = convergence(front, optimal_in_theory) frontier_size = len(front) valid_rate = len(vpop) / len(pop) return round(hv, 3), round(spread, 3), round(IGD, 3), frontier_size, valid_rate
pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) print(logbook.stream) return pop, logbook if __name__ == "__main__": with open("pareto_front/zdt1_front.json") as optimal_front_data: optimal_front = json.load(optimal_front_data) # Use 500 of the 1000 points in the json file optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2)) pop, stats = main() pop.sort(key=lambda x: x.fitness.values) print(stats) print("Convergence: ", convergence(pop, optimal_front)) print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1])) import matplotlib.pyplot as plt import numpy front = numpy.array([ind.fitness.values for ind in pop]) optimal_front = numpy.array(optimal_front) plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r") plt.scatter(front[:,0], front[:,1], c="b") plt.axis("tight") plt.show()
return pop, logbook if __name__ == "__main__": start = time.time() pop, stats = main() time_elapsed = time.time() - start pop_fit = numpy.array([ind.fitness.values for ind in pop]) pf = factory.get_problem(PROBLEM).pareto_front(n_pareto_points=MU) pop.sort(key=lambda x: x.fitness.values) print(stats) print("Time elapsed: ", time_elapsed) print("Convergence: ", convergence(pop, pf)) print("Diversity: ", diversity(pop, pf[0], pf[-1])) fig = plt.figure(figsize=(7, 7)) ax = fig.add_subplot(111) ax.scatter(pf[:, 0], pf[:, 1], marker="s", s=20, color='black', label="Ideal Pareto Front") ax.scatter(pop_fit[:, 0], pop_fit[:, 1], marker="o", s=20, color='red',
logbook.record(gen=gen, evals=len(invalid_ind), **record) print(logbook.stream) # print("Final population hypervolume is %f" % hypervolume(pop, [11.0, 11.0])) return pop, logbook if __name__ == "__main__": with open("pareto_front/zdt4_front.json") as optimal_front_data: optimal_front = json.load(optimal_front_data) # Use 500 of the 1000 points in the json file optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2)) pop, stats = main() pop.sort(key=lambda x: x.fitness.values) print(stats) print("Convergence: ", convergence(pop, optimal_front)) print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1])) import matplotlib.pyplot as plt import numpy front = numpy.array([ind.fitness.values for ind in pop]) optimal_front = numpy.array(optimal_front) plt.scatter(optimal_front[:, 0], optimal_front[:, 1], c="r") plt.scatter(front[:, 0], front[:, 1], c="b") plt.axis("tight") plt.show()
return population, halloffame, logbook if __name__ == "__main__": population, halloffame, logbook = main() pop = numpy.array([ind.fitness.values for ind in population]) population.sort(key=lambda x: x.fitness.values) print(halloffame) # print(logbook) print('DIVERSITY') print(diversity(population, pareto[0], pareto[-1])) print('CONVERGENCE') print(convergence(population, pareto)) fig = plt.figure() ax1 = fig.add_subplot() ax1.scatter(pareto[:, 0], pareto[:, 1], color="blue", label="Pareto") ax1.scatter(pop[:, 0], pop[:, 1], color="red", label="Population", marker="x") plt.xlabel('x1') plt.ylabel('x2') plt.title(ALGORITHM)
print "average hypervolume: ", sum_hypervolume /NTEST #print "average best cover front : ", sumHYPER_bestcover/NTEST print "\n" aux=ga.Cover2sets(optimalpareto, best_pareto_hypervolume) print "optimal coverage: ", aux print "\n" #rebuild Fronts optimal=[] best_pareto_hypervolume.sort(key=lambda x: x.fitness.values) #optimalpareto.sort(key=lambda x: x.fitness.values) for i in optimalpareto: optimal.append([i.fitness.values[0],i.fitness.values[1]]) print("Convergence: ", convergence(best_pareto_hypervolume, optimal)) print("Diversity: ", diversity(best_pareto_hypervolume, optimal[0], optimal[-1])) #converge and divers #print "best hypervolume front: ", aux[1] #print "best cover front : ", aux[2] # print "average best hypervolume front : ", sumCOVER_besthypervolume/NTEST # print "average best cover front : ", sumCOVER_bestcover/NTEST print "\n*************\n\n\n" print(lst_hyper) #save solution to file #my_world.WriteFileSolution(optimalpareto, 0) #0=Pareto; 1=Best Hypercube; 2=Best Cover #solution=my_world.GetFileSolution(0) #0=Pareto; 1=Best Hypercube; 2=Best Cover #my_world.WriteFileSolution(best_pareto_hypervolume, 1) #0=Pareto; 1=Best Hypercube; 2=Best Cover #solution=my_world.GetFileSolution(1) #0=Pareto; 1=Best Hypercube; 2=Best Cover
def mainnsga2(GEN,pb): def my_rand(): return random.random()*(V-U) - (V+U)/2 file = "pareto_front/"+pbs+"_front.json" optimal_front = json.load(open(file)) optimal_front = [optimal_front[i] for i in range(0, len(optimal_front), 2)] first_point_opt = optimal_front[0] last_point_opt = optimal_front[-1] creator.create("FitnessMax", base.Fitness, weights=(-1.0,-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) #@UndefinedVariable toolbox = base.Toolbox() toolbox.register("attr_float", my_rand) toolbox.register("individual", tools.initRepeat, creator.Individual,toolbox.attr_float, n=30) #@UndefinedVariable toolbox.register("evaluate", pb) # toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxSimulatedBinaryBounded, eta=0.5, low=U, up=V) toolbox.register("mutate", tools.mutPolynomialBounded, eta=0.5, low=U, up=V, indpb=1) toolbox.register("select", tools.selNSGA2) toolbox.register("selectTournament", tools.selTournamentDCD) obj_count = 0 dvrst_list = [] # init population pop = toolbox.population(n=N) for ind in pop: ind.fitness.values = toolbox.evaluate(ind) # sort using non domination sort (k is the same as n of population - only sort is applied) pop = toolbox.select(pop, k=N) for _ in xrange(GEN): #select parent pool with tournament dominated selection parent_pool = toolbox.selectTournament(pop, k=N) offspring_pool = map(toolbox.clone, parent_pool) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): if random.random() < 0.9: toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.1: toolbox.mutate(mutant) # evaluate offsprings for ind in offspring_pool: ind.fitness.values = toolbox.evaluate(ind) # extend base population with offsprings, pop is now 2N size pop.extend(offspring_pool) # sort and select new population pop = toolbox.select(pop, k=N) obj_count = tools.spea2_count + obj_count dvrst = diversity(pop, first_point_opt, last_point_opt) dvrst_list.append([obj_count , dvrst]) first_front = tools.sortFastND(pop, k=N)[0] return first_front,
def test010_(self): print("**** TEST {} ****".format(whoami())) NDIM = 30 BOUND_LOW, BOUND_UP = 0.0, 1.0 BOUND_LOW_STR, BOUND_UP_STR = '0.0', '1.0' RES_STR = '0.01' NGEN = 250 POPSIZE = 40 MU = 100 CXPB = 0.9 # Create variables var_names = [str(num) for num in range(NDIM)] myLogger.setLevel("CRITICAL") basis_set = [Variable.from_range(name, BOUND_LOW_STR, RES_STR, BOUND_UP_STR) for name in var_names] myLogger.setLevel("DEBUG") # Create DSpace thisDspace = DesignSpace(basis_set) # Create OSpace objective_names = ('obj1','obj3') objective_goals = ('Max', 'Min') this_obj_space = ObjectiveSpace(objective_names, objective_goals) mapping = Mapping(thisDspace, this_obj_space) # Statistics and logging stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.mean, axis=0) stats.register("std", np.std, axis=0) stats.register("min", np.min, axis=0) stats.register("max", np.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" creator.create("FitnessMin", base.Fitness, weights=(-1.0, -1.0)) toolbox = base.Toolbox() #--- Eval toolbox.register("evaluate", benchmarks.mj_zdt1_decimal) #--- Operators toolbox.register("mate", tools.cxSimulatedBinaryBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0) toolbox.register("mutate", tools.mutPolynomialBounded, low=BOUND_LOW, up=BOUND_UP, eta=20.0, indpb=1.0/NDIM) toolbox.register("select", tools.selNSGA2) # Create the population mapping.assign_individual(Individual2) mapping.assign_fitness(creator.FitnessMin) pop = mapping.get_random_population(POPSIZE) # Evaluate first pop invalid_ind = [ind for ind in pop if not ind.fitness.valid] toolbox.map(toolbox.evaluate, invalid_ind) logging.debug("Evaluated {} individuals".format(len(invalid_ind))) # Check that they are evaluated invalid_ind = [ind for ind in pop if not ind.fitness.valid] assert not invalid_ind pop = toolbox.select(pop, len(pop)) logging.debug("Crowding distance applied to initial population of {}".format(len(pop))) myLogger.setLevel("CRITICAL") for gen in range(1, NGEN): # Vary the population offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] logging.debug("Selected and cloned {} offspring".format(len(offspring))) #print([ind.__hash__() for ind in offspring]) #for ind in offspring: # print() pairs = zip(offspring[::2], offspring[1::2]) for ind1, ind2 in pairs: if random.random() <= CXPB: toolbox.mate(ind1, ind2) toolbox.mutate(ind1) toolbox.mutate(ind2) del ind1.fitness.values, ind2.fitness.values logging.debug("Operated over {} pairs".format(len(pairs))) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] processed_ind = toolbox.map(toolbox.evaluate, invalid_ind) logging.debug("Evaluated {} individuals".format(len(processed_ind))) #raise #for ind, fit in zip(invalid_ind, fitnesses): # ind.fitness.values = fit # Select the next generation population pop = toolbox.select(pop + offspring, MU) record = stats.compile(pop) logbook.record(gen=gen, evals=len(invalid_ind), **record) print(logbook.stream) ### with open(r"C:\Users\jon\git\deap1\examples\ga\pareto_front\zdt1_front.json") as optimal_front_data: optimal_front = json.load(optimal_front_data) # Use 500 of the 1000 points in the json file optimal_front = sorted(optimal_front[i] for i in range(0, len(optimal_front), 2)) pop.sort(key=lambda x: x.fitness.values) print(stats) print("Convergence: ", convergence(pop, optimal_front)) print("Diversity: ", diversity(pop, optimal_front[0], optimal_front[-1])) front = np.array([ind.fitness.values for ind in pop]) optimal_front = np.array(optimal_front) plt.scatter(optimal_front[:,0], optimal_front[:,1], c="r") print(front) plt.scatter(front[:,0], front[:,1], c="b") plt.axis("tight") #plt.savefig('C:\ExportDir\test1.png') plt.savefig('C:\\ExportDir\\out.pdf', transparent=True, bbox_inches='tight', pad_inches=0) #plt.show()
def mainspea2(GEN,pb,pbs): def my_rand(): return random.random() * (V - U) - (V + U) / 2 #------------------------------------------------- file = "pareto_front/"+pbs+"_front.json" optimal_front = json.load(open(file)) optimal_front = [optimal_front[i] for i in range(0, len(optimal_front), 2)] first_point_opt = optimal_front[0] last_point_opt = optimal_front[-1] # ----------------------------------------------- creator.create("FitnessMax", base.Fitness, weights=(-1.0, -1.0,)) creator.create("Individual", list, fitness=creator.FitnessMax) #@UndefinedVariable toolbox = base.Toolbox() toolbox.register("attr_float", my_rand) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=30) #@UndefinedVariable toolbox.register("evaluate", pb ) #benchmarks.zdt1 toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxSimulatedBinaryBounded, eta=0.5, low=U, up=V) toolbox.register("mutate", tools.mutPolynomialBounded, eta=0.5, low=U, up=V, indpb=1) toolbox.register("select", tools.selSPEA2) #binary tournament selection toolbox.register("selectTournament", tools.selTournament, tournsize=2) # Step 1 Initialization pop = toolbox.population(n=N) archive = [] curr_gen = 1 obj_count = 0 dvrst_list = [] while True: # Step 2 Fitness assignement for ind in pop: ind.fitness.values = toolbox.evaluate(ind) for ind in archive: ind.fitness.values = toolbox.evaluate(ind) obj_count = tools.spea2_count + obj_count # Step 3 Environmental selection archive = toolbox.select(pop + archive, k=Nbar) aa = [] for ee in archive: aa.append([ee.fitness.values[0],ee.fitness.values[1]]) dvrst = diversity(archive, first_point_opt, last_point_opt) hyprv = hv.compute(aa) dvrst_list.append([obj_count , float(hyprv)])# /float(dvrst) ]) # Step 4 Termination if curr_gen >= GEN: final_set = archive break # Step 5 Mating Selection mating_pool = toolbox.selectTournament(archive, k=N) offspring_pool = map(toolbox.clone, mating_pool) # Step 6 Variation # crossover 100% and mutation 6% for child1, child2 in zip(offspring_pool[::2], offspring_pool[1::2]): toolbox.mate(child1, child2) for mutant in offspring_pool: if random.random() < 0.06: toolbox.mutate(mutant) pop = offspring_pool curr_gen += 1 final_set.sort(key=lambda x: x.fitness.values) dvrst = diversity(final_set, first_point_opt, last_point_opt) return final_set,dvrst,dvrst_list,obj_count