Bonjour, j’ai un cours en ia et j’ai comme exercice de faire un programme faisant la plus courte phrase possible (en terme de nb de caractere)
Je dispose d’une liste2D de départ
1 | data=[["Un","Des","Une","On","Elle"],["a","eu","avait","est","était","fut"],["soif","rouge"]] |
je dois faire une phrase de 3 mots la plus petite possible (en nb de caractère), la solution serait ici "Un a soif"
voila ce que j’ai fais en algorithme génétique, je cherche la valeur la plus patite en modifiant a chaque gen 0.05% de la pop.
Un individu est une phrase une population est un ensemble de phrase je cherche à générer la plus petite phrase possible.
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 | from random import randint, random from operator import add from functools import reduce data=[["Un","Des","Une","On","Elle"],["a","eu","avait","est","était","fut"],["soif","rouge"]] def phrases(): myphrase=[] for element in data: mystring=element[randint(0, len(element)-1)] myphrase.append(mystring) myphrase.append(len(mystring)) return myphrase def individual(): 'Create a member of the population.' matrice=[] strings_length=[] strings_name=[] myphrase=phrases() strings_name.append(myphrase) strings_length.append(len(myphrase)) matrice.append(strings_length) matrice.append(strings_name) return matrice def population(count): population=[ individual() for x in range(count) ] return population def fitness(individual, target): sum = reduce(add, individual, 0) return abs(target-sum) def grade(pop, target): 'Find average fitness for a population.' pop_number=[] pop_string=[] for element in pop: pop_number.append(element[0]) pop_string.append(element[1]) summed = reduce(add, (fitness(x, target) for x in pop_number)) data=[[summed / (len(pop_number) * 1.0)], [pop_string[-1]]] return data def evolve(pop, target, retain=0.2, random_select=0.05, mutate=0.01): pop_number=[] pop_string=[] for element in pop: pop_number.append(element[0]) pop_string.append(element[1]) graded = [ (fitness(x, target), x) for x in pop_number] graded = [ x[1] for x in sorted(graded)] pop_new_string=[] retain_length = int(len(graded)*retain) parents = graded[:retain_length] for individual in graded[retain_length:]: parents.append(individual) myphrase=phrases() if random_select > random(): pop_new_string.append(myphrase) for individual in parents: myphrase=phrases() pop_new_string.append(myphrase) if mutate > random(): pos_to_mutate = randint(0, len(individual)-1) individual[pos_to_mutate] = randint( min(individual), max(individual)) parents_length = len(parents) desired_length = len(pop_number) - parents_length children = [] while len(children) < desired_length: male = randint(0, parents_length-1) female = randint(0, parents_length-1) if male != female: male = parents[male] female = parents[female] half = int(len(male) / 2) child = male[:half] + female[half:] children.append(child) parents.extend(children) i=0 pop=[] print(len(parents)) print(len(pop_new_string)) for element in parents: data=[] data.append(element) data.append(pop_new_string[i]) i=i+1 pop.append(data) return pop target = 0 p_count = 100 p = population(p_count) fitness_history = [grade(p, target),] for i in range(100): p = evolve(p, target) fitness_history.append(grade(p, target)) for datum in fitness_history: print(datum) |
le probleme c’est que ma valeur n’évolue pas. Par avance merci pour votre aide
+0
-0