|
| 1 | +import random |
| 2 | +import math |
| 3 | +import copy |
| 4 | + |
| 5 | +from functions import Sphere |
| 6 | +from particle import Particle |
| 7 | +from parameters import num_of_individuos, dimensions, iterations_number, number_of_offspring |
| 8 | +# u -> num_of_individuos / parental population size |
| 9 | + |
| 10 | + |
| 11 | +class ES(): |
| 12 | + |
| 13 | + def __init__(self, function_wrapper): |
| 14 | + self.function = function_wrapper |
| 15 | + |
| 16 | + def search(self): |
| 17 | + self._initialize_population() |
| 18 | + |
| 19 | + for iterations in range(iterations_number): |
| 20 | + for particle in self.population: |
| 21 | + particle.aply_function_on_current_position() |
| 22 | + |
| 23 | + for i in range(0, number_of_offspring): |
| 24 | + parents = self._select_random_parents() |
| 25 | + offspring = self._crossover_operator(parents) |
| 26 | + new_individuo = self.mutate_offspring(offspring) |
| 27 | + new_individuo.aply_function_on_current_position() |
| 28 | + self.population.append(new_individuo) |
| 29 | + # no nosso caso o melhor |
| 30 | + best = self.select_bests_for_population() |
| 31 | + self.population = [] |
| 32 | + self.population.append(best) |
| 33 | + |
| 34 | + def select_bests_for_population(self): |
| 35 | + best = min(self.population, key = lambda individuo : individuo.fitness) |
| 36 | + return best |
| 37 | + |
| 38 | + def _initialize_population(self): |
| 39 | + self.population = [] |
| 40 | + for i in range(num_of_individuos): |
| 41 | + random_position = [self._get_random_number() for index in range(dimensions)] |
| 42 | + random_strategies = [random.random() for index in range(dimensions)] |
| 43 | + p = Particle(self.function, random_position, random_strategies) |
| 44 | + self.population.append(p) |
| 45 | + |
| 46 | + def _get_random_number(self): |
| 47 | + return ( |
| 48 | + self.function.lower_bound + random.uniform(0, 1) * (self.function.upper_bound - self.function.lower_bound) |
| 49 | + ) |
| 50 | + |
| 51 | + def _select_random_parents(self): |
| 52 | + if num_of_individuos < 2: |
| 53 | + return [self.population[0], self.population[0]] |
| 54 | + else: |
| 55 | + rand = random.randint(len(self.population)) |
| 56 | + new_pop = copy.deepcopy(self.population) |
| 57 | + new_pop.remove(self.population[rand]) |
| 58 | + rand2 = random.randint(len(new_pop)) |
| 59 | + return [self.population[rand], new_pop[rand2]] |
| 60 | + |
| 61 | + # Create offspring through application of crossover operator on parent genotypes and strategy parameters; |
| 62 | + def _crossover_operator(self, parents): |
| 63 | + # simples |
| 64 | + position = [] |
| 65 | + strategy = [] |
| 66 | + parent1 = parents[0] |
| 67 | + parent2 = parents[1] |
| 68 | + for i in range(dimensions): |
| 69 | + if random.random() > 0.5: |
| 70 | + position.append(parent2.current_position[i]) |
| 71 | + strategy.append(parent2.strategy_parameters[i]) |
| 72 | + else: |
| 73 | + position.append(parent1.current_position[i]) |
| 74 | + strategy.append(parent1.strategy_parameters[i]) |
| 75 | + offspring = Particle(self.function, position, strategy) |
| 76 | + return offspring |
| 77 | + |
| 78 | + def mutate_offspring(self, offspring): |
| 79 | + current_position = [] |
| 80 | + strategy_parameters = [] |
| 81 | + for i in range(dimensions): |
| 82 | + current_position.append(self.mutation(offspring.current_position[i], offspring.strategy_parameters[i])) |
| 83 | + strategy_parameters.append(self.adapt_stepsize()) |
| 84 | + new_individuo = Particle(self.function, current_position, strategy_parameters) |
| 85 | + return new_individuo |
| 86 | + |
| 87 | + def mutation(self, value, q): |
| 88 | + rand = -1 if random.random() < 0.5 else 1 |
| 89 | + return value + rand*self.gauss(q) |
| 90 | + |
| 91 | + def gauss(self, q): |
| 92 | + x = random.random() * q * 3 |
| 93 | + n = (1.0 / math.sqrt(q * q * math.pi)) * math.exp((x * x / q * q) * (-1 / 2)) |
| 94 | + return n |
| 95 | +# |
| 96 | +# # def crossover_local_intermediare_recombination(parents): |
| 97 | +# # r = random.random() |
| 98 | +# # new_position = [] |
| 99 | +# # for i in range(len(parents.current_position)): |
| 100 | +# |
| 101 | + def adapt_stepsize(self): |
| 102 | + pass |
| 103 | +# # if self.adap in ['1/5th','1/5th-rule','1/5-Erfolgsregel','Erfolgsregel']: |
| 104 | +# # improv=0 |
| 105 | +# # for fdude in self.F1: |
| 106 | +# # if fdude.isbetter(self.F0[0]): improv+=1 |
| 107 | +# # if improv > self.l/5: # alternative: if improv > self.l/5+1 |
| 108 | +# # self.mstep*=self.adapf |
| 109 | +# # else: |
| 110 | +# # self.mstep/=self.adapf |
| 111 | +# # elif self.adap in ['const','const.','constant']: |
| 112 | +# # pass |
| 113 | +# |
| 114 | +# # def mutate_fixstep(self,stepsize=None,uCS=True,mirrorbds=True): |
| 115 | +# # # mutation as jump into a random direction with fixed step size |
| 116 | +# # if stepsize is None: stepsize=self.mstep |
| 117 | +# # step=randn(self.ng); step=stepsize*step/sqrt(np.sum(step*step)) |
| 118 | +# # if uCS: |
| 119 | +# # DNA=self.get_uDNA(); DNA+=step; self.set_uDNA(DNA) |
| 120 | +# # else: |
| 121 | +# # self.DNA+=step |
| 122 | +# |
| 123 | +# |
| 124 | +r = ES(Sphere()) |
| 125 | +r.search() |
0 commit comments