def __init__(self, parentChromosomes) -> None: """ """ self.parentChromosomes = parentChromosomes self.offsprings = {0: Chromosome(), 1: Chromosome()} self.offspringsItemsToOrder = {0: set(), 1: set()} self.offspringsItemsToOrder[0] = { (item, position) for item, itemDemands in enumerate( InputDataInstance.instance.demandsArrayZipped) for position in range(len(itemDemands)) } nZeros = InputDataInstance.instance.nPeriods - InputDataInstance.instance.demandsArray.sum( ) # print("nZeros : ", nZeros) self.offspringsItemsToOrder[0] = self.offspringsItemsToOrder[0].union({ (-1, i) for i in range(nZeros) }) self.offspringsItemsToOrder[1] = copy.deepcopy( self.offspringsItemsToOrder[0]) self.primeParent = self.parentChromosomes[0] if self.parentChromosomes[ 0] < self.parentChromosomes[1] else self.parentChromosomes[1] self._stopSearchEvents = {0: threading.Event(), 1: threading.Event()}
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 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_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
def test_localSearchPopulation(self): """ """ self.setUpInput() # c = Chromosome.createFromRawDNA([2, 1, 0, 2, 1]) c, population = Chromosome.createFromRawDNA([1, 2, 2, 2, 1, 3, 0, 0]), Population([]) # c, population = Chromosome.createFromIdentifier("36735108634444444144410748754494456471077677765131062777310822666693622666691810555996653211088261109661061512333111010101010910182821010810251292222552188881538111111913393555990000000000000000000000000000000000000000] : 82009, [36753108634444444144410748754494456471077677765131062777310822666693622666691810555996653211088261109661061512333111010101010910182821010810251292222552188881538111111913393555990000000000000000000000000000000000000000"), Population([]) # [36735108634444444144410748754494456471077677765131062777310822666693622666691810555996653211088261109661061512333111010101010910182821010810251292222552188881538111111913393555990000000000000000000000000000000000000000] : 82009, [36753108634444444144410748754494456471077677765131062777310822666693622666691810555996653211088261109661061512333111010101010910182821010810251292222552188881538111111913393555990000000000000000000000000000000000000000] print(c) (LocalSearchEngine()).populate(c, [0, population]) print(population) assert 0
def areItemsSwitchable(self, chromosome, periodGene, altPeriod, periodGeneLowerLimit, periodGeneUpperLimit): """ """ if chromosome.stringIdentifier[altPeriod] > 0: altPeriodGene = chromosome.genesByPeriod[altPeriod] altPeriodGeneLowerLimit, altPeriodGeneUpperLimit = Chromosome.geneLowerUpperLimit( chromosome, altPeriodGene) if (periodGeneLowerLimit <= altPeriod and altPeriod < periodGeneUpperLimit) and ( altPeriodGeneLowerLimit <= periodGene.period and periodGene.period < altPeriodGeneUpperLimit): return True else: if (periodGeneLowerLimit <= altPeriod and altPeriod < periodGeneUpperLimit): return True return False
def searchIndividu(self, chromosome, strategy): """ """ results = [] if strategy == "absolute_mutation": if self._visitedNodes[chromosome.stringIdentifier] is not None: return None print("chrom : ", chromosome, self.searchDepth) # shuffledPeriods = [(period, item) for period, item in enumerate(chromosome.stringIdentifier)] # print("shuffledPeriods : ", shuffledPeriods, chromosome.stringIdentifier) # random.shuffle(shuffledPeriods) orderedGenes = [ gene for itemGenes in chromosome.dnaArray for gene in itemGenes if gene.cost > 0 ] # orderedGenes.sort(key=lambda gene: gene.cost, reverse= True) random.shuffle(orderedGenes) # print("ordered : ", orderedGenes) for periodGene in orderedGenes: print("gene : ", periodGene) periodGeneLowerLimit, periodGeneUpperLimit = Chromosome.geneLowerUpperLimit( chromosome, periodGene) i = 1 backwardPeriod, forwardPeriod = periodGene.period, periodGene.period while True: if forwardPeriod is not None: forwardPeriod = periodGene.period + i if backwardPeriod is not None: backwardPeriod = periodGene.period - i if backwardPeriod is not None and backwardPeriod < 0: backwardPeriod = None if forwardPeriod is not None and forwardPeriod > InputDataInstance.instance.nPeriods - 1: forwardPeriod = None # print(backwardPeriod, forwardPeriod) if backwardPeriod is not None: if self.areItemsSwitchable(chromosome, periodGene, backwardPeriod, periodGeneLowerLimit, periodGeneUpperLimit): evaluationData = self.evaluateItemsSwitch( chromosome, periodGene, backwardPeriod) if strategy == "random_mutation": self.result = self.switchItems( chromosome, periodGene, backwardPeriod, evaluationData) return None if strategy == "population": results.append( self.switchItems(chromosome, periodGene, backwardPeriod, evaluationData)) if strategy == "positive_mutation": if evaluationData["variance"] > 0: self.result = self.switchItems( chromosome, periodGene, backwardPeriod, evaluationData) return None if strategy == "simple_mutation": if evaluationData["variance"] > 0: self.result = self.switchItems( chromosome, periodGene, backwardPeriod, evaluationData) return None results.append(evaluationData) if strategy == "absolute_mutation": if evaluationData["variance"] > 0: results.append(evaluationData) else: backwardPeriod = None if forwardPeriod is not None: if self.areItemsSwitchable(chromosome, periodGene, forwardPeriod, periodGeneLowerLimit, periodGeneUpperLimit): evaluationData = self.evaluateItemsSwitch( chromosome, periodGene, forwardPeriod) if strategy == "random_mutation": self.result = self.switchItems( chromosome, periodGene, forwardPeriod, evaluationData) return None if strategy == "population": results.append( self.switchItems(chromosome, periodGene, forwardPeriod, evaluationData)) if strategy == "positive_mutation": if evaluationData["variance"] > 0: self.result = self.switchItems( chromosome, periodGene, forwardPeriod, evaluationData) return None if strategy == "simple_mutation": if evaluationData["variance"] > 0: self.result = self.switchItems( chromosome, periodGene, forwardPeriod, evaluationData) return None results.append(evaluationData) if strategy == "absolute_mutation": if evaluationData["variance"] > 0: results.append(evaluationData) else: forwardPeriod = None if backwardPeriod is None and forwardPeriod is None: break i += 1 if strategy == "absolute_mutation": if len(results) > 0: self.searchDepth += 1 self._visitedNodes[chromosome.stringIdentifier] = 1 self.searchIndividu(results[-1], strategy) if self._stopSearchEvent.is_set(): return None if strategy == "absolute_mutation": if len(results) == 0: self.result = chromosome self._stopSearchEvent.set() return None elif strategy == "simple_mutation": # self.result = np.random.choice(results) return None elif strategy == "population": self.result = results return None
def evaluateItemsSwitch(self, chromosome, periodGene, altPeriod): """ """ print("Evaluating : ", chromosome, periodGene.period, altPeriod) evaluationData = {"variance": periodGene.cost, "periodGene": {}} if chromosome.stringIdentifier[altPeriod] > 0: altPeriodGene = chromosome.genesByPeriod[altPeriod] evaluationData["variance"] += altPeriodGene.cost nextPeriodGene = None if periodGene.nextGene is None else chromosome.dnaArray[ periodGene.nextGene[0]][periodGene.nextGene[1]] nextAltPeriodGene = None if altPeriodGene.nextGene is None else chromosome.dnaArray[ altPeriodGene.nextGene[0]][altPeriodGene.nextGene[1]] prevPeriodGene = None if periodGene.prevGene is None else chromosome.dnaArray[ periodGene.prevGene[0]][periodGene.prevGene[1]] prevAltPeriodGene = None if altPeriodGene.prevGene is None else chromosome.dnaArray[ altPeriodGene.prevGene[0]][altPeriodGene.prevGene[1]] if nextPeriodGene == altPeriodGene: if nextAltPeriodGene is not None: evaluationData[ "variance"] += nextAltPeriodGene.changeOverCost - InputDataInstance.instance.changeOverCostsArray[ periodGene.item][nextAltPeriodGene.item] evaluationData["altPeriodGene"] = { "changeOverCost": (0 if prevPeriodGene is None else InputDataInstance.instance.changeOverCostsArray[ prevPeriodGene.item][altPeriodGene.item]) } evaluationData["periodGene"][ "changeOverCost"] = InputDataInstance.instance.changeOverCostsArray[ altPeriodGene.item][periodGene.item] evaluationData["periodGene"]["prevGene"] = altPeriodGene evaluationData["periodGene"]["nextGene"] = nextAltPeriodGene evaluationData["altPeriodGene"]["prevGene"] = prevPeriodGene evaluationData["altPeriodGene"]["nextGene"] = periodGene elif nextAltPeriodGene == periodGene: if nextPeriodGene is not None: print( "compris --- : ", nextPeriodGene.changeOverCost, InputDataInstance.instance.changeOverCostsArray[ altPeriodGene.item][nextPeriodGene.item]) evaluationData[ "variance"] += nextPeriodGene.changeOverCost - InputDataInstance.instance.changeOverCostsArray[ altPeriodGene.item][nextPeriodGene.item] evaluationData["periodGene"]["changeOverCost"] = ( 0 if prevAltPeriodGene is None else InputDataInstance.instance.changeOverCostsArray[ prevAltPeriodGene.item][periodGene.item]) evaluationData["altPeriodGene"] = { "changeOverCost": InputDataInstance.instance.changeOverCostsArray[ periodGene.item][altPeriodGene.item] } evaluationData["periodGene"]["prevGene"] = prevAltPeriodGene evaluationData["periodGene"]["nextGene"] = altPeriodGene evaluationData["altPeriodGene"]["prevGene"] = periodGene evaluationData["altPeriodGene"]["nextGene"] = nextPeriodGene else: if nextAltPeriodGene is not None: evaluationData[ "variance"] += nextAltPeriodGene.changeOverCost - InputDataInstance.instance.changeOverCostsArray[ periodGene.item][nextAltPeriodGene.item] if nextPeriodGene is not None: evaluationData[ "variance"] += nextPeriodGene.changeOverCost - InputDataInstance.instance.changeOverCostsArray[ altPeriodGene.item][nextPeriodGene.item] evaluationData["periodGene"]["changeOverCost"] = ( 0 if prevAltPeriodGene is None else InputDataInstance.instance.changeOverCostsArray[ prevAltPeriodGene.item][periodGene.item]) evaluationData["altPeriodGene"] = { "changeOverCost": (0 if prevPeriodGene is None else InputDataInstance.instance.changeOverCostsArray[ prevPeriodGene.item][altPeriodGene.item]) } evaluationData["periodGene"]["prevGene"] = prevAltPeriodGene evaluationData["periodGene"]["nextGene"] = nextAltPeriodGene evaluationData["altPeriodGene"]["prevGene"] = prevPeriodGene evaluationData["altPeriodGene"]["nextGene"] = nextPeriodGene evaluationData["altPeriodGene"][ "stockingCost"] = InputDataInstance.instance.stockingCostsArray[ altPeriodGene.item] * ( InputDataInstance.instance.demandsArrayZipped[ altPeriodGene.item][altPeriodGene.position] - periodGene.period) evaluationData["periodGene"][ "stockingCost"] = InputDataInstance.instance.stockingCostsArray[ periodGene.item] * ( InputDataInstance.instance.demandsArrayZipped[ periodGene.item][periodGene.position] - altPeriod) # print("compris --- : ", evaluationData["variance"], periodGene.stockingCost, periodGene.changeOverCost) evaluationData["variance"] -= ( evaluationData["altPeriodGene"]["stockingCost"] + evaluationData["altPeriodGene"]["changeOverCost"]) else: prevGene0 = Chromosome.prevProdGene(altPeriod, chromosome.dnaArray, chromosome.stringIdentifier) nextGene0 = Chromosome.nextProdGene(altPeriod, chromosome.dnaArray, chromosome.stringIdentifier) if nextGene0 != periodGene and prevGene0 != periodGene: # prevPeriodGene = None if periodGene.prevGene is None else chromosome.dnaArray[periodGene.prevGene[0]][periodGene.prevGene[1]] nextPeriodGene = None if periodGene.nextGene is None else chromosome.dnaArray[ periodGene.nextGene[0]][periodGene.nextGene[1]] if nextPeriodGene is not None: evaluationData[ "variance"] += nextPeriodGene.changeOverCost - ( 0 if periodGene.prevGene is None else InputDataInstance.instance.changeOverCostsArray[ periodGene.prevGene[0]][nextPeriodGene.item]) if nextGene0 is not None: evaluationData[ "variance"] += nextGene0.changeOverCost - InputDataInstance.instance.changeOverCostsArray[ periodGene.item][nextGene0.item] evaluationData["periodGene"]["prevGene"] = prevGene0 evaluationData["periodGene"]["nextGene"] = nextGene0 # else: # pass evaluationData["periodGene"][ "stockingCost"] = InputDataInstance.instance.stockingCostsArray[ periodGene.item] * ( InputDataInstance.instance.demandsArrayZipped[ periodGene.item][periodGene.position] - altPeriod) evaluationData["periodGene"]["changeOverCost"] = ( 0 if prevGene0 is None else InputDataInstance.instance. changeOverCostsArray[prevGene0.item][periodGene.item]) # print("pop : ", evaluationData["periodGene"]["stockingCost"]) evaluationData["variance"] -= ( \ evaluationData["periodGene"]["stockingCost"] \ + evaluationData["periodGene"]["changeOverCost"] ) print("Evaluation result : ", chromosome, periodGene.period, altPeriod, " ---> ", evaluationData) return evaluationData
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
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())
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