def runDFS(): initial_state = Problem.CREATE_INITIAL_STATE() print("Initial State:") print(initial_state) global COUNT, BACKLINKS COUNT = 0 BACKLINKS = {} IterativeDFS(initial_state) print(str(COUNT)+" states examined.")
def IterativeDFS(initial_state): #print("In RecDFS, with depth_limit="+str(depth_limit)+", current_state is ") #print(Problem.DESCRIBE_STATE(current_state)) global COUNT, BACKLINKS OPEN = [initial_state] CLOSED = [] BACKLINKS[initial_state] = None while OPEN != []: S = OPEN.pop(0) CLOSED.append(S) if Problem.GOAL_TEST(S): print(Problem.GOAL_MESSAGE_FUNCTION(S)) backtrace(S) return 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 = [] 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 #print(Problem.DESCRIBE_STATE(new_state)) for s2 in L: for i in range(len(OPEN)): if (s2 == OPEN[i]): del OPEN[i] break OPEN = L + OPEN print_state_list("OPEN", OPEN)
def IterativeBFS(initial_state): global COUNT, BACKLINKS OPEN = [initial_state] CLOSED = [] BACKLINKS[initial_state] = None while OPEN != []: S = OPEN.pop(0) CLOSED.append(S) # DO NOT CHANGE THIS SECTION # the goal test, return path if reached goal if Problem.GOAL_TEST(S): print("\n" + Problem.GOAL_MESSAGE_FUNCTION(S)) #backtrace(S) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THE CODE ABOVE 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 = [] for op in Problem.OPERATORS: if op.precond(S): new_state = op.state_transf(S) if not occurs_in(new_state, CLOSED): #only update if new_state is not in OPEN if not occurs_in(new_state, OPEN): BACKLINKS[new_state] = S L.append(new_state) # print(Problem.DESCRIBE_STATE(new_state)) OPEN = OPEN + L
def IterativeBFS(initial_state): global COUNT, BACKLINKS OPEN = [initial_state] CLOSED = [] BACKLINKS[initial_state] = -1 while OPEN != []: S = OPEN[0] del OPEN[0] CLOSED.append(S) # DO NOT CHANGE THIS SECTION # the goal test, return path if reached goal if Problem.GOAL_TEST(S): print("\n" + Problem.GOAL_MESSAGE_FUNCTION(S)) path = backtrace(S) return path, Problem.PROBLEM_NAME # DO NOT CHANGE THE CODE ABOVE COUNT += 1 if (COUNT % 32) == 0: print(".", end="") if (COUNT % 128) == 0: print("COUNT = " + str(COUNT)) print("len(OPEN)=" + str(len(OPEN))) print("len(CLOSED)=" + str(len(CLOSED))) L = [] 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 OPEN) and not (new_state in CLOSED): L.append(new_state) BACKLINKS[new_state] = S # Uncomment for debugging: # print(new_state) OPEN = OPEN + L
def IterativeDFS(initial_state): global COUNT, BACKLINKS OPEN = [initial_state] CLOSED = [] BACKLINKS[initial_state] = -1 while OPEN != []: S = OPEN[0] del OPEN[0] CLOSED.append(S) if Problem.GOAL_TEST(S): print("\n"+Problem.GOAL_MESSAGE_FUNCTION(S)) backtrace(S) return COUNT += 1 if (COUNT % 32)==0: print(".") if (COUNT % 128)==0: print("COUNT = "+str(COUNT)) print("len(OPEN)="+str(len(OPEN))) print("len(CLOSED)="+str(len(CLOSED))) L = [] 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 OPEN) and not (new_state in CLOSED): L.append(new_state) BACKLINKS[new_state] = S #Uncomment for debugging: #print(new_state) OPEN = L + OPEN
def runBFS(): initial_state = Problem.CREATE_INITIAL_STATE() # if len(sys.argv)>2: # initial_state_file = importlib.import_module(sys.argv[2]) # initial_state = initial_state_file.CREATE_INITIAL_STATE() print("Initial State:") print(initial_state) global COUNT, BACKLINKS COUNT = 0 BACKLINKS = {} path, name = IterativeBFS(initial_state) print(str(COUNT) + " states examined.") return path, name
def main(): print('Starting the execution of the Towers of Henoi') toh = TowerOfHanoi(TOTAL_DISK) toh.run() print('Finished the execution of the Tower of Henoi')
import EightPuzzleWithHeuristics as Problem import BasicEightPuzzle as Problem2 import TowerOfHanoi as Problem3 initial_state = Problem.CREATE_INITIAL_STATE() print("Current State", initial_state) print(initial_state) initial_state = Problem2.CREATE_INITIAL_STATE() print("Current State", initial_state) print(initial_state) initial_state = Problem3.CREATE_INITIAL_STATE() print("Current State", initial_state) print(initial_state)
async def start_hanoi(height: int, id: int): hanoi_games[id] = TowerOfHanoi.TowerOfHanoi(height) game = hanoi_games[id] game.start_game() response = game.response return response