@logic-pad/core 0.25.3 → 0.26.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/assets/logic-core.global.d.ts +16 -104
  2. package/dist/src/data/rules/offByXRule.js +2 -0
  3. package/dist/src/data/solver/allSolvers.js +0 -2
  4. package/dist/src/data/solver/auto/autoSolver.d.ts +2 -1
  5. package/dist/src/data/solver/auto/autoSolver.js +13 -17
  6. package/dist/src/data/solver/backtrack/backtrackSolver.d.ts +3 -1
  7. package/dist/src/data/solver/backtrack/backtrackSolver.js +2 -2
  8. package/dist/src/data/solver/backtrack/backtrackWorker.js +2 -2
  9. package/dist/src/data/solver/backtrack/data.d.ts +1 -0
  10. package/dist/src/data/solver/backtrack/data.js +43 -0
  11. package/dist/src/data/solver/backtrack/symbols/areaNumber.js +4 -1
  12. package/dist/src/data/solver/backtrack/symbols/letter.js +4 -1
  13. package/dist/src/data/solver/cspuz/cspuzSolver.d.ts +2 -1
  14. package/dist/src/data/solver/cspuz/cspuzSolver.js +46 -19
  15. package/dist/src/data/solver/solver.d.ts +2 -1
  16. package/dist/src/data/solver/solver.js +8 -7
  17. package/dist/src/data/solver/universal/universalSolver.d.ts +0 -1
  18. package/dist/src/data/solver/universal/universalSolver.js +0 -7
  19. package/dist/src/data/symbols/areaNumberSymbol.d.ts +1 -1
  20. package/dist/src/data/symbols/areaNumberSymbol.js +2 -0
  21. package/dist/src/data/symbols/everyLetterSymbol.js +2 -0
  22. package/dist/src/data/symbols/houseSymbol.d.ts +1 -1
  23. package/dist/src/data/symbols/houseSymbol.js +2 -0
  24. package/dist/src/data/symbols/letterSymbol.js +2 -0
  25. package/dist/src/data/symbols/numberSymbol.d.ts +1 -1
  26. package/dist/src/data/symbols/numberSymbol.js +4 -1
  27. package/dist/src/data/symbols/symbol.d.ts +5 -0
  28. package/dist/src/data/symbols/symbol.js +32 -0
  29. package/dist/src/index.d.ts +2 -15
  30. package/dist/src/index.js +2 -15
  31. package/package.json +1 -3
  32. package/dist/src/data/solver/z3/modules/areaNumberModule.d.ts +0 -9
  33. package/dist/src/data/solver/z3/modules/areaNumberModule.js +0 -27
  34. package/dist/src/data/solver/z3/modules/cellCountModule.d.ts +0 -9
  35. package/dist/src/data/solver/z3/modules/cellCountModule.js +0 -51
  36. package/dist/src/data/solver/z3/modules/connectAllModule.d.ts +0 -9
  37. package/dist/src/data/solver/z3/modules/connectAllModule.js +0 -24
  38. package/dist/src/data/solver/z3/modules/dartModule.d.ts +0 -9
  39. package/dist/src/data/solver/z3/modules/dartModule.js +0 -61
  40. package/dist/src/data/solver/z3/modules/index.d.ts +0 -3
  41. package/dist/src/data/solver/z3/modules/index.js +0 -10
  42. package/dist/src/data/solver/z3/modules/letterModule.d.ts +0 -9
  43. package/dist/src/data/solver/z3/modules/letterModule.js +0 -33
  44. package/dist/src/data/solver/z3/modules/modules.gen.d.ts +0 -8
  45. package/dist/src/data/solver/z3/modules/modules.gen.js +0 -12
  46. package/dist/src/data/solver/z3/modules/myopiaModule.d.ts +0 -9
  47. package/dist/src/data/solver/z3/modules/myopiaModule.js +0 -56
  48. package/dist/src/data/solver/z3/modules/regionAreaModule.d.ts +0 -9
  49. package/dist/src/data/solver/z3/modules/regionAreaModule.js +0 -40
  50. package/dist/src/data/solver/z3/modules/viewpointModule.d.ts +0 -9
  51. package/dist/src/data/solver/z3/modules/viewpointModule.js +0 -29
  52. package/dist/src/data/solver/z3/modules/z3Module.d.ts +0 -7
  53. package/dist/src/data/solver/z3/modules/z3Module.js +0 -3
  54. package/dist/src/data/solver/z3/utils.d.ts +0 -2
  55. package/dist/src/data/solver/z3/utils.js +0 -26
  56. package/dist/src/data/solver/z3/z3Solver.d.ts +0 -12
  57. package/dist/src/data/solver/z3/z3Solver.js +0 -123
  58. package/dist/src/data/solver/z3/z3SolverContext.d.ts +0 -13
  59. package/dist/src/data/solver/z3/z3SolverContext.js +0 -40
@@ -1,9 +0,0 @@
1
- import { Solver, Optimize } from 'z3-solver';
2
- import GridData from '../../../grid.js';
3
- import Z3SolverContext from '../z3SolverContext.js';
4
- import Z3Module from './z3Module.js';
5
- export default class ViewpointModule extends Z3Module {
6
- readonly id: string;
7
- encode<Name extends string>(grid: GridData, ctx: Z3SolverContext<Name, Solver<Name> | Optimize<Name>>): void;
8
- }
9
- export declare const instance: ViewpointModule;
@@ -1,29 +0,0 @@
1
- import Z3Module from './z3Module.js';
2
- import { instance as viewpointInstance, } from '../../../symbols/viewpointSymbol.js';
3
- import { Point, reduceCells } from 'grilops';
4
- import { DIRECTIONS } from '../../../primitives.js';
5
- import { move } from '../../../dataHelper.js';
6
- import { convertDirection } from '../utils.js';
7
- export default class ViewpointModule extends Z3Module {
8
- id = viewpointInstance.id;
9
- encode(grid, ctx) {
10
- const symbols = grid.symbols.get(this.id);
11
- // optimizations
12
- if (!symbols || symbols.length === 0) {
13
- return;
14
- }
15
- // encode for real
16
- for (const symbol of symbols) {
17
- const x = Math.floor(symbol.x);
18
- const y = Math.floor(symbol.y);
19
- const origin = ctx.grid.cellAt(new Point(y, x));
20
- const sumTerms = [];
21
- DIRECTIONS.forEach(direction => {
22
- const startPos = move({ x, y }, direction);
23
- sumTerms.push(reduceCells(ctx.grid.ctx, ctx.grid, new Point(startPos.y, startPos.x), convertDirection(direction), ctx.ctx.Int.val(0), (acc, cell) => ctx.ctx.If(cell.eq(origin), acc.add(1), acc), (_, cell) => cell.neq(origin)));
24
- });
25
- ctx.solver.add(ctx.ctx.Sum(ctx.ctx.Int.val(1), ...sumTerms).eq(symbol.number));
26
- }
27
- }
28
- }
29
- export const instance = new ViewpointModule();
@@ -1,7 +0,0 @@
1
- import GridData from '../../../grid.js';
2
- import Z3SolverContext from '../z3SolverContext.js';
3
- export default abstract class Z3Module {
4
- abstract get id(): string;
5
- abstract encode<Name extends string>(grid: GridData, ctx: Z3SolverContext<Name>): void;
6
- }
7
- export declare const instance: undefined;
@@ -1,3 +0,0 @@
1
- export default class Z3Module {
2
- }
3
- export const instance = undefined;
@@ -1,2 +0,0 @@
1
- import { Direction, Orientation } from '../../primitives.js';
2
- export declare function convertDirection(direction: Orientation | Direction): import("grilops").Direction;
@@ -1,26 +0,0 @@
1
- import { RectangularLattice } from 'grilops';
2
- import { Direction, Orientation } from '../../primitives.js';
3
- export function convertDirection(direction) {
4
- switch (direction) {
5
- case Direction.Up:
6
- case Orientation.Up:
7
- return RectangularLattice.VERTEX_DIRECTIONS.N;
8
- case Direction.Down:
9
- case Orientation.Down:
10
- return RectangularLattice.VERTEX_DIRECTIONS.S;
11
- case Direction.Left:
12
- case Orientation.Left:
13
- return RectangularLattice.VERTEX_DIRECTIONS.W;
14
- case Direction.Right:
15
- case Orientation.Right:
16
- return RectangularLattice.VERTEX_DIRECTIONS.E;
17
- case Orientation.DownLeft:
18
- return RectangularLattice.VERTEX_DIRECTIONS.SW;
19
- case Orientation.DownRight:
20
- return RectangularLattice.VERTEX_DIRECTIONS.SE;
21
- case Orientation.UpLeft:
22
- return RectangularLattice.VERTEX_DIRECTIONS.NW;
23
- case Orientation.UpRight:
24
- return RectangularLattice.VERTEX_DIRECTIONS.NE;
25
- }
26
- }
@@ -1,12 +0,0 @@
1
- import GridData from '../../grid.js';
2
- import Solver from '../solver.js';
3
- export default class Z3Solver extends Solver {
4
- readonly id = "z3";
5
- readonly author = "Lysine";
6
- readonly description = "(Obsolete) A WebAssembly solver that supports a limited set of rules and symbols.";
7
- readonly supportsCancellation = false;
8
- protected isEnvironmentSupported(): Promise<boolean>;
9
- solve(grid: GridData): AsyncGenerator<GridData | null>;
10
- isInstructionSupported(instructionId: string): boolean;
11
- isGridSupported(grid: GridData): boolean;
12
- }
@@ -1,123 +0,0 @@
1
- import { Point, PointSet, SymbolGrid, SymbolSet, getRectangleLattice, } from 'grilops';
2
- import Solver from '../solver.js';
3
- import { allZ3Modules } from './modules/index.js';
4
- import { Color } from '../../primitives.js';
5
- import { init } from 'z3-solver';
6
- import Z3SolverContext from './z3SolverContext.js';
7
- import { array } from '../../dataHelper.js';
8
- export default class Z3Solver extends Solver {
9
- id = 'z3';
10
- author = 'Lysine';
11
- description = '(Obsolete) A WebAssembly solver that supports a limited set of rules and symbols.';
12
- supportsCancellation = false;
13
- async isEnvironmentSupported() {
14
- try {
15
- await init();
16
- return true;
17
- }
18
- catch (_ex) {
19
- return false;
20
- }
21
- }
22
- async *solve(grid) {
23
- console.log('Initializing dependencies');
24
- const { Z3, Context } = await init();
25
- const z3Ctx = Context('main');
26
- const grilopsCtx = {
27
- z3: Z3,
28
- context: z3Ctx,
29
- };
30
- const symbolSet = new SymbolSet([
31
- ['empty', ' '],
32
- [Color.Dark, 'B'],
33
- [Color.Light, 'W'],
34
- ]);
35
- const lattice = getRectangleLattice(grid.height, grid.width);
36
- const symbolGrid = new SymbolGrid(grilopsCtx, lattice, symbolSet, new z3Ctx.Solver('QF_FD'));
37
- const ctx = new Z3SolverContext(symbolGrid);
38
- console.log('Encoding constraints');
39
- grid.forEach((tile, x, y) => {
40
- // encode all empty tiles
41
- if (!tile.exists)
42
- ctx.solver.add(symbolGrid.cellAt(new Point(y, x)).eq(symbolSet.indices.empty));
43
- // encode all given tiles
44
- else if (tile.fixed)
45
- ctx.solver.add(symbolGrid.cellAt(new Point(y, x)).eq(symbolSet.indices[tile.color]));
46
- // make sure tiles are filled
47
- else {
48
- ctx.solver.add(symbolGrid.cellAt(new Point(y, x)).neq(symbolSet.indices.empty));
49
- }
50
- });
51
- // encode connections
52
- const visited = array(grid.width, grid.height, () => false);
53
- const queue = new PointSet();
54
- grid.connections.edges.forEach(edge => {
55
- queue.add(new Point(edge.y1, edge.x1));
56
- queue.add(new Point(edge.y2, edge.x2));
57
- });
58
- queue.forEach(point => {
59
- if (visited[point.y][point.x])
60
- return;
61
- visited[point.y][point.x] = true;
62
- const connected = grid.connections.getConnectedTiles({
63
- x: point.x,
64
- y: point.y,
65
- });
66
- connected.forEach(p => (visited[p.y][p.x] = true));
67
- const filtered = connected
68
- .filter(p => grid.getTile(p.x, p.y).exists)
69
- .map(p => new Point(p.y, p.x));
70
- if (filtered.length < 2)
71
- return;
72
- for (let i = 1; i < filtered.length; i++) {
73
- ctx.solver.add(symbolGrid.cellAt(point).eq(symbolGrid.cellAt(filtered[i])));
74
- }
75
- });
76
- [...new Set(grid.rules.map(r => r.id))].forEach(ruleId => {
77
- if (!allZ3Modules.has(ruleId))
78
- return;
79
- allZ3Modules.get(ruleId).encode(grid, ctx);
80
- });
81
- [...grid.symbols.keys()].forEach(symbolId => allZ3Modules.get(symbolId).encode(grid, ctx));
82
- const decodeResult = (model) => {
83
- const tiles = array(grid.width, grid.height, (x, y) => {
84
- const tile = grid.getTile(x, y);
85
- if (!tile.exists || tile.fixed)
86
- return tile;
87
- const color = Number(model.eval(symbolGrid.cellAt(new Point(y, x))));
88
- return tile.withColor(symbolSet.symbols.get(color).name);
89
- });
90
- return grid.withTiles(tiles);
91
- };
92
- console.log('Solving');
93
- console.time('Solve time');
94
- const result = await symbolGrid.solve();
95
- console.timeEnd('Solve time');
96
- if (!result) {
97
- yield null;
98
- return;
99
- }
100
- const model = ctx.solver.model();
101
- yield decodeResult(model);
102
- console.log('Checking uniqueness');
103
- console.time('Uniqueness time');
104
- const result2 = await symbolGrid.isUnique();
105
- console.timeEnd('Uniqueness time');
106
- if (result2) {
107
- yield null;
108
- return;
109
- }
110
- const model2 = ctx.solver.model();
111
- yield decodeResult(model2);
112
- }
113
- isInstructionSupported(instructionId) {
114
- return allZ3Modules.has(instructionId);
115
- }
116
- isGridSupported(grid) {
117
- if (!super.isGridSupported(grid))
118
- return false;
119
- if (grid.getTileCount(true, true, Color.Gray) > 0)
120
- return false;
121
- return true;
122
- }
123
- }
@@ -1,13 +0,0 @@
1
- import { RegionConstrainer, SymbolGrid } from 'grilops';
2
- import { Optimize, Solver, Z3LowLevel } from 'z3-solver';
3
- export default class Z3SolverContext<Name extends string, const Core extends Solver<Name> | Optimize<Name> = Solver<Name> | Optimize<Name>> {
4
- readonly grid: SymbolGrid<Name, Core>;
5
- private _regionConstrainer;
6
- constructor(grid: SymbolGrid<Name, Core>);
7
- get solver(): Core;
8
- get lattice(): import("grilops").Lattice;
9
- get symbolSet(): import("grilops").SymbolSet;
10
- get ctx(): import("z3-solver").Context<Name>;
11
- get z3(): Z3LowLevel['Z3'];
12
- get regionConstrainer(): RegionConstrainer<Name, Core>;
13
- }
@@ -1,40 +0,0 @@
1
- import { RegionConstrainer } from 'grilops';
2
- export default class Z3SolverContext {
3
- grid;
4
- _regionConstrainer;
5
- constructor(grid) {
6
- this.grid = grid;
7
- this.grid = grid;
8
- }
9
- get solver() {
10
- return this.grid.solver;
11
- }
12
- get lattice() {
13
- return this.grid.lattice;
14
- }
15
- get symbolSet() {
16
- return this.grid.symbolSet;
17
- }
18
- get ctx() {
19
- return this.grid.ctx.context;
20
- }
21
- get z3() {
22
- return this.grid.ctx.z3;
23
- }
24
- get regionConstrainer() {
25
- if (!this._regionConstrainer) {
26
- this._regionConstrainer = new RegionConstrainer(this.grid.ctx, this.lattice, this.solver, true, false, 1);
27
- for (const p of this.lattice.points) {
28
- for (const np of this.lattice.edgeSharingNeighbors(this.grid.grid, p)) {
29
- this.solver.add(this.grid
30
- .cellAt(p)
31
- .eq(this.grid.cellAt(np.location))
32
- .eq(this._regionConstrainer.regionIdGrid
33
- .get(p)
34
- .eq(this._regionConstrainer.regionIdGrid.get(np.location))));
35
- }
36
- }
37
- }
38
- return this._regionConstrainer;
39
- }
40
- }