def testStochasticHillClimbingSearch(self): from StochasticAlgorithms.StochasticHillClimbing import search # Problem Configuration numBits = 64 # Algorithm Configuration maxIterations = 1000 # Execute the SHC algorithm result = search(numBits, maxIterations) print "Stochastic Hill Climbing Search Results : " print "*" * 20 print "SHC Iteration " print "*" * 20 print result["iteration"] print "*" * 20 print "*" * 20 print "Initial One Max Count" print "*" * 20 print result["initialCost"] print "*" * 20 print "Final One Max Count" print "*" * 20 print result["cost"] print "*" * 20 print "*" * 20 print "*" * 20 print "Search FormattedOutput : (Final- Initial)/Iteration" print "*" * 20 efficacy = round( float(result["cost"] - result["initialCost"]) / float(result["iteration"]), 2) print efficacy
def testTabuSearch(self): from StochasticAlgorithms.TabuSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxIterations = 100 maxTabuCount = 15 maxCandidates = 50 # Execute the algorithm result = search(self.TSPLIB, maxIterations, maxTabuCount, maxCandidates) tspResult = TSPResult(7542, "Tabu Search Results") print tspResult.FormattedOutput(result)
def testIteratedLocalSearch(self): from StochasticAlgorithms.IteratedLocalSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxIterations = 100 maxNoImprove = 50 searchHistoryToKeep = 1 # since we don't plan to use it now # Execute the Algorithm result = search(self.TSPLIB, maxIterations, maxNoImprove, searchHistoryToKeep) tspResult = TSPResult(7542, "Iterated Local Search Results") print tspResult.FormattedOutput(result)
def testReactiveTabuSearch(self): from StochasticAlgorithms.ReactiveTabuSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxIterations = 100 maxCandidates = 50 increase = 1.3 decrease = 0.9 #Execute the algorithm result = search(self.TSPLIB, maxIterations, increase, decrease, maxCandidates) tspResult = TSPResult(7542, "Reactive Tabu Search") print tspResult.FormattedOutput(result)
def testRandomSearch(self): from StochasticAlgorithms.RandomSearch import search # Problem Configuration searchVector = [-5, 5] size = 2 # Algorithm Configuration iterations = 10000 # Execute the random search algorithm # Outputs a tuple containing the best cost and best input values result = search(searchVector, iterations, size) self.assertTrue( basinFunction(result["vector"]) == result["cost"], "Failed to get correct best basin value.") basin = BasinResult("Random Search") print basin.FormattedOutput(result)
def testGuidedLocalSearch(self): from StochasticAlgorithms.GuidedLocalSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxIterations = 150 maxNoImprove = 20 localSearchOptima = 12000.0 alpha = 0.3 scalingFactor = alpha * (localSearchOptima / float(len(self.TSPLIB))) # Execute the algorithm result = search(self.TSPLIB, maxIterations, maxNoImprove, scalingFactor) tspResult = TSPResult(7542, "Guided Local Search Results") print tspResult.FormattedOutput(result)
def testGreedyRandomizedAdaptiveSearch(self): from StochasticAlgorithms.GreedyRandomizedAdaptiveSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxNoImprove = 50 maxIterations = 50 greedinessFactor = 0.3 # should be in the range [0,1]. 0 is more greedy and 1 is more generalized # Execute the algorithm result = search(self.TSPLIB, maxIterations, maxNoImprove, greedinessFactor) tspResult = TSPResult(7542, "Greedy Randomized Adaptive Search Results") print tspResult.FormattedOutput(result)
def testVariableNeighborhoodSearch(self): from StochasticAlgorithms.VariableNeighborhoodSearch import search # Problem Configuration # Use Berlin52 instance of TSPLIB # Algorithm Configuration maxNoImprove = 50 maxNoImproveLocal = 70 neighborhoods = range( 1, 21) # since we want 20 runs for neighborhood starting with 1 # Execute the algorithm result = search(self.TSPLIB, neighborhoods, maxNoImprove, maxNoImproveLocal) tspResult = TSPResult(7542, "Variable Neighborhood Search Results") print tspResult.FormattedOutput(result)
def testAdaptiveRandomSearch(self): from StochasticAlgorithms.AdaptiveRandomSearch import search # Problem Configuration searchVector = [-5, 5] size = 2 # Algorithm Configuration maxIterations = 10000 initFactor = 0.05 lFactor = 3.0 sFactor = 1.3 iterFactor = 10 maxNoChange = 25 # Execute the adaptive random search algorithm # Outputs a tuple containing the best cost and best input values result = search(maxIterations, size, searchVector, initFactor, sFactor, lFactor, iterFactor, maxNoChange) self.assertTrue( basinFunction(result["vector"]) == result["cost"], "Failed to get correct best basin value.") basin = BasinResult("Adaptive Random Search") print basin.FormattedOutput(result)
def testScatterSearch(self): from StochasticAlgorithms.ScatterSearch import search # Problem Configuration searchVector = [-5, 5] problemSize = 2 # Algorithm Configuration maxIterations = 100 stepSize = (searchVector[1] - searchVector[0]) * 0.05 # 5 percent of search space maxNoImprove = 30 refSetSize = 10 diverseSetSize = 20 eliteCount = 5 # execute the algorithm result = search(searchVector, problemSize, refSetSize, diverseSetSize, maxIterations, maxNoImprove, eliteCount, stepSize) self.assertTrue( basinFunction(result["vector"]) == result["cost"], "Failed to get correct best basin value.") basin = BasinResult("Scatter Search") print basin.FormattedOutput(result)