Skip to content

Commit

Permalink
simulated annealing
Browse files Browse the repository at this point in the history
probably should redo this with randomized initial states
stdout:
N=4
K=3,diameter=36,BFS time=24
NREPS=119209
    REPS    ACCN    GN temp
       0       0   392 2.0
   11920    3703   364 1.8000150995310757
   23840    6917   360 1.6000301990621513
   35760    9703   362 1.400045298593227
   47680   12192   360 1.2000603981243025
   59600   14523   358 1.0000754976553785
   71520   16552   358 0.800090597186454
   83440   18415   356 0.6001056967175298
   95360   20141   356 0.4001207962486053
  107280   21830   356 0.20013589577968105
  119200   23451   356 1.5099531075679629E-4
  119209   23453   356 0.0
[8, 12, 4, 2, 10, 1, 9, 6, 14, 7, 15, 3, 5, 13, 11]
bestScr=356, best list of pieces to solve=[2, 8, 4, 10, 14, 12, 6, 11, 3, 5, 13, 1, 7, 9, 15]
SA time=13881
N=5
K=3,diameter=58,BFS time=33
K=4,diameter=52,BFS time=279
NREPS=20000
    REPS    ACCN    GN temp
       0       0   572 2.0
    2000     893   586 1.8
    4000    1729   572 1.6
    6000    2312   570 1.4
    8000    2897   576 1.2
   10000    3237   570 1.0
   12000    3473   568 0.8
   14000    3659   560 0.6000000000000001
   16000    3794   560 0.3999999999999999
   18000    3901   560 0.19999999999999996
   20000    4021   560 0.0
[7, 13, 2, 8, 20, 1, 12, 18, 14, 9, 10, 19, 5, 3, 22, 21, 6, 11, 17, 16, 4, 15, 23, 24]
bestScr=560, best list of pieces to solve=[14, 9, 2, 8, 13, 7, 18, 12, 3, 5, 6, 11, 1, 20, 21, 22, 10, 19, 17, 16, 23, 4, 24, 15]
SA time=193160
N=6
K=3,diameter=54,BFS time=67
K=4,diameter=54,BFS time=1374
NREPS=4651
    REPS    ACCN    GN temp
       0       0   878 2.0
     465     249   848 1.8000430015050526
     930     441   846 1.6000860030101054
    1395     647   846 1.400129004515158
    1860     866   842 1.2001720060202108
    2325    1025   842 1.0002150075252634
    2790    1176   838 0.800258009030316
    3255    1268   838 0.6003010105353688
    3720    1332   832 0.40034401204042136
    4185    1383   830 0.20038701354547417
    4650    1419   830 4.3001505052675526E-4
    4651    1419   830 0.0
[35, 2, 25, 16, 10, 5, 33, 26, 19, 13, 24, 29, 14, 32, 15, 31, 21, 11, 6, 20, 30, 1, 22, 8, 28, 3, 12, 7, 4, 23, 34, 17, 18, 27, 9]
bestScr=830, best list of pieces to solve=[5, 10, 2, 25, 24, 26, 17, 16, 13, 19, 29, 30, 32, 14, 1, 15, 3, 31, 6, 20, 7, 21, 8, 22, 28, 11, 12, 33, 23, 4, 34, 35, 18, 9, 27]
SA time=236915
N=7
K=3,diameter=57,BFS time=82
K=4,diameter=60,BFS time=5538
NREPS=1355
    REPS    ACCN    GN temp
       0       0  1258 2.0
     135      81  1272 1.8007380073800738
     270     157  1272 1.6014760147601477
     405     226  1277 1.4022140221402215
     540     302  1278 1.2029520295202953
     675     362  1266 1.003690036900369
     810     405  1258 0.8044280442804428
     945     429  1256 0.6051660516605166
    1080     456  1250 0.40590405904059046
    1215     469  1248 0.2066420664206643
    1350     485  1247 0.007380073800737907
    1355     485  1247 0.0
[33, 35, 18, 27, 45, 44, 34, 39, 40, 41, 37, 38, 11, 47, 25, 32, 30, 31, 22, 46, 28, 4, 10, 17, 26, 3, 16, 23, 12, 24, 48, 21, 8, 9, 7, 19, 2, 1, 5, 6, 43, 14, 15, 29, 42, 20, 36, 13]
bestScr=1247, best list of pieces to solve=[33, 3, 34, 45, 27, 36, 22, 39, 40, 41, 37, 11, 38, 47, 32, 25, 30, 31, 18, 46, 28, 4, 10, 17, 26, 35, 16, 23, 12, 24, 48, 42, 9, 8, 7, 19, 2, 1, 5, 6, 43, 14, 21, 29, 13, 20, 44, 15]
SA time=253151
N=8
K=3,diameter=60,BFS time=224
K=4,diameter=70,BFS time=18574
NREPS=465
    REPS    ACCN    GN temp
       0       0  1842 2.0
      46      18  1842 1.8021505376344087
      92      37  1836 1.6043010752688172
     138      56  1832 1.4064516129032258
     184      67  1826 1.2086021505376343
     230      74  1826 1.010752688172043
     276      82  1824 0.8129032258064517
     322      89  1816 0.6150537634408602
     368      95  1814 0.41720430107526885
     414     101  1812 0.2193548387096773
     460     105  1812 0.021505376344086002
     465     106  1812 0.0
[41, 42, 40, 38, 29, 10, 32, 12, 15, 61, 6, 9, 19, 36, 13, 26, 17, 39, 33, 23, 21, 22, 28, 14, 37, 4, 20, 27, 16, 30, 31, 56, 24, 18, 59, 46, 3, 5, 43, 35, 52, 25, 1, 62, 50, 48, 2, 53, 49, 58, 34, 44, 45, 51, 11, 47, 57, 54, 8, 55, 7, 60, 63]
bestScr=1812, best list of pieces to solve=[5, 42, 40, 38, 29, 10, 32, 26, 12, 58, 6, 9, 46, 36, 13, 15, 17, 39, 33, 23, 21, 22, 28, 14, 4, 20, 37, 27, 16, 30, 31, 56, 19, 18, 59, 24, 3, 41, 43, 35, 52, 25, 2, 62, 50, 48, 1, 53, 49, 61, 34, 44, 45, 51, 11, 47, 57, 54, 55, 8, 7, 60, 63]
SA time=411662
N=9
K=3,diameter=64,BFS time=382
K=4,diameter=72,BFS time=62012
NREPS=181
    REPS    ACCN    GN temp
       0       0  2436 2.0
      18       7  2428 1.8011049723756907
      36      14  2442 1.6022099447513813
      54      19  2444 1.4033149171270718
      72      26  2444 1.2044198895027625
      90      32  2438 1.005524861878453
     108      36  2438 0.8066298342541436
     126      43  2430 0.6077348066298343
     144      46  2430 0.40883977900552493
     162      49  2430 0.20994475138121538
     180      51  2430 0.011049723756906049
     181      51  2430 0.0
[45, 59, 49, 20, 75, 41, 69, 39, 25, 34, 11, 12, 21, 36, 46, 28, 17, 16, 38, 27, 13, 22, 23, 24, 3, 26, 15, 5, 29, 30, 31, 32, 40, 33, 35, 14, 37, 18, 9, 19, 44, 42, 43, 68, 63, 56, 47, 48, 51, 50, 10, 52, 2, 58, 55, 57, 62, 53, 4, 60, 61, 78, 76, 1, 65, 66, 67, 54, 7, 8, 71, 73, 72, 74, 80, 70, 77, 6, 79, 64]
bestScr=2428, best list of pieces to solve=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 14, 15, 16, 17, 18, 19, 20, 13, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 58, 47, 48, 49, 50, 51, 52, 53, 54, 55, 63, 57, 46, 59, 60, 61, 62, 56, 64, 65, 66, 67, 68, 69, 70, 71, 73, 72, 74, 75, 76, 77, 78, 79, 80]
SA time=462133
N=10
K=3,diameter=70,BFS time=659
K=4,diameter=76,BFS time=161044
NREPS=78
    REPS    ACCN    GN temp
       0       0  3230 2.0
       7       1  3230 1.8205128205128205
      14       2  3228 1.641025641025641
      21       7  3230 1.4615384615384617
      28       9  3228 1.282051282051282
      35       9  3228 1.1025641025641026
      42      12  3222 0.9230769230769231
      49      13  3222 0.7435897435897436
      56      15  3218 0.5641025641025641
      63      15  3218 0.3846153846153846
      70      18  3216 0.20512820512820507
      77      18  3216 0.02564102564102555
      78      18  3216 0.0
[38, 26, 3, 4, 5, 6, 7, 74, 9, 10, 11, 12, 13, 14, 15, 34, 17, 18, 33, 20, 21, 22, 23, 24, 25, 2, 27, 28, 42, 30, 31, 32, 19, 39, 35, 54, 37, 16, 1, 40, 41, 29, 43, 44, 45, 46, 47, 48, 49, 60, 51, 52, 53, 36, 55, 68, 57, 58, 59, 50, 56, 62, 63, 64, 65, 66, 83, 61, 8, 70, 71, 72, 96, 69, 75, 76, 77, 78, 73, 87, 81, 82, 67, 84, 85, 86, 97, 88, 89, 90, 91, 92, 93, 94, 95, 79, 80, 98, 99]
bestScr=3216, best list of pieces to solve=[38, 26, 3, 4, 5, 6, 7, 74, 9, 10, 11, 12, 13, 14, 15, 34, 17, 18, 33, 20, 21, 22, 23, 24, 25, 2, 27, 28, 42, 30, 31, 32, 19, 39, 35, 54, 37, 16, 1, 40, 41, 29, 43, 44, 45, 46, 47, 48, 49, 60, 51, 52, 53, 36, 55, 68, 57, 58, 59, 50, 56, 62, 63, 64, 65, 66, 83, 61, 69, 70, 71, 72, 96, 8, 75, 76, 77, 78, 73, 87, 81, 82, 67, 84, 85, 86, 97, 88, 89, 90, 91, 92, 93, 94, 95, 79, 80, 98, 99]
SA time=635961
 N    GN  best sequence of moves to solve
 4   356  [2, 8, 4, 10, 14, 12, 6, 11, 3, 5, 13, 1, 7, 9, 15]
 5   560  [14, 9, 2, 8, 13, 7, 18, 12, 3, 5, 6, 11, 1, 20, 21, 22, 10, 19, 17, 16, 23, 4, 24, 15]
 6   830  [5, 10, 2, 25, 24, 26, 17, 16, 13, 19, 29, 30, 32, 14, 1, 15, 3, 31, 6, 20, 7, 21, 8, 22, 28, 11, 12, 33, 23, 4, 34, 35, 18, 9, 27]
 7  1247  [33, 3, 34, 45, 27, 36, 22, 39, 40, 41, 37, 11, 38, 47, 32, 25, 30, 31, 18, 46, 28, 4, 10, 17, 26, 35, 16, 23, 12, 24, 48, 42, 9, 8, 7, 19, 2, 1, 5, 6, 43, 14, 21, 29, 13, 20, 44, 15]
 8  1812  [5, 42, 40, 38, 29, 10, 32, 26, 12, 58, 6, 9, 46, 36, 13, 15, 17, 39, 33, 23, 21, 22, 28, 14, 4, 20, 37, 27, 16, 30, 31, 56, 19, 18, 59, 24, 3, 41, 43, 35, 52, 25, 2, 62, 50, 48, 1, 53, 49, 61, 34, 44, 45, 51, 11, 47, 57, 54, 55, 8, 7, 60, 63]
 9  2428  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 14, 15, 16, 17, 18, 19, 20, 13, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 58, 47, 48, 49, 50, 51, 52, 53, 54, 55, 63, 57, 46, 59, 60, 61, 62, 56, 64, 65, 66, 67, 68, 69, 70, 71, 73, 72, 74, 75, 76, 77, 78, 79, 80]
10  3216  [38, 26, 3, 4, 5, 6, 7, 74, 9, 10, 11, 12, 13, 14, 15, 34, 17, 18, 33, 20, 21, 22, 23, 24, 25, 2, 27, 28, 42, 30, 31, 32, 19, 39, 35, 54, 37, 16, 1, 40, 41, 29, 43, 44, 45, 46, 47, 48, 49, 60, 51, 52, 53, 36, 55, 68, 57, 58, 59, 50, 56, 62, 63, 64, 65, 66, 83, 61, 69, 70, 71, 72, 96, 8, 75, 76, 77, 78, 73, 87, 81, 82, 67, 84, 85, 86, 97, 88, 89, 90, 91, 92, 93, 94, 95, 79, 80, 98, 99]

Process finished with exit code 0
  • Loading branch information
coolcomputery committed Jul 3, 2021
1 parent da00e9b commit 1dbcb4e
Showing 1 changed file with 63 additions and 35 deletions.
98 changes: 63 additions & 35 deletions LoopoverNRGUpper.java
Expand Up @@ -18,7 +18,7 @@ private static int upper(int N, LoopoverNRGSetup bfs3, LoopoverNRGSetup bfs4, Li
// so that we can move cur to t using a 3-cycle
for (int ri=ti+1; ri<T; ri++)
if (ri!=ci)
//3-cycle: cur-->t-->third-->cur
//3-cycle: cur-->t-->toSolve.get(ri)-->cur
best=Math.min(best,bfs3.cost(new int[] {cur,t,toSolve.get(ri)},gr,gc));
worst=Math.max(worst,best);
}
Expand Down Expand Up @@ -47,7 +47,7 @@ private static int upper(int N, LoopoverNRGSetup bfs3, LoopoverNRGSetup bfs4, Li
for (int bi=ti+2; bi<T; bi++)
if (ai!=bi) {
int a=toSolve.get(ai), b=toSolve.get(bi);
worst=Math.max(worst,bfs4.cost(new int[] {a,ta,b,tb},gr,gc)); //a-->ta, b-->tb
worst=Math.max(worst,bfs4.cost(new int[] {a,ta,b,tb},gr,gc));
}
//when a @ tb and b @ ta
{
Expand All @@ -56,7 +56,7 @@ private static int upper(int N, LoopoverNRGSetup bfs3, LoopoverNRGSetup bfs4, Li
for (int bi=ti+2; bi<T; bi++)
if (ai!=bi) {
int a=toSolve.get(ai), b=toSolve.get(bi);
best=Math.min(best,bfs4.cost(new int[] {a,b,ta,tb},gr,gc)); //a<-->b, ta<-->tb
best=Math.min(best,bfs4.cost(new int[] {a,b,ta,tb},gr,gc));
}
worst=Math.max(worst,best);
}
Expand Down Expand Up @@ -121,40 +121,68 @@ private static int upper(int N, LoopoverNRGSetup bfs3, LoopoverNRGSetup bfs4, Li
return out+2*(N/2);
}
public static void main(String[] args) {
/*int N=5;
System.out.println("N="+N);
LoopoverNRGSetup bfs3=LoopoverNRGSetup.cyc3bfs(N),
bfs4=LoopoverNRGSetup.swap22bfs(N);
//assume gripped piece is pc 0
List<Integer> toSolve=new ArrayList<>();
for (int i=1; i<N*N; i++) toSolve.add(i);
int T=toSolve.size();
//anneal on list of pieces toSolve
long NREPS=10000;
double TEMP0=5;
int scr=upper(N,bfs3,bfs4,toSolve);
SplittableRandom rnd=new SplittableRandom(1);
for (long reps=0, accn=0;; reps++) {
double temp=TEMP0*(1.0-reps/(double)NREPS);
if (reps%1000==0)
System.out.printf("%8d%8d%4d %.10f%n",reps,accn,scr,temp);
if (reps==NREPS) break;
int i=rnd.nextInt(T), j=rnd.nextInt(T-1); if (j>=i) j++;
int ti=toSolve.get(i), tj=toSolve.get(j);
toSolve.set(i,tj);
toSolve.set(j,ti);
int nscr=upper(N,bfs3,bfs4,toSolve);
if (nscr<=scr||rnd.nextDouble()<Math.exp((scr-nscr)/temp)) {
scr=nscr;
accn++;
int Nlo=4, Nhi=10;
int[] GNs=new int[Nhi+1];
int[][] bestSolveSeqs=new int[Nhi+1][];
for (int N=Nlo; N<=Nhi; N++) {
long st=System.currentTimeMillis();
System.out.println("N="+N);
LoopoverNRGSetup bfs3=LoopoverNRGSetup.cyc3bfs(N),
bfs4=LoopoverNRGSetup.swap22bfs(N);
//assume gripped piece is pc 0
List<Integer> toSolve=new ArrayList<>();
for (int i=1; i<N*N; i++) toSolve.add(i);
int T=toSolve.size();
//anneal on list of pieces to solve
long NREPS=(long)(20000*Math.pow(5,8)/Math.pow(N,8));
//do fewer SA iterations for larger N because iterations become very slow as N increases
System.out.println("NREPS="+NREPS);
double TEMP0=2;
int scr=upper(N,bfs3,bfs4,toSolve);
int bscr=scr;
List<Integer> bestTS=new ArrayList<>(toSolve);
SplittableRandom rnd=new SplittableRandom(1);
String form="%8s%8s%6s %s%n";
System.out.printf(form,"REPS","ACCN","GN","temp");
for (long reps=0, accn=0;; reps++) {
double temp=TEMP0*(1.0-reps/(double)NREPS);
if (reps%(NREPS/10)==0||reps==NREPS)
System.out.printf(form,reps,accn,scr,temp);
if (reps==NREPS) break;
int i=rnd.nextInt(T), j=rnd.nextInt(T-1); if (j>=i) j++;
int ti=toSolve.get(i), tj=toSolve.get(j);
toSolve.set(i,tj);
toSolve.set(j,ti);
int nscr=upper(N,bfs3,bfs4,toSolve);
if (nscr<=scr||rnd.nextDouble()<Math.exp((scr-nscr)/temp)) {
scr=nscr;
if (scr<bscr) {
bscr=scr;
bestTS=new ArrayList<>(toSolve);
}
accn++;
}
else {
toSolve.set(i,ti);
toSolve.set(j,tj);
}
}
else {
toSolve.set(i,ti);
toSolve.set(j,tj);
System.out.println(toSolve);
GNs[N]=bscr;
{
int[] tmp=new int[bestTS.size()];
for (int i=0; i<tmp.length; i++)
tmp[i]=bestTS.get(i);
bestSolveSeqs[N]=tmp;
}
System.out.println("bestScr="+bscr+", best list of pieces to solve="+bestTS);
System.out.println("SA time="+(System.currentTimeMillis()-st));
}
System.out.println(toSolve);*/
int Nlo=4, Nhi=10;
String form="%2s%6s %s%n";
System.out.printf(form,"N","GN","best sequence of moves to solve");
for (int N=Nlo; N<=Nhi; N++)
System.out.printf(form,N,GNs[N],Arrays.toString(bestSolveSeqs[N]));
/*
int[] S3=new int[Nhi-Nlo+1], S4=new int[Nhi-Nlo+1];
for (int N=Nlo; N<=Nhi; N++) {
System.out.println("N="+N);
Expand All @@ -181,6 +209,6 @@ public static void main(String[] args) {
for (int K=3; K<=N*N-1; K++) worst=Math.max(worst,Wrow[K]);
worst+=2*(int)Math.floor(N/2);
System.out.printf(form,N,S3[N-Nlo],S4[N-Nlo],worst);
}
}*/
}
}

0 comments on commit 1dbcb4e

Please sign in to comment.