Python Forum
python genetic algorithme find max value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python genetic algorithme find max value
#1
Hello i try to developpe genetic algorithme in python3 for found max value, but my probleme is that my code search minimum value...
I'm trying to set up a program that tries to make the greatest sum ascii from words

on the internet I find a lot of tutorials on genetic algo from which I could draw inspiration, I managed to put together a program that works...

well almost, it works but does the opposite, it looks for the smallest sum not the biggest

import random
import statistics
 
words = [
    ["aaa", "bbb", "ccc", "yyy", "zzz"],
    ["000", "111", "222", "555", "888", "999"],
    ["~~~", "!!!"]
]
 
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)
    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)
 
    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, 100)
    next_population = initial_population
    max_iter = 5
 
    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)
and that's where I block, I think the blank solution of the cross function or I calculate the average, I don't know if it's the most suitable calculation?

otherwise this is what my algo produces:
[Image: w4tqL.png]

It's very strange if I reverse the sum and multiply it by -1 I get this
[Image: 15228455260469_genetic2.PNG]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Genetic Algorithm rajeev1729 3 3,680 Jan-24-2019, 10:07 AM
Last Post: rajeev1729
  Genetic Algorithm UpperThrower4 3 3,173 Jul-16-2018, 09:32 AM
Last Post: manishti2004

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020