forked from clementgallet/FiveStage444
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PruningTable.java
190 lines (163 loc) · 4.32 KB
/
PruningTable.java
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
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
package cg.fivestage444;
import cg.fivestage444.Coordinates.RawCoord;
import cg.fivestage444.Coordinates.SymCoord;
import java.io.*;
public class PruningTable {
private int n_moves;
private int n_size;
private int inv_depth;
private SymCoord sym;
private RawCoord[] raws;
private byte[] table;
private PruningTable(){}
public PruningTable(SymCoord sym, RawCoord raw, int n_moves, int inv_depth){
this.sym = sym;
raws = new RawCoord[1];
raws[0] = raw;
this.n_moves = n_moves;
this.inv_depth = inv_depth;
n_size = this.sym.getSize();
for (RawCoord raw1 : raws) {
n_size *= raw1.getSize();
}
}
public PruningTable(SymCoord sym, RawCoord raw, RawCoord raw2, int n_moves, int inv_depth){
this.sym = sym;
raws = new RawCoord[2];
raws[0] = raw;
raws[1] = raw2;
this.n_moves = n_moves;
this.inv_depth = inv_depth;
n_size = this.sym.getSize();
for (RawCoord raw1 : raws) {
n_size *= raw1.getSize();
}
}
private void moveTo( int m, PruningTable p ){
sym.moveTo( m, p.sym );
for ( int i = 0; i < raws.length; i++){
raws[i].moveTo( m, p.raws[i] );
}
}
private void writeTable (int index, int value) {
table[index >> 1] ^= (0x0f ^ value) << ((index & 1) << 2);
}
public int readTable (int index) {
return (table[index >> 1] >> ((index & 1) << 2)) & 0x0f;
}
private int get(){
int idx = sym.coord;
for (RawCoord raw : raws) {
idx = idx * raw.getSize() + raw.conjugate(sym.sym);
}
return idx;
}
private void set( int idx ){
for ( int i = raws.length - 1; i >= 0; i--){
raws[i].coord = idx % raws[i].getSize();
idx /= raws[i].getSize();
}
sym.coord = idx;
sym.sym = 0;
}
private void normalise(){
for (RawCoord raw : raws) {
raw.coord = raw.conjugate(sym.sym);
}
sym.sym = 0;
}
public void initTable(){
fillTable();
}
public void initTable(File f){
table = new byte[(n_size+1)/2];
try {
DataInputStream in = new DataInputStream(new FileInputStream(f));
in.readFully(table);
} catch (Exception e) {
fillTable();
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(f));
out.write(table);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
private void fillTable(){
/* Create a new instance of PruningTable with the same classes in it */
PruningTable p = new PruningTable();
try{
p.sym = sym.getClass().newInstance();
p.raws = new RawCoord[raws.length];
for ( int i = 0; i < raws.length; i++){
p.raws[i] = raws[i].getClass().newInstance();
}
}
catch (InstantiationException ignored) {
}
catch (IllegalAccessException ignored) {
}
/* Create the pruning table and fill with the solved states */
table = new byte[(n_size+1)/2];
for (int i = 0; i < (n_size+1)/2; i++)
table[i] = -1;
int done = sym.getSolvedStates().length;
for (RawCoord raw1 : raws) done *= raw1.getSolvedStates().length;
for (int d = 0; d < done; d++){
int dd = d;
for (RawCoord raw : raws) {
raw.coord = raw.getSolvedStates()[dd % raw.getSolvedStates().length];
dd /= raw.getSolvedStates().length;
}
sym.coord = sym.getSolvedStates()[dd];
sym.sym = 0;
writeTable(get(), 0);
}
/* Build the table */
int depth = 0;
while (( done < n_size ) && ( depth < 15 )) {
boolean inv = depth > inv_depth;
int select = inv ? 0x0f : depth;
int check = inv ? depth : 0x0f;
depth++;
int pos = 0;
int unique = 0;
for (int i=0; i<n_size; i++) {
if (readTable(i) != select) continue;
set(i);
for (int m=0; m<n_moves; m++) {
moveTo(m, p);
int idx = p.get();
if (readTable(idx) != check) continue;
done++;
if (inv) {
writeTable(i, depth);
break;
} else {
writeTable(idx, depth);
int nsym = 1;
unique++;
p.normalise();
for (int j=0; j<p.sym.getSyms().length; j++) {
long symS = p.sym.getSyms()[j];
for (int k=0; symS != 0; symS>>=1, k++) {
if ((symS & 0x1L) == 0) continue;
p.sym.sym = k*p.sym.getSyms().length + j;
int sym_idx = p.get();
if( sym_idx == idx )
nsym++;
if (readTable(sym_idx) == 0x0f) {
writeTable(sym_idx, depth);
done++;
}
}
}
pos += 48/nsym; // TODO: find the correct value or drop off
}
}
}
System.out.println(String.format("%2d%12d%10d", depth, pos, unique));
}
}
}