def analyzeAllBenchmarksAreaOnly(): for dataset in utils.benchmarks: (root, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) anneal(dataset, annealingParameters,metrics.costArea) alignImages.createImages('Output/initialMatchPairs_','Output/annealing_','Output/Comparison of Initial to Annealed (area cost)_') #analyzeAllBenchmarksAreaOnly()
def analyzeAllBenchmarks(): for dataset in utils.benchmarks: # Initial Floorplan (root, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) # Anneal - Consider area only anneal(dataset, annealingParameters, metrics.costArea, "Output/annealing_", 'Annealed Floorplan - Area Only') # Anneal - Consider area and connections length = len(rectangles) lambdas = copy(matrix) costParameters = classes.CostParameters(np.ones((length, length)), 0.5, 1, lambdas, dictionary) # Curry cost function def newCost(inRoot): #return metrics.costWithLamdas(rectangles, costParameters) return metrics.costWithLamdasFromRoot(inRoot, costParameters) anneal(dataset, annealingParameters, newCost, "Output/annealingNewCost_", 'Annealed Floorplan - Area and Connections') alignImages.createImages( 'Output/initialMatchPairs_', 'Output/annealing_', 'Output/annealingNewCost_', 'Output/Comparison of Initial to Annealed (Area and All)_')
def analyzeAllBenchmarksAreaOnly(): for dataset in utils.benchmarks: (root, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) anneal(dataset, annealingParameters, metrics.costArea) alignImages.createImages( 'Output/initialMatchPairs_', 'Output/annealing_', 'Output/Comparison of Initial to Annealed (area cost)_') #analyzeAllBenchmarksAreaOnly()
def analyzeAllBenchmarks(): for dataset in utils.benchmarks: # Initial Floorplan (root, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) # Anneal - Consider area only anneal(dataset, annealingParameters,metrics.costArea,"Output/final_annealing_",'Annealed Floorplan - Area Only') # Anneal - Consider area and connections length = len(rectangles) lambdas = copy(matrix) costParameters = classes.CostParameters(np.ones((length,length)),0.5,2,lambdas,dictionary) # Curry cost function def newCost(inRoot): #return metrics.costWithLamdas(rectangles, costParameters) return metrics.costWithLamdasFromRoot(inRoot, costParameters) anneal(dataset, annealingParameters,newCost,"Output/final_annealingNewCost_",'Annealed Floorplan - Area and Connections') alignImages.createImagesForFinal('Output/final_annealing_','Output/final_annealingNewCost_','Output/Final Comparison_')
def anneal(dataset, annealingParameters, cost, outputPrefix,scenario): # Solutions will be represented by roots to a slicing tree # Setup updateMethods = [updateSolutionRandomChain,updateSwapOperands] #updateMethods = [updateSolutionRandomChain,updateSwapOperands,updateFlipNode] #updateMethods = [updateSolutionRandomChain,updateSolutionFitNode,updateSwapOperands,updateFlipNode] #updateMethods = [updateSolutionRandomChain] start = time.time() (initialSolution, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) count = 0 currentSolution = initialSolution bestSolution = initialSolution T = annealingParameters.T M = 0 MT = 1 uphill = 0 n = len(rectangles) N = annealingParameters.k * n reject=0 timeDiff = time.time() - start #Iterations while (reject/MT <= 1-annealingParameters.thresholdAccepted) and T>=annealingParameters.thresholdTemp and timeDiff<=annealingParameters.thresholdTime: (flagChain,flagFit,flagFlip) = (0,0,0) MT = 1 # start at 1 to avoid divide by 0 uphill = 0 reject = 0 timeDiff = time.time() - start while uphill<=N and MT<=2*N: # randomly choose which method to use for updating randomIndex = random.randint(0,len(updateMethods)-1) newSolution = updateMethods[randomIndex](currentSolution,rectangles,dictionary) #print(cost(newSolution)) count+=1 # to track total iterations. Should not be updated elsewhere. MT += 1 deltaCost = cost(newSolution) - cost(currentSolution) # Updates if deltaCost <= 0 or random.uniform(0,1) < math.exp(-deltaCost/T): if deltaCost>0: uphill += 1 currentSolution = newSolution if cost(currentSolution)<cost(bestSolution): bestSolution = currentSolution else: reject += 1 timeDiff = time.time() - start if bestSolution == currentSolution: print(dataset + ': ' + str(count) + '= ' + str(cost(bestSolution)) + ' T=' + str(T) + ' timeDiff=' + str(timeDiff)) T = annealingParameters.r*T newRectangles = [] def constructNewRectangleMatrix(node): if node.type == 'rect': newRectangles.append(node.rect) else: if node.fitRect is not None: constructNewRectangleMatrix(node.left.fitRect) if node.left is not None: constructNewRectangleMatrix(node.left) if node.right is not None: constructNewRectangleMatrix(node.right) constructNewRectangleMatrix(bestSolution) print("Total area for " + dataset + " = " + str(bestSolution.w*bestSolution.h)) print("(countChain,countFit,countFlip)="+str((countChain,countFit,countFlip))) pfp.printFloorplan(newRectangles,dataset,outputPrefix + dataset + '.png',scenario) return (bestSolution, rectangles, dictionary, matrix)
def anneal(dataset, annealingParameters, cost, outputPrefix, scenario): # Solutions will be represented by roots to a slicing tree # Setup updateMethods = [updateSolutionRandomChain, updateSwapOperands] #updateMethods = [updateSolutionRandomChain,updateSwapOperands,updateFlipNode] #updateMethods = [updateSolutionRandomChain,updateSolutionFitNode,updateSwapOperands,updateFlipNode] #updateMethods = [updateSolutionRandomChain] start = time.time() (initialSolution, rectangles, dictionary, matrix) = initializeMatchPairs(dataset) count = 0 currentSolution = initialSolution bestSolution = initialSolution T = annealingParameters.T M = 0 MT = 1 uphill = 0 n = len(rectangles) N = annealingParameters.k * n reject = 0 timeDiff = time.time() - start #Iterations while ( reject / MT <= 1 - annealingParameters.thresholdAccepted ) and T >= annealingParameters.thresholdTemp and timeDiff <= annealingParameters.thresholdTime: (flagChain, flagFit, flagFlip) = (0, 0, 0) MT = 1 # start at 1 to avoid divide by 0 uphill = 0 reject = 0 timeDiff = time.time() - start while uphill <= N and MT <= 2 * N: # randomly choose which method to use for updating randomIndex = random.randint(0, len(updateMethods) - 1) newSolution = updateMethods[randomIndex](currentSolution, rectangles, dictionary) #print(cost(newSolution)) count += 1 # to track total iterations. Should not be updated elsewhere. MT += 1 deltaCost = cost(newSolution) - cost(currentSolution) # Updates if deltaCost <= 0 or random.uniform(0, 1) < math.exp( -deltaCost / T): if deltaCost > 0: uphill += 1 currentSolution = newSolution if cost(currentSolution) < cost(bestSolution): bestSolution = currentSolution else: reject += 1 timeDiff = time.time() - start if bestSolution == currentSolution: print(dataset + ': ' + str(count) + '= ' + str(cost(bestSolution)) + ' T=' + str(T) + ' timeDiff=' + str(timeDiff)) T = annealingParameters.r * T newRectangles = [] def constructNewRectangleMatrix(node): if node.type == 'rect': newRectangles.append(node.rect) else: if node.fitRect is not None: constructNewRectangleMatrix(node.left.fitRect) if node.left is not None: constructNewRectangleMatrix(node.left) if node.right is not None: constructNewRectangleMatrix(node.right) constructNewRectangleMatrix(bestSolution) print("Total area for " + dataset + " = " + str(bestSolution.w * bestSolution.h)) print("(countChain,countFit,countFlip)=" + str((countChain, countFit, countFlip))) pfp.printFloorplan(newRectangles, dataset, outputPrefix + dataset + '.png', scenario) return (bestSolution, rectangles, dictionary, matrix)