def main(): # Initialize global variables global_var.init() # Generate a new solution grid sg.generate_grid(global_var.NEW_BOARD) # Print formatted new solution grid print("\nGenerated solution grid: ") print_board(global_var.NEW_BOARD) # Strip values from solution grid to create puzzle pg.strip_values(global_var.NEW_BOARD) # Print new puzzle print("\nNew puzzle: ") print_board(global_var.NEW_BOARD) # Print given puzzle print("\nGiven puzzle: ") print_board(global_var.NEW_BOARD) # Solve given puzzle s.solve(global_var.NEW_BOARD) # Print formatted solution to given puzzle print("\nSolved puzzle: ") print_board(global_var.NEW_BOARD) print(s.BRANCH_DIFFICULTY_SCORE)
def solve(self): cases = [f"case{i}_{j}" for i in range(0, 9) for j in range(0, 9)] sudoku = [[None for i in range(9)] for k in range(9)] for case in cases: textedit = self.centralwidget.findChild(QtGui.QTextEdit, case) number = textedit.toPlainText() if number == '': number = 0 index = (int(case[6]), int(case[4])) try: number = int(number) except: raise ValueError("Not numeric value entered") if int(number) not in range(0, 10): raise ValueError("Out of range value entered.") else: sudoku[index[1]][index[0]] = int(number) # solving.sudo_print(sudoku) sudoku = solving.solve(sudoku) for case in cases: textedit = self.centralwidget.findChild(QtGui.QTextEdit, case) if textedit.toPlainText() == '': textedit.setTextColor(QtGui.QColor(0, 0, 200)) textedit.setText(str(sudoku[int(case[6])][int(case[4])])) textedit.setAlignment(QtCore.Qt.AlignCenter)
def main(): # Initialize the data network to be the correct size/ shape nodes = initialize(networkfile) # Run through the actual simulation stuff results = solve(nodes, 1.0e-6) # Display the results for node in results: printNode(node)
def percent_solved(iterations): cube_reward = training(iterations) solved_times = 0 for i in tqdm(range(100)): scrambled_cube = scramble()[0] solved_cube = solve(scrambled_cube, cube_reward)[0] if cube_completeness(solved_cube) == 1: solved_times += 1 print("after " + str(iterations) + " training iterations") print(str(solved_times) + " cubes were solved out of 100")
from iteration import training from solving import scramble, solve from cube_utilities import cube_completeness import pycuber as pc cube_reward = training(10) scrambled_cube, scramble_steps = scramble() print("scrambled cube") print(scrambled_cube) print("formula used to scramble cube:") print(scramble_steps) solved_cube, total_steps, actions = solve(scrambled_cube, cube_reward) print("after solving") print(solved_cube) print("number of steps taken " + str(total_steps)) print("actions taken to solve cube: ") print(pc.Formula(actions)) print("Completeness of cube: " + str(cube_completeness(solved_cube)))
def project(v, V, bcs=None, mesh=None, solver_parameters=None, form_compiler_parameters=None, name=None): """Project an :class:`.Expression` or :class:`.Function` into a :class:`.FunctionSpace` :arg v: the :class:`.Expression`, :class:`ufl.Expr` or :class:`.Function` to project :arg V: the :class:`.FunctionSpace` or :class:`.Function` to project into :arg bcs: boundary conditions to apply in the projection :arg mesh: the mesh to project into :arg solver_parameters: parameters to pass to the solver used when projecting. :arg form_compiler_parameters: parameters to the form compiler :arg name: name of the resulting :class:`.Function` If ``V`` is a :class:`.Function` then ``v`` is projected into ``V`` and ``V`` is returned. If `V` is a :class:`.FunctionSpace` then ``v`` is projected into a new :class:`.Function` and that :class:`.Function` is returned. Currently, `bcs`, `mesh` and `form_compiler_parameters` are ignored.""" if isinstance(V, functionspace.FunctionSpaceBase): ret = function.Function(V, name=name) elif isinstance(V, function.Function): ret = V V = V.function_space() else: raise RuntimeError( 'Can only project into functions and function spaces, not %r' % type(V)) if isinstance(v, expression.Expression): shape = v.shape() # Build a function space that supports PointEvaluation so that # we can interpolate into it. if isinstance(V.ufl_element().degree(), tuple): deg = max(V.ufl_element().degree()) else: deg = V.ufl_element().degree() if v.rank() == 0: fs = functionspace.FunctionSpace(V.mesh(), 'DG', deg+1) elif v.rank() == 1: fs = functionspace.VectorFunctionSpace(V.mesh(), 'DG', deg+1, dim=shape[0]) else: raise NotImplementedError( "Don't know how to project onto tensor-valued function spaces") f = function.Function(fs) f.interpolate(v) v = f elif isinstance(v, function.Function): if v.function_space().mesh() != ret.function_space().mesh(): raise RuntimeError("Can't project between mismatching meshes") elif not isinstance(v, ufl.expr.Expr): raise RuntimeError("Can't only project from expressions and functions, not %r" % type(v)) if v.shape() != ret.shape(): raise RuntimeError('Shape mismatch between source %s and target function spaces %s in project' % (v.shape(), ret.shape())) p = ufl_expr.TestFunction(V) q = ufl_expr.TrialFunction(V) a = ufl.inner(p, q) * V.mesh()._dx L = ufl.inner(p, v) * V.mesh()._dx # Default to 1e-8 relative tolerance if solver_parameters is None: solver_parameters = {'ksp_type': 'cg', 'ksp_rtol': 1e-8} else: solver_parameters.setdefault('ksp_type', 'cg') solver_parameters.setdefault('ksp_rtol', 1e-8) solving.solve(a == L, ret, bcs=bcs, solver_parameters=solver_parameters, form_compiler_parameters=form_compiler_parameters) return ret
cube = rotations.rotate_F_counter(cube) elif r == 6: cube = rotations.rotate_R(cube) elif r == 7: cube = rotations.rotate_R_counter(cube) elif r == 8: cube = rotations.rotate_B(cube) elif r == 9: cube = rotations.rotate_B_counter(cube) elif r == 10: cube = rotations.rotate_D(cube) elif r == 11: cube = rotations.rotate_D_counter(cube) if edge_all_good(cube, a, b): print("\n" + str(r)) return cube = copy_cube(prevcube) print() t += 1 if t == 2: break cube = cube_to_solved() cube = scramble(cube, 20) print_cube(cube) cube = solving.solve(cube) print_cube(cube) get_edge_moves(cube, 'y', 'o')