1
$\begingroup$

I wrote a code that implements a simple genetic algorithm to maximize: $$f(x) = 15x - x^2.$$ The function has its maximum at $x=7.5$, so the code output should be $7$ or $8$ since the population are integers. When I run the code ten times I get $7$ or $8$ around three times out of $10$. I chose mating probability of $0.7$ and mutation probability of $0.001$. What modification should I make to further improve the algorithm and what are different types of genetic algorithms?

Here is the code:

from random import *
import numpy as np

#fitness function
def fit(x):
    return 15*x -x**2
#covert binary list to decimal number
def to_dec(x):
    return int("".join(str(e) for e in x), 2)
#picks pairs from the original population
def gen_pairs(populationl, prob):
    pairsl = []
    test = [0, 1, 2, 3, 4, 5]
    for i in range(3):
        pair = []
        for j in range(2):
            temp = np.random.choice(test, p=prob)
            pair.append(populationl[temp].copy())
        pairsl.append(pair)

    return pairsl
#mating function
def cross_over(prs, mp):
    new = []
    for pr in prs:
        if mp[prs.index(pr)] == 1:
            index = np.random.choice([1,2,3], p=[1/3, 1/3, 1/3])
            pr[0][:index], pr[1][:index] = pr[1][:index], pr[0][:index]

    for pr in prs:
        new.append(pr[0])
        new.append(pr[1])

    return new

#mutation
def mutation(x):
    for chromosome in x:
        for gene in chromosome:
            mutation_prob = np.random.choice([0, 1], p=[0.999, .001])
            if mutation_prob == 1:
                #m_index = np.random.choice([0,1,2,3])
                if gene == 0:
                    gene = 1
                else:
                    gene = 0

#generate initial population
randlist = lambda n:[randint(0,1) for b in range(1, n+1)]
for j in range(10):
    population = [randlist(4) for i in range(6)]
    for _ in range(20):
        fittness = [fit(to_dec(y)) for y in population]

        s = sum(fittness)
        prob = [e/s for e in fittness]
        pairsg = gen_pairs(population.copy(), prob)

        mating_prob = []
        for i in pairsg:
            mating_prob.append(np.random.choice([0,1], p=[0.4,0.6]))

        new_population = cross_over(pairsg, mating_prob)
        mutated = mutation(new_population)
        decimal_p = [to_dec(i)for i in population]
        decimal_new = [to_dec(i)for i in new_population]
        # print(decimal_p)
        # print(decimal_new)
        population = new_population
    print(decimal_new)
$\endgroup$
  • $\begingroup$ This seems to be better for Stack Overflow or some other sites… $\endgroup$ – Saad Jun 1 '18 at 17:22
  • $\begingroup$ @Alex Francisco I asked on Satckoverflow, someone said I should post it here :P $\endgroup$ – Mohammed Obeidat Jun 1 '18 at 18:36
  • $\begingroup$ Ask it on Math Overflow? $\endgroup$ – ray lin Jun 1 '18 at 21:56
  • 1
    $\begingroup$ Math overflow is not at all the place for this. Anyway genetic algorithms are a lot of fun. Unfortunately this code is hard to read because there aren't any comments and the variable names aren't great. $\endgroup$ – Cameron Williams Jun 2 '18 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.