import java.applet.*; import java.awt.*; import java.lang.*; public class Ising2c extends Applet implements Runnable { // Display variables final int X0=10,Y0=320,H=256,W=256; final int XMAX=256,YMAX=256; final int veryLargeNumber=100; final int Nobj = 5; int numTiles = 1,numLevels=7; int numSubtiles = 4,tileRecount=0; Atom obj[] = new Atom[Nobj+1]; Atom cur[] = new Atom[veryLargeNumber+1]; int tileX=128,tileY=128; double cost,newCost; Thread anneal; //------------------------------------------ public void init() { setup(); add(new Button("Start")); add(new Button("Stop")); anneal = new Thread(this); anneal.start(); }//end init //------------------------------------------ void restart(){ setup(); repaint(); } //------------------------------------------ void setup() { cur[1] = new Atom(tileX/2,tileY/2); obj[1] = new Atom(0,0); obj[2] = new Atom(128,128); obj[3] = new Atom(56,79); obj[4] = new Atom(79,56); obj[5] = new Atom(200,200); int cnn=0,xnn,ynn; for (int n=2;n<=100;n++) { xnn = tileX*(n-1)%XMAX+tileX/2; ynn = tileY*(tileX*(n-1)/XMAX)+tileY/2; cur[n] = new Atom(xnn,ynn); } } //------------------------------------------ public void run() { for (int level=1;level<=numLevels;level++) { //Run the algorithm at the current resolution TestLevel(level,cur,numTiles); //Set up next generation of tiles if (numSubtiles*tileRecount > veryLargeNumber) { System.out.println("Too many tiles"); return; } if (level == numLevels ){ System.out.println("Final results:"); for(int n=1;n<=numTiles*numSubtiles;n++) if (cur[n].isHigh()==true) { System.out.println("(x,y) = (" +cur[n].x()+","+cur[n].y()+")" ); } } MovetoNewLevel(cur); } } //------------------------------------------ void TestLevel(int lev, Atom [] CS, int numTiles) { int index; double acceptRate = 0.5; Atom test[] = new Atom[numSubtiles*numTiles+1]; for (int n=1;n<=numSubtiles*numTiles;n++) test[n] = new Atom(CS[n].x(),CS[n].y(),CS[n].isHigh()); cost = findCost(CS,obj); repaint(); int i1,n1,n2,n3,n4; for (int group=1;group<=numTiles;group++) { //Cycle each group i1 = numSubtiles*(group-1); for(int s=1;s<=16;s++){ //Begin configuration loop for each group //Reconfigure local tiling n1 = (s-1)/8; n2 = ((s-1)%8)/4; n3 = (((s-1)%8)%4)/2; n4 = ((((s-1)%8)%4)%2); if (n1 == 0) test[i1+1].doneTesting(); else test[i1+1].nowTesting(); if (n2 == 0) test[i1+2].doneTesting(); else test[i1+2].nowTesting(); if (n3 == 0) test[i1+3].doneTesting(); else test[i1+3].nowTesting(); if (n4 == 0) test[i1+4].doneTesting(); else test[i1+4].nowTesting(); //Find cost function newCost = findCost(test,obj); //Accept tiling if O.K. if (newCost >= cost) { cost = newCost; for(int num=1;num<=numSubtiles;num++) { //subtile loop index = numSubtiles*(group-1)+num; CS[index].copyHighlight(test[index].isHigh() ); } } repaint(); }//End configuration loop }//End group loop //Count the number of remaining tiles tileRecount = 0; for(int n=1;n<=numSubtiles*numTiles;n++) if (CS[n].isHigh()==true) tileRecount++; }//End TestLevel //------------------------------------------ void MovetoNewLevel(Atom [] CS) { int i; Atom TempCS[] = new Atom[tileRecount+1]; i=0; for (int n=1;n<=numTiles*numSubtiles;n++) if (CS[n].isHigh() == true){ i++; TempCS[i] = new Atom(CS[n].x(),CS[n].y(),CS[n].isHigh() ); } for (int g=1;g<=tileRecount;g++) { i = numSubtiles*(g-1); CS[i+1].copy(TempCS[g].x()-tileX/4,TempCS[g].y()-tileY/4, TempCS[g].isHigh() ); CS[i+2].copy(TempCS[g].x()+tileX/4,TempCS[g].y()-tileY/4, TempCS[g].isHigh() ); CS[i+3].copy(TempCS[g].x()-tileX/4,TempCS[g].y()+tileY/4, TempCS[g].isHigh() ); CS[i+4].copy(TempCS[g].x()+tileX/4,TempCS[g].y()+tileY/4, TempCS[g].isHigh() ); } // Change tile size tileX/=2; tileY/=2; numTiles = tileRecount; } //------------------------------------------ boolean metrop(double dE, double T) { if (dE<0) return true; else if (Math.exp(-dE/T)>Math.random()) return true; else return false; } //------------------------------------------ // // Find Cost Function // double findCost(Atom in1[], Atom in2[]) { double cst=0; for(int n=1;n<=numSubtiles*numTiles;n++) { if (in1[n].isHigh() == true) cst += goodTile(in1[n],in2); if (in1[n].isHigh() == false) cst += noTile(in1[n],in2); } return cst; } //------------------------------------------ int goodTile(Atom in, Atom [] real) { int count=0; for (int nobj=1; nobj<=5; nobj++) for (int xx=-tileX/2;xx=256) x-=256; while(y<0) y+=256; while(y>=256) y-=256; } void moveTo(Atom rhs) { x=rhs.x(); y=rhs.y(); } void moveBy(int xin, int yin) { x +=xin; y +=yin; } void flipAboutY() {x = 256-x;} void flipAboutX() {y = 256-y;} void flipAboutDiagonal() { int temp; temp = y; y = x; x = temp; } void rotate90() { int temp; temp = y; y = x; x = 256-temp; } }