首页 > 解决方案 > 遗传算法 - 继承问题

问题描述

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

1 次迭代的示例: 在此处输入图像描述

最后一次迭代的例子: 在此处输入图像描述

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

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

标签: pythongenetic-algorithm

解决方案


推荐阅读