Ejemplo n.º 1
0
def plotit():
    plt.rcParams['font.family'] = 'SimHei'
    plt.rcParams['axes.unicode_minus'] = False
    inicoding = generateit()[3]
    inidecoding = ga.decodeit(inicoding, boudary[0], brange)
    initfittness = ga.fitnessevaluations(inidecoding)
    decoding = generateit()[1][-1]
    fitnessvalue = generateit()[2][-1]
    fitness = generateit()[4]
    x = np.linspace(boudary[0], boudary[1], 1000)
    fits = x + 10*np.sin(5*x) + 7*np.cos(4*x)
    plt.suptitle("简单遗传算法")
    plt.subplot(121)
    ax = plt.gca()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    plt.plot(x, fits)                                  # 适应度曲线
    plt.scatter(inidecoding, initfittness, c='blue')  # 初始编码
    plt.scatter(decoding, fitnessvalue, c='red')      # 进化后的编码
    plt.xlabel("编码")
    plt.ylabel("适应度值")
    plt.title("适应度曲线")
    plt.subplots_adjust(wspace=0.5)
    plt.subplot(122)
    ax = plt.gca()
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    plt.plot(range(g), [j for j in fitness])         # 适应度值变化曲线
    plt.xlabel("进化次数")
    plt.ylabel("适应度值")
    plt.title("适应度值变化曲线")
    plt.savefig("简单遗传算法")
Ejemplo n.º 2
0
def multiply(coding):
    #
    # 繁衍一代
    # 传入参数,上一代编码coding
    # 返回,新一代编码mutationcoding,新一代解码值decoding,新一代适应度值fitnessvalues
    #
    inipol = coding
    decoding = ga.decodeit(inipol, boudary[0], brange)
    fitnessvalues = ga.fitnessevaluations(decoding)
    copycoding = ga.copyoperator(inipol, fitnessvalues)
    crossovercoding = ga.crossoveroprator(copycoding, crossoverpossibility)
    mutationcoding = ga.mutationoprator(crossovercoding, mutationpossibility, boudary[0], brange)
    return mutationcoding, decoding, fitnessvalues