Solver Manager
noqx.manager
The unified manager for solvers.
Classes:
-
Solver–Base class to create solvers.
Functions:
-
load_solver–Load a solver from a valid directory and record the solver module to a dictionary.
-
list_solver_metadata–Generate A dictionary containing the attributes for each solver.
-
prepare_puzzle–Convert the raw puzzle from Penpa+ format to a
Puzzleobject. -
generate_program–Generate the solver program in the Answer Set Programming language.
-
store_solution–Convert the solution from Clingo to a
Puzzleobject and refine it.
Solver
Base class to create solvers.
- New solvers should inherit this class and set appropriate attribute values.
Attributes:
-
name(str) –The name of the solver.
-
category(str) –The category of the solver, should be
shade(Shading),loop(Loop / Path),region(Area Division),num(Number),var(Variety),draw(Drawing),unk(Unknown). -
aliases(List[str] = []) –A list of alternative names for the solver.
-
examples(List[Dict[str, Any]] = []) –A list of examples of the solver, each example can be created in two conflicting ways,
dataandurl:data: directly draw the board in noqx and get the data URL by usingShare → Editing URL → Copy. The URL are suggested to be generated with the following conditions:- contains all the required
modesin this puzzle. - contains the required
sub-typesin this puzzle. - contains necessary initial conditions to pass the coverage tests.
- set
edit modetosolution modeinstead ofproblem mode.
- contains all the required
url: draw the board in puzz.link and useFile → Export URLto get the board URL.config(Optional): the configuration of the solver, which will be passed to the solver when it is created, and the keys ofconfigare the same asparameterskeys.test(Optional): whether the example is used as test case, the default value isTrue, and cannot be used together withurlway.- Lots of examples can be found at pzplus.
-
parameters(Dict[str, Any]) –A dictionary of parameters of the solver, which will be passed to the solver when it is created.
Warning
When you directly draw the board in noqx, make sure to set the puzzle type first. Currently, the puzzle type selection is locked if the user starts drawing the board.
Methods:
-
__init__–Initialize an internal program.
-
add_program_line–Add a line to the internal program.
-
reset–Clear the internal program.
-
solve–Generate the solver program in the Answer Set Programming language.
-
refine–Refine the solution.
Source code in noqx/manager.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
__init__
add_program_line
add_program_line(line: str)
Add a line to the internal program.
Parameters:
-
(linestr) –A line in the Answer Set Programming language.
reset
solve
Generate the solver program in the Answer Set Programming language.
- New solvers should at least implement this method.
Parameters:
-
(_Puzzle) –A
Puzzleobject for the program.
Raises:
-
NotImplementedError–If this method is not implemented.
Source code in noqx/manager.py
refine
Refine the solution.
- The default operation is passing the original
solutionobject to the output.
Parameters:
-
(solutionPuzzle) –A
Puzzleobject to be refined.
load_solver
load_solver(solver_dir: str, solver_name: str) -> None
Load a solver from a valid directory and record the solver module to a dictionary.
Parameters:
-
(solver_dirstr) –The directory where the solver is located.
-
(solver_namestr) –The name of the solver to load.
Raises:
-
ValueError–If the solver already exists.
-
ImportError–If the solver module cannot be imported.
Source code in noqx/manager.py
list_solver_metadata
Generate A dictionary containing the attributes for each solver.
Source code in noqx/manager.py
prepare_puzzle
prepare_puzzle(puzzle_name: str, puzzle_content: str, param: Dict[str, Any]) -> Puzzle
Convert the raw puzzle from Penpa+ format to a Puzzle object.
Parameters:
-
(puzzle_namestr) –The name of the puzzle.
-
(puzzle_contentstr) –The puzzle content exported in Penpa+ format.
-
(paramDict[str, Any]) –Additional parameters for the puzzle.
Source code in noqx/manager.py
generate_program
Generate the solver program in the Answer Set Programming language.
- The program generator is based on the puzzle name and the corresponding solver module.
Parameters:
-
(puzzlePuzzle) –A
Puzzleobject for the program.
Source code in noqx/manager.py
store_solution
Convert the solution from Clingo to a Puzzle object and refine it.
-
The solution refiner is based on the puzzle name and the corresponding solver module. It does nothing by default.
-
Since the solution from Clingo is in a raw string format, this function will parse the string and fill in the corresponding attributes of the
Puzzleobject. The parsing order will be: edges, lines, texts (numbers/contents), triangle symbols, colors, other symbols and debugging elements.
Parameters:
-
(puzzlePuzzle) –A
Puzzleobject without stored solution. -
(model_strstr) –The raw solution string generated by the Clingo solver.