def selection(self): pos1 = generateNewValue(0, self.__param['popSize'] - 1) pos2 = generateNewValue(0, self.__param['popSize'] - 1) if (self.__population[pos1].fitness < self.__population[pos2].fitness): return pos1 else: return pos2
def __init__(self, problParam=None): self.__problParam = problParam self.__repres = [ generateNewValue(problParam['min'], problParam['max']) for _ in range(problParam['noDim']) ] self.__fitness = 0.0
def __init__(self, problParam=None): self.__problParam = problParam self.__fitness = 0.0 self.__repres = [ generateNewValue(1, self.__problParam['noNodes']) for _ in range(self.__problParam['noNodes']) ]
def mutationWithProbability(self, mutationProbability): # each gene it may or may not be swap with another one according to a probability for i in range(1, self.__problParam['noNodes']): probability = generateNewValue(0, 1) if probability < mutationProbability: pos = randint(1, self.__problParam['noNodes'] - 1) self.__repres[i], self.__repres[pos] = self.__repres[ pos], self.__repres[i]
def desen(ret): seed(1) # plot the function to be optimised noDim = 1 xref = [[generateNewValue(0, ret['noNodes'] - 1) for _ in range(noDim)] for _ in range(0, 1000)] xref.sort() yref = [fcEval(xi) for xi in xref] plt.ion() plt.plot(xref, yref, 'b-') plt.xlabel('x values') plt.ylabel('y = f(x) values') plt.show() # initialise de GA parameters # gaParam = {'popSize' : 10, 'noGen' : 3, 'pc' : 0.8, 'pm' : 0.1} # problem parameters # problParam = {'min' : MIN, 'max' : MAX, 'function' : fcEval, 'noDim' : noDim, 'noBits' : 8} # store the best/average solution of each iteration (for a final plot used to anlyse the GA's convergence) allBestFitnesses = [] allAvgFitnesses = [] generations = [] ga = GA(gaParam, problParam) ga.initialisation() ga.evaluation() for g in range(gaParam['noGen']): #plotting preparation allPotentialSolutionsX = [c.repres for c in ga.population] allPotentialSolutionsY = [c.fitness for c in ga.population] bestSolX = ga.bestChromosome().repres bestSolY = ga.bestChromosome().fitness allBestFitnesses.append(bestSolY) allAvgFitnesses.append( sum(allPotentialSolutionsY) / len(allPotentialSolutionsY)) generations.append(g) plotAFunction(xref, yref, allPotentialSolutionsX, allPotentialSolutionsY, bestSolX, [bestSolY], 'generation: ' + str(g)) #logic alg ga.oneGeneration() # ga.oneGenerationElitism() # ga.oneGenerationSteadyState() bestChromo = ga.bestChromosome() print('Best solution in generation ' + str(g) + ' is: x = ' + str(bestChromo.repres) + ' f(x) = ' + str(bestChromo.fitness)) plt.ioff() best, = plt.plot(generations, allBestFitnesses, 'ro', label='best') mean, = plt.plot(generations, allAvgFitnesses, 'bo', label='mean') plt.legend([best, (best, mean)], ['Best', 'Mean']) plt.show()
def binomial_crossover(self, c): newrepres = [] for i in range(len(self.__repres)): r = generateNewValue(0, len(self.__repres)) j = uniform(0.0, 1.0) if r == i or j <= self.__problParam['cr']: newrepres.append(c.__repres[i]) else: newrepres.append(self.__repres[i]) offspring = Chromosome(c.__problParam) offspring.repres = newrepres return offspring
def mutation(self): pos = randint(0, len(self.__repres) - 1) self.__repres[pos] = generateNewValue(self.__problParam['min'], self.__problParam['max'])
def mutation(self): position = randint(0, self.__problParam['retea']['noNodes'] - 1) # iau o gena la intamplare si pun # o alta valoare acolo self.__repres[position] = self.__repres[generateNewValue( 0, self.__problParam['retea']['noNodes'] - 1)]
def mutation(self): pos = randint(0, self.__problParam['retea']['noNodes'] - 1) self.__repres[pos] = self.__repres[generateNewValue( 0, self.__problParam['retea']['noNodes'] - 1)]