What role does max play in genetic algorithms?

45 Views Asked by At

I found a genetic algorithm code on the Internet, which solves the maximum value of f(x)=2*sin(x) + cos(x), but I found that there is a parameter max_value in the code, and I don’t know what it does.

the GA code as follow:

import random
import math
import matplotlib.pyplot as plt


def species_origin(population_size, chromosome_length):
    population = [[]]  
    for i in range(population_size):
        temporary = [] 
        for j in range(chromosome_length):
            temporary.append(random.randint(0, 1)) 
        population.append(temporary)  
    return population[1:] 


def translation(population, chromosome_length):
    temporary = []
    for i in range(len(population)):  
        total = 0
        for j in range(chromosome_length):
            total = total + population[i][j] * (math.pow(2, j))
        temporary.append(total)
    return temporary


def function(population, chtomosome_length, max_value):
    temporary = []
    function1 = []
    temporary = translation(population, chtomosome_length)  
    for i in range(len(temporary)):
        x = temporary[i] * max_value / (math.pow(2, chtomosome_length) - 1)
        function1.append(2 * math.sin(x) + math.cos(x))
    return function1

def fitness(function1):
    fitness1 = []
    min_fitness = mf = 0
    for i in range(len(function1)):
        if (function1[i] + mf > 0):
            temporary = mf + function1[i]
        else:  
            temporary = 0.0
        fitness1.append(temporary)  
    return fitness1


def sum(fitness1):
    total = 0
    for i in range(len(fitness1)):
        total += fitness1[i]
    return total

# https://blog.csdn.net/weixin_39068956/article/details/105121469
def cumsum(fitness1):
    for i in range(len(fitness1) - 2, -1, -1):
        total = 0
        j = 0
        while (j <= i):
            total += fitness1[j]
            j += 1
        fitness1[i] = total
        fitness1[len(fitness1) - 1] = 1


def selection(population, fitness1):
    new_fitness = [] 
    total_fitness = sum(fitness1)
    for i in range(len(fitness1)):
        new_fitness.append(fitness1[i] / total_fitness)


    cumsum(new_fitness)

    ms = []  
    population_length = pop_len = len(population)
    for i in range(pop_len):
        ms.append(random.random())  
    ms.sort()

    fitin = 0  
    newin = 0  
    new_population = new_pop = population

    while newin < pop_len:  
        if (ms[newin] < new_fitness[fitin]):
            new_pop[newin] = population[fitin]
            newin += 1
        else:  
            fitin += 1
    population = new_pop


def crossover(population, pcB00):
    pop_len = len(population)

    for i in range(pop_len - 1):
        cpoint = random.randint(0, len(population[0]))
        temporary1 = []
        temporary2 = []

        temporary1.extend(population[i][0:cpoint])
        temporary1.extend(population[i + 1][cpoint:len(population[i])])

        temporary2.extend(population[i + 1][0:cpoint])
        temporary2.extend(population[i][cpoint:len(population[i])])

        population[i] = temporary1
        population[i + 1] = temporary2


def mutation(population, pm):
    px = len(population)
    py = len(population[0])

    for i in range(px):
        if (random.random() < pm):  
            mpoint = random.randint(0, py - 1)
            if (population[i][mpoint] == 1):
                population[i][mpoint] = 0
            else:
                population[i][mpoint] = 1


def b2d(b, max_value, chromosome_length):
    total = 0
    for i in range(len(b)):
        total = total + b[i] * math.pow(2, i)
    total = total * max_value / (math.pow(2, chromosome_length) - 1)
    return total


def best(population, fitness1):
    px = len(population)
    bestindividual = []
    bestfitness = fitness1[0]

    for i in range(1, px):
        if (fitness1[i] > bestfitness):
            bestfitness = fitness1[i]
            bestindividual = population[i]

    return [bestindividual, bestfitness]

# main
population_size = 500
max_value = 10
chromosome_length = 10
pc = 0.6
pm = 0.01
results = [[]]
fitness1 = []
fitmean = []

population = pop = species_origin(population_size, chromosome_length)


for i in range(population_size):
    function1 = function(population, chromosome_length, max_value)
    fitness1 = fitness(function1)
    best_individual, best_fitness = best(population, fitness1)
    results.append([best_fitness, b2d(best_individual, max_value, chromosome_length)])

    selection(population, fitness1)
    crossover(population, pc)
    mutation(population, pm)

results = results[1:]
results.sort()
X = []
Y = []
for i in range(500):
    X.append(i)
    Y.append(results[i][0])
plt.plot(X, Y)
plt.show()

I tried adjusting the value of max_value and found that it had a big impact on the results, which made me very confused.

0

There are 0 best solutions below