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 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