import java.applet.Applet; import java.awt.*; //import java.awt.event.*; import java.io.*; import java.util.*; import java.net.*; public class Sida extends Applet implements Runnable { sidaCanvas sc; //input public int xValue = 1, yValue = 1, index = 0, printcnt = 0; public boolean hasBeenPaused = false, started = false; public Choice agentDelay; // Specify GUI layout items. public Choice agentInput; public Button startAgentButton; public Button pauseButton; public Button resumeButton; public Button stopButton; public Button resetLearning; Thread t; int inputfilesize; float indata[][]; public int array_size = 3000, cnt = 0; public int xVector[]; public int yVector[]; public int activationVector[]; int no_of_cycles = 1000; //agent int a_window_size; float a_window[][]; //this table will contain the porbabilities for various actions float selectiontable[][]; //descriptions of directions String dirtoidxmap[]; //movements Movement move[]; //map move; public void set_dirtoidxmap() { //dir_map table_direction; dirtoidxmap[0] = "NORTH"; dirtoidxmap[1] = "SOUTH"; dirtoidxmap[2] = "EAST"; dirtoidxmap[3] = "WEST"; dirtoidxmap[4] = "NORTHEAST"; dirtoidxmap[5] = "SOUTHEAST"; dirtoidxmap[6] = "SOUTHWEST"; dirtoidxmap[7] = "NORTHWEST"; } public void setmovement() { int a_window_size = 1; move[0] = new Movement(-1 * a_window_size, 0); move[1] = new Movement(1 * a_window_size, 0); move[2] = new Movement(0, 1 * a_window_size); move[3] = new Movement(0, -1 * a_window_size); move[4] = new Movement(-1 * a_window_size, 1 * a_window_size); move[5] = new Movement(1 * a_window_size, 1 * a_window_size); move[6] = new Movement(1 * a_window_size, -1 * a_window_size); move[7] = new Movement(-1 * a_window_size, -1 * a_window_size); } void loaddata(String filename) { indata = new float[inputfilesize][inputfilesize]; try { URL url; URLConnection urlConn; DataInputStream dis; //System.out.println("came here"); url = new URL( "http://faculty.cs.tamu.edu/choe/demo/sida/" + filename); // Note: a more portable URL: //url = new URL(getCodeBase().toString() + "/java-choe/Out45.txt"); urlConn = url.openConnection(); urlConn.setDoInput(true); urlConn.setUseCaches(false); dis = new DataInputStream(urlConn.getInputStream()); String str; int i = 0; while ((str = dis.readLine()) != null && i < inputfilesize) { StringTokenizer tk = new StringTokenizer(str, " ", false); for (int j = 0; j < inputfilesize; ++j) { String s = tk.nextToken(); int ch = Integer.parseInt(s); indata[j][i] = ch; } i++; } /*String s; //System.out.println("came here"); while ((s = dis.readLine()) != null) { StringTokenizer tk = new StringTokenizer(s, " ", false); String s1 = tk.nextToken(); int xc = Integer.parseInt(s1); String s2 = tk.nextToken(); int yc = Integer.parseInt(s2); String s3 = tk.nextToken(); int activation = Integer.parseInt(s3); xVector[cnt] = xc; yVector[cnt] = yc; activationVector[cnt] = activation; /* xChoice.add(new Integer(xc)); yChoice.add(new Integer(yc)); */ /* if(cnt%10==0){ System.out.println(s+" : "+s1+ " : "+ s2); System.out.println(xc+" : "+yc); System.out.println("xVector: "+xVector[cnt]+" yVector: "+yVector[cnt]); //System.out.println("xChoice: "+(Integer)xChoice.get(cnt)+" yChoice: "+(Integer)yChoice.get(cnt)); } */ //cnt++; //toDoChoice.addItem(s); //} System.out.println("the cnt is " + cnt); dis.close(); } // end of "try" catch (ArrayIndexOutOfBoundsException ae) { System.out.println("ArrayIndex out of bound: The cnt is " + cnt); } catch (MalformedURLException mue) { mue.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } /* try { BufferedReader in = new BufferedReader(new FileReader(filename)); String str; int i = 0; while ((str = in.readLine()) != null && i < inputfilesize) { StringTokenizer tk = new StringTokenizer(str, " ", false); for (int j = 0; j < inputfilesize; ++j) { String s = tk.nextToken(); int ch = Integer.parseInt(s); indata[j][i] = ch; } i++; } in.close(); } catch (IOException e) { }*/ /* for (int i=0; i= 0; i--) { int j = a_window_size - 1 - i; a_window[i][j] = 1; if ((i - 1 < a_window_size) && (i - 1 >= 0)) a_window[i - 1][j] = 1; if ((i + 1 < a_window_size) && (i + 1 >= 0)) a_window[i + 1][j] = 1; } a_window_print(); break; } case 4 : { System.out.println("45 obtuse : 45 degree "); for (int i = 0; i < a_window_size; i++) { int j = i; a_window[i][j] = 1; if ((i - 1 < a_window_size) && (i - 1 >= 0)) a_window[i - 1][j] = 1; if ((i + 1 < a_window_size) && (i + 1 >= 0)) a_window[i + 1][j] = 1; } System.out.println("agent window for 45 is"); a_window_print(); break; } } //end switch } // end a_config public void selectiontable_print() { for (int i = 0; i < 5; ++i) { for (int j = 0; j < 8; ++j) { System.out.print(selectiontable[i][j]); } System.out.println(); } } public void config_table() { selectiontable = new float[5][8]; Random rand = new Random(34823465); // //srand(34823465); for (int i = 0; i < 5; i++) { float sum = 0; for (int j = 0; j < 8; j++) { selectiontable[i][j] = rand.nextFloat(); sum += selectiontable[i][j]; } for (int j = 0; j < 8; j++) selectiontable[i][j] = selectiontable[i][j] / sum; } //Normalise the values so as set the sumo f probabilities = 1 //This will still retain the random nature of the values System.out.println("configtable"); selectiontable_print(); } //check again !! public int selectiontable_max_rowindex_orig(int col) { int max_indx = 0; for (int i = 1; i < 5; ++i) { if (selectiontable[col][i] > selectiontable[col][max_indx]) max_indx = i; } return max_indx; } public int selectiontable_max_rowindex(int row) { int max_indx = 0; for (int i = 1; i < 8; ++i) { if (selectiontable[row][i] > selectiontable[row][max_indx]) max_indx = i; } return max_indx; } public int select_action(int type) { int idx = selectiontable_max_rowindex(type); return (idx); } // return the next best action public int select_nextbest_action_orig(int type, int curr_best_idx) { int max_idx = -1; float diff = 1; float temp; //get value of current best index float limit = selectiontable[type][curr_best_idx]; for (int i = 0; i < 5; i++) { if (((temp = (limit - selectiontable[type][i])) >= 0) && (curr_best_idx != i)) { if (diff > temp) { max_idx = i; diff = temp; } } } return max_idx; } public int select_nextbest_action(int type, int curr_best_idx) { int max_idx = -1; float diff = 1; float temp; //get value of current best index float limit = selectiontable[type][curr_best_idx]; for (int i = 0; i < 8; i++) { if (((temp = (limit - selectiontable[type][i])) >= 0) && (curr_best_idx != i)) { if (diff > temp) { max_idx = i; diff = temp; } } } return max_idx; } public void modifytable(int type, int dir, int change) { // change = +1 or -1 float newvalue = selectiontable[type][dir] + (float) (change * .01); if (newvalue > 1) // || (newvalue < 0)) selectiontable[type][dir] = 1; else if (newvalue < 0) selectiontable[type][dir] = 0; else selectiontable[type][dir] = newvalue; } public boolean compare_matrix( float[][] window1, float[][] window2, int i, int j) { for (int k = 0; k < i; k++) for (int l = 0; l < j; l++) { if (window1[k][l] != window2[k][l]) return false; } return true; } public String start_epoch( int inputtype, int startx, int starty, int angle) { //make sure the start position is inside the imagefile // x = column index // y = row index int count = 0, cnt = 0; int rewardcycles = 0; int neg_rewardcycles = 0; int previousstate = inputtype; int x = startx; int y = starty; int actionidx = 0; if ((startx - a_window_size / 2 < 0) || (startx + a_window_size / 2 >= inputfilesize)) return ("improper x startpoint"); if ((starty - a_window_size / 2 < 0) || (starty + a_window_size / 2 >= inputfilesize)) return ("improper y startpoint"); //make sure the type of activation is correct float cur_window[][] = new float[a_window_size][a_window_size]; for (int i = 0; i < a_window_size; i++) for (int j = 0; j < a_window_size; j++) cur_window[i][j] = 0; int topX = x - a_window_size / 2; int topY = y - a_window_size / 2; //System.out.println("top x:" + topX); //System.out.println("top y:" + topY); /*for (int i = 0; i < inputfilesize; ++i) { for (int j = 0; j < inputfilesize; ++j) { System.out.print(indata[j][i]); } System.out.println(); }*/ //System.out.println("25"+ indata[3][25]); //System.out.println("24"+ indata[3][25]); //System.out.println("26"+ indata[3][25]); for (int i = 0; i < a_window_size; i++, topY++) { for (int j = 0; j < a_window_size; j++, topX++) { cur_window[j][i] = indata[topX][topY]; //System.out.print(cur_window[j][i] + " "); } topX -= a_window_size; //System.out.println(); } a_config(inputtype); if (compare_matrix(cur_window, a_window, a_window_size, a_window_size)) System.out.println("Correct type of input provided"); else { System.out.println( "Incorrect neuron activation info. Stopping training"); return ("stop"); } do { //look up for prooper action Movement m; actionidx = select_action(previousstate); m = move[actionidx]; //do the action //start creating array data here!! System.out.println( "sida-trace" + angle + ": " + x + "\t" + y + "\t" + actionidx); //storing SIDA movements in an Array xVector[cnt] = x; yVector[cnt] = y; activationVector[cnt] = actionidx; cnt++; x += m.delcol; y += m.delrow; // cout << "sida-trace:"<< x << "\t" << y << endl; //if outside the file boubries start again while (((x - a_window_size / 2 < 0) || (x + a_window_size / 2 >= inputfilesize)) || ((y - a_window_size / 2 < 0) || (y + a_window_size / 2 >= inputfilesize))) { int num_of_falls; x -= m.delcol; y -= m.delrow; rewardcycles = 0; modifytable(previousstate, actionidx, -1); actionidx = select_nextbest_action(previousstate, actionidx); if (actionidx != -1) { m = move[actionidx]; x += m.delcol; y += m.delrow; } else { //this will never happen x = startx; y = starty; System.out.println("the rare " + count + "thing happens"); selectiontable_print(); break; } System.out.println( "sida-trace" + angle + ": " + x + "\t" + y + "\t" + actionidx); xVector[cnt] = x; yVector[cnt] = y; activationVector[cnt] = actionidx; cnt++; } /*indata -> sub_matrix( cur_window - > mat, x - a_window_size / 2, y - a_window_size / 2, x + a_window_size / 2, y + a_window_size / 2);*/ topX = x - a_window_size / 2; topY = y - a_window_size / 2; for (int i = 0; i < a_window_size; i++, topY++) { for (int j = 0; j < a_window_size; j++, topX++) { cur_window[j][i] = indata[topX][topY]; //System.out.print(cur_window[j][i] + " "); } topX -= a_window_size; //System.out.println(); } //compare with desired activation if (compare_matrix(cur_window, a_window, a_window_size, a_window_size)) { //increment for previous state the action //modify the table for the action rewardcycles++; neg_rewardcycles = 0; modifytable(previousstate, actionidx, rewardcycles); } else { neg_rewardcycles--; rewardcycles = 0; modifytable(previousstate, actionidx, neg_rewardcycles); previousstate = inputtype; //go back to beginning x = startx; y = starty; } count++; // yschoe if (count % 10 == 0 && count < 500) { System.out.println("iteration: " + count); selectiontable_print(); } } while (count < no_of_cycles); return ("End"); } public boolean training(int inputOption) { switch (inputOption) { case 1 : { //vert input loaddata("data.vert_bar"); start_epoch(1, 25, 3, 90); System.out.println("Selection Table"); selectiontable_print(); break; } case 4 : { //45 degree input loaddata("data.45obtuse"); start_epoch(4, 3, 3, 45); System.out.println("Selection Table"); selectiontable_print(); break; } case 2 : { //horizontal input loaddata("data.horiz_bar"); start_epoch(2, 3, 25, 0); System.out.println("Selection Table"); selectiontable_print(); break; } case 3 : { //45 degree input loaddata("data.45acute"); start_epoch(3, 3, 47, 135); System.out.println("Selection Table"); selectiontable_print(); break; } } return true; } public void createFile() { try { BufferedWriter out = new BufferedWriter(new FileWriter("out.txt")); int i = 0; while (xVector[i] != 0) { out.write( xVector[i] + "\t" + yVector[i] + "\t" + activationVector[i] + "\n"); i++; } out.close(); } catch (IOException e) { } } public void reset_arrays() { xVector = new int[array_size]; yVector = new int[array_size]; activationVector = new int[array_size]; for (int j = 0; j < array_size; ++j) { xVector[j] = 0; yVector[j] = 0; activationVector[j] = 0; } } /*public static void main(String[] args) { Sida mySida = new Sida(); mySida.reset_arrays(); mySida.config_table(); mySida.set_dirtoidxmap(); mySida.setmovement(); System.out.println("Selection Table"); mySida.selectiontable_print(); mySida.training(); mySida.createFile(); }*/ public Sida() { inputfilesize = 51; a_window_size = 7; a_window = new float[a_window_size][a_window_size]; dirtoidxmap = new String[8]; move = new Movement[8]; } public void init() { Sida mySida = new Sida(); mySida.reset_arrays(); mySida.config_table(); mySida.set_dirtoidxmap(); mySida.setmovement(); System.out.println("Selection Table"); mySida.selectiontable_print(); sc = new sidaCanvas(); Panel pLeft = new Panel(); Panel pRight = new Panel(); setLayout(new BorderLayout()); pLeft.setLayout(new GridLayout(1, 4)); pLeft.add(new Label("SIDA Input")); reset_arrays(); agentInput = new Choice(); agentInput.addItem("Horizontal"); // Removals for 5-Nim. agentInput.addItem("Vertical"); agentInput.addItem("Acute"); agentInput.addItem("Obtuse"); agentInput.select(0); pLeft.add(agentInput); pLeft.add(new Label("Delay time for SIDA agent movement")); // Add label to GUI layout. agentDelay = new Choice(); // Create drop-down menu Choice Choice (agentDelay). agentDelay.addItem("10"); agentDelay.addItem("25"); agentDelay.addItem("50"); agentDelay.addItem("100"); agentDelay.addItem("200"); agentDelay.addItem("500"); agentDelay.select(0); pLeft.add(agentDelay); // Add drop-down menu Choice Choice to GUI layout. pRight.setLayout(new GridLayout(1, 5)); startAgentButton = new Button("Start Agent Action"); // Create Button (StartGameButton). pRight.add(startAgentButton); // Add Button to GUI layout. resumeButton = new Button("Resume"); // Create Button (StartGameButton). pRight.add(resumeButton); // Add Button to GUI layout. pauseButton = new Button("Pause"); // Create Button (StartGameButton). pRight.add(pauseButton); // Add Button to GUI layout. stopButton = new Button("Stop"); // Create Button (StartGameButton). pRight.add(stopButton); // Add Button to GUI layout. resetLearning = new Button("Reset"); // Create Button (StartGameButton). pRight.add(resetLearning); // Add Button to GUI layout.*/ add("North", pLeft); add("South", pRight); add("Center", sc); } public void run() { while (true) { try { while (xVector[index] != 0) { java.lang.Thread.sleep( Integer.parseInt(agentDelay.getSelectedItem())); repaint(); index++; } } catch (InterruptedException ie) { ie.printStackTrace(); } } } public boolean action( Event event, Object object) // Detect user action from the GUI (the game starts here). { if (event.target == startAgentButton && started == false) { System.out.println( "INput Agent type is " + agentInput.getSelectedItem()); if (agentInput.getSelectedItem() == "Horizontal") training(2); else if (agentInput.getSelectedItem() == "Vertical") training(1); else if (agentInput.getSelectedItem() == "Acute") training(3); else if (agentInput.getSelectedItem() == "Obtuse") training(4); //createFile(); //printcnt = 0; index = 0; //loadAgent(); sc.setSida(this); t = new Thread(this); t.start(); started = true; } if (event.target == pauseButton) // If the user clicks the startGameButton, t.suspend(); if (event.target == resumeButton) // If the user clicks the startGameButton, t.resume(); if (event.target == stopButton) { // If the user clicks the startGameButton, t.stop(); started = false; } if (event.target == resetLearning) { // If the user clicks the startGameButton, t.stop(); config_table(); started = false; if (agentInput.getSelectedItem() == "Horizontal") training(2); else if (agentInput.getSelectedItem() == "Vertical") training(1); else if (agentInput.getSelectedItem() == "Acute") training(3); else if (agentInput.getSelectedItem() == "Obtuse") training(4); //createFile(); //printcnt = 0; index = 0; //loadAgent(); t = new Thread(this); t.start(); started = true; } return true; } } class sidaCanvas extends Canvas { Sida mySida; public sidaCanvas() {} public void setSida(Sida _mySida) { mySida = _mySida; } private void splitWindowInto2(Graphics g) { g.setColor(Color.WHITE); //g.fillRect(0,0,510,510); g.drawRect(0, 0, 510, 510); g.fillRect(0, 510, 510, 3); } private void animateDots(Graphics g) { g.setColor(Color.BLUE); //All previous dots in blue for (int loop = 0; loop < mySida.index; loop++) { g.fillOval(findXCordinate(loop), findYCordinate(loop), 2, 2); } // The immediate parent dot will be a simple red dot. if (mySida.index > 1) { g.setColor(Color.RED); g.fillOval( findXCordinate(mySida.index - 1), findYCordinate(mySida.index - 1), 2, 2); } // The last dot will be a simple red dot with a red oval around it. g.setColor(Color.RED); g.fillOval(findXCordinate(mySida.index), findYCordinate(mySida.index), 2, 2); g.drawOval( findXCordinate(mySida.index) - 5, findYCordinate(mySida.index) - 5, 10, 10); } private void paintHorizontalLine(Graphics g, Color lineColor) { g.setColor(Color.WHITE); //g.fillRect(0,0,510,510); g.drawRect(0, 510, 127, 127); //Draw horizontal line g.setColor(lineColor); int centerOfFilterSection = 510 + (637 - 510) / 2; for (int loop = 0; loop < 100; loop++) { g.fillOval(loop + 10, centerOfFilterSection, 2, 2); } } private void paintVerticalLine(Graphics g, Color lineColor) { g.setColor(Color.WHITE); //g.fillRect(127,510,127,127); g.drawRect(127, 510, 127, 127); //g.setColor(Color.WHITE); //g.fillRect(127,510,127,127); //Draw horizontal line g.setColor(lineColor); int centerXOfFilterSection = 127 + 127 / 2; for (int loop = 0; loop < 100; loop++) { g.fillOval(centerXOfFilterSection, 510 + 10 + loop, 2, 2); } } private void paintAcuteLine(Graphics g, Color lineColor) { g.setColor(Color.WHITE); //g.fillRect(0,0,510,510); g.drawRect(127 * 2, 510, 127, 127); //Draw horizontal line g.setColor(lineColor); //int centerXOfFilterSection=127*2+127/2; for (int loop = 0; loop < 100; loop++) { g.fillOval(127 * 2 + 10 + loop, 637 - 10 - loop, 2, 2); } } private void paintObtuseLine(Graphics g, Color lineColor) { g.setColor(Color.WHITE); //g.fillRect(0,0,510,510); g.drawRect(127 * 3, 510, 127, 127); //Draw horizontal line g.setColor(lineColor); //int centerXOfFilterSection=127*2+127/2; for (int loop = 0; loop < 100; loop++) { g.fillOval(127 * 3 + 10 + loop, 510 + 10 + loop, 2, 2); } } private void paintFilterObjects(Graphics g, int currentSensor) { if (currentSensor == 1) { paintHorizontalLine(g, Color.RED); } else { paintHorizontalLine(g, Color.BLUE); } if (currentSensor == 2) { paintVerticalLine(g, Color.RED); } else { paintVerticalLine(g, Color.BLUE); } if (currentSensor == 3) { paintAcuteLine(g, Color.RED); } else { paintAcuteLine(g, Color.BLUE); } if (currentSensor == 4) { paintObtuseLine(g, Color.RED); } else { paintObtuseLine(g, Color.BLUE); } } public void paint(Graphics g) { splitWindowInto2(g); animateDots(g); paintFilterObjects(g, findSensor(mySida.index)); } public void update(Graphics g) { if (mySida.hasBeenPaused) paint(g); else super.update(g); } public int findXCordinate(int i) { return mySida.xVector[i] * 10; } public int findYCordinate(int i) { return mySida.yVector[i] * 10; } public int findSensor(int i) { switch (mySida.activationVector[i]) { case 0 : //vertical case 1 : //vertical return 2; case 2 : //horizontal case 3 : //horizontal return 1; case 4 : //Acute case 6 : //Acute return 3; case 5 : //Obtuse case 7 : //Obtuse return 4; } return 1; } }