Skip to content
Snippets Groups Projects
Commit df9a5c2c authored by Muller Sacha's avatar Muller Sacha
Browse files

Correction des bugs de population.py

parent 3ff492be
No related branches found
No related tags found
No related merge requests found
...@@ -63,7 +63,7 @@ class RotTable: ...@@ -63,7 +63,7 @@ class RotTable:
################### ###################
table1 = RotTable() # table1 = RotTable()
print(table1.orta()) # print(table1.orta())
print(table1.rot_table["AA"]) # print(table1.rot_table["AA"])
import random import random
from random import random, randint, randrange
from individu import Individu
from RotTable import RotTable
from croisement import *
class Population: class Population:
def __init__(self,n): 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 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=[] newself=[]
vu={} vu=set()
m=None 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 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 len(newself)<p:
while m in vu: while m in vu:
m=random.randrange(0,len(self)) m=randrange(0,len(self))
while t in vu: while t in vu:
t=random.randrange(0,len(self)) t=randrange(0,len(self))
x=self[m] x=self.indiv[m]
y=self[t] y=self.indiv[t]
vu.add(t) vu.add(t)
vu.add(m) vu.add(m)
p=uniform(0,1) p=random(0,1)
if p>x.score/(x.score+y.score): if p>x.score/(x.score+y.score):
newself.append(y) newself.append(y)
else: else:
...@@ -27,18 +34,20 @@ class Population: ...@@ -27,18 +34,20 @@ class Population:
return(newself) return(newself)
def selection_duel(self,p=(self.n)//2): def selection_duel(self,p=None):
if p == None :
p = (self.n)//2
newself=[] newself=[]
vu={} vu=set()
t=None t=None
m=None m=None
while len(newself)<p: while len(newself)<p:
while m in vu: while m in vu:
m=random.randrange(0,len(self)) m=randrange(0,len(self))
while t in vu: while t in vu:
t=random.randrange(0,len(self)) t=randrange(0,len(self))
x=self[m] x=self.indiv[m]
y=self[t] y=self.indiv[t]
vu.add(t) vu.add(t)
vu.add(m) vu.add(m)
if x.score<=y.score: if x.score<=y.score:
...@@ -47,7 +56,9 @@ class Population: ...@@ -47,7 +56,9 @@ class Population:
newself.append(y) newself.append(y)
return(newself) 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 liste_individus = self.indiv
n = self.n n = self.n
...@@ -58,7 +69,7 @@ class Population: ...@@ -58,7 +69,7 @@ class Population:
echanger(tableau,debut,randint(debut,fin-1)) echanger(tableau,debut,randint(debut,fin-1))
partition=debut partition=debut
for i in range(debut+1,fin): for i in range(debut+1,fin):
if tableau[i] < tableau[debut]: # if tableau[i] < tableau[debut]:
if tableau[i].score<tableau[debut].score: if tableau[i].score<tableau[debut].score:
partition+=1 partition+=1
echanger(tableau,i,partition) echanger(tableau,i,partition)
...@@ -85,7 +96,7 @@ class Population: ...@@ -85,7 +96,7 @@ class Population:
j+=1 j+=1
#on doit prendre l'individu avec le jème score #on doit prendre l'individu avec le jème score
# print("individus selectionés", individus_selectionnes) # 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): def modifier_population(self, liste_individus):
self.n = len(liste_individus) self.n = len(liste_individus)
...@@ -94,30 +105,50 @@ class Population: ...@@ -94,30 +105,50 @@ class Population:
self = modifier_population(self, individus_selectionnes) 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=[] newself=[]
somme=0 somme=0
for indiv in self: for indiv in self.indiv:
somme=somme+indiv.score somme=somme+indiv.score
while len(newself)<p: while len(newself)<p:
m=m=random.randrange(0,len(self)) m=m=randrange(0,len(self))
x=self[m] x=self.indiv[m]
p=uniform(0,1) p=random(0,1)
if p<=x.score/somme: if p<=x.score/somme:
newself.append(x) newself.append(x)
return(newself) 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) newself=selection(self,p)
while len(newself)<self.n: while len(newself)<self.n:
m=random.randrange(0,len(newself)) m=randrange(0,len(newself))
t=random.randrange(0,len(newself)) t=randrange(0,len(newself))
x=newself[m] x=newself[m]
y=newself[t] y=newself[t]
newself.append(enfant(x,y)) newself.append(enfant(x,y))
return(newself) 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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment