Saataa Andagii !
This commit is contained in:
parent
4f43c149ee
commit
d39467f070
3 changed files with 261 additions and 67 deletions
|
@ -51,6 +51,43 @@ def new_individual(): # -> set(str)
|
|||
"chromozome": ""
|
||||
}
|
||||
|
||||
def randomize(individual, l) -> str:
|
||||
"""
|
||||
Methode qui change la valeur d'un chromozome pour une valeur aléatoire
|
||||
"""
|
||||
new = ""
|
||||
for i in range(l):
|
||||
new += chr(random.randint(0, 255))
|
||||
individual["chromozome"] = new
|
||||
|
||||
def fitness1(individual, pm) -> int:
|
||||
"""
|
||||
Première methode de fitness, fait la somme des différences entre les codages des caractères des deux chaînes.
|
||||
"""
|
||||
sum = 0
|
||||
for i in range(len(individual["chromozome"])):
|
||||
sum += abs(ord(individual["chromozome"][i]) - ord(pm[i]))
|
||||
return -sum
|
||||
|
||||
def fitness2(individual, pm, alpha) -> int:
|
||||
"""
|
||||
Deuxième methode de fitness qui compte les caractères bien placés et mal placés et qui renvoie un int pondéré par alpha
|
||||
"""
|
||||
match = 0
|
||||
missed_placed = 0
|
||||
for i in range(len(individual["chromozome"])):
|
||||
if individual["chromozome"][i] == pm[i]:
|
||||
match += 1
|
||||
else:
|
||||
missed_placed += 1
|
||||
return match + alpha * missed_placed
|
||||
|
||||
def fitness3(individual, pm) -> int:
|
||||
"""
|
||||
Troisième methode de fitness qui utilise la distance de Levenshtein
|
||||
"""
|
||||
return -Levenshtein.distance(individual["chromozome"], pm)
|
||||
|
||||
def get_fitness(population, individual) -> int:
|
||||
match population["fm"]:
|
||||
case 1:
|
||||
|
@ -62,6 +99,29 @@ def get_fitness(population, individual) -> int:
|
|||
case _:
|
||||
return fitness1(individual, population["pm"])
|
||||
|
||||
def get_best(population):
|
||||
"""
|
||||
Methode qui renvoie le meilleur individu de la population
|
||||
"""
|
||||
fitness_list = []
|
||||
for individual in population["individuals"]:
|
||||
fitness_list.append(get_fitness(population, individual))
|
||||
return population["individuals"][max_i(fitness_list)]
|
||||
|
||||
def print_best(population) -> None:
|
||||
"""
|
||||
Methode qui affiche le meilleur individu de la population
|
||||
"""
|
||||
print(get_best(population)["chromozome"])
|
||||
|
||||
def mutate(individual) -> None:
|
||||
"""
|
||||
Methode qui change un des caractères du chromozome
|
||||
"""
|
||||
new = list(individual["chromozome"])
|
||||
new[random.randint(0, len(new) - 1)] = chr(random.randint(0, 255))
|
||||
individual["chromozome"] = "".join(new)
|
||||
|
||||
def select(population) -> None:
|
||||
"""
|
||||
Methode qui sélectionne les meilleurs individus
|
||||
|
@ -106,21 +166,6 @@ def mutate_pop(population) -> None:
|
|||
mutate(population["individuals"][to_mutate])
|
||||
mutated.append(to_mutate)
|
||||
|
||||
def get_best(population):
|
||||
"""
|
||||
Methode qui renvoie le meilleur individu de la population
|
||||
"""
|
||||
fitness_list = []
|
||||
for individual in population["individuals"]:
|
||||
fitness_list.append(get_fitness(population, individual))
|
||||
return population["individuals"][max_i(fitness_list)]
|
||||
|
||||
def print_best(population) -> None:
|
||||
"""
|
||||
Methode qui affiche le meilleur individu de la population
|
||||
"""
|
||||
print(get_best(population)["chromozome"])
|
||||
|
||||
def run(population) -> None:
|
||||
"""
|
||||
Boucle principale
|
||||
|
@ -130,48 +175,3 @@ def run(population) -> None:
|
|||
reproduct(population)
|
||||
mutate_pop(population)
|
||||
print_best(population)
|
||||
|
||||
def randomize(individual, l) -> str:
|
||||
"""
|
||||
Methode qui change la valeur d'un chromozome pour une valeur aléatoire
|
||||
"""
|
||||
new = ""
|
||||
for i in range(l):
|
||||
new += chr(random.randint(0, 255))
|
||||
individual["chromozome"] = new
|
||||
|
||||
def fitness1(individual, pm) -> int:
|
||||
"""
|
||||
Première methode de fitness, fait la somme des différences entre les codages des caractères des deux chaînes.
|
||||
"""
|
||||
sum = 0
|
||||
for i in range(len(individual["chromozome"])):
|
||||
sum += abs(ord(individual["chromozome"][i]) - ord(pm[i]))
|
||||
return -sum
|
||||
|
||||
def fitness2(individual, pm, alpha) -> int:
|
||||
"""
|
||||
Deuxième methode de fitness qui compte les caractères bien placés et mal placés et qui renvoie un int pondéré par alpha
|
||||
"""
|
||||
match = 0
|
||||
missed_placed = 0
|
||||
for i in range(len(individual["chromozome"])):
|
||||
if individual["chromozome"][i] == pm[i]:
|
||||
match += 1
|
||||
else:
|
||||
missed_placed += 1
|
||||
return match + alpha * missed_placed
|
||||
|
||||
def fitness3(individual, pm) -> int:
|
||||
"""
|
||||
Troisième methode de fitness qui utilise la distance de Levenshtein
|
||||
"""
|
||||
return -Levenshtein.distance(individual["chromozome"], pm)
|
||||
|
||||
def mutate(individual) -> None:
|
||||
"""
|
||||
Methode qui change un des caractères du chromozome
|
||||
"""
|
||||
new = list(individual["chromozome"])
|
||||
new[random.randint(0, len(new) - 1)] = chr(random.randint(0, 255))
|
||||
individual["chromozome"] = "".join(new)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue