繁体   English   中英

多项式的遗传算法优化

[英]Genetic algorithm optimization of polynomial

我一直在研究以下代码以使用遗传算法最大化多项式,但它卡在低端并且变异函数似乎不起作用。 这是代码:

# Genetic Algorithm to optimize polynomial
import numpy as np
import tabulate
import random

def fitness_score(arr):
    f=[]
    integers=[]
    for x in arr:
        s=np.sum(2**np.arange(len(x))*x)#binary to decimal
        integers.append(s)
        f.append(2*s-(s*s/16))
    return f,integers

def takefifth(elem):
    return elem[5]

def deterministic_sampling(fitness,population,population_size):
    g=[]
    sorted_population=[]
    for i in range(population_size):
        normalize=population_size*(fitness[i]/sum(fitness))
        x=np.append(population[i],[normalize])
        sorted_population.append(x)
        for j in range(int(normalize)):
            g.append(list(population[i][0:5]))
    sorted_population=sorted(sorted_population,key=takefifth)
    if(len(g)!=population_size):
        for i in range(population_size):
            if(sorted_population[i][5]<1):
                g.append(list(sorted_population[i][0:5].astype(int)))
            if(len(g)==population_size):
                break
    return g,sorted_population

def crossover(initial_population,population_size):
    c=[]
    new_population=[]
    mates=initial_population[:]
    np.random.shuffle(mates)
    for i in range(population_size):
        r = random.randint(0, 5)
        c.append(r)
        if(i%2==0):
            temp=initial_population[i][0:r]
            temp.extend(list(mates[i][r:]))
        else:
            temp=mates[i][0:r]
            temp.extend(list(initial_population[i][r:]))
        new_population.append(temp)
    header = ['Initial Population', 'Mates','Crossover site','New Population']
    print(tabulate.tabulate([[initial_population[i][::-1],mates[i][::-1],c[i],new_population[i][::-1]] for i in range(population_size)], headers=header, tablefmt='grid'))
    return new_population

def mutation(population,populationsize):
    r= random.randint(0, 4)
    i= random.randint(0, 3)
    print(r,i)
    if(population[i][r]==0):
        population[i][r]==1
    else:
        population[i][r]==0
    return population

def genetic():
    population_size=4
    #generating initial population of 32 chromosomes(integers)
    chromosomes=np.arange(32)
    #converting to binary
    chromosomes_bin= (((chromosomes[:,None] & (1 << np.arange(5))))>0).astype(int)
    print("Chromosomes\n",chromosomes_bin)
    
    #selecting initial population(replace=False to avoid repititions)
    random_rows=np.random.choice(chromosomes_bin.shape[0],population_size,replace=False)
    initial_population=chromosomes_bin[random_rows,:]
    
    #calculate fitness scores of initial population
    fitness_scores,integers=fitness_score(initial_population)

    #deterministic sampling for parent selection
    #chromosomes with low fitness discarded
    #chromosomes with high fitness repeated
    parents,sorted_population=deterministic_sampling(fitness_scores,initial_population,population_size)
 
    header = ['Population', 'Integers','Fitness Score','Parents for next generation']
    print(tabulate.tabulate([[initial_population[i][::-1],integers[i],fitness_scores[i],parents[i][::-1]] for i in range(population_size)], headers=header, tablefmt='grid'))

    def sublists_equal(a, b):
        return all(l for l in b if l in a)

    while(max(fitness_scores)!=16):
        initial_population=parents[:]
        #generate new population using simple crossover method
        new_population=crossover(initial_population,population_size)
        
        #calculate fitness score
        fitness_scores,integers=fitness_score(new_population)

        #deterministic sampling for parent selection
        #chromosomes with low fitness discarded
        #chromosomes with high fitness repeated
        parents,sorted_population=deterministic_sampling(fitness_scores,new_population,population_size)
        parents=mutation(parents,population_size)

        header = ['Population', 'Integers','Fitness Score','Parents for next generation']
        print(tabulate.tabulate([[new_population[i][::-1],integers[i],fitness_scores[i],parents[i][::-1]] for i in range(population_size)], headers=header, tablefmt='grid'))
    
        

genetic()

由于函数不会停止执行,这里是一个示例输出:

Chromosomes
 [[0 0 0 0 0]
 [1 0 0 0 0]
 [0 1 0 0 0]
 [1 1 0 0 0]
 [0 0 1 0 0]
 [1 0 1 0 0]
 [0 1 1 0 0]
 [1 1 1 0 0]
 [0 0 0 1 0]
 [1 0 0 1 0]
 [0 1 0 1 0]
 [1 1 0 1 0]
 [0 0 1 1 0]
 [1 0 1 1 0]
 [0 1 1 1 0]
 [1 1 1 1 0]
 [0 0 0 0 1]
 [1 0 0 0 1]
 [0 1 0 0 1]
 [1 1 0 0 1]
 [0 0 1 0 1]
 [1 0 1 0 1]
 [0 1 1 0 1]
 [1 1 1 0 1]
 [0 0 0 1 1]
 [1 0 0 1 1]
 [0 1 0 1 1]
 [1 1 0 1 1]
 [0 0 1 1 1]
 [1 0 1 1 1]
 [0 1 1 1 1]
 [1 1 1 1 1]]
+--------------+------------+-----------------+-------------------------------+
| Population   |   Integers |   Fitness Score | Parents for next generation   |
+==============+============+=================+===============================+
| [0 1 1 0 1]  |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+--------------+------------+-----------------+-------------------------------+
| [1 0 0 1 0]  |         18 |         15.75   | [1, 0, 0, 1, 0]               |
+--------------+------------+-----------------+-------------------------------+
| [0 0 0 0 1]  |          1 |          1.9375 | [1, 0, 1, 1, 0]               |
+--------------+------------+-----------------+-------------------------------+
| [1 0 1 1 0]  |         22 |         13.75   | [0, 0, 0, 0, 1]               |
+--------------+------------+-----------------+-------------------------------+
+----------------------+-----------------+------------------+------------------+
| Initial Population   | Mates           |   Crossover site | New Population   |
+======================+=================+==================+==================+
| [0, 1, 1, 0, 1]      | [0, 0, 0, 0, 1] |                4 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [1, 0, 0, 1, 0]      | [1, 0, 1, 1, 0] |                3 | [1, 0, 1, 1, 0]  |
+----------------------+-----------------+------------------+------------------+
| [1, 0, 1, 1, 0]      | [0, 1, 1, 0, 1] |                0 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 1]      | [1, 0, 0, 1, 0] |                1 | [0, 0, 0, 0, 0]  |
+----------------------+-----------------+------------------+------------------+
1 1
+-----------------+------------+-----------------+-------------------------------+
| Population      |   Integers |   Fitness Score | Parents for next generation   |
+=================+============+=================+===============================+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [1, 0, 1, 1, 0] |         22 |         13.75   | [1, 0, 1, 1, 0]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 0, 0, 0, 0] |          0 |          0      | [0, 0, 0, 0, 0]               |
+-----------------+------------+-----------------+-------------------------------+
+----------------------+-----------------+------------------+------------------+
| Initial Population   | Mates           |   Crossover site | New Population   |
+======================+=================+==================+==================+
| [0, 1, 1, 0, 1]      | [0, 1, 1, 0, 1] |                3 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [1, 0, 1, 1, 0]      | [0, 1, 1, 0, 1] |                5 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 1, 1, 0, 1]      | [1, 0, 1, 1, 0] |                2 | [1, 0, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 0]      | [0, 0, 0, 0, 0] |                2 | [0, 0, 0, 0, 0]  |
+----------------------+-----------------+------------------+------------------+
4 1
+-----------------+------------+-----------------+-------------------------------+
| Population      |   Integers |   Fitness Score | Parents for next generation   |
+=================+============+=================+===============================+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [1, 0, 1, 0, 1] |         21 |         14.4375 | [1, 0, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 0, 0, 0, 0] |          0 |          0      | [0, 0, 0, 0, 0]               |
+-----------------+------------+-----------------+-------------------------------+
+----------------------+-----------------+------------------+------------------+
| Initial Population   | Mates           |   Crossover site | New Population   |
+======================+=================+==================+==================+
| [0, 1, 1, 0, 1]      | [0, 0, 0, 0, 0] |                1 | [0, 0, 0, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 1, 1, 0, 1]      | [1, 0, 1, 0, 1] |                3 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [1, 0, 1, 0, 1]      | [0, 1, 1, 0, 1] |                2 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 0]      | [0, 1, 1, 0, 1] |                4 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
4 3
+-----------------+------------+-----------------+-------------------------------+
| Population      |   Integers |   Fitness Score | Parents for next generation   |
+=================+============+=================+===============================+
| [0, 0, 0, 0, 1] |          1 |          1.9375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 0, 0, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
+----------------------+-----------------+------------------+------------------+
| Initial Population   | Mates           |   Crossover site | New Population   |
+======================+=================+==================+==================+
| [0, 1, 1, 0, 1]      | [0, 1, 1, 0, 1] |                0 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 1, 1, 0, 1]      | [0, 1, 1, 0, 1] |                2 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 1, 1, 0, 1]      | [0, 0, 0, 0, 1] |                0 | [0, 0, 0, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 1]      | [0, 1, 1, 0, 1] |                1 | [0, 0, 0, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
4 1
+-----------------+------------+-----------------+-------------------------------+
| Population      |   Integers |   Fitness Score | Parents for next generation   |
+=================+============+=================+===============================+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 1, 1, 0, 1] |         13 |         15.4375 | [0, 1, 1, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 0, 0, 0, 1] |          1 |          1.9375 | [0, 0, 0, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
| [0, 0, 0, 0, 1] |          1 |          1.9375 | [0, 0, 0, 0, 1]               |
+-----------------+------------+-----------------+-------------------------------+
+----------------------+-----------------+------------------+------------------+
| Initial Population   | Mates           |   Crossover site | New Population   |
+======================+=================+==================+==================+
| [0, 1, 1, 0, 1]      | [0, 0, 0, 0, 1] |                3 | [0, 0, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 1, 1, 0, 1]      | [0, 1, 1, 0, 1] |                1 | [0, 1, 1, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 1]      | [0, 1, 1, 0, 1] |                4 | [0, 0, 0, 0, 1]  |
+----------------------+-----------------+------------------+------------------+
| [0, 0, 0, 0, 1]      | [0, 0, 0, 0, 1] |                2 | [0, 0, 0, 0, 1]  |
+----------------------+-----------------+------------------+------------------+

谁能帮我吗? 我哪里错了?

几点...

你的代码不是很可读。 您有很多难以破译的 1 字符命名变量,并且您的某些构造非常奇怪,可能可以更清楚地表达,特别是在您的deterministic_sampling函数中。

也就是说,在你的变异函数中,你有一个致命的错字。 您并没有像希望的那样重新分配 1/0。 改变:

    population[i][r]==1

    population[i][r]=1

与更改为零的行类似。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM