def test_rr_searches(restarts, initial_max): hill_restart_solution = 0 hill_avg = [] annealing_restart_solution = 0 annealing_avg = [] for i in range(restarts): # Get new starting point internal_p = SineVariant(randrange(0, initial_max), initial_max, delta=1) # Solve using hill-climbing + random restarts current_hill_solution = hill_climbing(internal_p) hill_avg.append(current_hill_solution) if current_hill_solution > hill_restart_solution: hill_restart_solution = current_hill_solution # Solve the problem using simulated annealing + random restarts current_annealing_solution = simulated_annealing( internal_p, exp_schedule(k=30, lam=0.005, limit=1000)) annealing_avg.append(current_annealing_solution) if current_annealing_solution > annealing_restart_solution: annealing_restart_solution = current_annealing_solution # Print results of each test print('Hill-climbing (RR) solution x: ' + str(hill_restart_solution) + '\t\tvalue: ' + str(internal_p.value(hill_restart_solution)) + '\taverage of runs: ' + str(sum(hill_avg) / len(hill_avg))) print('Simulated annealing (RR) solution x: ' + str(annealing_restart_solution) + '\t\tvalue: ' + str(internal_p.value(annealing_restart_solution)) + '\taverage of runs: ' + str(sum(annealing_avg) / len(annealing_avg)))
def analyze_all_algorithms(problem_set): section_break = '\n' + '_' * 100 + '\n' print(section_break) print( 'Results from steepest ascent hill climb (no sideways moves allowed):\n' ) analyze_performance(problem_set, steepest_ascent_hill_climb) print(section_break) print( 'Results from steepest ascent hill climb (up to 100 consecutive sideways moves allowed):\n' ) analyze_performance( problem_set, lambda x: steepest_ascent_hill_climb(x, allow_sideways=True)) print(section_break) print( 'Results from first choice hill climb (no sideways moves allowed):\n') analyze_performance(problem_set, first_choice_hill_climb) print(section_break) print('Result from random restart hill climb:\n') analyze_performance( problem_set, lambda x: random_restart_hill_climb(problem_set[0].__class__)) print(section_break) print('Result from simulated annealing:\n') analyze_performance( problem_set, lambda x: simulated_annealing( x, [0.9**(0.05 * i - 10) for i in range(1, 2000)])) print(section_break)
def NQueenDriver(): attempted = 0 hc_solved = 0 sa_solved = 0 saf_solved = 0 for i in range(1000): attempted += 1 hc = hill_climbing(MyNQueen(8)) if -1 not in hc: hc_solved += 1 print(f"hill_climbing solution: {hc}") sa = simulated_annealing(MyNQueen(8)) if -1 not in sa: sa_solved += 1 print(f"simulated annealing solution: {sa}") saf = simulated_annealing_full(MyNQueen(8)) if -1 not in saf: saf_solved += 1 print(f"Simulated annealing full solved: {saf}") print( f"N-Queen: Hill climbing (solved/attempted) {hc_solved} / {attempted}" ) print( f"N-Queen: Simulated annealing (solved/attempted) {sa_solved} / {attempted}" ) print( f"N-Queen: Simluated annealing full (solved/attempted) {saf_solved} / {attempted}" )
def run_max_sat(): problem = MAXSAT("uuf50-0331.cnf") output_file = open('maxsat_results.txt', 'w+') # t0 = time.time() # search.hill_climbing(problem) # total = time.time() - t0 # print total t0 = time.time() search.simulated_annealing(problem) total = time.time() - t0 print total t0 = time.time() search.genetic_search(problem) total = time.time() - t0 print total output_file.close()
def compare_algorithms_for_this_world(world, dist_dict, attempts=30, print_solution=False): # using the initial as our baseline best_hill_climber = world best_annealing = world # initializing averages avg_hill_climber = 0 avg_annealing = 0 # our small world problem representation tsp = TSP(world, dist_dict) # try both algorithms over and over for i in range(0, attempts): hill_climber = hill_climbing(tsp) annealing = simulated_annealing( tsp, exp_schedule(k=20, lam=0.005, limit=1000)) # keep track of the best solutions if tsp.value(hill_climber) > tsp.value(best_hill_climber): best_hill_climber = hill_climber if tsp.value(annealing) > tsp.value(best_annealing): best_annealing = annealing # maintain average values avg_hill_climber += tsp.value(hill_climber) avg_annealing += tsp.value(annealing) # Calculate the average avg_hill_climber /= attempts avg_annealing /= attempts # Ouptut: world statistics print("World of size " + str(len(world)) + " with " + str(attempts) + " per algorithm") hill_path = str(best_hill_climber) annealing_path = str(best_annealing) if not print_solution: hill_path = "" annealing_path = "" # Output: hill climbing results print("Hill Climbing:\t" + hill_path + "\tbest: " + str(tsp.value(best_hill_climber)) + "\tavg: " + str(round(avg_hill_climber, 2))) # Output: simulated annealing results print("Annealing:\t" + annealing_path + "\tbest: " + str(tsp.value(best_annealing)) + "\tavg: " + str(round(avg_annealing, 2)))
def test_knapsack_simulated_annealing_large_scale_1(): params = open_file("../dataset/large_scale/knapPI_1_100_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem, exp_schedule(1000, 0.0005, 4000)) optimum = int(( open("../dataset/large_scale-optimum/knapPI_1_100_1000_1").readline())) assert optimum == 9147 assert result.value <= optimum assert result.value >= 8000
def test_knapsack_simulated_annealing_large_scale_6(): params = open_file("../dataset/large_scale/knapPI_1_5000_1000_1") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem) optimum = int((open( "../dataset/large_scale-optimum/knapPI_1_5000_1000_1").readline())) assert optimum == 276457 assert result.value <= 40000 assert result.value >= 25000
def test_searches(searchspace): # Solve the problem using hill-climbing. hill_solution = hill_climbing(searchspace) print('Hill-climbing solution x: ' + str(hill_solution) + '\t\tvalue: ' + str(p.value(hill_solution))) # Solve the problem using simulated annealing. annealing_solution = simulated_annealing( searchspace, exp_schedule(k=30, lam=0.005, limit=1000)) print('Simulated annealing solution x: ' + str(annealing_solution) + '\t\tvalue: ' + str(p.value(annealing_solution)))
def test_knapsack_simulated_annealing_low_dimensional_2(): params = open_file("../dataset/low-dimensional/f2_l-d_kp_20_878") problem = KnapsackProblem(params[0], params[1]) ins_problem = InstrumentedProblem(problem) result = simulated_annealing(ins_problem, exp_schedule(2000, 0.00005, 200000)) optimum = int((open( "../dataset/low-dimensional-optimum/f2_l-d_kp_20_878").readline())) assert optimum == 1024 assert result.value == optimum
def test_maxsat_sim_annealing(fn): if VERBOSE_TESTING: print 'Simulated annealing...' ms = MAXSAT(fn) # print "Initial: ", ms.true_clauses(ms.initial) t0 = time.time() solution = search.simulated_annealing(ms, schedule) td = time.time() - t0 if VERBOSE_TESTING: print ms.true_clauses(ms.initial), '->', ms.true_clauses(solution.state) print "Time: %.2f" % td return ms.true_clauses(solution.state), round(td, DEC_PRECISION)
def EightPuzzleDriver(): hill_solved_count = 0 hill_attempted_count = 0 sa_attempted_count = 0 sa_solved_count = 0 saf_attempted_count = 0 saf_solved_count = 0 solution = (1, 2, 3, 4, 5, 6, 7, 8, 0) for i in itertools.permutations(list("012345678")): initial = tuple([int(j) for j in list(i)]) my_8_pz = MyEightPuzzle(initial) hc = hill_climbing(my_8_pz) hill_attempted_count += 1 if (solution == hc): print(f"Hill climbing solved {initial} -> {state}") hill_solved_count += 1 my_8_pz = MyEightPuzzle(initial) sa = simulated_annealing(my_8_pz) sa_attempted_count += 1 if (solution == sa): print(f"Simulated annealing solved {initial} -> {state}") sa_solved_count += 1 my_8_pz = MyEightPuzzle(initial) saf = simulated_annealing_full(my_8_pz) saf_attempted_count += 1 if (solution == saf): print(f"Simulated annealing full solved {initial} -> {state}") saf_solved_count += 1 if hill_attempted_count > 1000: break print( f"8-puzzle: Hill climbing (solved / attempted): {hill_solved_count} / {hill_attempted_count}" ) print( f"8-puzzle: Simulated annealing (solved / attempted): {sa_solved_count} / {sa_attempted_count}" ) print( f"8-puzzle: Simulated annealing full (solved / attempted): {saf_solved_count} / {saf_attempted_count}" )
def run_local_search(runType, inFile, outFile=OUTFILE): print >> outFile, '--------------------------------' fi = FORBIDDEN_ISLAND(inFile) if runType == 'SA': print >> outFile, 'Simulated Annealing' sTime = time.time() rtn = search.simulated_annealing(fi, annealing_schedule()) print >> outFile, ' Time: {} secs'.format(time.time()-sTime) print >> outFile, ' Value: {}'.format(fi.value(rtn.state)) print >> outFile, ' baseValues: {}'.format(rtn.state) elif runType == 'GA': print >> outFile, 'Genetic Algorithm' sTime = time.time() rtn = genetic_search(fi, fi.value) print >> outFile, ' Time: {} secs'.format(time.time()-sTime) print >> outFile, ' Value: {} / {}'.format(fi.value(rtn), fi.numClause) elif runType == 'StA': print >> outFile, 'Steepest ascent' sTime = time.time() rtn = search.hill_climbing(fi) print >> outFile, ' Time: {} secs'.format(time.time()-sTime) print >> outFile, ' Value: {} / {}'.format(fi.value(rtn), fi.numClause) elif runType == 'StAR': print >> outFile, 'Steepest ascent random restarts' valueL = [] timeL = [] for r in range(20): sTime = time.time() fi.random_start() rtn = search.hill_climbing(fi) timeL.append(time.time()-sTime) valueL.append(fi.value(rtn)) print >> outFile, ' Time: {} secs'.format(sum(timeL)/float(len(timeL))) print >> outFile, ' Mean: {} / {}'.format(sum(valueL)/float(len(valueL)), fi.numClause) print >> outFile, ' Min: {} / {}'.format(min(valueL), fi.numClause) print >> outFile, ' Max: {} / {}'.format(max(valueL), fi.numClause)
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=1.1) print('Initial x: ' + str(p.initial) + '\t\tvalue: ' + str(p.value(initial))) # Solve the problem using hill-climbing. hill_solution = hill_climbing(p) annealing_solution = simulated_annealing( p, exp_schedule(k=20, lam=0.005, limit=1000)) hill_restarts = hill_solution hill_value_accumulator = 0 hill_x_accumulator = 0 annealing_restarts = annealing_solution annealing_value_accumulator = 0 annealing_x_accumulator = 0 restarts = 100 for i in range(restarts): initial = randrange(0, maximum) p = SineVariant(initial, maximum, delta=1.1) hill_solution = hill_climbing(p)
eforie=(562, 293), fagaras=(305, 449), giurgiu=(375, 270), hirsova=(534, 350), iasi=(473, 506), lugoj=(165, 379), mehadia=(168, 339), neamt=(406, 537), oradea=(131, 571), pitesti=(320, 368), rimnicuvilcea=(233, 410), sibiu=(207, 457), timisoara=(94, 410), urziceni=(456, 350), vaslui=(509, 444), zerind=(108, 531)) initial1 = random.choice(list(locations.keys())) problem1 = TSP(locations, initial1) initial2 = random.choice(list(locations.keys())) problem2 = TSP(locations, initial2) hill_solution = hill_climbing(problem1) print('\nHill-climbing solution x: ' + '\tvalue: ' + str(math.fabs(problem1.value(hill_solution))) + "\n\n") annealing_solution = simulated_annealing( problem2, exp_schedule(k=20, lam=0.005, limit=15000)) print('\nAnnealing solution x: ' + '\tvalue: ' + str(math.fabs(problem2.value(annealing_solution))) + "\n\n")
# print("new route:",new_route) #self.state = new_route return new_route def cost(self, state): # arvuta (või leia muul viisil) praeguse marsruudi kogupikkus. Ära unusta, et marsruut on suletud. cost = 0 # print(self.matrix1[1][1]) for i in range(0, len(state) - 1): # print(i,"cost",self.matrix1[state[i]][state[i+1]]) cost = cost + int(self.matrix1[i][i + 1]) # print(cost) return cost def value(self, state): # kuna valmis otsingufunktsioonid arvavad, et mida suurem väärtus, seda parem, siis meie minimeerimisülesande TSP # lahendamiseks tuleb teepikkusest pöördväärtus võtta. return 1 / (self.cost(state) + 1) p = search.InstrumentedProblem(TSP("gr17")) g = search.simulated_annealing(p) #g = search.simulated_annealing(p, search.exp_schedule(limit=10000)) #g = search.hill_climbing(p) print("p", p) print("g", g) print(g.state) print(p.cost(g.state))
distances[col][row] = distances[row][col] # print("Distances:") # print(distances) # p = TSP(initial, distances, num_cities) # print("Initial:") # print(initial) print("Run " + str(n)) hill_solution = hill_climbing(p) # print("\nHill-climbing solution:") # print(hill_solution) print("Hill-climbing value = " + str(p.value(hill_solution))) annealing_solution = simulated_annealing(p) # print("\nSimulated Annealing solution:") # # print(annealing_solution) print("Simulated Annealing value = " + str(p.value(annealing_solution))) """ Amendment: When I changed the actions() method to select only 6 random pairs for swapping rather than all the possible pairs, I found that I also needed to change the distances between cities in order to have simulated annealing work better. Once I used the new actions() method and make the range of distances between cities larger, the results were typically better for the simulated annealing. I also tried it with the larger range of distances but the old actions() method. Based on the trials I recorded (see the TSP results Excel document for the specific values), it was clear that for the new actions() method and the larger range of city distances simulated annealing works better. """
class TSP2(Problem): def __init__(self, n, initial): self.n = n self.initial = initial def actions(self, state): actions = [] for column in range(self.n): for row in [x for x in range(self.n) if x != state[column]]: actions.append([column, row]) for i in range(1,10): return actions def result(self, state, move): """Makes the given move on a copy of the given state.""" new_state = state[:] new_state[move[0]] = move[1] return new_state def goal_test(self, state): """Check to see if there are no conflicts.""" return not any(self.conflicted(state, state[col], col) for col in range(len(state))) def conflicted(self, state, row, col): """Check to see if placing a queen at (row, col) would conflict with anything. """ return any(self.conflict(row, col, state[c], c) for c in range(col)) def conflict(self, row1, col1, row2, col2): """Check to see if two queens placed in (row1, col1) and (row2, col2) respectively would conflict with each other. """ return (row1 == row2 # # same row or col1 == col2 # # same column or row1 - col1 == row2 - col2 # # same \ diagonal or row1 + col1 == row2 + col2) # # same / diagonal def value(self, state): """This method computes a value of given state based on the number of conflicting pairs of queens. It doesn't follow AIMA-Python's NQueens gradient-descent formulation; instead, it counts the number of non-conflicting pairs (e.g., top score: 28 for a 8-queen problem; worst score: 0). """ # Start with the highest possible score (n combined 2). value = math.factorial(self.n) / (2 * math.factorial(self.n - 2)) # Loop through all pairs, subtracting one for every conflicted pair. for pair in itertools.combinations(range(self.n), 2): if self.conflict(state[pair[0]], pair[0], state[pair[1]], pair[1]): value -= 1 return value if __name__ == '__main__': # Set the board size. n = 8 # Initialize the board with all queens in the first row. board = [0] * n print('Start: ' + str(board)) # Initialize the NQueens problem p = TSP2(n, board) print('Value: ' + str(p.value(board))) # Solve the problem using hill climbing. hill_solution = hill_climbing(p) print('Hill-climbing:') print('\tSolution: ' + str(hill_solution)) print('\tValue: ' + str(p.value(hill_solution))) print('\tGoal? ' + str(p.goal_test(hill_solution))) # Solve the problem using simulated annealing. annealing_solution = simulated_annealing( p, exp_schedule(k=20, lam=0.005, limit=10000) ) print('Simulated annealing:') print('\tSolution: ' + str(annealing_solution)) print('\tValue: ' + str(p.value(annealing_solution))) print('\tGoal? ' + str(p.goal_test(annealing_solution)))
return len(possibilities) - 1 return 0 choices = { 'depth_first': depth_first_tree_search, # 'best_first_uniform': uniform_cost_tree_search, 'best_first_h1': lambda x: best_first_tree_search(x, h1), 'best_first_h2': lambda x: best_first_tree_search(x, h2), 'best_first_greedy_h1': lambda x: best_first_greedy_tree_search(x, h1), 'best_first_greedy_h2': lambda x: best_first_greedy_tree_search(x, h2), 'best_first_h3': lambda x: best_first_tree_search(x, h3), 'best_first_greedy_h3': lambda x: best_first_greedy_tree_search(x, h3), 'best_first_graph_h1': lambda x: best_first_graph_search(x, h1), 'best_first_graph_h2': lambda x: best_first_graph_search(x, h2), 'hill_climbing': lambda x: hill_climbing(x), 'annealing': lambda x: simulated_annealing(x, schedule=sim) } # print(sys.argv) searchers = sys.argv[1:] lewisProblems = map(LewisSudokuProblem, sudokus) naiveProblems = map(SudokuProblem, sudokus) #csv header print("problem,searcher,explored,states,tests,solution,value") for (i, (p1, p2)) in enumerate(zip(lewisProblems, naiveProblems)): #print("Initial state:") for s in searchers: if s in ['hill_climbing', 'annealing']: ip = InstrumentedProblem(p1) else: