//Hopfield neural net taken from: // // HNet hoppy; Buttons [] button; PFont font; int state = 0; void setup(){ size(200,100); hoppy = new HNet(); button = new Buttons[5]; button[0] = new Buttons(100, 0, 100, 20, #EEEEEE, "Single step", null); button[1] = new Buttons(100, 20, 100, 20, #EEEEEE, "Pattern0", null); button[2] = new Buttons(100, 40, 100, 20, #EEEEEE, "Pattern1", null); button[3] = new Buttons(100, 60, 100, 20, #EEEEEE, "Pattern2", null); button[4] = new Buttons(100, 80, 100, 20, #EEEEEE, "Pattern3", null); font = loadFont("Kartika-48.vlw"); textFont(font,20); } void draw(){ background(220); switch(state){ case 0: hoppy.drawMainScreen(0,0); break; default: hoppy.drawTrainingPatterns(0,0,state-1); break; } for(int i = 0; i < button.length; i++) button[i].draw(); } void mousePressed(){ int choice = -1; for(int i = 0; i < button.length; i++) if(button[i].over()) choice = i; switch(choice){ case 0: if (state == 0) hoppy.runSingleStep(); else{ state = 0; button[0].label = "Single Step"; } break; case 1: case 2: case 3: case 4: button[0].label = "Test pattern"; state = choice; break; default: //grid button listener int cr = -1; int cc = -1; for(int i = 0; i < hoppy.rows; i++) for(int j = 0; j < hoppy.cols; j++) if (overRect(i*12,j*12,12,12)){ cr = i; cc = j; } if (state == 0 && cr > -1 && cc > -1) hoppy.testPattern[cr][cc] = - hoppy.testPattern[cr][cc]; else if (cr > -1 && cc > -1){ hoppy.trainPatterns[state-1][cr][cc] = - hoppy.trainPatterns[state-1][cr][cc]; hoppy.retrain(); } } } class HNet{ int maxpatterns,rows,cols,mainscreen,trainpatts,screenMode; int [][][][] t; int [][][] trainPatterns; int [][] testPattern; HNet(){ maxpatterns = 4; rows = 8; cols = 8; mainscreen = 0; trainpatts = 1; trainPatterns = new int [maxpatterns][cols][rows]; testPattern = new int [cols][rows]; t = new int [cols][rows][cols][rows]; //connection weights screenMode = mainscreen; setupTrainingPatterns(); // Initialise test pattern to blank for (int i = 0; i < cols; i++) for (int j = 0; j < rows; j++) testPattern[i][j] = -1; } void drawMainScreen(float x, float y){ for (int j = 0; j < rows; j++) for (int i = 0; i < cols; i++) drawCell(testPattern[i][j],x + 12 * i, y + 12 * j); } void drawTrainingPatterns(float x, float y, int pattern){ for (int i = 0; i < cols; i++) for (int j = 0; j < rows; j++) drawCell(trainPatterns[pattern][i][j],x + 12 * i, y + 12 * j); } void drawCell(int value, float x, float y){ if(value == 1) fill(255); else fill(0); rect(x,y,12,12); } void runSingleStep(){ int new_mu[][] = new int[cols][rows]; // Holds new values of mu before copying back int i,j,k,l; // FOR loop counters for (k = 0; k < cols; k++) for (l = 0; l < rows; l++){ int sum = 0; for (i = 0; i < cols; i++) for (j = 0; j < rows; j++) sum += t[i][j][k][l] * testPattern[i][j]; if (sum > 0) // Pass through hard-limiting non-linearity new_mu[k][l] = 1; else new_mu[k][l] = -1; } // Now copy the values present in new_mu back into the test pattern ready to be displayed for (i = 0; i < cols; i++) for (j = 0; j < rows; j++) testPattern[i][j] = new_mu[i][j]; } void retrain (){ for (int i = 0; i < cols; i++) for (int j = 0; j < rows; j++) for (int k = 0; k < cols; k++) for (int l = 0; l < rows; l++) if (i == k && j == l) t[i][j][k][l] = 0; else { int sum = 0; for (int pattern = 0; pattern < maxpatterns; pattern++) sum += trainPatterns[pattern][i][j] * trainPatterns[pattern][k][l]; t[i][j][k][l] = sum; } } void setupTrainingPatterns (){ initialisePattern(0,"00000000","01111110","01000010","01000010","01000010","01000010", "01111110","00000000"); initialisePattern(1,"00000000","00010000","00010000","00010000","11111110","00010000", "00010000","00010000"); initialisePattern(2,"10011110","10010010","10010010","10010010","10010010","10010010", "10010010","11110011"); initialisePattern(3,"00000000","00010000","00011000","00100100","00100100","01000010", "01000010","11111111"); retrain(); } void initialisePattern (int patNum, String line0, String line1, String line2, String line3, String line4, String line5, String line6, String line7){ initialiseline(patNum,0,line0); initialiseline(patNum,1,line1); initialiseline(patNum,2,line2); initialiseline(patNum,3,line3); initialiseline(patNum,4,line4); initialiseline(patNum,5,line5); initialiseline(patNum,6,line6); initialiseline(patNum,7,line7); } void initialiseline (int patNum, int lineNum, String line){ for (int i = 0; i < line.length(); i++) if (line.charAt(i) == '1') trainPatterns[patNum][i][lineNum] = +1; else trainPatterns[patNum][i][lineNum] = -1; } }