Skip to content
Snippets Groups Projects
Commit a13196da authored by Kappes Marques Rodrigo's avatar Kappes Marques Rodrigo
Browse files

cleaned the population

parent fe7ae3c5
No related branches found
No related tags found
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -5,16 +5,20 @@ from croisement import croisement_un_point, croisement_deux_points
import copy
class Population:
#Class initialization
def __init__(self,n):
self.indiv=[Individu(RotTable()) for k in range (n)]
self.n = n
#Updates the current individuals in the population
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
#Select a fixed number of individuals in a population
def selection_p_best(self,p=None):
if p==None:
p=(self.n)//2
......@@ -23,7 +27,7 @@ class Population:
individus_selectionnes = [element for element in liste_individus[:p]]
self = self.modifier_population(individus_selectionnes)
#Select via comparing two random individuals with some randomness involved and compared
def selection_duel_pondere(self,p=None):
if p == None :
p = (self.n)//2
......@@ -33,7 +37,8 @@ class Population:
meilleur = individu
newself=[meilleur]
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
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
non_vu = [i for i in range(0, self.n)]
while len(newself)<p:
m = choice(non_vu)
......@@ -50,6 +55,7 @@ class Population:
self = self.modifier_population(newself)
#Selection via comparing the player`s score randomly
def selection_duel(self,p=None):
if p == None :
p = (self.n)//2
......@@ -75,8 +81,7 @@ class Population:
newself.append(y)
self = self.modifier_population(newself)
#Selection using a random method to select players based on their rank in score
def selection_par_rang(self,p = None):
if p == None :
p = (self.n)//2
......@@ -97,6 +102,7 @@ class Population:
self = self.modifier_population(individus_selectionnes)
#Selecting using a random method that takes the score into ponderation
def selection_proportionnelle(self,p= None):
if p == None :
p = (self.n)//2
......@@ -116,8 +122,12 @@ class Population:
newself.append(x)
self = self.modifier_population(newself)
#The Function that makes the selection and reduces the quantity of players
#and after reproduces and mutates the remaining ones into a new population
def reproduction(self,proba_mutation = None, selection=None,enfant=croisement_un_point, p = None):
liste_selections = [self.selection_p_best, self.selection_duel_pondere, self.selection_duel, self.selection_par_rang, self.selection_proportionnelle]
#Values of variables if there are no initial conditions
if proba_mutation == None :
proba_mutation = 0.001
if selection == None :
......@@ -126,14 +136,19 @@ class Population:
selection = liste_selections[selection]
if p == None :
p = (self.n)//2
vieille_taille = self.n
selection(p)
newself = [element for element in self.indiv]
while len(newself)<vieille_taille:
m=randrange(0,self.n)
t=randrange(0,self.n)
#We have to make a deep copy so we dont lose the values of the parents
x=copy.deepcopy(newself[m])
y=copy.deepcopy(newself[t])
#Creation of the childs
couple_enfant = enfant(x,y)
for child in couple_enfant :
child.mutation_close_values(proba_mutation, number_of_mutations = 2)
......@@ -142,40 +157,15 @@ class Population:
newself.append(couple_enfant[1])
self = self.modifier_population(newself)
#Prints the current population(used for debugging)
def afficher(popu):
for individu in popu.indiv :
print("\n individu \n")
# print(individu.table.rot_table)
print ("score", individu.score)
def test():
popu = Population(4)
print("\n POPULATION INITIALE \n")
for individu in popu.indiv :
individu.evaluate()
afficher(popu)
popu.reproduction(selection = popu.selection_duel)
print("\n REPRODUCTION \n")
afficher(popu)
#test()
def test2():
popu = Population(10)
for individu in popu.indiv :
lineList = [line.rstrip('\n') for line in open("plasmid_8k.fasta")]
brin = ''.join(lineList[1:])
individu.evaluate()
print("\n \n POPULATION INITIALE \n \n")
afficher(popu)
popu.selection_proportionnelle()
print("\n\nAPRES SELECTION \n\n")
afficher(popu)
# test2()
......
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