Esempio n. 1
0
    def find_tsp_solution(self, fitness_function, dist_function, pop_size, gen,
                          m_rate, elite):
        min_value = 1
        max_value = self.repository.num_nodes
        dim = self.repository.num_nodes + 1

        ga = GA(fitness_function, min_value, max_value, dim, pop_size, elite,
                m_rate)
        ga.initialisation()
        ga.evaluation()

        overall_best = ga.best_chromosome()
        progress = []
        for g in range(gen):
            best_chromosome = ga.best_chromosome()
            progress.append(dist_function(best_chromosome.repres))
            ga.next_generation_pool()
            best_chromosome = ga.best_chromosome()
            if best_chromosome.fitness > overall_best.fitness:
                overall_best = best_chromosome
            print('Best solution in generation ' + str(g) + ' is: x = ' +
                  str(best_chromosome.repres) + ' f(x) = ' +
                  str(best_chromosome.fitness))

        print[i + 1 for i in overall_best.repres]
        print dist_function(overall_best.repres)

        self.repository.write_network("data/graph_out.txt",
                                      overall_best.repres,
                                      dist_function(overall_best.repres))

        plt.plot(progress)
        plt.ylabel('Distance')
        plt.xlabel('Generation')
        plt.show()
Esempio n. 2
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. 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