Esempio n. 1
0
def main():
    graph = read_graph_from_file(input_file)

    gaParams = GAParams(populationSize=20, noOfGenerations=1000, crossoverProb=0.7, mutationProb=0.15)
    problemParams = ProblemParams(network=graph, dim=len(graph), function=calculate_route_length)

    ga = GA(gaParams, problemParams)
    ga.initialisation()
    ga.evaluation()

    allBestFitnesses = []
    generationsBest = []
    overallBest = Chromosome(problemParams)

    for generation in range(gaParams.noOfGenerations):
        ga.oneGenerationSteadyState()

        bestChromo = ga.bestChromosome()
        print('Best solution in generation ' + str(generation) + ' f(x) = ' + str(bestChromo.fitness))
        allBestFitnesses.append(bestChromo.fitness)
        generationsBest.append(bestChromo)
        if bestChromo.fitness < overallBest.fitness:
            overallBest = bestChromo

    print(overallBest.representation)
    print(calculate_route_length(overallBest.representation, problemParams.network))