Exemple #1
0
    def process(self, chromosome, strategy="simple_mutation"):
        """Process the given chromosome in order to return a mutated version
        strategy: random_mutation|absolute_mutation|simple_mutation
        """

        print("mutatiooon", strategy, chromosome, chromosome.dnaArray)

        self._visitedNodes = defaultdict(lambda: None)

        self.searchIndividu(chromosome, strategy)

        # print("cro : ", chromosome.genesByPeriod)
        print("Mutation results : ", strategy, chromosome, self.result)

        c = Chromosome.createFromIdentifier(self.result.stringIdentifier)
        if c.dnaArray != self.result.dnaArray or c.cost != self.result.cost or c.genesByPeriod != self.result.genesByPeriod:
            print("Mutation error : ", self.result, self.result.dnaArray)

        # if strategy != "population":
        #     if self.result.dnaArray != Chromosome.createFromIdentifier(self.result.stringIdentifier).dnaArray:
        #         print("Oaaaaaaaaaaaaaaaaauuuuuuuuuuuuuuuuuuuuuuuuuhhhhhhhhhhhhhhh")
        #         print(self.result.dnaArray, "\n", Chromosome.createFromIdentifier(self.result.stringIdentifier).dnaArray)

        #     print("cro ***** : ", self.result.genesByPeriod)
        return (self.result if self.result is not None else chromosome)
    def test_crossover(self):
        """
        """

        self.setUpInput()

        # cA, cB = Chromosome.createFromIdentifier(stringIdentifier=(2, 1, 1, 2, 0)), Chromosome.createFromIdentifier(stringIdentifier=(2, 1, 0, 1, 2))
        cA, cB = Chromosome.createFromIdentifier(
            stringIdentifier=(0, 1, 2, 2, 2, 3, 1,
                              0)), Chromosome.createFromIdentifier(
                                  stringIdentifier=(0, 0, 2, 2, 2, 1, 3, 1))
        # cA, cB = Chromosome.createFromIdentifier(stringIdentifier=(0, 0, 0, 6, 1, 8, 7, 10, 5, 4, 4, 2, 9, 3, 1)), Chromosome.createFromIdentifier(stringIdentifier=(0, 4, 0, 5, 10, 0, 8, 2, 6, 4, 3, 1, 1, 7, 9))
        # [(5, 6, 10, 8, 0, 0, 9, 7, 0, 4, 4, 2, 1, 1, 3) : 1811, (7, 2, 8, 5, 10, 9, 0, 0, 0, 4, 4, 6, 1, 1, 3) : 1857]

        print(cA, "\n", cB, "\n -----------------------------")
        print((CrossOverOperator([cA, cB])).process())

        assert 0
    def test_mutation(self):
        """
        """

        self.setUpInput()

        # c = Chromosome.createFromIdentifier(stringIdentifier=(2, 1, 0, 1, 2))
        # c = Chromosome.createFromIdentifier(stringIdentifier=(0, 2, 2, 2, 3, 1, 0, 1))
        c = Chromosome.createFromIdentifier(stringIdentifier=(3, 2, 2, 2, 0, 1,
                                                              1, 0))
        # c = Chromosome.createFromIdentifier(stringIdentifier=(0, 0, 0, 2, 4, 4, 8, 7, 5, 3, 1, 1, 6, 10, 9))
        print("Chromosome ", c)
        # # [2, 1, 2, 0, 1]
        LspRuntimeMonitor.mutation_strategy = "random_mutation"
        result = (MutationOperator()).processInstance(c)
        print("1 -- ", result,
              (Chromosome.createFromIdentifier(result.stringIdentifier)))

        assert 0
    def test_evaluateDnaArray(self):
        """
        """

        self.setUpInput()

        chromosome = Chromosome.createFromIdentifier(
            stringIdentifier=(10, 0, 0, 0, 9, 8, 7, 6, 5, 4, 4, 2, 1, 1, 3))
        print(Chromosome.evaluateDnaArray(chromosome.dnaArray))

        assert 0
    def test_localSearch(self):
        """
        """

        self.setUpInput()

        # c = Chromosome.createFromIdentifier(stringIdentifier=(0, 0, 0, 2, 4, 4, 8, 7, 5, 3, 1, 1, 6, 10, 9))
        c = Chromosome.createFromIdentifier(stringIdentifier=(0, 0, 2, 2, 2, 1,
                                                              3, 1))
        # (0, 0, 0, 9, 10, 8, 7, 5, 6, 4, 4, 2, 1, 1, 3)
        print("Input : ", c)
        print("Output : ", (LocalSearchEngine()).process(c, "simple_mutation"))

        assert 0
Exemple #6
0
    def process(self, offspring_result=2):
        """
        """

        if offspring_result not in [1, 2]:
            # TODO: throw an error
            return None, None

        if self.parentChromosomes[0] == self.parentChromosomes[1]:
            return self.parentChromosomes[0], self.parentChromosomes[1]

        # print("Crossover : ", self.parentChromosomes, self.parentChromosomes[0].dnaArray, self.parentChromosomes[1].dnaArray)
        print("Crossover : ", self.parentChromosomes)

        # before launching the recursive search
        gapLength = int(InputDataInstance.instance.nPeriods / 3)
        # random.seed()
        self.crossOverPeriod = random.randint(
            gapLength + 1,
            InputDataInstance.instance.nPeriods - (gapLength + 1))

        # checking the crossover memory for previous occurences of this context
        # memoryResult1 = CrossOverNode.crossOverMemory["db"][((self.parentChromosomes[0]).stringIdentifier, (self.parentChromosomes[1]).stringIdentifier, crossOverPeriod)]
        # if  memoryResult1 is not None:
        #     return memoryResult1

        # memoryResult2 = CrossOverNode.crossOverMemory["db"][((self.parentChromosomes[1]).stringIdentifier, (self.parentChromosomes[0]).stringIdentifier, crossOverPeriod)]
        # if memoryResult2 is not None:
        #     return memoryResult2

        # Initializing offsprings' stringIdentifier property

        # looping
        self.crossOverPeriod = 3
        print("crossOverPeriod : ", self.crossOverPeriod)
        self.offspringLastPlacedGene = {0: None, 1: None}

        self.offsprings[0].stringIdentifier = [
            "*"
        ] * InputDataInstance.instance.nPeriods
        self.offsprings[1].stringIdentifier = [
            "*"
        ] * InputDataInstance.instance.nPeriods

        self.setOffsprings()

        if self.offsprings[0] is not None:
            self.offsprings[0].stringIdentifier = tuple(
                self.offsprings[0].stringIdentifier)
            c = Chromosome.createFromIdentifier(
                self.offsprings[0].stringIdentifier)
            if self.offsprings[0].dnaArray != c.dnaArray:
                print(" Watch out 0", self.offsprings[0].dnaArray, " --- ",
                      c.dnaArray)
        else:
            print("Crossover offspring 0 None")
            # del self.offsprings[0]

        if self.offsprings[1] is not None:
            self.offsprings[1].stringIdentifier = tuple(
                self.offsprings[1].stringIdentifier)
            c = Chromosome.createFromIdentifier(
                self.offsprings[1].stringIdentifier)
            if self.offsprings[1].dnaArray != c.dnaArray:
                print(" Watch out 1", self.offsprings[1].dnaArray)
        else:
            print("Crossover offspring 1 None")
            # del self.offsprings[1]

        print("Cross Over result : ",
              [self.parentChromosomes, self.offsprings])

        # storing this result in the crossover memory before returning
        # with CrossOverNode.crossOverMemory["lock"]:
        #     CrossOverNode.crossOverMemory["db"][((self.parentChromosomes[0]).stringIdentifier, (self.parentChromosomes[1]).stringIdentifier, crossOverPeriod)] = tuple(self.offsprings.values())

        return tuple(self.offsprings.values())