def parse_file(self, file_name, k_value): gac = GAC() # Read all lines in the file while stripping the ending newline lines = [line.rstrip('\n') for line in open(file_name)] vertices_and_edges = map(int, lines[0].split(' ')) # Create vertices for i in range(1, vertices_and_edges[0] + 1): # Get the state state = map(float, lines[i].split(' ')) # Init new Node and set the correct values variable = Variable(i) variable.state = (state[1], state[2]) variable.domain = range(k_value) # Add node to CSP class gac.variables.append(variable) # Create constraints for i in range(vertices_and_edges[0] + 1, len(lines)): new_constraint = Constraint() # Set the correct constraint new_constraint.method = makefunc(['n'], 'n[0] != n[1]') # Get the constraint line constraint_line = map(int, lines[i].split(' ')) # Loop all vars in the constraint for var in constraint_line: new_constraint.vars.append(var) # Apply the new constraint to the list GAC.Constraints.append(new_constraint) # Set the correct gac for GACState GACState.GAC = GAC # Create the initial csp state gac_state = GACState() gac_state.gac = gac gac_state.type = GACState.START # Set the csp state to astar_gac self.astar_gac.gac_state = gac_state # Begin CSP self.astar_gac.start() # Run the GUI self.run()
def parse_file(self, file_name): # Read all lines in the file while stripping the ending newline lines = [line.rstrip('\n') for line in open(file_name)] # Get the sizes rows_and_columns = map(int, lines[0].split(' ')) # Store board data board_data = [] # Add row specs to board data row_specs = [] for line in lines[1:rows_and_columns[1] + 1]: row_specs.append(map(int, line.split(' '))) board_data.append({ 'specs': row_specs[::-1], 'prefix': 'r', 'length': rows_and_columns[0] }) # Add column specs to board data column_specs = [] for line in lines[rows_and_columns[1] + 1:]: column_specs.append(map(int, line.split(' '))) board_data.append({ 'specs': column_specs, 'prefix': 'c', 'length': rows_and_columns[1] }) # Set references to the Builder class Builder.makefunc = staticmethod(makefunc) # Build the variables variables = Builder.build_variables(board_data) # Build the constraints constraints = Builder.build_constraints(variables) # Set the constraints GAC.Constraints = constraints # Initiate GAC and set the variables gac = NonoGAC() gac.variables = variables # Set the correct gac for GACState GACState.GAC = NonoGAC gac_state = GACState() gac_state.gac = gac gac_state.type = GACState.START # Set the gac state self.astar_gac.gac_state = gac_state # Begin CSP self.astar_gac.start() # Run the GUI self.run()