def dfs(level): smap = level.smap first_node = Node(level.start, None, []) stack = [] #metemos al nodo inicial en la cola stack.append(first_node) processed = set() nodes_processed = 0 # mientras que la cola tenga elementos y no gane won = False while stack and not won: # saco el primer nodo del stack node = stack.pop() # print('ITERATION: ', nodes_processed, ' --------------------------------------------------------------') # print("Current node: ", node.config) # agrego este nodo a los nodos procesados processed.add(node.config) # primero me fijo si gane if(finished(node.config.boxes, level)): # si gane listo # print("Found solution!") won = True else: nodes_processed += 1 # si no gane pido mis movimientos legales possible_configs = next_configs(node.config, level.smap) # print("Possible configs: ", possible_configs) children = node.children #por cada movimiento legal me fijo si ya tube esta config antes y si no la apendeo a la cola # print("Procesed: ===>", processed) for config in possible_configs.difference(processed): # print("Config: +++>", config) # print("Was not in processed") new_node = Node(copy.copy(config), node, []) children.append(new_node) stack.append(new_node) # print("Added move: ", new_node.config) # print("Used configs: ", processed) # print("Stack is: ", stack) if won: path = build_path(node) return process_results(won, smap, node, path, ALGORITHM_NAME, nodes_processed - 1, len(stack)) else: return process_results(won, smap, None, [], ALGORITHM_NAME, nodes_processed - 1, len(stack))
def bfs(level): smap = level.smap first_node = Node(level.start, None, []) queue = [] #metemos al nodo inicial en la cola queue.append(first_node) processed = set() nodes_processed = 0 # mientras que la cola tenga elementos y no gane won = False while queue and not won: # saco el primer nodo de la cola node = queue.pop(0) # print("Current node: ", node.config) # agrego este nodo a los nodos procesados processed.add(node.config) # primero me fijo si gane if(finished(node.config.boxes, level)): # si gane listo print("Found solution!") won = True else: nodes_processed += 1 # si no gane pido mis movimientos legales possible_configs = next_configs(node.config, level.smap) # print("Possible configs: ", possible_configs) children = node.children #por cada movimiento legal me fijo si ya tube esta config antes y si no la apendeo a la cola for config in possible_configs.difference(processed): new_node = Node(copy.copy(config), node, []) children.append(new_node) queue.append(new_node) # print("Added move: ", new_node.config) # print("Used configs: ", processed) # print("Queue is: ", queue) if won: path = build_path(node) return process_results(won, smap, node, path, ALGORITHM_NAME, nodes_processed - 1, len(queue)) else: return process_results(won, smap, None, [], ALGORITHM_NAME, nodes_processed - 1, len(queue))
def iddfs(level, n, testall): initial_time = datetime.now() smap = level.smap first_node = Node(level.start, None, [], 0) deque = [] #metemos al nodo inicial en la cola deque.append(first_node) known_nodes = HashTable() known_nodes.put(first_node.config, first_node) nodes_processed = 0 # curr_n = n # mientras que la cola tenga elementos y no gane won = False limit_nodes_per_n = [] limit_nodes_per_n_size = 0 while deque and not won: # saco el primer nodo del stack node = deque.pop() # if node in processed: # l = list(processed) # idx = l.index(node) # if idx >= 0 and idx < len(l): # proc_node = l[idx] # if node.depth >= proc_node.depth: # pase = True # continue # else: # continue # primero me fijo si gane if (finished(node.config.boxes, level)): # si gane listo won = True else: nodes_processed += 1 # si no gane pido mis movimientos legales possible_configs = next_configs(node.config, level.smap) children = node.children if (node.depth + 1) % n == 0: children_at_limit = True else: children_at_limit = False for config in possible_configs: proc_node = known_nodes.get(config) if proc_node and node.depth + 1 >= proc_node.depth: continue new_node = Node(copy.copy(config), node, [], node.depth + 1) known_nodes.put(config, new_node) children.append(new_node) if children_at_limit: limit_idx = int(new_node.depth / n) - 1 if len(limit_nodes_per_n) < limit_idx + 1: limit_nodes_per_n.append(0) limit_nodes_per_n[limit_idx] += 1 deque.insert(0, new_node) else: deque.append(new_node) # print("Added move: ", new_node.config) # print("Used configs: ", processed) # print("deque is: ", deque) finish_time = datetime.now() elapsed_time = finish_time - initial_time #print(limit_nodes_per_n) if won: path = build_path(node) return process_results(won, testall, elapsed_time, smap, node, path, f"{ALGORITHM_NAME} (n='{n}')", nodes_processed - 1, len(deque)) else: return process_results(won, testall, elapsed_time, smap, None, [], f"{ALGORITHM_NAME} (n='{n}')", nodes_processed - 1, len(deque))
def idastar(level, heu, testall): h=heu["func"] initial_time = datetime.now() smap = level.smap first_node = Node(level.start, None, [], 0, {'g': 0, 'h': h(smap, level.goals, level.start)}) lim = first_node.meta['g'] + first_node.meta['h'] # f(n0) min_exceeded_lim = -1 def cp(e1, e2): g1 = e1.meta['g'] g2 = e2.meta['g'] h1 = e1.meta['h'] h2 = e2.meta['h'] f1 = g1 + h1 f2 = g2 + h2 if f1 == f2: return h1 - h2 else: return f1 - f2 q1 = [] q2 = [] out_of_frontier = OrderedList(cp) q1.append(first_node) known_nodes = HashTable() known_nodes.put(first_node.config, first_node) nodes_processed = 0 won = False first_found = False while (q1 or q2) and not won: if not q1: q1 = q2 q2 = [] lim = min_exceeded_lim min_exceeded_lim = -1 node = q1.pop() f = node.meta['g'] + node.meta['h'] if f > lim: q2.append(node) continue if(finished(node.config.boxes, level)): won = True else: nodes_processed += 1 possible_configs = next_configs(node.config, level.smap) children = node.children for config in possible_configs: proc_node = known_nodes.get(config) if proc_node and node.depth + 1 >= proc_node.depth: continue new_node = Node(copy.copy(config), node, [], node.depth + 1, {'g': node.depth + 1, 'h': h(smap, level.goals, config)}) known_nodes.put(config, new_node) children.append(new_node) f = new_node.meta['g'] + new_node.meta['h'] if f > lim: q2.append(new_node) if min_exceeded_lim == -1 or f < min_exceeded_lim: min_exceeded_lim = f else: q1.append(new_node) finish_time = datetime.now() elapsed_time = finish_time - initial_time if won: path = build_path(node) return process_results(won, testall, elapsed_time, smap, node, path, f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, len(q1) + len(q2)) else: return process_results(won, testall, elapsed_time, smap, None, [], f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, len(q1) + len(q2))
def astar(level, heu, testall): h = heu["func"] initial_time = datetime.now() smap = level.smap first_node = Node(level.start, None, [], 0, { 'g': 0, 'h': h(smap, level.goals, level.start) }) def cp(e1, e2): g1 = e1.meta['g'] g2 = e2.meta['g'] h1 = e1.meta['h'] h2 = e2.meta['h'] f1 = g1 + h1 f2 = g2 + h2 if f1 == f2: return h1 - h2 else: return f1 - f2 nodes_list = OrderedList(cp) nodes_list.add(first_node) #known_cfgs = set() known_nodes = HashTable() #known_cfgs.add(first_node.config) known_nodes.put(first_node.config, first_node) nodes_processed = 0 won = False while nodes_list.length() > 0 and not won: node = nodes_list.pop() if (finished(node.config.boxes, level)): won = True else: nodes_processed += 1 possible_configs = next_configs(node.config, level.smap) children = node.children for config in possible_configs: proc_node = known_nodes.get(config) new_node = Node(copy.copy(config), node, [], node.depth + 1, { 'g': node.depth + 1, 'h': h(smap, level.goals, config) }) if proc_node: new_node_f = new_node.meta['g'] + new_node.meta['h'] proc_node_f = proc_node.meta['g'] + proc_node.meta['h'] if (new_node_f >= proc_node_f): continue # if config in known_cfgs: # continue #known_cfgs.add(config) known_nodes.put(config, new_node) children.append(new_node) nodes_list.add(new_node) finish_time = datetime.now() elapsed_time = finish_time - initial_time if won: path = build_path(node) return process_results(won, testall, elapsed_time, smap, node, path, f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, nodes_list.length()) else: return process_results(won, testall, elapsed_time, smap, None, [], f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, nodes_list.length())
def ggs(level, heu, testall): h = heu["func"] initial_time = datetime.now() smap = level.smap first_node = Node(level.start, None, [], 0, {'h': h(smap, level.goals, level.start)}) def cp(e1, e2): return e1.meta['h'] - e2.meta['h'] nodes_list = OrderedList(cp) #metemos al nodo inicial en la cola ( ͡° ͜ʖ ͡°) nodes_list.add(first_node) known_cfgs = set() known_cfgs.add(first_node.config) nodes_processed = 0 # mientras que la cola tenga elementos y no gane won = False while nodes_list.length() > 0 and not won: # saco el primer nodo del nodes_list node = nodes_list.pop() # print('ITERATION: ', nodes_processed, ' --------------------------------------------------------------') # print("Current node: ", node.config) # primero me fijo si gane if (finished(node.config.boxes, level)): # si gane listo # print("Found solution!") won = True else: nodes_processed += 1 # si no gane pido mis movimientos legales possible_configs = next_configs(node.config, level.smap) # print("Possible configs: ", possible_configs) children = node.children #por cada movimiento legal me fijo si ya tube esta config antes y si no la apendeo a la cola # print("Procesed: ===>", processed) for config in possible_configs: if config in known_cfgs: continue known_cfgs.add(config) new_node = Node(copy.copy(config), node, [], node.depth + 1, {'h': h(smap, level.goals, config)}) children.append(new_node) nodes_list.add(new_node) # print("Added move: ", new_node.config) # print("Used configs: ", processed) # print("Stack is: ", stack) finish_time = datetime.now() elapsed_time = finish_time - initial_time if won: path = build_path(node) return process_results(won, testall, elapsed_time, smap, node, path, f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, nodes_list.length()) else: return process_results(won, testall, elapsed_time, smap, None, [], f"{ALGORITHM_NAME} ({heu['name']})", nodes_processed - 1, nodes_list.length())