def run_map_coloring(adjacency_file, num_colors=4, map_type=None): """ :param adjacency_file: a txt file with contrys and thir nighbers :param num_colors:a selaction from the user :param map_type: the type of map we are useing :return: if there is a solotion it shows it and None if there is no solotion """ countries = read_adj_file(adjacency_file) list_of_items = [] for key in countries.keys(): list_of_items.append(key) dict_items_to_vals = {} for key in countries.keys(): dict_items_to_vals[key] = "color" if num_colors == 1: return None else: set_of_assignments = num_colors_check(num_colors) if general_backtracking(list_of_items, dict_items_to_vals, 0, set_of_assignments, legal_assignment_func, countries): return dict_items_to_vals else: return None
def run_game(sudoku_file, print_mode=False): state = load_game(sudoku_file) if print_mode: print_board(state) ret = general_backtracking( [key for key, value in state.items() if value == 0], state, 0, range(1, 10), check_board) if print_mode: print("~") print_board(state)
def back_track_degree_heuristic(adj_dict, colors): '''A function that sort all the countries by num of there neighbors and than preform the genral back track''' color_dic = {key: None for key in adj_dict} block_dic = re.get_land_blocks(adj_dict) for block in block_dic: list_of_countreys = list(block_dic[block]) list_of_countreys = sorted(list_of_countreys, key=lambda x: len(adj_dict[x]), reverse=True) if not general_backtracking(list_of_countreys, color_dic, 0, colors, re.legal_coloring_func, adj_dict): return None return color_dic
def run_game(sudoku_file, print_mode=False): """ :param sudoku_file: a location for a sudoku file :param print_mode: a boolean indicating to print the solution or not :return: True if a solution exists False otherwise """ board = load_game(sudoku_file) empty_cells = get_empty_cells_in_sudoku(board) solution_exits = general_backtracking(empty_cells, board, 0, LEGAL_ASSIGNMENTS, check_board) if print_mode: if solution_exits: print_board(board) return solution_exits
def run_game(sudoku_file, print_mode=False): '''A function that use the general_backtracking function to soulve the sudouko table :return true if the table is solvable and print the board if so the function will and False if the table is unsolvable''' board = load_game(sudoku_file) set_of_ass = [] for i in range(1, 10): set_of_ass.append(i) list_of_items = [i for i in board if board[i] == 0] list_of_items.sort() if general_backtracking(list_of_items, board, 0, set_of_ass, check_board): print_mode = True if print_mode == True: print_board(board) return True return False
def run_map_coloring(adjacency_file, num_colors = 4, map_type = None): """ This set colors for a given list of countries on a map so that no neighboring countries will have the same color. if there is no such option, the function will return None :param adjacency_file: path :param num_colors: the amount of colors to check :return: if there is a solution - a dictionary representing countries and their colors. """ dict_of_neighbor = read_adj_file(adjacency_file) colored_map = {} for country in dict_of_neighbor.keys(): colored_map[country] = "" if general_backtracking(list(colored_map.keys()), colored_map, 0, COLORS[:num_colors], legal_color, dict_of_neighbor): return colored_map
def run_game(sudoku_file, print_mode=False): starting_index = 0 list_of_items = [] board = load_game(sudoku_file) for key in board: if board[key] == EMPTY_CELL: list_of_items.append(key) list_of_items.sort() if general_backtracking(list_of_items, board, starting_index, SUDOKU_ASSIGNMENTS, check_board): print_mode = True if print_mode == True: print_board(board)
def back_track_degree_heuristic(adj_dict, colors): """ :param adj_dict: dictionary whose keys are the names of the countries and the values of each key are neighboring of the country. :param colors: List of colors that are allowed to paint the countries :return: The output of the helper_run_map_coloring function that returns True if possible paint all the country in a different color from all neighboring countries, and None if not. """ dict_items_to_vals = {} for key in adj_dict.keys(): dict_items_to_vals[key] = "color" max_list = max_finder(adj_dict) if general_backtracking(max_list, dict_items_to_vals, 0, colors, ex11_map_coloring.legal_assignment_func, adj_dict): return dict_items_to_vals else: return None
def back_track_FC(adj_dict, colors): """ use general backtracking in order to solve map coloring problem with an improvement of neighbours legality forward checking per recursion stage :param adj_dict: dict contains countries in the map & their neighbours :param colors: list containing all legal colors :return: dict with the solution of {country : color}, or None if no solution """ list_of_items = list(adj_dict.keys()) color_map = adj_dict.fromkeys(adj_dict, None) # naming the check func legal_assignment_func = FC_check if general_backtracking(list_of_items, color_map, mc.START_INDEX, colors, legal_assignment_func, adj_dict, colors): return color_map else: return None
def run_game(sudoku_file, print_mode=False): """ :param sudoku_file: input file location :param print_mode: should we print in case of solution :return: True if there is a solution, False otherwise """ board = load_game(sudoku_file) list_of_items = [] for key in board.keys(): if board[key] == 0: list_of_items.append(key) set_of_assignments = range(1, 10) legal_assignment_func = check_board if general_backtracking(list_of_items, board, 0, set_of_assignments, legal_assignment_func): if print_mode: print_board(board) return True else: return False
def run_map_coloring(adjacency_file, num_colors=4, map_type=None): """ :param adjacency_file: a file containing nodes and their connections :return: a dict representing the nodes and their connections :param num_colors: the number of colors to paint the map in :param map_type: not implemented in my code :return: True if a solution exists False otherwise """ nodes_and_connections_dict = read_adj_file(adjacency_file) list_of_nodes = list(nodes_and_connections_dict.keys()) nodes_coloring = {} solution_exits = general_backtracking(list_of_nodes, nodes_coloring, 0, COLORS[:num_colors], coloring_is_legal, nodes_and_connections_dict) if solution_exits: return nodes_coloring return None
def run_game(sudoku_file, print_mode=False): """ This function checks if the given Sudoku board has solution. if it is and print_mode=True - prints the solution. :param sudoku_file: path :param print_mode: boolean variable. default to False. :return: True or False """ game_borad = load_game(sudoku_file) list_of_items = [] # create a list of the empty squares: for key in game_borad.keys(): if game_borad[key] == 0: list_of_items.append(key) if general_backtracking(sorted(list_of_items), game_borad, 0, range(1, 10), check_board): if print_mode is True: print_board(game_borad) return True else: return False
def run_map_coloring(adjacency_file, num_colors=4, map_type=None): """ main func - solving map coloring problem using general backtracking :param adjacency_file: dict contains countries in the map & their neighbours :param num_colors: number of wanted colors for solving the problem :param map_type: :return: dict with the solution of {country : color}, or None if no solution """ adjacency = read_adj_file(adjacency_file) color_map = dict(adjacency) list_of_items = list(adjacency.keys()) set_of_assignments = COLORS[:num_colors] legal_assignment_func = check_map if general_backtracking(list_of_items, color_map, START_INDEX, set_of_assignments, legal_assignment_func, adjacency): return color_map else: return None
def back_track_degree_heuristic(adj_dict, colors): """ use general backtracking in order to solve map coloring problem with an improvement of list_of_items sorted by countries with maximal to minimal num of neighbours :param adj_dict: dict contains countries in the map & their neighbours :param colors: list containing all legal colors :return: dict with the solution of {country : color}, or None if no solution """ neighbour_num = {} # dict with {countries : number of neighbours for country in adj_dict: neighbour_num[country] = len(adj_dict[country]) list_of_items = sorted(neighbour_num, key=neighbour_num.get, reverse=True) color_map = adj_dict.fromkeys(adj_dict, None) # naming the check func legal_assignment_func = mc.check_map if general_backtracking(list_of_items, color_map, mc.START_INDEX, colors, legal_assignment_func, adj_dict): return color_map else: return None
def run_game(sudoku_file, print_mode=False): """ the main function of the game :param sudoku_file: file that contains a sudoku table :param print_mode: Accepted as False. If the user wants to print the board he needs to input true in the run commend :return: True or false depending on the solution of the board if the user changes the print mode to true it will print the bord if we have a solution """ board = load_game(sudoku_file) set_of_assignments = LEGAL_NUMBERS_RANGE list_of_items = empty_position_exists(board, []) print(list_of_items) if general_backtracking(list_of_items, board, 0, set_of_assignments, check_board): if print_mode: print_board(board) return True else: if print_mode: print_board(board) return False
if neibhour not in map.keys(): print("missing " + str(neibhour)) return False if map[neibhour] == map[item]: print(str(item), str(neibhour), "have the same color") return False return True if __name__ == "__main__": items = [1, 2, 3, 4] assignments = [1, 2, 3, 4, 5, 6, 7] # 0.1 expected = True result = general_backtracking(items, {}, 0, assignments, legal_assignment2) check(0.1, expected, result, "") # 0.2 expected = True result = general_backtracking(items, {}, 0, assignments, legal_assignment3) check(0.2, expected, result, "") # 0.3 expected = False result = general_backtracking(items, {}, 0, assignments, legal_assignment4) check(0.3, expected, result, "") # 0.4 expected = True result = general_backtracking(items, {}, 0, assignments,
def run_map_coloring(adjacency_file, num_colors=4, map_type=None): graph = read_adj_file(adjacency_file) State = {key: None for key in graph.keys()} general_backtracking([key for key in graph.keys()], State, 0, COLORS[:num_colors], check_map, graph)
number_of_queens_list = [8, 4, 3] expected_result = [True, True, False] for i in range(len(number_of_queens_list)): number_of_queens = number_of_queens_list[i] expected = expected_result[i] print('Running the ' + str(number_of_queens) + ' queens problem ----> ', end='') try: queens = make_queen_list(number_of_queens) stating_dict = make_starting_queen_dict(queens) board = make_board(number_of_queens) with Timeout2.Timeout(420): student_result = ex11_backtrack.general_backtracking( queens, stating_dict, 0, board, is_location_safe) if isinstance(student_result, bool) and student_result == expected: print("test passed!\n") else: print('test failed...') print('expected: ' + str(expected) + '. but got ' + str(student_result)) print(str(BIG_TEST_FAIL_REDUCTION) + " points were reduced\n") total_points = total_points - BIG_TEST_FAIL_REDUCTION except Exception as e: print("your code raised and exception: ") print(e) traceback.print_exc() print(str(BIG_TEST_FAIL_REDUCTION) + " points were reduced\n") total_points = total_points - BIG_TEST_FAIL_REDUCTION