Skip to content

Commit b92db57

Browse files
committed
Lots of things
1 parent f4dbd40 commit b92db57

18 files changed

+30273
-41
lines changed
5 Bytes
Binary file not shown.

__pycache__/particle.cpython-35.pyc

1.53 KB
Binary file not shown.

abc.py

Whitespace-only changes.
Binary file not shown.
300 Bytes
Binary file not shown.
1.03 KB
Binary file not shown.

evolucionary/evolution_strategies.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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()

evolucionary/functions.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import math
2+
3+
from abc import ABCMeta, abstractmethod
4+
5+
6+
class AFunction:
7+
__metaclass__ = ABCMeta
8+
9+
upper_bound = 100
10+
lower_bound = -100
11+
12+
@abstractmethod
13+
def calculate_fitness(self, position):
14+
pass
15+
16+
17+
class Sphere(AFunction):
18+
19+
def __init__(self):
20+
AFunction.upper_bound = 100
21+
AFunction.lower_bound = -100
22+
23+
def calculate_fitness(self, position_list):
24+
solution = 0
25+
for position in position_list:
26+
solution += position ** 2
27+
return solution
28+
29+
30+
class Rastrigin(AFunction):
31+
32+
def __init__(self):
33+
AFunction.upper_bound = 5.12
34+
AFunction.lower_bound = -5.12
35+
36+
def calculate_fitness(self, position_list):
37+
solution = 0
38+
for position in position_list:
39+
solution += (position ** 2 + 10 - 10 * math.cos(2 * math.pi * position))
40+
return solution
41+
42+
43+
class Rosenbrocks(AFunction):
44+
45+
def __init__(self):
46+
AFunction.upper_bound = 30
47+
AFunction.lower_bound = -30
48+
49+
def calculate_fitness(self, position_list):
50+
solution = 0
51+
for i in range(0, len(position_list) - 1):
52+
solution += (100 * ((position_list[i] ** 2 - position_list[i + 1]) ** 2) + ((position_list[i] - 1) ** 2))
53+
return solution

evolucionary/parameters.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
iterations_number = 10000
2+
num_of_individuos = 1
3+
number_of_offspring = 5
4+
probability_of_recombination = 0.6
5+
dimensions = 10

evolucionary/particle.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import copy
2+
3+
4+
class Particle():
5+
6+
def __init__(self, function_wrapper, positions, strategy_parameters):
7+
self.function_wrapper = function_wrapper
8+
self.current_position = positions
9+
self.strategy_parameters = strategy_parameters
10+
self.best_particle = self.current_position
11+
self.fitness = 1.0
12+
13+
def aply_function_on_current_position(self):
14+
self.fitness = self.function_wrapper.calculate_fitness(self.current_position)
15+
16+
def clone_particle(self):
17+
clone_object = copy.copy(self)
18+
clone_object.current_position = copy.deepcopy(self.current_position)
19+
clone_object.fitness = copy.deepcopy(self.fitness)
20+
return clone_object

fish.py

-31
This file was deleted.

fish_school_search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import random
44
import math
55

6-
from fish import Fish
6+
from particle import Fish
77
from parameters import num_of_individuos, dimensions, iterations_number
88

99

0 commit comments

Comments
 (0)