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))
Esempio n. 2
0
def main():
    net = readFile("date2.txt")
    # print(net['mat'])
    dim = net['nrCities']

    problParams = {'dim': dim}
    gaParams = {'dimPop': 100, 'nrGen': 200}
    ga = GA(net['mat'], problParams, gaParams)
    ga.initPopulation()
    ga.evaluateFitness()

    maxFitness = -1
    bestRepres = []
    filename = "hardE_out.txt"
    f = open(filename, "w")

    for gen in range(gaParams['nrGen']):
        bestChrom = ga.bestChromosome()
        bestSolution = ga.bestChromosome().repres
        bestSolutionFitness = ga.bestChromosome().fitness

        if bestSolutionFitness > maxFitness:
            maxFitness = bestSolutionFitness
            bestRepres = bestSolution
        f.write("best chromosome in generation " + str(gen) + " is " +
                str(bestSolution) + " with distance = " +
                str(ga.getDistance(bestChrom)) + " with fitness = " +
                str(bestSolutionFitness) + "\n")
        # ga.oneGeneration()
        ga.oneGenerationElitism()

    bestChromo = ga.bestChromosome()
    repres = bestChromo.repres
    repres.append(bestChromo.repres[0])
    # for i in range(len(repres)):
    #  repres[i] += 1

    f.write("best solution over all: " + str(repres) + " with distance = " +
            str(ga.getDistance(bestChromo)) + " and fitness = " +
            str(bestChromo.fitness) + "\n")
    f.close()
Esempio n. 3
0
    def ga(self):
        network = self.__repo.read_file()
        self.__probParam['noNodes'] = network['noNodes']
        self.__probParam['mat'] = network['mat']
        ga = GA(self.__param, self.__probParam)
        ga.initialisation()
        ga.evaluation()
        g = 0
        fitnessuri = []
        while g < 500:
            # ga.oneGenerationElitism()
            ga.oneGenerationElitism2()
            g += 1
            best_chromo = ga.bestChromosome()
            print('Best solution in generation ' + str(g) + ' is: x = ' +
                  str(best_chromo.repres) + '\nhas fit: ' +
                  str(best_chromo.fitness) + '\n')
            fitnessuri.append(best_chromo.fitness)

        return fitnessuri