Ejemplo n.º 1
0
    def __init__(self, learningData, testData, size):
        self.chromosomes = []
        self.learningData = learningData
        self.testData = testData

        for index in range(size):
            chromosome = Chromosome()
            chromosome.initialise()
            self.chromosomes.append(chromosome)
    def solve(self):
        graph = read_graph_from_file(self.__file)

        self.__probParameters = ProblemParams(graph, get_route_length)

        ga = GA(self.__gaParameters, self.__probParameters)
        ga.initialisation()
        ga.evaluation()

        best = Chromosome(self.__probParameters)

        for generation in range(self.__gaParameters.noOfGenerations):
            ga.one_generation_steady_state()

            bestChromo = ga.best_chromosome()
            print('Generatia ' + str(generation) +
                  ' are cel mai bun cromozom:' + str(best.representation) +
                  ' cu fitness ' + str(bestChromo.fitness))
            if bestChromo.fitness < best.fitness:
                best = bestChromo

        print(best.representation)

        print(
            get_route_length(best.representation,
                             self.__probParameters.network))
Ejemplo n.º 3
0
    def one_generation_steady_state(self):
        bestOff = Chromosome(self.problParams)

        for _ in range(self.params.populationSize):
            p1 = self.population[self.selection()]
            p2 = self.population[self.selection()]

            off = p1.crossover(p2, self.params.crossoverProb)

            off.mutation()

            off.fitness = self.problParams.function(off.representation,
                                                    self.problParams.network)

            if off.fitness < bestOff.fitness:
                bestOff = off

        self.population.remove(self.worst_chromosome())
        self.population.append(bestOff)
Ejemplo n.º 4
0
 def initialisation(self):
     for _ in range(0, self.__param['popSize']):
         c = Chromosome(self.__problParam)
         self.__population.append(c)
Ejemplo n.º 5
0
 def initialization(self):
     for _ in range(0, self.__populationSize):
         c = Chromosome(self.__problParam)
         self.__population.append(c)
     self.evaluation(self.__population)
Ejemplo n.º 6
0
    def crossover(chromosome1, chromosome2):

        offspring1 = Chromosome()
        offspring2 = Chromosome()

        stop = True

        while stop:
            startChromosome1 = randint(0, chromosome1.size - 1)
            endChromosome1 = chromosome1.traverse(startChromosome1)
            startChromosome2 = randint(0, chromosome2.size - 1)
            endChromosome2 = chromosome2.traverse(startChromosome2)

            if len(offspring1.representation) > endChromosome1 + (endChromosome2 - startChromosome2 - 1) + (
                    chromosome1.size - endChromosome1 - 1) and \
                len(offspring2.representation) > endChromosome2 + (endChromosome1 - startChromosome1 - 1) + (
                    chromosome2.size - endChromosome2 - 1):
                stop = False

        # startChromosome1 = randint(0, chromosome1.size - 1)
        # endChromosome1 = chromosome1.traverse(startChromosome1)
        # startChromosome2 = randint(0, chromosome2.size - 1)
        # endChromosome2 = chromosome2.traverse(startChromosome2)

        # print("start1: " + str(startChromosome1))
        # print("end1: " + str(endChromosome1))
        # print("size1: " + str(chromosome1.size))
        # print("start2: " + str(startChromosome2))
        # print("end2: " + str(endChromosome2))
        # print("size2: " + str(chromosome2.size))
        #
        # print("off1 size: " + str(len(offspring1.representation)))
        # print("off2 size: " + str(len(offspring2.representation)))



        i = -1
        for i in range(startChromosome1):
            offspring1.representation[i] = chromosome1.representation[i]
            # print("current i value: " + str(i))
        for j in range(startChromosome2, endChromosome2):
            i += 1
            # print("current i value: " + str(i))
            offspring1.representation[i] = chromosome2.representation[j]
        for j in range(endChromosome1, chromosome1.size):
            i += 1
            # print("current i value: " + str(i))
            offspring1.representation[i] = chromosome1.representation[j]
        offspring1.size = i + 1

        i = -1
        for i in range(startChromosome2):
            # print("current i value: " + str(i))
            offspring2.representation[i] = chromosome2.representation[i]
        for j in range(startChromosome1, endChromosome1):
            i += 1
            # print("current i value: " + str(i))
            offspring2.representation[i] = chromosome1.representation[j]
        for j in range(endChromosome2, chromosome2.size):
            i += 1
            # print("current i value: " + str(i))
            offspring2.representation[i] = chromosome2.representation[j]
        offspring2.size = i + 1

        return [offspring1, offspring2]
Ejemplo n.º 7
0
 def initialisation(self):
     for i in range(self.params.populationSize):
         chromo = Chromosome(self.problParams)
         self.population.append(chromo)