-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreResidualValueForcing.java
More file actions
142 lines (117 loc) · 5.45 KB
/
PreResidualValueForcing.java
File metadata and controls
142 lines (117 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Prunes values based on Global Residual Analysis.
* Logic: Total Pips available - Sum of all 'SUM' Region Targets = Required Pips for 'Non-Sum' cells.
*/
public final class PreResidualValueForcing {
public static boolean DEBUG = false;
/**
* Entry point for residual-based domain reduction.
* @return count of values eliminated from cell domains.
*/
public static int apply(PipsGame game) {
ResidualState state = calculateResidualState(game);
if (DEBUG) {
System.out.printf("[Residual] Total Pips: %d | Sum Targets: %d | Residual Capacity: %d%n",
state.totalPips, state.totalSumTargets, state.residualCapacity);
}
if (state.residualCapacity < 0) {
throw new IllegalStateException("Logic Error: Total sum targets exceed total available pips.");
}
return pruneImpossibleResidualValues(game, state);
}
/**
* Data class to hold the results of the board-wide pip audit.
*/
static class ResidualState {
int totalPips;
int totalSumTargets;
int residualCapacity;
}
private static ResidualState calculateResidualState(PipsGame game) {
ResidualState state = new ResidualState();
// 1. Calculate "Supply": Every pip on every domino provided in the game
for (Domino domino : game.dominoes) {
state.totalPips += domino.a + domino.b;
}
// 2. Calculate "Reserved Demand": Pips that MUST go into 'sum' regions
for (Region region : game.regions) {
if (region.constraintTypeId == PipsConstants.TYPE_SUM) {
state.totalSumTargets += region.target;
}
}
// 3. The "Residual": Pips that MUST be distributed among all other types of regions
state.residualCapacity = state.totalPips - state.totalSumTargets;
return state;
}
/**
* Iterates through all non-sum cells and prunes values that are
* mathematically incompatible with the global residual.
*/
private static int pruneImpossibleResidualValues(PipsGame game, ResidualState state) {
int totalEliminated = 0;
int pipsAlreadyUsedInNonSum = getPipsUsedByFixedNonSumCells(game);
int remainingResidualToFill = state.residualCapacity - pipsAlreadyUsedInNonSum;
int emptyNonSumCellCount = countEmptyNonSumCells(game);
if (DEBUG) {
System.out.printf("[Residual] Remaining Cells: %d | Remaining Residual: %d%n",
emptyNonSumCellCount, remainingResidualToFill);
}
if (remainingResidualToFill < 0) {
throw new IllegalStateException("Residual underflow: Non-sum assignments exceed available residual.");
}
// Check every empty cell that does NOT belong to a 'sum' region
for (int r = 0; r < game.rows; r++) {
for (int c = 0; c < game.cols; c++) {
Cell cell = game.cells[r][c];
if (cell == null || cell.value >= 0) continue;
Region region = game.regions.get(cell.regionId);
if (region.constraintTypeId == PipsConstants.TYPE_SUM) continue;
// How many other non-sum cells must we account for?
int otherCellsCount = emptyNonSumCellCount - 1;
for (int v = PipsConstants.MIN_PIP; v <= PipsConstants.MAX_PIP; v++) {
// Bitwise check: Is bit v set (forbidden)?
if ((cell.forbiddenMask & (1 << v)) != 0) continue;
// Hull Consistency check
int minPotentialTotal = v + (otherCellsCount * PipsConstants.MIN_PIP);
int maxPotentialTotal = v + (otherCellsCount * PipsConstants.MAX_PIP);
if (minPotentialTotal > remainingResidualToFill || maxPotentialTotal < remainingResidualToFill) {
// Bitwise set: Mark bit v as forbidden
cell.forbiddenMask |= (1 << v);
totalEliminated++;
if (DEBUG) {
System.out.printf("[Residual] Pruned %d at (%d,%d) - Out of global residual bounds.%n", v, r, c);
}
}
}
}
}
return totalEliminated;
}
// --- Helper Logic for Bookkeeping ---
private static int countEmptyNonSumCells(PipsGame game) {
int count = 0;
for (int r = 0; r < game.rows; r++) {
for (int c = 0; c < game.cols; c++) {
Cell cell = game.cells[r][c];
if (cell != null && cell.value < 0) {
Region region = game.regions.get(cell.regionId);
if (region.constraintTypeId != PipsConstants.TYPE_SUM) count++;
}
}
}
return count;
}
private static int getPipsUsedByFixedNonSumCells(PipsGame game) {
int sum = 0;
for (int r = 0; r < game.rows; r++) {
for (int c = 0; c < game.cols; c++) {
Cell cell = game.cells[r][c];
if (cell != null && cell.value >= 0) {
Region region = game.regions.get(cell.regionId);
if (region.constraintTypeId !=PipsConstants.TYPE_SUM) sum += cell.value;
}
}
}
return sum;
}
}