def value(self, x): return math.fabs(x * math.sin(x)) if __name__ == '__main__': # Formulate a problem with a 2D hill function and a single maximum value. maximum = 30 initial = randrange(0, maximum) p = AbsVariant(initial, maximum, delta=1.0) print('Initial x: ' + str(p.initial) + '\t\tvalue: ' + str(p.value(initial))) # Solve the problem using hill-climbing. startTime = time.time() hill_solution = hill_climbing(p) endTime = time.time() - startTime print('Hill-climbing solution x: ' + str(hill_solution) + '\tvalue: ' + str(p.value(hill_solution)) + '\t\ttime: ' + str(endTime)) # Solve the problem using simulated annealing. startTime = time.time() annealing_solution = simulated_annealing( p, exp_schedule(k=20, lam=0.005, limit=1000)) endTime = time.time() - startTime print('Simulated annealing solution x: ' + str(annealing_solution) + '\tvalue: ' + str(p.value(annealing_solution)) + '\t\ttime: ' + str(endTime))
def value(self, x): return math.fabs(x * math.sin(x)) if __name__ == '__main__': # Formulate a problem with a 2D hill function and a single maximum value. maximum = 30 initial = randrange(0, maximum) p = SineVariant(initial, maximum, delta=5.0) print('Initial x: ' + str(p.initial) + '\t\tvalue: ' + str(p.value(initial))) # Solve the problem using hill-climbing. then0 = time.time() hill_solution0 = hill_climbing(p) print('Hill-climbing solution 0 x: ' + str(hill_solution0) + '\tvalue: ' + str(p.value(hill_solution0))) hill_solution1 = hill_climbing(p) print('Hill-climbing solution 1 x: ' + str(hill_solution1) + '\tvalue: ' + str(p.value(hill_solution1))) hill_solution2 = hill_climbing(p) print('Hill-climbing solution 2 x: ' + str(hill_solution2) + '\tvalue: ' + str(p.value(hill_solution2))) hill_climbing_data = [ p.value(hill_solution0), p.value(hill_solution1), p.value(hill_solution2) ] print('Average hill climbing solution time:', statistics.mean(hill_climbing_data))
#print('Initial x: ' + str(p.initial) # + '\t\tvalue: ' + str(p.value(initial)) # ) # Solve the problem using hill-climbing. # Implement random-start hill-climbing startTime = time.time() solutions = {} # Create an empty dictionary numIteration = 20 # Set the number of iteration for i in range(numIteration): # Do 20 random starts maximum = 30.0 initial = randrange(0, maximum) p = AbsVariant(initial, maximum, delta=1.0) solutions.update({p.value(hill_climbing(p)): hill_climbing(p)}) # Add an key:value as value:x endTime = time.time() - startTime fsolutionval = max(solutions.keys()) # Find and store the maximum value fsolutionx = solutions[fsolutionval] # Find and store the x value of the maximum value average = sum(solutions.keys()) / numIteration # Find and store the average of the values print('Hill-climbing solution (random-restart) x: ' + str(fsolutionval) + '\tvalue: ' + str(fsolutionx) + '\t\ttime: ' + str(endTime) + '\t\taverage: ' + str(average) ) # Solve the problem using simulated annealing. # Implement random-start simulated annealing
# assign each path links and randomized distances with other points # ae '(0, 1): 10' is point 0's link to point 1 with a distance of 10 for i in range(0, mapSize): for k in range(i + 1, mapSize): path = (i, k) path_dist = randrange(1, 11) pathsList[path] = path_dist # don't over-clutter the screen with paths and distances if mapSize <= 10: print('Paths:\t' + str(pathsList )) problem = TSP(mapSize, pathsList) t = time.time() hill_climbing = hill_climbing(problem) hc_time = time.time() - t print('Hill climbing:\t' + str(hill_climbing) + '\n\tvalue: ' + str(problem.value(hill_climbing)) + '\n\ttime: ' + str(hc_time) ) t = time.time() sim_annealing = simulated_annealing(problem, exp_schedule(k=20, lam=0.005, limit=1000)) sa_time = time.time() - t print('Simulated annealing\t: ' + str(sim_annealing ) + '\n\tvalue: ' + str(problem.value(sim_annealing )) + '\n\ttime: ' + str(sa_time) )
if __name__ == '__main__': # Formulate a problem with a 2D hill function and a single maximum value. maximum = 30 initial = randrange(0, maximum) p = SineVariant(initial, maximum, delta=1.0) print('Initial x: ' + str(p.initial) + '\t\tvalue: ' + str(p.value(initial))) # Solve the problem using hill-climbing. t0 = time.time() hill_solution = 0 for i in range(1, 10): initial = randrange(0, maximum) p = SineVariant(initial, maximum, delta=1.0) hill_solution = max(hill_climbing(p), hill_solution) t1 = time.time() print('Hill-climbing solution x: ' + str(hill_solution) + '\tvalue: ' + str(p.value(hill_solution)) + ' \ttime: ' + str(t1 - t0)) # Solve the problem using simulated annealing. t2 = time.time() annealing_solution = 0 for i in range(1, 10): initial = randrange(0, maximum) p = SineVariant(initial, maximum, delta=1.0) annealing_solution = max( simulated_annealing(p, exp_schedule(k=20, lam=0.005, limit=1000)), annealing_solution)
if distance not in TSPstate: TSPaction.append(distance) return TSPaction if __name__ == '__main__': #change the number of cities it travels randomCity = 50 randomPath = {} #indicates path and distances that have been randomized for cityNumber in range(0, randomCity): for space in range(cityNumber + 1, randomCity): local = (cityNumber, space) newPathDistance = randrange(1, 9) randomPath[local] = newPathDistance TSPproblem = TSP(randomCity, randomPath) #implementations hillClimbingImp = hill_climbing(TSPproblem) simulatedAnnealingImp = simulated_annealing(TSPproblem, exp_schedule(k=10, lam=0.005, limit=100)) #prints hill climbing and simulated annealing results print('Hill climbing:\t' + str(hillClimbingImp) + '\n\tvalue: ' + str(TSPproblem.value(hillClimbingImp)) ) print('Simulated annealing\t: ' + str(simulatedAnnealingImp) + '\n\tvalue: ' + str(TSPproblem.value(simulatedAnnealingImp)) )