Bonjour, j'ai une liste 2D contenant des mots, je souhaite trouver la combinaison de ces mots qui me donnera la somme ascii la plus grosse

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
words = [
        ["Un", "Des", "Une", "On", "Elle"],
        ["a", "eu", "avait", "est", "était", "fut"],
        ["soif", "rouge"]
        ]
comme phrase je peut avoir par exemple :
Un eu rouge
Elle a soif
Une avait rouge
On avait soit
....etc.
les phrases n'ont pas de sens mais ce n'est pas important

bref j'ai regardé pas mal de tuto et voila ce que j'ai développé pour résoudre mon probleme avec un algorithme génétique, car je veut résoudre mn probleme avec cette méthode.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
import random
import statistics
 
EVOLUTION=[]
 
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)
    somme = 0
    for caractere in chaine:
        somme = somme + ord(caractere)
 
    print(chaine)
    print(somme)
    EVOLUTION.append(somme)
    return somme
    #return sum(data[i][individual[i]] for i in range(len(individual)))
 
 
def grade(population, data):
    fit = [fitness(ind, data) for ind in population]
    return statistics.mean(fit)
 
 
def mutate(ind, data):
    gene = random.randrange(0, len(ind))
    clone = list(ind)
    clone[gene] = random.randrange(0, len(data[gene]))
    #print(sentence(tuple(clone),words))
    return tuple(clone)
 
 
def cross(mother, father):
    return tuple(round(statistics.mean(genes)) for genes in zip(mother, father))
 
def sentence(individual, words):
    return ' '.join([words[i][individual[i]] for i in range(len(words))])
 
def evolve(population, data, retain=0.2, random_select=0.05, mutation_rate=0.01):
    def cmp_ind(ind):
        return fitness(ind, data)
    sorted_population = sorted(population, key=cmp_ind, reverse=True)
 
    len_retained = round(len(population) * retain)
    retained = sorted_population[:len_retained]
 
    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),
              random.choice(sorted_population))
        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 = 3
 
    for i in range(max_iter):
        next_population = evolve(next_population, data)
 
    sorted_population = sorted(next_population, key=lambda x: fitness(x, data))
    best_individual = sorted_population[0]
 
    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)
    plt.savefig('myfig')

je pense que globalement mon code, ou plutot ma méthode est bonne, mais il y'a un petit détail quand je ragrde le résultat obtenue, au lieu d'obtenir une courbe qui montrais vers le haut, car je rapelle le but c'est de trouver la somme ascii la plus élevée j'ai un effet yoyo comme sur cette image :
Nom : Hf40F3a.png
Affichages : 1131
Taille : 47,9 Ko


moi je pensait plus obtenir un truc dans ce style :
Nom : image005.jpg
Affichages : 1026
Taille : 13,8 Ko


mais mon plus gros probleme c'est que j'ai l'impression que mon programme tends vers la somme la plus petite pas la plus grande, quelqu'un peut t'il m'aider ?