使用 python 和 deap 的遗传算法,在图中找到最短路径

问题描述 投票:0回答:0

我有一个当前代码,我想在其中添加一个停止条件。因此,如果最佳适应度暂时没有改变,程序将停止计算。我该怎么做?

程序正在寻找从起始城市到任何其他城市的最短路径。城市之间的距离表示为矩阵 D.

from deap import base, algorithms
from deap import creator
from deap import tools

from graph_show import show_graph

import random
import matplotlib.pyplot as plt
import numpy as np

inf = 100
D = ((0, 3, 1, 3, inf, inf),
     (3, 0, 4, inf, inf, inf),
     (1, 4, 0, inf, 7, 5),
     (3, inf, inf, 0, inf, 2),
     (inf, inf, 7, inf, 0, 4),
     (inf, inf, 5, 2, 4, 0))

startV = 0             
LENGTH_D = len(D)
LENGTH_CHROM = len(D)*len(D[0])   

POPULATION_SIZE = 500   
P_CROSSOVER = 0.9       
P_MUTATION = 0.1        
MAX_GENERATIONS = 30   
HALL_OF_FAME_SIZE = 1

hof = tools.HallOfFame(HALL_OF_FAME_SIZE)

RANDOM_SEED = 42
random.seed(RANDOM_SEED)

creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)

toolbox = base.Toolbox()
toolbox.register("randomOrder", random.sample, range(LENGTH_D), LENGTH_D)
toolbox.register("individualCreator", tools.initRepeat, creator.Individual, toolbox.randomOrder, LENGTH_D)
toolbox.register("populationCreator", tools.initRepeat, list, toolbox.individualCreator)

population = toolbox.populationCreator(n=POPULATION_SIZE)


def dikstryFitness(individual):
    s = 0
    for n, path in enumerate(individual):
        path = path[:path.index(n)+1]

        si = startV
        for j in path:
            s += D[si][j]
            si = j

    return s,         # кортеж

def cxOrdered(ind1, ind2):
    for p1, p2 in zip(ind1, ind2):
        tools.cxOrdered(p1, p2)

    return ind1, ind2

def mutShuffleIndexes(individual, indpb):
    for ind in individual:
        tools.mutShuffleIndexes(ind, indpb)

    return individual,


toolbox.register("evaluate", dikstryFitness)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("mate", cxOrdered)
toolbox.register("mutate", mutShuffleIndexes, indpb=1.0/LENGTH_CHROM/10)

stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("min", np.min)
stats.register("avg", np.mean)

MAX_FITNESS_VALUE = 100  # maximum fitness value to reach
stop_criteria = lambda _, __, g: g['min'] <= MAX_FITNESS_VALUE
population, logbook = algorithms.eaSimple(population, toolbox,
                                          cxpb=P_CROSSOVER/LENGTH_D,
                                          mutpb=P_MUTATION/LENGTH_D,
                                          ngen=MAX_GENERATIONS,
                                          halloffame=hof,
                                          stats=stats,
                                          verbose=True,
                                          stop_criteria=stop_criteria)

maxFitnessValues, meanFitnessValues = logbook.select("min", "avg")

best = hof.items[0]
print(best)

plt.plot(maxFitnessValues, color='red')
plt.plot(meanFitnessValues, color='green')
plt.xlabel('Generation')
plt.ylabel('Max\mid fitness')

fig, ax = plt.subplots()
show_graph(ax, best)
plt.show()

我试着询问 chatgpt,但没用 :D 我认为 deap 应该可以选择使用 eaSimple 函数来执行此操作,但我不知道它是什么。

python evolutionary-algorithm deap
© www.soinside.com 2019 - 2024. All rights reserved.