def DummyClearing(population, fit_funct, dist_funct, clear_radius, niche_cap): # evaluate the fitness clearing for ind_idx in range(len(population)): # evaluate the fitness of the central_niche individual population[ind_idx].fitness.values = fit_funct(population[ind_idx]) # List of best fitness individual, the list is sort by fitness value: [ [index 1, 0.12], ..., [index N, 0.92] ] BestNicheInd = [[0, 0]] for oth in range(len(population)): if dist_funct(NormBinSeqToNum(population[ind_idx]), NormBinSeqToNum(population[oth])) < clear_radius: population[oth].fitness.values = fit_funct( population[oth] ) # You could verify if it is already update to not evaluate it again # evaluate if the individual has a best fitness if (population[oth].fitness.values[0] > BestNicheInd[0][1]): if (len(BestNicheInd) >= niche_cap): # reset the clearing fitness of the lest best ind in the BestNicheList if its fitness is less than a new individual of the niche population[BestNicheInd.pop(0) [0]].fitclearing.values = 0, # assign the clearing fitness population[oth].fitclearing.values = population[ oth].fitness.values BestNicheInd.append( [oth, population[oth].fitness.values[0]]) BestNicheInd.sort(key=lambda tup: tup[1]) else: population[ oth].fitclearing.values = 0, # reset the clearing fitness BestNicheInd.clear() return population
def animate(frame, population=population, toolbox=toolbox): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0) for ind in offspring: ind.fitshare.values = toolbox.evalfitsh(ind, offspring) population = toolbox.select(offspring, k=len(population)) xdata=np.asarray([NormBinSeqToNum(i) for i in population]) ydata=np.asarray([FitnessFunction(i)[0] for i in population]) scatt.set_array(np.c_(xdata,ydata))
def Sharing(population, fit_funct, dist_funct, share_radius, shape_factor=1, scaling_factor=1): """ The sharing techinque reduces the fitness of each individual proportionally at the population density of the belonging niche. The niche size is identify by the share radius parameter. This parameter encloses the information on the estimated distance between two peak. """ for ind in population: niche = 0 ind.fitness.values = fit_funct(ind) # evaluate the distance from the individual and the rest of population and update the niche factor for others in population: dist = dist_funct(NormBinSeqToNum(ind), NormBinSeqToNum(others)) if dist < share_radius: niche = niche + (1 - (dist / share_radius)**shape_factor) # evaluate and assign the fit sharing value ind.fitshare.values = ((ind.fitness.values[0]**scaling_factor) / niche),
toolbox.register("select", tools.selTournament, tournsize=5, fit_attr='fitshare') rapresentative=toolbox.population(n=len(PeackValues)) for ind,rap in zip(rapresentative,PeackValues): ind.value=rap[0] ind.fitness=rap[1] csq_dev=[] for num_gen in range(MAX_NUM_GEN): population = toolbox.population(n=NUM_IND) for gen in range(num_gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0.01) for ind in offspring: ind.fitshare.values = toolbox.evalfitsh(ind, offspring) population = toolbox.select(offspring, k=len(population)) for ind in population: ind.value=NormBinSeqToNum(ind) ind.fitness.values=toolbox.evalfit(ind) csq_dev.append(ChiSquareLike(population, rapresentative, nich_radius=0.1)) plt.plot([i for i in range(MAX_NUM_GEN)],csq_dev) plt.title('generation VS ChiSquareLike') plt.xlabel('generation') plt.ylabel('ChiSquareLike') plt.show()
def himmelblau(individual): coord=SplitBinSeq(individual,2) x=NormBinSeqToNum(coord[0],5) y=NormBinSeqToNum(coord[1],5) return ( ( x**2 + y - 11 )**2 + ( x + y**2 -7 )**2 )
toolbox.register("evalfit", FitnessFunction) toolbox.register("evalfitsh", FitSharing, fitFunction=toolbox.evalfit, distanceFunction=NormHamming2, sigma=0.2) toolbox.register("attr_bool", random.randint, 0, 1) toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=IND_SIZE) toolbox.register("population", tools.initRepeat, list, toolbox.individual) toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selStochasticUniversalSampling, fit_attr='fitshare') # toolbox.register("select", tools.selTournament, tournsize=3, fit_attr='fitshare') population = toolbox.population(n=NUM_IND) AllBinSeq = GenBinSeq(IND_SIZE) AllPossibleFits = [FitnessFunction(i)[0] for i in AllBinSeq] NumSequences=[NormBinSeqToNum(i) for i in AllBinSeq] fig, ax = plt.subplots() ax.plot(NumSequences, AllPossibleFits, '-r') scatt = ax.scatter([NormBinSeqToNum(i) for i in population],[FitnessFunction(i)[0] for i in population]) def animate(frame, population=population, toolbox=toolbox): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0) for ind in offspring: ind.fitshare.values = toolbox.evalfitsh(ind, offspring) population = toolbox.select(offspring, k=len(population)) xdata=np.asarray([NormBinSeqToNum(i) for i in population]) ydata=np.asarray([FitnessFunction(i)[0] for i in population]) scatt.set_array(np.c_(xdata,ydata)) ani = animation.FuncAnimation(fig, animate, frames=[i for i in range(NUM_GEN)], blit=True)
def eggcrate(individual): coord=SplitBinSeq(individual,2) x=NormBinSeqToNum(coord[0],5) y=NormBinSeqToNum(coord[1],5) return ( x**2 + y**2 + 25*( (sin(x))**2 + (sin(y))**2 ) )
def FnctB(individual): x=NormBinSeqToNum(individual) return ( exp( -2*(log(2))*(( (x-0.08)/(0.834) )**2) ) )*( FnctA(individual)[0] ),
def FnctA(individual): x=NormBinSeqToNum(individual) return ( sin( 5*pi*( (x)**(3/4) - 0.05 ) ) )**6,
toolbox.register("mate", tools.cxTwoPoint) toolbox.register("mutate", tools.mutFlipBit, indpb=0.05) toolbox.register("select", tools.selStochasticUniversalSampling, fit_attr='fitshare') # toolbox.register("select", tools.selTournament, tournsize=5, fit_attr='fitshare') BenchMark = [] for num_gen in range(MAX_NUM_GEN): population = toolbox.population(n=NUM_IND) for gen in range(num_gen): offspring = algorithms.varAnd(population, toolbox, cxpb=0.1, mutpb=0.01) for ind in offspring: ind.fitshare.values = toolbox.evalfitsh(ind, offspring) population = toolbox.select(offspring, k=len(population)) final_pop = list([[NormBinSeqToNum(i), toolbox.evalfit(i)[0]] for i in population]) BenchMark.append(MaxPeakRatio(final_pop, MaxFnctA)) plt.scatter([i for i in range(MAX_NUM_GEN)], BenchMark) plt.title('generation VS MaxPeakRatio') plt.xlabel('generation') plt.ylabel('MaxPeakRatio') plt.show()
ScatBinFct(population, FitnessFunction) plt.title('First generation') plt.xlabel('Individuals') plt.ylabel('Fitness') plt.grid(True) # GAs evaluation for gen in range(NUM_GEN): # Mating offspring = algorithms.varAnd(population, toolbox, cxpb=CxProbability, mutpb=MxProbability) # evaluate the individual postion for both offspring and parents populations for par, child in zip(population, offspring): par.position = NormBinSeqToNum(par) child.position = NormBinSeqToNum(child) # Evaluation fits = toolbox.map(toolbox.evalfit, offspring) for fit, ind in zip(fits, offspring): ind.fitness.values = fit # Selection population = toolbox.select(population, offspring) for (ind, idx) in zip(population, range(len(population))): ind.position = NormBinSeqToNum(ind) MinFitness.append(min([ind.fitness.values[0] for ind in population])) MaxFitness.append(max([ind.fitness.values[0] for ind in population])) AvgFitness.append(mean([ind.fitness.values[0] for ind in population]))