1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
import random
import statistics
words = [
["Un", "Des", "Une", "On", "Elle"],
["a", "eu", "avait", "est", "était", "fut"],
["soif", "rouge"]
]
def individual(data):
#return tuple(random.choice(range(len(feature))) for feature in data)
return tuple(random.choice(range(len(feature))) for feature in data)
def population(data, initial=100):
return [individual(data) for i in range(initial)]
def fitness(individual, data):
chaine=sentence(individual,words)
return sum(ord(carac) for carac in chaine)
def mutate(ind, data):
gene = random.randrange(0, len(ind))
clone = list(ind)
clone[gene] = random.randrange(0, len(data[gene]))
return tuple(clone)
def cross(mother, father):
children=[]
for i in range(0,len(mother)):
selection=random.choice([True, False])
if selection:
children.append(mother[i])
else:
children.append(father[i])
children=tuple(children)
return children
def sentence(individual, words):
return ' '.join([words[i][individual[i]] for i in range(len(words))])
def evolve(population, data, retain=0.0, random_select=0.00, mutation_rate=0.00):
def cmp_ind(ind):
return fitness(ind, data)
sorted_population = sorted(population, key=cmp_ind)
#sorted_population = sorted(population, key=cmp_ind, reverse=True)
len_retained = round(len(population) * retain)
random_selected = [
ind
for ind in sorted_population[len_retained:]
if random.random() <= random_select
]
mutated = [
mutate(ind, data)
for ind in sorted_population[len_retained:]
if random.random() <= mutation_rate
]
children = [
cross(random.choice(sorted_population[len_retained:]),
random.choice(sorted_population[len_retained:]))
for i in range(len(population) - len(random_selected) - len(mutated))
]
return random_selected + mutated + children
if __name__ == '__main__':
data = [[len(w) for w in ws] for ws in words]
initial_population = population(data, 30)
next_population = initial_population
max_iter = 5
EVOLUTION_moyenne=[]
EVOLUTION_minimal=[]
EVOLUTION_maximal=[]
for i in range(max_iter):
population_score=[]
moyenne=0
for element in next_population :
chaine=sentence(element,words)
score=sum(ord(carac) for carac in chaine)
population_score.append(score)
moyenne=moyenne+score
moyenne=moyenne/len(next_population)
print("*************")
print("Moyenne : "+str(moyenne))
print("Max : "+str(max(population_score)))
print("Min : "+str(min(population_score)))
EVOLUTION_moyenne.append(moyenne)
EVOLUTION_minimal.append(min(population_score))
EVOLUTION_maximal.append(max(population_score))
next_population = evolve(next_population, data, retain=0.9)
sorted_population = sorted(next_population, key=lambda x: fitness(x, data))
best_individual = sorted_population[0]
print("************************************************")
print("best solution :")
chaine=sentence(best_individual,words)
somme = 0
for caractere in chaine:
somme = somme + ord(caractere)
print(chaine)
print(somme)
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
plt.plot(EVOLUTION_moyenne)
plt.plot(EVOLUTION_minimal)
plt.plot(EVOLUTION_maximal)
plt.savefig('myfig') |
Partager