diff --git a/RotTable.py b/RotTable.py index efdebce4cf42480c3be6c111f39fcce5c4cfa90e..e3486c1b21452cfed789090694fdb3d372884c98 100644 --- a/RotTable.py +++ b/RotTable.py @@ -63,7 +63,7 @@ class RotTable: ################### -table1 = RotTable() -print(table1.orta()) +# table1 = RotTable() +# print(table1.orta()) -print(table1.rot_table["AA"]) +# print(table1.rot_table["AA"]) diff --git a/population.py b/population.py index c1a1a413240574732ee27affacd13bb894d49c11..43eb61abd5be591e5865345d95a619e3bd434839 100644 --- a/population.py +++ b/population.py @@ -1,25 +1,32 @@ import random +from random import random, randint, randrange +from individu import Individu +from RotTable import RotTable +from croisement import * class Population: def __init__(self,n): - self.indiv=[Individu(rot_table.alea) for k in range (n)] + self.indiv=[Individu(RotTable()) for k in range (n)] self.n = n - def selection_duel_pondere(self,p=(self.n)//2): + + def selection_duel_pondere(self,p=None): + if p == None : + p = (self.n)//2 newself=[] - vu={} + vu=set() m=None t=None #méthode des duels pondérée: si x=10 et y=1, y a une chance sur 11 de passer while len(newself)<p: while m in vu: - m=random.randrange(0,len(self)) + m=randrange(0,len(self)) while t in vu: - t=random.randrange(0,len(self)) - x=self[m] - y=self[t] + t=randrange(0,len(self)) + x=self.indiv[m] + y=self.indiv[t] vu.add(t) vu.add(m) - p=uniform(0,1) + p=random(0,1) if p>x.score/(x.score+y.score): newself.append(y) else: @@ -27,18 +34,20 @@ class Population: return(newself) - def selection_duel(self,p=(self.n)//2): + def selection_duel(self,p=None): + if p == None : + p = (self.n)//2 newself=[] - vu={} + vu=set() t=None m=None while len(newself)<p: while m in vu: - m=random.randrange(0,len(self)) + m=randrange(0,len(self)) while t in vu: - t=random.randrange(0,len(self)) - x=self[m] - y=self[t] + t=randrange(0,len(self)) + x=self.indiv[m] + y=self.indiv[t] vu.add(t) vu.add(m) if x.score<=y.score: @@ -47,7 +56,9 @@ class Population: newself.append(y) return(newself) - def selection_par_rang(self, p=(self.n)//2): + def selection_par_rang(self,p = None): + if p == None : + p = (self.n)//2 liste_individus = self.indiv n = self.n @@ -58,7 +69,7 @@ class Population: echanger(tableau,debut,randint(debut,fin-1)) partition=debut for i in range(debut+1,fin): - if tableau[i] < tableau[debut]: + # if tableau[i] < tableau[debut]: if tableau[i].score<tableau[debut].score: partition+=1 echanger(tableau,i,partition) @@ -85,7 +96,7 @@ class Population: j+=1 #on doit prendre l'individu avec le jème score # print("individus selectionés", individus_selectionnes) - individus_selectionnes.append(liste[j-1]) + individus_selectionnes.append(liste_individus[j-1]) def modifier_population(self, liste_individus): self.n = len(liste_individus) @@ -94,30 +105,50 @@ class Population: self = modifier_population(self, individus_selectionnes) - def selection_proportionelle(self,p=(self.n)//2): + def selection_proportionelle(self,p= None): + if p == None : + p = (self.n)//2 newself=[] somme=0 - for indiv in self: + for indiv in self.indiv: somme=somme+indiv.score while len(newself)<p: - m=m=random.randrange(0,len(self)) - x=self[m] - p=uniform(0,1) + m=m=randrange(0,len(self)) + x=self.indiv[m] + p=random(0,1) if p<=x.score/somme: newself.append(x) return(newself) - def reproduction(self,selection=selection_duel,enfant=mixage,p=n//2): + def reproduction(self,selection=selection_duel,enfant=croisement_un_point, p = None): + if p == None : + p = (self.n)//2 newself=selection(self,p) while len(newself)<self.n: - m=random.randrange(0,len(newself)) - t=random.randrange(0,len(newself)) + m=randrange(0,len(newself)) + t=randrange(0,len(newself)) x=newself[m] y=newself[t] newself.append(enfant(x,y)) return(newself) - +# def afficher(popu): +# for individu in popu.indiv : +# print("\n individu \n") +# print(individu.table.rot_table) + +# def test(): +# popu = Population(4) +# print("\n POPULATION INITIALE \n") +# afficher(popu) +# popu.selection_duel() +# print("\n SELECTION DUEL \n") +# afficher(popu) +# popu.reproduction +# print("\n REPRODUCTION \n") +# afficher(popu) + +# test()