-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopulation_methods.py
71 lines (56 loc) · 2.34 KB
/
Population_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
'''
POPULATION METHODS (camelCase implementations)
'''
# Funzioni fittizie per il momento.
def energyCost( xSolution: list, individuals: list ) -> int:
return None
def tweakOp( xCurrent: list, individuals: list ) -> int:
return None
# Evolutionary Algorithm (mu, lambda)
# mu: numero di genitori scelti per creare le nuove generazioni - i migliori
# lambda: numero della popolazione iniziale
def evolutionaryAlgorithmMuLam(
initialPopulation: list,
mu: int,
lam: int,
tMax: int,
individuals: list
) -> list:
'''
Function that applies the evolutionary algorithm using lam (lambda)
random individuals and mu best individuals at each generation
to generate other new individuals.
Input:
- initialPopulation: a candidate initial list of solutions
- mu: mu value
- lam: lambda value
- tMax: number of generations to use to found the solution
- individuals: list containing all the individuals studied
Output:
- Best solution found in the entire population.
'''
# Initialize a list containing all the population used for each generation
population = initialPopulation
# Initialize a list to store the best solution found
best = []
t = 0
while t <= tMax:
# Check in the population the best individual by checking the fitness
# of each individual of the candidate solution analysed: if it finds
# a better individual with a less fitness rate, it becomes the new best
# one found
for ind in population:
if ( best == [] or energyCost(ind, individuals) < energyCost(best, individuals)):
best = ind
# Ordering the current population based on their fitness value
# Creates a new list containing 2-tuples of individuals and their
# respective fitness value
Q = [ (ind, energyCost(ind, individuals)) for ind in population ]
Q.sort(key=lambda x: x[1], reverse=True)
# Take the mu individuals with the best fitness values in the population
Q = Q[:mu]
# Eliminating all the individuals of the population and creating
# new individuals mutating the best ones
population = [ tweakOp(ind[0], individuals) for ind in Q for _ in range(int(lam/mu)) ]
t += 1
return best