def brute_force_search(): ''' Search algorithm ''' actual = START print_state(actual) if len(sys.argv) > 1: mode = int(sys.argv[1]) else: mode = choose_mode() while True: print_state(actual) if is_goal(actual, GOAL_ROWS): break if mode == 2: O = applicable_operators(actual, True) else: O = applicable_operators(actual) if len(O) != 0: o = choose(O, mode) actual = use(actual, o) else: break if is_goal(actual, GOAL_ROWS): print(actual) else: print("Sikertelen keresés")
def write_nodes(opened, closed, nodes, step, selected): one_step = [] one_step.append("{step}. lépés\n".format(step=step)) one_step.append("\nNyíltak:\n") if opened: for i in opened: predicted_cost = 0 real_cost = 0 if nodes[i].parent: predicted_cost = nodes[ nodes[i].parent].heuristic - nodes[i].heuristic real_cost = nodes[i].cost - nodes[nodes[i].parent].cost s = '' if i == selected: s = '->' elif is_goal(nodes[selected].state, GOAL_ROWS): s = 'cél->' one_step.append('{s}{node} {a}\n'.format( s=s, node=str(nodes[i]), a=predicted_cost <= real_cost)) one_step.append("\nZártak:\n") if closed: for i in closed: predicted_cost = 0 if nodes[i].parent: predicted_cost = nodes[ nodes[i].parent].heuristic - nodes[i].heuristic real_cost = nodes[i].cost - nodes[nodes[i].parent].cost one_step.append('{node} {a}\n'.format( node=str(nodes[i]), a=predicted_cost <= real_cost)) with open("test/test{}.txt".format(step), "w") as f: f.write(''.join(one_step))
def a_algorithm(): ''' Search algorithm ''' step = 0 opened = [] closed = [] nodes = [] new_node = node() new_node.state = START new_node.parent = None new_node.operator = None new_node.cost = 0 new_node.heuristic = heuristic(START) nodes.append(new_node) opened.append(0) while True: print("[{}{}. lépés] Keresés folyamatban...".format( (10 - len(str(step))) * '-', step), end='\r') if len(opened) == 0: break #selected = choose_node( nodes, opened ) selected = opened.pop(0) '''if step==1000: write_nodes( opened, closed, nodes, step, selected ) exit(0)''' step += 1 #print( nodes[ selected ].get_state() ) if is_goal(nodes[selected].state, GOAL_ROWS): break extend(selected, opened, closed, nodes) print('') if len(opened) != 0: solution = get_solution(nodes, selected, ['op', 'he', 'co']) with open('solution.txt', 'w') as f: for i in range(len(solution)): f.write('{i:2d}. {node}'.format(i=i, node=solution[i])) print("Megoldás: solution.txt") else: solution = get_solution(nodes, selected, ['op', 'he', 'co']) with open('solution.txt', 'w') as f: for i in range(len(solution)): f.write('{i:2d}. {node}'.format(i=i, node=solution[i])) print("Nincs megoldás")
def write_nodes(opened, closed, nodes, step, selected): one_step = [] one_step.append("{step}. lépés\n".format(step=step)) one_step.append("\nKiválasztott:\n") one_step.append('{node}\n'.format(node=str(nodes[selected]))) one_step.append("\nNyíltak:\n") if opened: for i in opened: s = '' if is_goal(nodes[selected].state, GOAL_ROWS): s = 'cél->' one_step.append('{s}{node}\n'.format(s=s, node=str(nodes[i]))) one_step.append("\nZártak:\n") if closed: for i in closed: one_step.append('{node}\n'.format(node=str(nodes[i]))) with open("test/test{}.txt".format(step), "w") as f: f.write(''.join(one_step))
def depth(): ''' Search algorithm ''' step = 0 if (len(sys.argv) == 1): mode = choose_mode() else: mode = int(sys.argv[1]) if mode == 0: opened = deque([]) elif mode == 1: opened = [] closed = [] nodes = [] new_node = node() new_node.state = START new_node.parent = None new_node.operator = None new_node.depth = 0 nodes.append(new_node) opened.append(0) while True: print("[{}{}. lépés] Keresés folyamatban...".format( (10 - len(str(step))) * '-', step), end='\r') if len(opened) == 0: break if mode == 0: selected = opened.popleft() elif mode == 1: selected = opened.pop() '''if step == 500: write_nodes( opened, closed, nodes, step, selected ) exit(0)''' step += 1 if is_goal(nodes[selected].state, GOAL_ROWS): break extend(selected, opened, closed, nodes) if len(opened) != 0: states = get_solution_states(nodes, selected) operators = get_solution_operators(nodes, selected) f = open('solution.txt', 'w') for i in range(len(states)): f.write('{i:2d}. {op}:\n{state}\n'.format(i=i, op=operators[i], state=states[i])) f.close() print("Megoldás: solution.txt") else: print("Nincs megoldás")