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/croisement.py b/croisement.py index a59546f35101170f94063bcc93992754ca4c402a..48a399f3e03e49d46867b53a1695e99657f13c67 100644 --- a/croisement.py +++ b/croisement.py @@ -1,5 +1,6 @@ import numpy from RotTable import RotTable +from individu import Individu ROT_TABLE = {\ "AA": [35.62, 7.2, -154, 0.06, 0.6, 0],\ @@ -22,17 +23,17 @@ ROT_TABLE = {\ def croisement_un_point(parent1, parent2): - enfant1 = RotTable() - enfant2 = RotTable() + enfant1 = Individu(RotTable()) + enfant2 = Individu(RotTable()) comp = 0 point_crois= numpy.random.random_integers(0,16) for doublet in ROT_TABLE: if comp < point_crois: - enfant1.rot_table[doublet] = parent1.rot_table[doublet] - enfant2.rot_table[doublet] = parent2.rot_table[doublet] + enfant1.table.rot_table[doublet] = parent1.table.rot_table[doublet] + enfant2.table.rot_table[doublet] = parent2.table.rot_table[doublet] else : - enfant1.rot_table[doublet] = parent2.rot_table[doublet] - enfant2.rot_table[doublet] = parent1.rot_table[doublet] + enfant1.table.rot_table[doublet] = parent2.table.rot_table[doublet] + enfant2.table.rot_table[doublet] = parent1.table.rot_table[doublet] comp += 1 return enfant1, enfant2 diff --git a/individu.py b/individu.py index 88b9dc13f204b0ff18f866208e40ed8b8e3eab31..0bc68e4926298da3172bec0b670ea360435d13ca 100644 --- a/individu.py +++ b/individu.py @@ -24,7 +24,7 @@ class Individu(): first_name = brin[0] last_name = brin[-1] - rot_computed = self.table.Rot_Table[last_name+first_name] + rot_computed = self.table.rot_table[last_name+first_name] rot_traj = first_nucleotide - last_nucleotide # print(rot_traj) # print(rot_computed) diff --git a/population.py b/population.py index 69e6bd8d9ec53803fc22e6753efb9996d0c0a7a5..f3daad1c6d133392c6474d0923951837dfe7c6fb 100644 --- a/population.py +++ b/population.py @@ -1,53 +1,69 @@ 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 modifier_population(self, liste_individus): + """Fonction qui renvoie une nouvelle instance de population a partir d'une liste d'individus""" + self.n = len(liste_individus) + self.indiv = liste_individus + return self + + def selection_duel_pondere(self,p=None): + if p == None : + p = (self.n)//2 newself=[] - vu={} - 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 + vu=set() + m=randrange(0,self.n) + t=randrange(0,self.n) #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,self.n) while t in vu: - t=random.randrange(0,len(self)) - x=self[m] - y=self[t] + t=randrange(0,self.n) + x=self.indiv[m] + y=self.indiv[t] vu.add(t) vu.add(m) - p=uniform(0,1) + p=random() if p>x.score/(x.score+y.score): newself.append(y) else: newself.append(x) - return(newself) + self = self.modifier_population(newself) - def selection_duel(self,p=(self.n)//2): + def selection_duel(self,p=None): + if p == None : + p = (self.n)//2 newself=[] - vu={} - t=None - m=None + vu=set() + t=randrange(0,self.n) + m=randrange(0,self.n) while len(newself)<p: while m in vu: - m=random.randrange(0,len(self)) + m=randrange(0,self.n) while t in vu: - t=random.randrange(0,len(self)) - x=self[m] - y=self[t] + t=randrange(0,self.n) + x=self.indiv[m] + y=self.indiv[t] vu.add(t) vu.add(m) if x.score<=y.score: newself.append(x) else: newself.append(y) - return(newself) + self = self.modifier_population(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 +74,11 @@ class Population: echanger(tableau,debut,randint(debut,fin-1)) partition=debut for i in range(debut+1,fin): +<<<<<<< HEAD #if tableau[i] < tableau[debut]: +======= + # if tableau[i] < tableau[debut]: +>>>>>>> ea595ada3becbb697c8735a8fbdc55f04e1606a2 if tableau[i].score<tableau[debut].score: partition+=1 echanger(tableau,i,partition) @@ -85,39 +105,62 @@ 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]) - - def modifier_population(self, liste_individus): - self.n = len(liste_individus) - self.indiv = liste_individus - return self + individus_selectionnes.append(liste_individus[j-1]) - self = modifier_population(self, individus_selectionnes) + self = self.modifier_population(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, self.n) + x=self.indiv[m] + p=random() if p<=x.score/somme: newself.append(x) - return(newself) - - def reproduction(self,selection=selection_duel,enfant=mixage,p=n//2): - newself=selection(self,p) - while len(newself)<self.n: - m=random.randrange(0,len(newself)) - t=random.randrange(0,len(newself)) + self = self.modifier_population(newself) + + def reproduction(self,selection=None,enfant=croisement_un_point, p = None): + if selection == None : + selection = self.selection_duel + if p == None : + p = (self.n)//2 + vieille_taille = self.n + selection(p) + newself = list(self.indiv) + while len(newself)<vieille_taille: + m=randrange(0,self.n) + t=randrange(0,self.n) x=newself[m] y=newself[t] - newself.append(enfant(x,y)) - return(newself) - - + couple_enfant = enfant(x,y) + newself.append(couple_enfant[0]) + newself.append(couple_enfant[1]) + self = self.modifier_population(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") + for individu in popu.indiv : + individu.evaluate("AAAGGATCTTCTTGAGATCCTTTTTTTCTGCGCGTAATCTGCTGCCAGTAAACGAAAAAACCGCCTGGGGAGGCGGTTTAGTCGAA") + afficher(popu) + # popu.selection_par_rang() + # print("\n SELECTION PAR RANG \n") + # afficher(popu) + popu.reproduction(selection = popu.selection_proportionelle) + print("\n REPRODUCTION \n") + afficher(popu) + +test() diff --git a/selection_par_rang.py b/selection_par_rang.py deleted file mode 100644 index ded5567fe7094249e4e3f1e0222031f0010a0164..0000000000000000000000000000000000000000 --- a/selection_par_rang.py +++ /dev/null @@ -1,45 +0,0 @@ -from random import random - -def creer_population(self, liste_individus): - self.n = len(liste_individus) - self.indiv = set(liste_individus) - return self - -def selection_par_rang(self, p = n//2): - set_individus = self.indiv - n = self.n - - def partitionner(tableau,debut,fin): - echanger(tableau,debut,random.randint(debut,fin-1)) - partition=debut - for i in range(debut+1,fin): - if tableau[i].score<tableau[debut].score: - partition+=1 - echanger(tableau,i,partition) - echanger(tableau,debut,partition) - return partition - - def tri_rapide_aux(tableau,debut,fin): - if debut < fin-1: - positionPivot=partitionner(tableau,debut,fin) - tri_rapide_aux(tableau,debut,positionPivot) - tri_rapide_aux(tableau,positionPivot+1,fin) - - def tri_rapide(tableau): - tri_rapide_aux(tableau,0,len(tableau)) - - liste = list(set_individus) - tri_rapide(liste) - individus_selectionnes = [] - - for _ in range(p): - curseur = random()*n*(n+1)/2 - j = 1 - while j*(j+1)/2 < curseur : - j+=1 - #on doit prendre l'individu avec le jème score - individus_selectionnes.append(liste[j]) - self = creer_population(self, liste_individus) - - - \ No newline at end of file