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))
# 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) )
t = time.time() # Solve the problem using hill-climbing. hill_solution = hill_climbing(p) print('Hill-climbing solution x: ' + str(hill_solution) + '\tvalue: ' + str(p.value(hill_solution))) tDelta = time.time() - t print("time to solve:\t" + str(tDelta)) #HCt.append((initial,tDelta)) HCt.append( (initial, p.value(hill_solution))) #I know in random-restart #you arn't supposed to store the solutions in memory, but I want to keep #them for sake of graphing # Solve the problem using simulated annealing. t = time.time() annealing_solution = simulated_annealing( p, exp_schedule(k=20, lam=0.005, limit=maximum)) print('Simulated annealing solution x: ' + str(annealing_solution) + '\tvalue: ' + str(p.value(annealing_solution))) tDelta = time.time() - t SAt.append((initial, p.value(annealing_solution))) print("time to solve:\t" + str(tDelta)) x1, y1 = zip(*HCt) x2, y2 = zip(*SAt) #plt.scatter(*(x,y)) print("Best hill-climbing solution:\t" + str(max(y1))) print("Best Simulated-annealing solution:\t" + str(max(y2))) print("Average hill-climbing solution:\t" + str(sum(y1) / len(y1))) print("Average simulated annealing solution:\t" + str(sum(y2) / len(y2))) # plt.hist(y1) # plt.show()
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) t3 = time.time() print('Simulated annealing solution x: ' + str(annealing_solution) + '\tvalue: ' + str(p.value(annealing_solution)) + '\ttime: ' + str(t3 - t2))
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)) )