Newer
Older
import random
class Population:
def __init__(self,n):
self.indiv=[Individu(rot_table.alea) for k in range (n)]
self.n = n
def selection_duel_pondere(self,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
while len(newself)<p:
while m in vu:
m=random.randrange(0,len(self))
while t in vu:
t=random.randrange(0,len(self))
p=uniform(0,1)
if p>x.score/(x.score+y.score):
newself.append(y)
else:
newself.append(x)
return(newself)
def selection_duel(self,p=(self.n)//2):
newself=[]
vu={}
t=None
m=None
while len(newself)<p:
while m in vu:
m=random.randrange(0,len(self))
while t in vu:
t=random.randrange(0,len(self))
if x.score<=y.score:
newself.append(x)
else:
newself.append(y)
return(newself)
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
liste_individus = self.indiv
n = self.n
def echanger(tableau, i, j):
tableau[i], tableau[j] = tableau[j], tableau[i]
def partitionner(tableau,debut,fin):
echanger(tableau,debut,randint(debut,fin-1))
partition=debut
for i in range(debut+1,fin):
if tableau[i] < tableau[debut]:
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))
tri_rapide(liste_individus)
individus_selectionnes = []
for _ in range(p):
curseur = random()*n*(n+1)/2
# print("curseur", curseur)
j = 1
while j*(j+1)/2 < curseur :
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
self = modifier_population(self, individus_selectionnes)
def selection_proportionelle(self,p=(self.n)//2):
newself=[]
somme=0
for indiv in self:
somme=somme+indiv.score
while len(newself)<p:
m=m=random.randrange(0,len(self))
x=self[m]
p=uniform(0,1)
if p<=x.score/somme:
newself.append(x)
return(newself)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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))
x=newself[m]
y=newself[t]
newself.append(enfant(x,y))
return(newself)