Skip to content

Commit

Permalink
improve debug info
Browse files Browse the repository at this point in the history
stdout:

 32:1536 33:384 34:2592 35:1632 36:4512 37:2688 38:6720 39:3552 40:4800 41:2880 42:2112 43:288 44:288
final distribution:
 32:768 33:192 34:1176 35:792 36:1704 37:1248 38:2352 39:1392 40:1296 41:648 42:504 43:48 44:24
N=5,tP=[1, 2, 0],diameter=44,BFS time=651
 30:3840 31:512 32:15360 33:4736 34:29568 35:25984 36:72448 37:50048 38:170880 39:86016 40:301568 41:123392 42:355456 43:90880 44:187648 45:23424 46:28288
final distribution:
 30:480 31:64 32:3552 33:864 34:6400 35:6160 36:15440 37:11944 38:33072 39:20000 40:50544 41:25120 42:47728 43:15808 44:14656 45:2080 46:1112
N=5,tP=[1, 0, 3, 2],diameter=46,BFS time=16902
verification time=43
verification time=777
   X   X   X   X   X
   X  '0  '1  '2   X
   X  '3  G4  '5   X
   X  '6  '7  '8   X
   X   X   X   X   X
ncombos=362880
list of allowed positions for gripped piece after solving: 4
0:1 32:16 33:20 34:44 35:34 36:32 37:20 38:8 39:8 40:32 41:4 42:72 43:16 44:14 45:1 46:1 64:176 65:336 66:690 67:1036 68:978 69:652 70:632 71:328 72:416 73:276 74:316 75:240 76:228 77:196 78:100 79:76 80:24 81:2 82:28 84:16 85:12 86:10 88:1 96:448 97:1216 98:3632 99:2728 100:1984 101:1208 102:408 103:672 104:268 105:148 106:140 107:144 108:36 112:24 113:12
# combos reached=20160
Warning: ncombos=362880!=reached=20160 (could be the result of a restriction on parity or reachable locations of the gripped piece).
diameter=113
BFS time (ms)=5954
tscrm=[4, 2, 3, 5, 1, 6, 7, 8, 0]
test scramble to solve
.....
.SLG.
.HMI.
.NQR.
.....
DLDLURDRUURURDLULUULULDRURUURDRULDL DDLURURDLULDRRULDDRULULDRURDLLUR UULDRDRULDLURRDLUURDLDLURDRULLDR 

Process finished with exit code 0
  • Loading branch information
coolcomputery committed Aug 25, 2021
1 parent 82143f9 commit ba52523
Showing 1 changed file with 89 additions and 70 deletions.
159 changes: 89 additions & 70 deletions LoopoverNRGDijkstra.java
Expand Up @@ -33,15 +33,15 @@ public static String mvseqStr(List<int[]> S) {
public static boolean[][] tobmat(String[] rows) {
boolean[][] out=new boolean[rows.length][rows[0].length()];
for (int i=0; i<out.length; i++)
for (int j=0; j<rows[i].length(); j++) {
char c=rows[i].charAt(j);
if (c=='0') out[i][j]=false;
else if (c=='1') out[i][j]=true;
else throw new RuntimeException("Not parseable as a binary matrix.");
}
for (int j=0; j<rows[i].length(); j++) {
char c=rows[i].charAt(j);
if (c=='0') out[i][j]=false;
else if (c=='1') out[i][j]=true;
else throw new RuntimeException("Not parseable as a binary matrix.");
}
return out;
}
private int R, C;
private int R, C, grippedPc;
private int F; //F=number of locations that are not locked
private int[] tofree, freeto; //freeto[f]=l=location of the f-th unlocked location; tofree[l]=f
public int K;
Expand All @@ -53,12 +53,15 @@ public static boolean[][] tobmat(String[] rows) {
public LoopoverNRGDijkstra(int gr, int gc, String A, String B) {
this(gr,gc,tobmat(A.split(",")),tobmat(B.split(",")));
}
public LoopoverNRGDijkstra(int gr, int gc, String A, String B, LoopoverNRGSetup[] bfss) {
this(gr,gc,tobmat(A.split(",")),tobmat(B.split(",")),bfss);
}
private LoopoverNRGDijkstra(int gr, int gc, boolean[][] A, boolean[][] B) {
this(gr,gc,A,B,null);
}
private LoopoverNRGDijkstra(int gr, int gc, boolean[][] A, boolean[][] B, LoopoverNRGSetup[] bfss) {
long st=System.currentTimeMillis();
R=A.length; C=A[0].length;
R=A.length; C=A[0].length; grippedPc=gr*C+gc;
if (R!=C) throw new RuntimeException("Only square board sizes allowed."); //need to refactor LoopoverNRGSetup before I can remove this constraint
if (R!=B.length||C!=B[0].length) throw new RuntimeException("Mismatch in dimensions.");
if (!A[gr][gc]) throw new RuntimeException("Gripped pieces locked at starting state.");
Expand All @@ -77,7 +80,7 @@ private LoopoverNRGDijkstra(int gr, int gc, boolean[][] A, boolean[][] B, Loopov
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++)
System.out.printf("%4s",
A[r][c]?
A[r][c]?
((B[r][c]?(r==gr&&c==gc?"g":""):(r==gr&&c==gc?"G":"'")))+tofree[r*C+c]
:"X"
//X: locked
Expand Down Expand Up @@ -141,78 +144,78 @@ private LoopoverNRGDijkstra(int gr, int gc, boolean[][] A, boolean[][] B, Loopov
this.bfss=bfss;
List<List<int[][]>> cycleInfos=new ArrayList<>();
for (int bfsi=0; bfsi<bfss.length; bfsi++)
if (bfss[bfsi]==null) cycleInfos.add(null);
else {
cycleInfos.add(new ArrayList<>());
LoopoverNRGSetup bfs=bfss[bfsi];
int nP=bfs.tP.length;
int[] tuple=new int[nP];
//we want to cycle the pieces at locations freeto[tuple[i]]
while (tuple[nP-1]<F) {
boolean good=true;
for (int i=0; i<nP; i++)
for (int j=0; j<i; j++)
if (tuple[i]==tuple[j]) good=false;
if (good) {
int[] L=new int[nP];
if (bfss[bfsi]==null) cycleInfos.add(null);
else {
cycleInfos.add(new ArrayList<>());
LoopoverNRGSetup bfs=bfss[bfsi];
int nP=bfs.tP.length;
int[] tuple=new int[nP];
//we want to cycle the pieces at locations freeto[tuple[i]]
while (tuple[nP-1]<F) {
boolean good=true;
for (int i=0; i<nP; i++)
L[i]=freeto[tuple[i]];
int[] action=new int[F];
for (int i=0; i<F; i++) action[i]=i;
for (int i=0; i<nP; i++) action[tofree[L[i]]]=tofree[L[bfs.tP[i]]];
cycleInfos.get(bfsi).add(new int[][] {L,action});
}
tuple[0]++;
for (int i=0; i<nP-1&&tuple[i]==F; i++) {
tuple[i]=0;
tuple[i+1]++;
for (int j=0; j<i; j++)
if (tuple[i]==tuple[j]) good=false;
if (good) {
int[] L=new int[nP];
for (int i=0; i<nP; i++)
L[i]=freeto[tuple[i]];
int[] action=new int[F];
for (int i=0; i<F; i++) action[i]=i;
for (int i=0; i<nP; i++) action[tofree[L[i]]]=tofree[L[bfs.tP[i]]];
cycleInfos.get(bfsi).add(new int[][] {L,action});
}
tuple[0]++;
for (int i=0; i<nP-1&&tuple[i]==F; i++) {
tuple[i]=0;
tuple[i+1]++;
}
}
}
}
diam=0;
for (int D=0;; D++) {
boolean finished=true;
int fsz=0;
for (int f=0; f<ncombos; f++)
if (depth[f]>D&&depth[f]!=Integer.MAX_VALUE) finished=false;
else if (depth[f]==D) {
fsz++;
finished=false;
int[] scrm=codeCombo(f);
int lr=freeto[scrm[0]]/C, lc=freeto[scrm[0]]%C;
int[] mvis={lr*2,lr*2+1,2*R+lc*2,2*R+lc*2+1};
for (int mvi:mvis) if (mvactions[mvi]!=null) {
int nf=comboCode(scrm,mvactions[mvi]);
int ndepth=depth[f]+1;
if (ndepth<depth[nf]) {
depth[nf]=ndepth;
par[nf]=f;
tuplecode[nf]=(mvi<2*R?(mvi%2==0?0:1):(mvi%2==0?2:3))*(bfss.length+1);
}
}
for (int bfsi=0; bfsi<bfss.length; bfsi++) if (bfss[bfsi]!=null) {
LoopoverNRGSetup bfs=bfss[bfsi];
for (int[][] cycinfo:cycleInfos.get(bfsi)) if (cycinfo[1][scrm[0]]==scrm[0]) {
int[] L=cycinfo[0], action=cycinfo[1];
int nf=comboCode(scrm,action);
int tcode=bfs.tupleCode(L,lr,lc);
int ndepth=depth[f]+bfs.cost(tcode);
if (depth[f]>D&&depth[f]!=Integer.MAX_VALUE) finished=false;
else if (depth[f]==D) {
fsz++;
finished=false;
int[] scrm=codeCombo(f);
int lr=freeto[scrm[0]]/C, lc=freeto[scrm[0]]%C;
int[] mvis={lr*2,lr*2+1,2*R+lc*2,2*R+lc*2+1};
for (int mvi:mvis) if (mvactions[mvi]!=null) {
int nf=comboCode(scrm,mvactions[mvi]);
int ndepth=depth[f]+1;
if (ndepth<depth[nf]) {
depth[nf]=ndepth;
par[nf]=f;
tuplecode[nf]=tcode*(bfss.length+1)+bfsi+1;
tuplecode[nf]=(mvi<2*R?(mvi%2==0?0:1):(mvi%2==0?2:3))*(bfss.length+1);
}
}
for (int bfsi=0; bfsi<bfss.length; bfsi++) if (bfss[bfsi]!=null) {
LoopoverNRGSetup bfs=bfss[bfsi];
for (int[][] cycinfo:cycleInfos.get(bfsi)) if (cycinfo[1][scrm[0]]==scrm[0]) {
int[] L=cycinfo[0], action=cycinfo[1];
int nf=comboCode(scrm,action);
int tcode=bfs.tupleCode(L,lr,lc);
int ndepth=depth[f]+bfs.cost(tcode);
if (ndepth<depth[nf]) {
depth[nf]=ndepth;
par[nf]=f;
tuplecode[nf]=tcode*(bfss.length+1)+bfsi+1;
}
}
}
}
}
if (fsz>0) {
System.out.println(D+":"+fsz);
System.out.print((D>0?" ":"")+D+":"+fsz);
diam=Math.max(diam,D);
}
reached+=fsz;
if (finished) break;
}
System.out.println("# combos reached="+reached);
System.out.println("\n# combos reached="+reached);
if (reached!=ncombos) System.out.println("Warning: ncombos="+ncombos+"!=reached="+reached+" (could be the result of a restriction on parity or reachable locations of the gripped piece).");
System.out.println("diameter="+diam);
System.out.println("BFS time (ms)="+(System.currentTimeMillis()-st));
Expand Down Expand Up @@ -279,21 +282,37 @@ public String solveseq(int code) {
}
public String test() {
for (int code=0; code<ncombos; code++) if (depth[code]!=Integer.MAX_VALUE) {
System.out.println("tscrm="+Arrays.toString(codeCombo(code)));
int[] scrm=codeCombo(code);
System.out.println("tscrm="+Arrays.toString(scrm));
int[] targets=null;
for (int c=0; c<ncombos; c++) if (depth[c]==0) {
int[] t=codeCombo(c);
if (freeto[t[0]]==grippedPc) {
targets=t;
break;
}
}
int[] board=new int[R*C]; Arrays.fill(board,-1);
for (int k=0; k<K; k++)
board[freeto[scrm[k]]]=freeto[targets[k]];
System.out.println("test scramble to solve");
for (int r=0; r<R; r++) {
for (int c=0; c<C; c++) {
int pc=board[r*C+c];
System.out.print(pc==-1?".":(char)('A'+pc));
}
System.out.println();
}
return solveseq(code);
}
return null;
}
public static void main(String[] args) {
//TODO: SPEED OPTIMIZATIONS
int N=5;
LoopoverNRGSetup[] bfss=new LoopoverNRGSetup[] {LoopoverNRGSetup.cyc3(N), LoopoverNRGSetup.cyc2n2(N)};
String[] states={
"11111,11111,10000,10000,10000",
"11011,11000,10000,10000,10000",
"00000,00000,00000,00000,00000",
};
for (int i=0; i<states.length-1; i++)
System.out.println(new LoopoverNRGDijkstra(0,0,tobmat(states[i].split(",")),tobmat(states[i+1].split(",")),bfss).test());
for (LoopoverNRGSetup bfs:bfss) LoopoverNRGSetup.verify(bfs);
boolean[][] state0=tobmat("00000,01110,01110,01110,00000".split(",")),
state1=tobmat("00000,00000,00000,00000,00000".split(","));
System.out.println(new LoopoverNRGDijkstra(2,2,state0,state1,bfss).test());
}
}

0 comments on commit ba52523

Please sign in to comment.