0

Hello i need help for create genetic algorithme for converge to maximum or minimum value. I develop a code for found maximum sentence ascii sum, but my code not converge to maximum, my code make "yoyo" value

like this picture : matploltib output

i share my code :

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.0, random_select=0.00, mutation_rate=0.00):
    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')

i want to found highter solution in my fitness function

thanks for advance for your help

ilapasle
  • 349
  • 4
  • 16
  • 1
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. – Prune Apr 04 '18 at 14:53
  • 1
    Specifically, you gave us the entire program, rather than a minimal example. Second, you describe your output as "yoyo", but "yoyo" does not appear anywhere in your output. Third, your output is in French; this is an English-language site, and only a few of us read even the simple French you use here. Finally, we need you to minimize the problem: give us the shortest code necessary to reproduce it. If your problem is in the fitness function, then unit-test that function, and leave all of the genetic parts out of the posting. – Prune Apr 04 '18 at 14:55
  • @Prune I think „yoyo“ is used to describe the progressing of the value, like the movement of the [yo-yo](https://en.wikipedia.org/wiki/Yo-yo) toy. It's not converging but bouncing up and down. – BlackJack Apr 04 '18 at 16:50
  • @BlackJack: that's what I hoped when I read it, but there's no specific output in the posting to clarify. Even though I read French well enough to follow the text, I'm hopeful that a "proper" example will get the problem solved quickly. The photo shows the effect, but there's nothing about what the function *should* return. In my DL work, there's generally nothing wrong with that graph for the first 120 iterations. – Prune Apr 04 '18 at 17:09

0 Answers0