def setFixedPartitions(self,listTrees,packer,GVector): vector_g = [0] * len(GVector) for tree in listTrees: if(tree.left == None and tree.right == None): numberOfNodes = tree.data.number_of_nodes() if(numberOfNodes<=GVector[0]): packer.items.append(Item('A', round(GVector[0]))) for i in range(len(GVector)-1): if (numberOfNodes < GVector[i+1] and numberOfNodes >= GVector[i]): packer.items.append(Item('A', round(GVector[i+1])))
def getBestPartitionMethod2(self,listTrees,possiblePartitionsCost,vectorG,index,GVector,packer,gDictionary): if(index == len(listTrees)): packer.items = [] for el in range(len(vectorG)): for j in range(int(vectorG[el])): packer.items.append(Item('A', round(GVector[el]))) vectorG = tuple(vectorG) if (vectorG in self.feasibleSets): return 0,[] elif(vectorG not in self.unfeasibleSets and len(packer.items) >= self.k and packer.pack_items(self.k)): # Return True se riesce a farceli stare self.feasibleSets.add(vectorG) return 0,[] elif(vectorG not in self.unfeasibleSets): self.unfeasibleSets.add(vectorG) return float("inf"),[] costs = [] partitions = [] for i in range(len(possiblePartitionsCost[index])): currentG = self.createPartitionNodesMethod2(listTrees, index, i, GVector) newVectorG = (vectorG+currentG) if((index,tuple(newVectorG)) in gDictionary): cost, minP = gDictionary[(index,tuple(newVectorG))] minP = list(minP) else: cost, minP = self.getBestPartitionMethod2(listTrees, possiblePartitionsCost, (vectorG + currentG), index + 1, GVector, packer, gDictionary) gDictionary[(index,tuple(newVectorG))] = cost,tuple(minP) costs.append(cost+possiblePartitionsCost[index][i]) partitions.append(minP) min = np.argmin(costs) partitions[min].append(min) return (costs[min],partitions[min])
def createPartitionNodes(self, listTrees, n, PossiblePartitionsCost, GVector, packer, lastPartitions, indexLastPartitions): partition = [] currentG = np.zeros(len(GVector)) for i in range(len(listTrees)): #tree in listTrees[::-1]: temp = [] temp.append(listTrees[-i - 1]) module = len(PossiblePartitionsCost[-i - 1]) tempPartition = self.getPartitionInTree(temp, n % module, [0]) indexLastPartitions[i] = n lastPartitions[i] = tempPartition n -= (n % module) n /= module if (tempPartition != None): for el in tempPartition: partition.append(el) numberOfNodes = len(el) num = math.ceil( math.log(numberOfNodes, 1 + self.epsilon / 2)) num = math.pow(1 + self.epsilon / 2, num) i = bisect.bisect_left(GVector, num) if i == len(GVector): i -= 1 currentG[i] += 1 packer.items.append(Item('A', round(GVector[i]))) return partition, tuple(currentG)
def getBestPartitionMethod2(self, listTrees, possiblePartitionsCost, vectorG, index, GVector, packer, gDictionary): #dynamic programming part if (index == len(listTrees) ): # if I gerate a partition verify if it is feasible packer.items = [] for el in range(len(vectorG)): for j in range(int(vectorG[el])): packer.items.append(Item('A', round(GVector[el]))) if (len(packer.items) >= self.k and packer.pack_items(self.k) ): # BinPackingProblem(we use the BinPackerDynamic library) return 0, [] # return cost = 0 if feasible return float("inf"), [] # return cost = infinite if unfeasible costs = [] partitions = [] for i in range(len(possiblePartitionsCost[index])): currentG = self.createPartitionNodesMethod2( listTrees, index, i, GVector) # create the G vector for the selected element newVectorG = ( vectorG + currentG ) # cumulate the new G vetor with the one generated in the previous tree if ( (index, tuple(newVectorG)) in gDictionary ): # if I already know the result, stop the recursion and return the result cost, minP = gDictionary[(index, tuple(newVectorG))] minP = list(minP) else: # if I still don' t know the result, calculate it cost, minP = self.getBestPartitionMethod2( listTrees, possiblePartitionsCost, (vectorG + currentG), index + 1, GVector, packer, gDictionary) gDictionary[(index, tuple(newVectorG))] = cost, tuple( minP) # save the result in the dictionary for the future costs.append(cost + possiblePartitionsCost[index][i]) partitions.append(minP) min = np.argmin(costs) partitions[min].append(min) return (costs[min], partitions[min] ) # return the temp-partition with the lowest cost