Пример #1
0
    def switchItems(self, chromosome, periodGene, altPeriod, evaluationData):
        """
        """

        mutation = Chromosome()
        mutation.stringIdentifier = self.mutationStringIdentifier(
            chromosome.stringIdentifier, periodGene, altPeriod)
        mutation.dnaArray = copy.deepcopy(chromosome.dnaArray)
        mutation.genesByPeriod = copy.deepcopy(chromosome.genesByPeriod)
        mutation.cost = chromosome.cost - evaluationData["variance"]

        period = (
            mutation.dnaArray[periodGene.item][periodGene.position]).period
        (mutation.dnaArray[periodGene.item][periodGene.position]
         ).period = altPeriod
        (mutation.dnaArray[periodGene.item][periodGene.position]
         ).changeOverCost = evaluationData["periodGene"]["changeOverCost"]
        (mutation.dnaArray[periodGene.item][periodGene.position]
         ).stockingCost = evaluationData["periodGene"]["stockingCost"]
        if "prevGene" in evaluationData["periodGene"]:
            (mutation.dnaArray[periodGene.item][periodGene.position]
             ).prevGene = None if evaluationData["periodGene"][
                 "prevGene"] is None else (
                     (evaluationData["periodGene"]["prevGene"]).item,
                     (evaluationData["periodGene"]["prevGene"]).position)
        if "nextGene" in evaluationData["periodGene"]:
            (mutation.dnaArray[periodGene.item][periodGene.position]
             ).nextGene = None if evaluationData["periodGene"][
                 "nextGene"] is None else (
                     (evaluationData["periodGene"]["nextGene"]).item,
                     (evaluationData["periodGene"]["nextGene"]).position)
        (mutation.dnaArray[periodGene.item][periodGene.position]
         ).cost = evaluationData["periodGene"][
             "stockingCost"] + evaluationData["periodGene"]["changeOverCost"]

        if chromosome.stringIdentifier[altPeriod] == 0:
            # genesByPeriod
            del mutation.genesByPeriod[periodGene.period]
            mutation.genesByPeriod[altPeriod] = mutation.dnaArray[
                periodGene.item][periodGene.position]

        else:
            altPeriodGene = chromosome.genesByPeriod[altPeriod]

            # genesByPeriod
            mutation.genesByPeriod[periodGene.period] = mutation.dnaArray[
                altPeriodGene.item][altPeriodGene.position]
            mutation.genesByPeriod[altPeriod] = mutation.dnaArray[
                periodGene.item][periodGene.position]

            print("printo : ", period)
            (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
             ).period = period
            (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
             ).changeOverCost = evaluationData["altPeriodGene"][
                 "changeOverCost"]
            (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
             ).stockingCost = evaluationData["altPeriodGene"]["stockingCost"]
            if "prevGene" in evaluationData["altPeriodGene"]:
                (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
                 ).prevGene = None if evaluationData["altPeriodGene"][
                     "prevGene"] is None else (
                         (evaluationData["altPeriodGene"]["prevGene"]).item,
                         (evaluationData["altPeriodGene"]["prevGene"]
                          ).position)
            if "nextGene" in evaluationData["altPeriodGene"]:
                (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
                 ).nextGene = None if evaluationData["altPeriodGene"][
                     "nextGene"] is None else (
                         (evaluationData["altPeriodGene"]["nextGene"]).item,
                         (evaluationData["altPeriodGene"]["nextGene"]
                          ).position)
            (mutation.dnaArray[altPeriodGene.item][altPeriodGene.position]
             ).cost = evaluationData["altPeriodGene"][
                 "stockingCost"] + evaluationData["altPeriodGene"][
                     "changeOverCost"]

            if altPeriodGene.prevGene is not None:
                (mutation.dnaArray[altPeriodGene.prevGene[0]]
                 [altPeriodGene.prevGene[1]]).nextGene = (periodGene.item,
                                                          periodGene.position)
            if periodGene.nextGene is not None:
                (mutation.dnaArray[periodGene.nextGene[0]]
                 [periodGene.nextGene[1]]).prevGene = (altPeriodGene.item,
                                                       altPeriodGene.position)

        return mutation
Пример #2
0
    def createMutatedChromosome(cls, chromosome, swap):
        """
        """

        # checking if this combination of chromosome swap has already been visited
        chromosome1, chromosome2, theChromosome = LocalSearchNode.mutationsMemory[
            "db"][(chromosome.stringIdentifier, swap[0],
                   swap[1])], LocalSearchNode.mutationsMemory["db"][(
                       chromosome.stringIdentifier, swap[1], swap[0])], None
        if chromosome1 is not None:
            theChromosome = chromosome1

        if chromosome2 is not None:
            theChromosome = chromosome2

        if theChromosome is not None:
            # print("SAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWWWWWWWWWWWWWWWWWWWWWWWW")
            return theChromosome

        # Making up the stringIdentifier of the result chromosome
        stringIdentifier = list(chromosome.stringIdentifier)
        dnaArray = None

        gene1Item, gene1Position = swap[0][0], swap[0][1]
        # print("swap : ", swap, chromosome, chromosome.dnaArray, swap[1][0] == -1)
        if swap[1][0] == -1:
            newPeriod = swap[1][1]
            stringIdentifier[newPeriod] = gene1Item + 1
            stringIdentifier[(
                chromosome.dnaArray[gene1Item][gene1Position]).period] = 0
            stringIdentifier = tuple(stringIdentifier)
            if Chromosome.pool[stringIdentifier] is not None:
                with LocalSearchNode.mutationsMemory["lock"]:
                    LocalSearchNode.mutationsMemory["db"][(
                        stringIdentifier, swap[0],
                        swap[1])] = Chromosome.pool[stringIdentifier]
                return Chromosome.pool[stringIdentifier]

            dnaArray = copy.deepcopy(chromosome.dnaArray)
            gene1 = dnaArray[gene1Item][gene1Position]

            cost = chromosome.cost
            cost -= gene1.cost

            nextGene1, nextGene0 = Chromosome.nextProdGene(
                gene1.period, dnaArray,
                chromosome.stringIdentifier), Chromosome.nextProdGene(
                    newPeriod, dnaArray, chromosome.stringIdentifier)
            prevGene0 = Chromosome.prevProdGene(newPeriod, dnaArray,
                                                chromosome.stringIdentifier)
            condition1 = nextGene0 is not None and nextGene0 == gene1
            condition2 = prevGene0 is not None and prevGene0 == gene1

            if not (condition1 or condition2):

                # print("nextGene1, nextGene0 : ", nextGene1, nextGene0)
                cost -= nextGene1.changeOverCost if nextGene1 is not None else 0
                cost -= nextGene0.changeOverCost if nextGene0 is not None else 0

                if nextGene1 is not None:
                    # print("before nextGene1 : ", nextGene1, nextGene1.changeOverCost)
                    nextGene1.prevGene = gene1.prevGene
                    nextGene1.calculateChangeOverCost()
                    nextGene1.calculateCost()
                    # print("after nextGene1 : ", nextGene1, nextGene1.changeOverCost)
                    cost += nextGene1.changeOverCost

                if nextGene0 is not None:
                    # print("before nextGene0 : ", nextGene0, nextGene0.changeOverCost)
                    (dnaArray[gene1Item][gene1Position]
                     ).prevGene = nextGene0.prevGene

                    nextGene0.prevGene = (gene1.item, gene1.position)
                    nextGene0.calculateChangeOverCost()
                    nextGene0.calculateCost()
                    # print("after nextGene0 : ", nextGene0, nextGene0.changeOverCost)
                    cost += nextGene0.changeOverCost
                else:
                    (dnaArray[gene1Item][gene1Position]).prevGene = (
                        prevGene0.item, prevGene0.position)

            (dnaArray[gene1Item][gene1Position]).period = newPeriod
            (dnaArray[gene1Item][gene1Position]).calculateChangeOverCost()
            (dnaArray[gene1Item][gene1Position]).calculateStockingCost()
            (dnaArray[gene1Item][gene1Position]).calculateCost()

            # print("Ending with gene1 : ", (dnaArray[gene1Item][gene1Position]))

            cost += (dnaArray[gene1Item][gene1Position]).cost

            # print("kokooooooooooo : ", cost, stringIdentifier, dnaArray)

        else:
            gene2Item, gene2Position = swap[1][0], swap[1][1]
            period1, period2 = (
                chromosome.dnaArray[gene1Item][gene1Position]).period, (
                    chromosome.dnaArray[gene2Item][gene2Position]).period
            stringIdentifier[period1] = gene2Item + 1
            stringIdentifier[period2] = gene1Item + 1
            stringIdentifier = tuple(stringIdentifier)

            if Chromosome.pool[stringIdentifier] is not None:
                with LocalSearchNode.mutationsMemory["lock"]:
                    LocalSearchNode.mutationsMemory["db"][(
                        stringIdentifier, swap[0],
                        swap[1])] = Chromosome.pool[stringIdentifier]
                return Chromosome.pool[stringIdentifier]

            dnaArray = copy.deepcopy(chromosome.dnaArray)

            # fixing the chromosome dnaArray and calculating the cost
            # print("dnaArray : ", dnaArray)
            gene1 = (dnaArray[gene1Item][gene1Position])
            gene2 = (dnaArray[gene2Item][gene2Position])

            cost = chromosome.cost

            cost -= (gene1.cost + gene2.cost)

            # print("preveeees --- : ", gene1.prevGene, gene2.prevGene)

            if gene1.prevGene == (gene2Item, gene2Position):
                gene1.prevGene = gene2.prevGene
                gene2.prevGene = (gene1.item, gene1.position)
                nextGene = Chromosome.nextProdGene(gene1.period, dnaArray,
                                                   chromosome.stringIdentifier)

                # print("before before nextGene A: ", nextGene)

                if nextGene is not None:
                    # print("before nextGene A: ", nextGene, nextGene.changeOverCost)
                    cost -= nextGene.changeOverCost
                    prevGene = (gene2.item, gene2.position)
                    # print("prevGene A: ", prevGene)
                    nextGene.prevGene = prevGene
                    nextGene.calculateChangeOverCost()
                    nextGene.calculateCost()
                    cost += nextGene.changeOverCost
                    # print("after nextGene A: ", nextGene, nextGene.changeOverCost)

            elif gene2.prevGene == (gene1Item, gene1Position):
                gene2.prevGene = gene1.prevGene
                gene1.prevGene = (gene2.item, gene2.position)
                nextGene = Chromosome.nextProdGene(gene2.period, dnaArray,
                                                   chromosome.stringIdentifier)

                # print("before before nextGene B: ", nextGene)

                if nextGene is not None:
                    # print("before nextGene B: ", nextGene, nextGene.changeOverCost)
                    cost -= nextGene.changeOverCost
                    prevGene = (gene1.item, gene1.position)
                    # print("prevGene B: ", prevGene)
                    nextGene.prevGene = prevGene
                    nextGene.calculateChangeOverCost()
                    nextGene.calculateCost()
                    cost += nextGene.changeOverCost
                    # print("after nextGene B: ", nextGene, nextGene.changeOverCost)

            else:
                gene1.prevGene, gene2.prevGene = gene2.prevGene, gene1.prevGene
                nextGene1, nextGene2 = Chromosome.nextProdGene(
                    gene1.period, dnaArray,
                    chromosome.stringIdentifier), Chromosome.nextProdGene(
                        gene2.period, dnaArray, chromosome.stringIdentifier)

                # print("before before nextGene1 nextGene2 : ", nextGene1, nextGene2)

                if nextGene1 is not None:
                    # print("before nextGene1 : ", nextGene1, nextGene1.changeOverCost)
                    cost -= nextGene1.changeOverCost
                    prevGene = (gene2.item, gene2.position)
                    # print("prevGene : ", prevGene)
                    nextGene1.prevGene = prevGene
                    nextGene1.calculateChangeOverCost()
                    nextGene1.calculateCost()
                    cost += nextGene1.changeOverCost
                    # print("after nextGene1 : ", nextGene1, nextGene1.changeOverCost)

                if nextGene2 is not None:
                    # print("before nextGene2 : ", nextGene2, nextGene2.changeOverCost)
                    cost -= nextGene2.changeOverCost
                    prevGene = (gene1.item, gene1.position)
                    # print("prevGene : ", prevGene)
                    nextGene2.prevGene = prevGene
                    nextGene2.calculateChangeOverCost()
                    nextGene2.calculateCost()
                    cost += nextGene2.changeOverCost
                    # print("after nextGene2 : ", nextGene2, nextGene2.changeOverCost)

            gene1.period, gene2.period = gene2.period, gene1.period

            gene1.calculateStockingCost()
            gene1.calculateChangeOverCost()
            gene1.calculateCost()

            gene2.calculateStockingCost()
            gene2.calculateChangeOverCost()
            gene2.calculateCost()

            cost += (gene1.cost + gene2.cost)

        # print("Coooooost : ", cost, stringIdentifier, dnaArray, Chromosome.createFromIdentifier(stringIdentifier))

        result = Chromosome()
        result.dnaArray = dnaArray
        result.stringIdentifier = stringIdentifier
        result.cost = cost

        with LocalSearchNode.mutationsMemory["lock"]:
            LocalSearchNode.mutationsMemory["db"][(result.stringIdentifier,
                                                   swap[0], swap[1])] = result
        return result