def AStar(initial_state): global COUNT, BACKLINKS # priority queue with respective priority # add any auxiliary data structures as needed OPEN = PriorityQ() # inserts the initial state with a priority of 0 (priority for initial state is irrelevant to algorithm) OPEN.insert(initial_state, 0) CLOSED = [] BACKLINKS[initial_state] = None while OPEN.__len__() > 0: # S = min-priority state (priority value gets discarded, only state is looked at) S = OPEN.deletemin()[0] while S in CLOSED: S = OPEN.deletemin() CLOSED.append(S) # DO NOT CHANGE THIS SECTION: beginning if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THIS SECTION: end COUNT += 1 # if (COUNT % 32)==0: if True: # print(".",end="") # if (COUNT % 128)==0: if True: print("COUNT = " + str(COUNT)) print("len(OPEN)=" + str(len(OPEN))) print("len(CLOSED)=" + str(len(CLOSED))) L = [] # looks at all possible new states from operations on S # if the new state has not already been put on CLOSED, append to L for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) #print(new_state.__str__()) if not occurs_in(new_state, CLOSED): L.append(new_state) BACKLINKS[new_state] = S # adds new states in L into OPEN with their priorities # if any state already occurs in OPEN... # if L's state has a lower priority, change OPEN's state priority to L's # otherwise, keep OPEN's state in OPEN, ignore L's for state in L: g_state = G(state) if OPEN.__contains__(state): if g_state < OPEN.getpriority(state): OPEN.remove(state) else: break OPEN.insert(state, g_state)
def AStar(initial_state): global COUNT, BACKLINKS # priority queue with respective priority # add any auxiliary data structures as needed OPEN = PriorityQ() CLOSED = [] BACKLINKS[initial_state] = None g = {initial_state: 0} OPEN.insert(initial_state, heuristics(initial_state)) while not len(OPEN) == 0: S, f = OPEN.deletemin() while S in CLOSED: S, f = OPEN.deletemin() CLOSED.append(S) # DO NOT CHANGE THIS SECTION: begining if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THIS SECTION: end COUNT += 1 print(COUNT) L = [] for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) if not occurs_in(new_state, CLOSED): L.append(new_state) BACKLINKS[new_state] = S if new_state not in g.keys(): g[new_state] = g[S] + 1 for s2 in L: if s2 in OPEN: OPEN.remove(s2) for elt in L: OPEN.insert(elt, heuristics(elt) + g[elt])
def AStar(initial_state): global COUNT, BACKLINKS # TODO: initialze and put first state into # priority queue with respective priority # add any auxiliary data structures as needed OPEN = PriorityQ() CLOSED = [] #define the g score which means #the cost of the move will increase by 1 at each depth g = {} g[initial_state] = 0 BACKLINKS[initial_state] = None #in priority queue, the state is the element #and the F score is the priority #F socre is the heuristics score plus g scorce OPEN.insert(initial_state, g[initial_state] + 0) while not OPEN.isEmpty(): COUNT += 1 S = OPEN.deletemin() while S in CLOSED: S = OPEN.deletemin() CLOSED.append(S) # DO NOT CHANGE THIS SECTION: begining if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THIS SECTION: end # TODO: finish A* implementation for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) if not (new_state in CLOSED): h = heuristics(new_state) g[new_state] = g[S] + 1 if new_state not in OPEN: OPEN.insert(new_state, h + g[new_state]) BACKLINKS[new_state] = S
def AStar(initial_state): # this breaks pq ties global COUNT, BACKLINKS # TODO: initialze and put first state into # priority queue with respective priority # add any auxiliary data structures as needed OPEN = PriorityQ() CLOSED = [] OPEN.insert(initial_state,0) BACKLINKS[initial_state] = None while not OPEN.isEmpty(): COUNT += 1 S = OPEN.deletemin() while S in CLOSED: S = OPEN.deletemin() CLOSED.append(S) # DO NOT CHANGE THIS SECTION: begining if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THIS SECTION: end # TODO: finish A* implementation for op in Problem.OPERATORS: #Optionally uncomment the following when debugging #a new problem formulation. # print("Trying operator: "+op.name) if op.precond(S): new_state = op.state_transf(S) if not (new_state in CLOSED): h = heuristics(new_state) #new_pq_item = ( new_state,h) OPEN.insert(new_state , h) BACKLINKS[new_state] = S
def AStar(initial_state): global COUNT, BACKLINKS OPEN = PriorityQ() #currently discovered, not yet evaluated states CLOSED = [] #already evaluated states BACKLINKS[initial_state] = None #Tracks most efficient previous step #Calculate F, G, H scores for Starting state initialize_scores(initial_state) #Only one state is known as of now OPEN.insert(initial_state, F_SCORE[initial_state]) while OPEN.isEmpty() != True: S, priority = OPEN.deletemin() while S in CLOSED: S = OPEN.deletemin() CLOSED.append(S) # DO NOT CHANGE THIS SECTION: beginning if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THIS SECTION: end COUNT += 1 if (COUNT % 32) == 0: # if True: # print(".",end="") # if (COUNT % 128*128)==0: if True: print("COUNT = " + str(COUNT)) #print("len(OPEN)=" + str(len(OPEN))) #PriorityQ OPEN doesn't have len() print("len(CLOSED)=" + str(len(CLOSED))) for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) if not occurs_in(new_state, CLOSED): #ignore already evaluated neighbors #find tentative score of neighbor tentative_g_score = G_SCORE[S] + 1 if new_state not in G_SCORE: #Default INFINITY BACKLINKS[ new_state] = S #First known path to new_state elif tentative_g_score <= G_SCORE[new_state]: BACKLINKS[ new_state] = S # Found better path to new_State else: continue #current path is not the best path to the neighbor G_SCORE[new_state] = tentative_g_score F_SCORE[new_state] = G_SCORE[new_state] + h_score_fn( new_state) # Delete previous F-score in PriorityQ if it exists if OPEN.__contains__(new_state): OPEN.remove(new_state) #Update PriorityQ with new priority OPEN.insert(new_state, F_SCORE[new_state]) # print(Problem.DESCRIBE_STATE(new_state)) #print(OPEN) #Failure, if goal_test has not succeeded until now print("COULD NOT FIND GOAL") return
from priorityq import PriorityQ import EightPuzzle import EightPuzzleWithHeuristics OPEN = PriorityQ() s1 = EightPuzzle.State([0, 1, 2, 3, 4, 5, 6, 7, 8]) s2 = EightPuzzle.State([1, 0, 2, 3, 4, 5, 6, 7, 8]) s3 = EightPuzzle.State([1, 0, 3, 2, 4, 5, 6, 7, 8]) h1 = EightPuzzleWithHeuristics.h_euclidean(s1) h2 = EightPuzzleWithHeuristics.h_euclidean(s2) h3 = EightPuzzleWithHeuristics.h_euclidean(s3) OPEN.insert(s1, h1) OPEN.insert(s2, h2) OPEN.insert(s3, h3) print(OPEN) s = OPEN.deletemin() print(s) s = OPEN.deletemin() print(s) OPEN.deletemin() print(OPEN)
def AStar(initial_state): # print("In RecDFS, with depth_limit="+str(depth_limit)+", current_state is ") # print(Problem.DESCRIBE_STATE(current_state)) global COUNT, BACKLINKS #Tracks most efficient previous step BACKLINKS[initial_state] = None #already evaluated states CLOSED = [] #currently discovered, not yet evaluated states OPEN = PriorityQ() #Calculate F, G, H scores initialize_scores(initial_state) #Only initial node is known as of now OPEN.insert(initial_state, F_SCORE[initial_state]) while OPEN.isEmpty() != True: S = OPEN.deletemin() CLOSED.append(S) if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) backtrace(S) return #FOUND GOAL COUNT += 1 if (COUNT % 32) == 0: # if True: # print(".",end="") # if (COUNT % 128*128)==0: if True: print("COUNT = " + str(COUNT)) #print("len(OPEN)=" + str(len(OPEN))) #PriorityQ OPEN doesn't have len() print("len(CLOSED)=" + str(len(CLOSED))) for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) if not occurs_in(new_state, CLOSED): #ignore already evaluated neighbors #find tentative score of neighbor tentative_g_score = G_SCORE[S] + 1 if new_state not in G_SCORE: #Default INFINITY BACKLINKS[ new_state] = S #First known path to new_state elif tentative_g_score >= G_SCORE[new_state]: continue #current path is not the best path to the neighbor else: BACKLINKS[ new_state] = S #Found better path to new_State G_SCORE[new_state] = tentative_g_score F_SCORE[new_state] = G_SCORE[new_state] + h_score_fn( new_state) # discovered a new State if not OPEN.__contains__(new_state): OPEN.insert(new_state, F_SCORE[new_state]) # print(Problem.DESCRIBE_STATE(new_state)) #print(OPEN) #Failure, if goal_test has not succeeded until now print("COULD NOT FIND GOAL") return