import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HopfieldNetwork extends JFrame implements ActionListener{ private JPanel patternPanel, messagePanel; private MyPanel finalDataPanel; private JButton patternZero, patternOne, getThePattern, clearAllButton; private JLabel missing, change, result; private JTextField percentageMissing, percentageChange, matchResult; private Color colors[]; private int xValue, yValue; private int missingNumber, changeNumber; private int rawData[]; private int matrix[][]; private int input[], output[]; private final int prePatterns[][] = {{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,1,1,-1,1,1,-1,-1,1,-1,-1,-1,1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1, -1,1,1,-1,-1,-1,-1,-1,1,-1,1,-1,-1,-1,1,-1,-1,1,1,-1,1,1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, {-1,-1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,1, -1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,1,-1,-1,-1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1}}; public HopfieldNetwork(){ super("Hopfield Network: 0-1 Pattern"); Container c = getContentPane(); c.setLayout(new BorderLayout(0, 0)); patternPanel=new JPanel(); messagePanel = new JPanel(); finalDataPanel = new MyPanel(); c.add(patternPanel, BorderLayout.NORTH); c.add(finalDataPanel, BorderLayout.CENTER); c.add(messagePanel, BorderLayout.SOUTH); finalDataPanel.addMouseListener( new MouseAdapter(){ public void mouseClicked(MouseEvent e){ xValue = e.getX(); yValue = e.getY(); action(); } } ); colors = new Color[77]; for(int i=0; i<77; i++){ colors[i] = Color.white; } finalDataPanel.draw(colors); patternZero=new JButton("Pattern 0"); patternOne=new JButton("Pattern 1"); clearAllButton = new JButton("Clear All"); getThePattern = new JButton("Get Result"); missing=new JLabel(" % missing"); change=new JLabel(" % change"); result=new JLabel(" Result"); percentageMissing=new JTextField(10); percentageChange=new JTextField(10); matchResult=new JTextField(10); patternPanel.setLayout(new GridLayout(1, 2, 0, 0)); messagePanel.setLayout(new GridLayout(4, 2, 0, 0)); patternZero.addActionListener(this); patternOne.addActionListener(this); clearAllButton.addActionListener(this); getThePattern.addActionListener(this); percentageMissing.addActionListener(this); percentageChange.addActionListener(this); matchResult.setEditable(false); patternPanel.add(patternZero); patternPanel.add(patternOne); messagePanel.add(getThePattern); messagePanel.add(clearAllButton); messagePanel.add(missing); messagePanel.add(percentageMissing); messagePanel.add(change); messagePanel.add(percentageChange); messagePanel.add(result); messagePanel.add(matchResult); rawData = new int[77]; for(int i=0; i<77; i++) rawData[i] = -1; matrix = new int[77][77]; input = new int[77]; output = new int[77]; for(int j=0; j<77; j++) for(int k=0; k<77; k++){ if(j==k) matrix[j][k] = 0; else matrix[j][k] = prePatterns[0][j] * prePatterns[0][k] + prePatterns[1][j] * prePatterns[1][k]; } setSize(220, 495); show(); } public void action(){ int row = yValue/30; int column = xValue/30; if(rawData[row*7+column]==1){ colors[row*7+column] = Color.lightGray; rawData[row*7+column] =0; //missing data } else if(rawData[row*7+column]==-1){ colors[row*7+column] = Color.lightGray; rawData[row*7+column] = 0; } else{ //if already missing, do nothing } finalDataPanel.draw(colors); } public void actionPerformed(ActionEvent e){ if(e.getSource()==percentageMissing){ missingNumber=(int)Math.round(0.77*Double.parseDouble(e.getActionCommand())); randomMissing(); } else if(e.getSource()==percentageChange){ changeNumber=(int)Math.round(0.77*Double.parseDouble(e.getActionCommand())); randomChange(); } else{ String action = e.getActionCommand(); if(action=="Pattern 0"){ for(int i=0; i<77; i++){ rawData[i] = prePatterns[0][i]; if(rawData[i]==1) colors[i] = Color.black; else colors[i]=Color.white; } finalDataPanel.draw(colors); return; } if(action=="Pattern 1"){ for(int i=0; i<77; i++){ rawData[i] = prePatterns[1][i]; if(rawData[i]==1) colors[i] = Color.black; else colors[i]=Color.white; } finalDataPanel.draw(colors); return; } if(action=="Clear All"){ for(int i=0; i<77; i++){ colors[i] = Color.white; rawData[i] = -1; } finalDataPanel.draw(colors); return; } if(action=="Get Result"){ findThePattern(); return; } } } public void randomMissing(){ int missingCell; int safeguard=0; while(missingNumber!=0){ missingCell=(int)(77*Math.random()); if(rawData[missingCell]!=0){ colors[missingCell] = Color.lightGray; rawData[missingCell] =0; //missing data missingNumber--; } safeguard++; if(safeguard>100000) break; } finalDataPanel.draw(colors); } public void randomChange(){ int changeCell; int safeguard=0; int original[]=new int[77]; for(int i=0; i<77; i++) original[i]=rawData[i]; while(changeNumber!=0){ changeCell=(int)(77*Math.random()); if(rawData[changeCell]==1&&original[changeCell]==1){ colors[changeCell] = Color.white; rawData[changeCell] =-1; //missing data changeNumber--; } else if(rawData[changeCell]==-1&&original[changeCell]==-1){ colors[changeCell] = Color.black; rawData[changeCell] =1; //missing data changeNumber--; } safeguard++; if(safeguard>100000) break; } finalDataPanel.draw(colors); } public void findThePattern(){ for(int j=0; j<77; j++) for(int k=0; k<77; k++) if(j==k) matrix[j][k] =1; for(int i=0; i<77; i++){ output[i]=rawData[i]; } boolean flaggy=true; int iteration=0; do{ System.out.println("Iteration " + iteration + ":"); for(int i=0; i<11; i++){ for(int j=0; j<7; j++){ if(output[i*7+j]==-1) System.out.print(output[i*7+j]+ " "); else System.out.print(" " + output[i*7+j]+" "); } System.out.println(); } flaggy=true; for(int i=0; i<77; i++){ input[i]=output[i]; output[i]=0; } for(int m=0; m<77; m++) for(int i=0; i<77; i++) output[m] += matrix[m][i] * input[i]; for(int m=0; m<77; m++){ if(output[m]>0) output[m]=1; else if(output[m]==0) output[m]=input[m]; else output[m]=-1; } iteration++; if(iteration>999) break; for(int i=0; i<77; i++){ if(output[i]!=input[i]) flaggy=false; } } while(!flaggy); System.out.println("There are " + iteration + " iterations"); for(int i=0; i<77; i++){ rawData[i]=output[i]; if(output[i]==1) colors[i] = Color.black; else if(output[i]==-1) colors[i] = Color.white; else colors[i]=Color.lightGray; } //output the matching result to the textfield int p0=0, p1=0; for(int i=0; i<77; i++){ p0+=prePatterns[0][i]*output[i]; p1+=prePatterns[1][i]*output[i]; } if(p0==77||p0==-77) matchResult.setText("Pattern 0"); else if(p1==77||p1==77) matchResult.setText("Pattern 1"); else matchResult.setText("No match"); finalDataPanel.draw(colors); } public static void main(String args[]){ HopfieldNetwork app = new HopfieldNetwork(); app.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); } } class MyPanel extends JPanel{ private Color c[]; public MyPanel(){ c = new Color[77]; } public void paintComponent(Graphics g){ super.paintComponent(g); for(int i=0; i<77; i++){ g.setColor(c[i]); g.fillRect((i%7)*30, (i/7)*30, 29, 29); } } public void draw (Color color[]){ for(int i=0; i<77; i++) c[i] = color[i]; repaint(); } }