This commit is contained in:
Lukian 2024-11-05 11:16:30 +01:00
parent 6b28ba2331
commit 3e144acb88
2 changed files with 93 additions and 18 deletions

View file

@ -1,21 +1,85 @@
# Librairie du projet en version orientée objet
import random
def min_i(array: list[int]) -> int:
min_val = array[0]
min_i = 0
for i in range(len(array)):
if array[i] < min_val:
min_val = array[i]
min_i = i
return min_i
def max_i(array: list[int]) -> int:
max_val = array[0]
max_i = 0
for i in range(len(array)):
if array[i] > max_val:
max_val = array[i]
max_i = i
return max_i
class Population:
"""
Classe qui représente notre population d'individuts
"""
def __init__(self, ng, l, n, ts, pm, tm):
self.individuals = [Individual().randomize(l) for _ in range(n)]
def __init__(self, pm, ng, n, ts, tm):
self.individuals = [Individual() for _ in range(n)]
for individual in self.individuals:
individual.randomize(len(pm))
self.pm = pm
self.ng = ng
self.l = l
self.l = len(pm)
self.n = n
self.ts = ts
self.pm = pm
self.tm = tm
def select(self) -> None:
# TODO
pass
fitness_list = []
for individual in self.individuals:
fitness_list.append(individual.fitness(self.pm))
for i in range(int(1 - (self.ts * self.l))):
least = min_i(self.individuals)
fitness.pop(least)
self.individuals.pop(least)
def reproduct(self) -> None:
new = []
while len(self.individuals) != self.n:
cut = random.randint(int(self.l / 3), int(2 * self.l / 3))
indivi_1 = self.individuals[random.randint(0, len(self.individuals) - 1)]
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
while indivi_1 == indivi_2:
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
new_chromozome = indivi_1.getChromozome()[:cut] + indivi_2.getChromozome()[cut:]
new.append(Individual.setChromozome(new_chromozome))
self.individuals += new
def mutate(self) -> None:
mutated = []
for i in range(int(self.tm * self.n)):
to_mutate = self.individuals[random.randint(0, self.n - 1)]
while to_mutate in mutated:
to_mutate = self.individuals[random.randint(0, self.n - 1)]
to_mutate.mutate()
mutated.append(to_mutate)
def print_best(self) -> None:
fitness_list = []
for individual in self.individuals:
fitness_list.append(individual.fitness(self.pm))
print(self.individuals[max_i(fitness_list)].getChromozome)
def run(self) -> None:
for i in range(self.ng):
print("select")
self.select()
print("reproduct")
self.reproduct()
print("mutate")
self.mutate()
self.print_best()
class Individual:
"""
@ -26,15 +90,24 @@ class Individual:
def setChromozome(self, c: str) -> None:
self.chromozome = c
def getChromozome(self) -> str:
return self.chromozome
def randomize(self, l: int) -> None:
# TODO
pass
def randomize(self, l) -> None:
new = ""
for i in range(l):
new += chr(random.randint(0, 255))
self.chromozome = new
def fitness(self, pm: str) -> float:
# TODO
pass
def fitness(self, pm) -> float:
sum = 0
for i in range(len(self.chromozome)):
sum += abs(ord(self.chromozome[i]) - ord(pm[i]))
return -sum
def mutate(self) -> None:
new = list(len(self.chromozome))
new[random.randint(0, len(self.chromozome))] = chr(random.randint(0, 255))
self.chromozome = "".join(new)
def mutate(self, tm: float) -> None:
# TODO
pass

View file

@ -1,11 +1,13 @@
# fichier d'exemple 'run' du projet
# project libs importations
import lib.ultra_mastermind_obj
import lib.ultra_mastermind_imp
import lib.ultra_mastermind_obj as libobj
import lib.ultra_mastermind_imp as libomp
def main() -> None:
pass
pop = libobj.Population('saucisse', 1000, 100, 0.3, 0.3)
pop.run()
if __name__ == "__main__":
main()