def test_moves_count(self): root_node = Node(None, "185432_67", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) final_node_moves = final_node.get_path_moves() expected_moves_count = 20 self.assertEqual(expected_moves_count, len(final_node_moves), "incorrect expected moves count")
def test_two_moves_to_objective(self): root_node = Node(None, "123456_78", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) final_node_moves = final_node.get_path_moves() expected_path_moves = ['direita', 'direita'] expected_expansions = 2 self.assertEqual(expected_path_moves, final_node_moves, "path is incorrect") self.assertEqual(expected_expansions, monitor.expansions, "incorrect expected expansions")
def test_root_is_objective(self): root_node = Node(None, "12345678_", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) final_node_moves = final_node.get_path_moves() expected_path_moves = [] expected_expansions = 0 self.assertEqual(expected_path_moves, final_node_moves, "path is incorrect") self.assertEqual(expected_expansions, monitor.expansions, "incorrect expected expansions")
def test_unsolvable(self): root_node = Node(None, "2_5341687", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) n = 9 fact = 1 for i in range(1, n + 1): fact = fact * i max_expansions = fact / 2 self.assertFalse(final_node) self.assertEqual(max_expansions, monitor.expansions, "incorrect expected expansions")
def test_real(self): root_node = Node(None, "3456_8172", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) final_node_moves = final_node.get_path_moves() expected_path_moves = [ 'esquerda', 'abaixo', 'direita', 'acima', 'acima', 'esquerda', 'abaixo', 'direita', 'direita', 'abaixo', 'esquerda', 'acima', 'direita', 'acima', 'esquerda', 'abaixo', 'direita', 'abaixo' ] expected_expansions = 1411 self.assertEqual(expected_path_moves, final_node_moves, "path is incorrect") self.assertEqual(expected_expansions, monitor.expansions, "incorrect expected expansions")
def test_node_counter(self): root_node = Node(None, "2_3541687", 0, None) final_node, monitor = heuristic_search(root_node, self.heuristic) final_node_moves = final_node.get_path_moves() expected_path_moves = [ 'abaixo', 'abaixo', 'direita', 'acima', 'esquerda', 'abaixo', 'esquerda', 'acima', 'direita', 'abaixo', 'direita', 'acima', 'esquerda', 'abaixo', 'esquerda', 'acima', 'direita', 'acima', 'esquerda', 'abaixo', 'direita', 'abaixo', 'direita' ] expected_expansions = 12736 self.assertEqual(expected_path_moves, final_node_moves, "path is incorrect") self.assertEqual(expected_expansions, monitor.expansions, "incorrect expected expansions")
def menu(classroom_list, subjects_list, teachers_list): print('\n IA Search \n') print(u'The allocation of classrooms (scheduling) is a problem in which the objective is to organize a set of ' u'materials of different workloads on a set of classrooms available.') print(u'This issue is implemented for*:') print(u'- ', len(subjects_list), ' subjects') print(u'- ', len(teachers_list), ' teachers') print(u'- ', len(classroom_list), ' classrooms') print(u'* To change this setting change the source code of this file.\n') print(u'Choose one of the searches below to solve the problem:') print('1 - Blind Search') print('2 - Heuristic Search') print('3 - Restriction Search') print('4 - Show subjects') print('5 - Exit') print('\n') number_nodes = 0 start_time = 0 try: user_option = int(input('What is your choice: ')) if user_option == 1: # Blind Search print('This search may take a long time to display the result. Be patient!') start_time = time.time() return_success, number_nodes = blind_search(classroom_list, subjects_list, number_nodes) elif user_option == 2: # Heuristic Search start_time = time.time() return_success, number_nodes = heuristic_search(classroom_list, subjects_list, teachers_list, number_nodes) elif user_option == 3: # Restriction Search start_time = time.time() return_success, number_nodes = restriction_search(classroom_list, subjects_list, teachers_list, number_nodes) elif user_option == 4: print("\n_________________________________________________") print("| Subject ID | Teacher ID | Workload |") print("_________________________________________________") for subject in subjects_list: print("| ", subject.id_subject, " | ", subject.id_teacher, " | ", subject.lessons_quantity, " |") print("_________________________________________________") elif user_option == 5: exit() else: os.system("cls") print('Invalid option, choice must be (1-5). Try Again!') main() if user_option in [1, 2, 3]: print('\n') print("Runtime: %s seconds" % (time.time() - start_time)) print("Number of nodes visited: %s \n" % number_nodes) print("--- Schedules ---") print('[Classroom/ Day][--Monday--] [--Tuesday--] [--Wednesday--] [--Thursday--] [--Friday--]') for class_option in classroom_list: print('Classroom ', class_option.id_classroom, ' ', class_option.schedule_matrix) option = input('\nReturn to menu (Y or N)? ') if option == 'y' or option == 'Y': os.system("cls") main() else: exit() except ValueError: os.system("cls") print('Number must be a integer') main()
def run_heuristic_algorithm(initial_state, heuristic): root_node = Node(None, initial_state, 0, None) final_node, monitor = heuristic_search(root_node, heuristic) log_results(final_node, monitor)