遗传算法-继承问题

时间:2019-03-20 22:15:46

标签: python genetic-algorithm

我已经实现了一种遗传算法来拟合多项式,该多项式应划分2组点。我生成了第一个随机种群,结果似乎是理想的,但是当我跨种群时,这个问题开始增长。我想我把交叉或“健身功能”弄乱了。第一次运行时,程序会找到好的解决方案,但是每次迭代都会变得很糟糕。

1次迭代的示例: enter image description here

最后一次迭代的示例: enter image description here

我用来检查多项式的函数。

def evaluation_mutation(points, population, degree):
    temp = convert_to_dec(population, degree)
    result = []
    counter = []
    counter2 = []

    for inx2 in range(len(temp)):
        counter.append([])
        counter2.append([])
        for i in range(len(points[0])):
            if (np.polyval(temp[inx2], points[0][i, 0]) < points[0][i, 0]) and points[1][i]:
                if not counter[inx2]:
                    counter[inx2] = 1
                else:
                    counter[inx2] = counter[inx2] + 1

            elif (np.polyval(temp[inx2], points[0][i, 0]) > points[0][i, 0]) and not points[1][i]:
                if not counter2[inx2]:
                    counter2[inx2] = 1
                else:
                    counter2[inx2] = counter2[inx2] + 1

        if counter2[inx2] and counter[inx2]:
            result.append([counter[inx2], counter2[inx2]])
        else:
            result.append([])

    return result

完整代码:https://pastebin.com/u5rr62QV

0 个答案:

没有答案