def solve(self): ''' Starts the algorithm. ''' self.reset() self.problem = Probleminstance(self.board.domain_dict, self.board.constraint_dict) self.problem.initialize() if self.problem.is_solution(): self.color_vertexes(self.problem) self.solve_loop()
def solve(self, number): ''' Initializes a solve run, and calls the loop to solve. ''' self.k = int(number) self.board.K = int(number) self.reset() self.board.make_domain_dict() self.board.make_constraint_dict() self.vc = Probleminstance(self.board.domain_dict, self.board.constraint_dict) self.vc.initialize() if self.vc.is_solution(): self.color_vertexes(self.vc) return self.solve_loop()
class Controller: ''' Class responsible for initializing a run, and drawing to the gui. ''' def __init__(self, gui): ''' Initializes with a gui. ''' self.gui = gui self.K = 2 self.colored = list() def reset(self): ''' Resets the gui, preparing it for a new run. ''' self.gui.clear() self.gui.build(self.board) self.colored = dict() def open_board(self): ''' Opens a board when it is selected in the board menu. ''' filename = askopenfilename(parent=root) self.board = Board(filename, self.K) self.board.parse_text_file() self.reset() gui.pack() def solve(self, number): ''' Initializes a solve run, and calls the loop to solve. ''' self.k = int(number) self.board.K = int(number) self.reset() self.board.make_domain_dict() self.board.make_constraint_dict() self.vc = Probleminstance(self.board.domain_dict, self.board.constraint_dict) self.vc.initialize() if self.vc.is_solution(): self.color_vertexes(self.vc) return self.solve_loop() def solve_loop(self): ''' Recursively calls one step of the algorithm at a time, coloring the gui for each time. ''' solutions = self.vc.solve() problem_object = solutions[0] current = solutions[1] prev_current = solutions[2] if isinstance(current, Probleminstance): self.color_vertexes(current) root.after(refresh_time, self.solve_loop) else: print("------------------") if not prev_current.is_solution(): print("Run terminated, the problem is not solvable") print("------------------") print(current) count = 0 for domain in prev_current.domains: if len(prev_current.domains[domain]) > 1: count += 1 print("Variables without color assignment: " + str(count)) self.color_vertexes(prev_current) def color_vertexes(self, current): ''' Colors the vertexes that has a set color. ''' for vertex in current.domains: if len(current.domains[vertex]) == 1: a = self.board.vertexes[vertex] num = current.domains[vertex][0] x = a[1] y = a[2] try: if self.colored[x, y] == num: continue except KeyError: self.colored[x, y] = num if num == 0: self.gui.color_vertex(x, y, 'blue') elif num == 1: self.gui.color_vertex(x, y, 'green') elif num == 2: self.gui.color_vertex(x, y, 'yellow') elif num == 3: self.gui.color_vertex(x, y, 'red') elif num == 4: self.gui.color_vertex(x, y, 'cadet blue') elif num == 5: self.gui.color_vertex(x, y, 'lemon chiffon') elif num == 6: self.gui.color_vertex(x, y, 'pink') elif num == 7: self.gui.color_vertex(x, y, 'dark khaki') elif num == 8: self.gui.color_vertex(x, y, 'chocolate') elif num == 9: self.gui.color_vertex(x, y, 'seashell2') else: self.gui.color_vertex(x, y, 'black') self.colored[x, y] = num
class Controller: ''' Class responsible for initializing a run, and drawing to the gui. ''' def __init__(self, gui): ''' Initializes with a gui. ''' self.gui = gui self.K = 2 self.colored = list() def reset(self): ''' Resets the gui, preparing it for a new run. ''' self.gui.clear() self.gui.build(self.board) self.colored = dict() def open_board(self): ''' Opens a board when it is selected in the board menu. ''' filename = askopenfilename(parent=root) self.board = Board(filename, self.K) self.board.parse_text_file() self.reset() gui.pack() def solve(self, number): ''' Initializes a solve run, and calls the loop to solve. ''' self.k = int(number) self.board.K = int(number) self.reset() self.board.make_domain_dict() self.board.make_constraint_dict() self.vc = Probleminstance(self.board.domain_dict, self.board.constraint_dict) self.vc.initialize() if self.vc.is_solution(): self.color_vertexes(self.vc) return self.solve_loop() def solve_loop(self): ''' Recursively calls one step of the algorithm at a time, coloring the gui for each time. ''' solutions = self.vc.solve() problem_object = solutions[0] current = solutions[1] prev_current = solutions[2] if isinstance(current, Probleminstance): self.color_vertexes(current) root.after(refresh_time, self.solve_loop) else: print("------------------") if not prev_current.is_solution(): print("Run terminated, the problem is not solvable") print("------------------") print(current) count = 0 for domain in prev_current.domains: if len(prev_current.domains[domain]) > 1: count += 1 print ("Variables without color assignment: " + str(count)) self.color_vertexes(prev_current) def color_vertexes(self, current): ''' Colors the vertexes that has a set color. ''' for vertex in current.domains: if len(current.domains[vertex]) == 1: a = self.board.vertexes[vertex] num = current.domains[vertex][0] x = a[1] y = a[2] try: if self.colored[x, y] == num: continue except KeyError: self.colored[x, y] = num if num == 0: self.gui.color_vertex(x, y, 'blue') elif num == 1: self.gui.color_vertex(x, y, 'green') elif num == 2: self.gui.color_vertex(x, y, 'yellow') elif num == 3: self.gui.color_vertex(x, y, 'red') elif num == 4: self.gui.color_vertex(x, y, 'cadet blue') elif num == 5: self.gui.color_vertex(x, y, 'lemon chiffon') elif num == 6: self.gui.color_vertex(x, y, 'pink') elif num == 7: self.gui.color_vertex(x, y, 'dark khaki') elif num == 8: self.gui.color_vertex(x, y, 'chocolate') elif num == 9: self.gui.color_vertex(x, y, 'seashell2') else: self.gui.color_vertex(x, y, 'black') self.colored[x, y] = num
class Controller(): ''' Class responsible for controlling the algorithm, and updates the gui. ''' def __init__(self, gui): ''' Initializes the controller with a gui ''' self.gui = gui def open_board(self): ''' Opens the board selected from the file menu, and prepares for a new run. ''' filename = askopenfilename(parent=root) self.board = Board(filename) self.board.parse_text_file() self.reset() gui.pack() def reset(self): ''' Resets the gui and builds a new board. ''' self.gui.clear() self.gui.build(self.board) self.board.parse_text_file() def solve(self): ''' Starts the algorithm. ''' self.reset() self.problem = Probleminstance(self.board.domain_dict, self.board.constraint_dict) self.problem.initialize() if self.problem.is_solution(): self.color_vertexes(self.problem) self.solve_loop() def solve_loop(self): ''' Iteratively solves the problem, printing steps each time. ''' solutions = self.problem.solve() current = solutions[0] prev_current = solutions[1] if isinstance(current, Probleminstance): root.after(refresh_time, self.solve_loop) else: print("---------------") if not prev_current.is_solution(): print("Run terminated, the problem is not solvable") print("------------------") print(current) self.color_vertexes(prev_current) def color_vertexes(self, current): ''' Colors all of the vertexes. ''' for domain in current.domains: if len(current.domains[domain]) == 1 and domain[0] == 0: for index in range(len(current.domains[domain][0])): self.gui.draw_square(domain[1], index, current.domains[domain][0][index])