//Ludovic Hilde

//COMP572

//Neural Networks

//Kwinner Networks Java Classes

//NOTE: The code pertaining to the kwinner networks is in bold text.

 

package kwinner;

 

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

 

public class kWinnerApplet extends Applet

{

static final int N = 20; //Number of neurons

static final int MAX = 1;//Maximum activation

static final int MIN = 0;//Minimum activation

private boolean isStandalone = false;

JButton jButtonRun = new JButton();

JComboBox jComboBoxExt = new JComboBox();

JComboBox jComboBoxStep = new JComboBox();

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

JLabel jLabel3 = new JLabel();

int [][]W;

double []a;

double []e;

double step_size;

double total_energy;

JScrollPane jScrollPane1 = new JScrollPane();

JTextArea jTextAreaSimOut = new JTextArea();

JLabel jLabel4 = new JLabel();

JButton jButtonRun1 = new JButton();

 

//Get a parameter value

            public String getParameter(String key, String def)

            {

return isStandalone ? System.getProperty(key, def) :

                        (getParameter(key) != null ? getParameter(key) : def);

            }

 

            //Construct the applet

            public kWinnerApplet()

            {

            }

 

            //Initialize the applet

            public void init()

            {

                        try

                        {

                                    jbInit();

     

                                    //Init the connection strength

                                    int i, j;

                                    W = new int[N][N];

                                    for (i=0 ; i<N ; i++)

                                                for (j=0; j<N; j++)

                                                            if (i==j)

                                                                        W[i][j] = 0;

                                                            else

                                                                        W[i][j] = -1;

                        }

                        catch(Exception e)

                        {

                                    e.printStackTrace();

                        }

            }

 

            //Component initialization

            private void jbInit() throws Exception

{

                        jButtonRun.setBounds(new Rectangle(219, 26, 80, 25));

                        jButtonRun.setAlignmentX((float) 0.0);

                        jButtonRun.setPreferredSize(new Dimension(80, 25));

                        jButtonRun.setRequestFocusEnabled(true);

                        jButtonRun.setActionCommand("jButtonRun");

                        jButtonRun.setHorizontalAlignment(SwingConstants.CENTER);

                        jButtonRun.setHorizontalTextPosition(SwingConstants.TRAILING);

                        jButtonRun.setMargin(new Insets(2, 14, 2, 14));

                        jButtonRun.setText("Run");

                        jButtonRun.setVerticalAlignment(SwingConstants.CENTER);

                        jButtonRun.setVerticalTextPosition(SwingConstants.CENTER);

                        jButtonRun.addActionListener(new kWinnerApplet_jButtonRun_actionAdapter(this));

                        jComboBoxExt.setMinimumSize(new Dimension(90, 25));

                        jComboBoxExt.setPreferredSize(new Dimension(90, 25));

                        jComboBoxExt.setToolTipText("0.500");

                        jComboBoxExt.addItem("0.500");

                        jComboBoxExt.addItem("1.500");

                        jComboBoxExt.addItem("2.500");

                        jComboBoxExt.setBounds(new Rectangle(15, 26, 95, 25));

                        jComboBoxStep.setPreferredSize(new Dimension(90, 25));

                        jComboBoxStep.setToolTipText("");

                        jComboBoxStep.setVerifyInputWhenFocusTarget(true);

                        jComboBoxStep.setEditable(false);

                        jComboBoxStep.setBounds(new Rectangle(117, 26, 95, 25));

                        jComboBoxStep.setAlignmentY((float) 0.5);

                        jComboBoxStep.setAutoscrolls(false);

                        jComboBoxStep.setMinimumSize(new Dimension(90, 25));

                        jComboBoxStep.addItem("0.005");

                        jComboBoxStep.addItem("0.025");

                        jComboBoxStep.addItem("0.05");

                        jComboBoxStep.addItem("0.1");

                        this.setLayout(null);

                        jLabel1.setToolTipText("");

                        jLabel1.setText("External Input");

                        jLabel1.setBounds(new Rectangle(15, 8, 75, 15));

                        jLabel2.setToolTipText("");

                        jLabel2.setText("Step Size");

                        jLabel2.setBounds(new Rectangle(119, 8, 75, 15));

                        jLabel3.setToolTipText("");

                        jLabel3.setText("Simulation Output");

                        jLabel3.setBounds(new Rectangle(16, 114, 282, 15));

                        jScrollPane1.setBounds(new Rectangle(12, 153, 290, 319));

                        jLabel4.setText("Each line equals the total energy starting a t = 0");

                        jLabel4.setBounds(new Rectangle(16, 131, 282, 15));

                        jButtonRun1.addActionListener(new kWinnerApplet_jButtonRun1_actionAdapter(this));

                        jButtonRun1.setVerticalTextPosition(SwingConstants.CENTER);

                        jButtonRun1.addActionListener(new kWinnerApplet_jButtonRun1_actionAdapter(this));

                        jButtonRun1.setVerticalAlignment(SwingConstants.CENTER);

                        jButtonRun1.setText("All a = 0");

                        jButtonRun1.setMargin(new Insets(2, 14, 2, 14));

                        jButtonRun1.setHorizontalTextPosition(SwingConstants.TRAILING);

                        jButtonRun1.setHorizontalAlignment(SwingConstants.CENTER);

                        jButtonRun1.setActionCommand("jButtonRun");

                        jButtonRun1.setRequestFocusEnabled(true);

                        jButtonRun1.setPreferredSize(new Dimension(80, 25));

                        jButtonRun1.setAlignmentX((float) 0.0);

                        jButtonRun1.setBounds(new Rectangle(219, 62, 80, 25));

                        this.add(jComboBoxStep, null);

                        this.add(jComboBoxExt, null);

                        this.add(jButtonRun, null);

                        this.add(jLabel1, null);

                        this.add(jLabel2, null);

                        this.add(jScrollPane1, null);

                        this.add(jLabel3, null);

                        this.add(jLabel4, null);

                        this.add(jButtonRun1, null);

                        jScrollPane1.getViewport().add(jTextAreaSimOut, null);

            }

 

            //Get Applet information

            public String getAppletInfo()

{

                        return "Applet Information";

            }

 

            //Get parameter info

            public String[][] getParameterInfo()

{

                        return null;

            }

 

            void jButtonRun_actionPerformed(ActionEvent event)

{

                        int i;

                        String tempStr = new String();

 

                        //Init Randomly the activations

                        a = new double[N];

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        jTextAreaSimOut.append("Initial Activation\n");

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        for(i = 0 ; i < N ; i++)

                        {

                                    a[i] = Math.random();

                                    jTextAreaSimOut.append(tempStr.valueOf(a[i]));

                                    jTextAreaSimOut.append("\n");

                        }

                        runSim();

            }

 

            void jButtonRun1_actionPerformed(ActionEvent e)

{

                        int i;

                        String tempStr = new String();

                       

//Init the activations to 0

                        a = new double[N];

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        jTextAreaSimOut.append("Initial Activation\n");

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        for(i = 0 ; i < N ; i++)

                        {

                                    a[i] = 0.7;

                                    jTextAreaSimOut.append(tempStr.valueOf(a[i]));

                                    jTextAreaSimOut.append("\n");

                        }

                        runSim();

            }

 

            private void runSim ()

            {

                        double[] netInput = new double[N];

                        String tempStr = new String();

                         int i, j, t;

                        double energyFirstTerm = 0;

                        double energySecondTerm = 0;

                        double previousEnergy = 0;

 

                        //Init External input for each neuron

                        String tempVal;

                        Double tempDbl = new Double(0.0);

                        e = new double[N];

                        tempVal = (String)jComboBoxExt.getSelectedItem();

                        tempDbl = tempDbl.valueOf(tempVal);

                        e[0] = tempDbl.doubleValue();

                        for(i = 1 ; i < N ; i++)

                                    e[i] = e[0];

 

                        //Init the step size

                        step_size = 0;

                        tempVal = (String)jComboBoxStep.getSelectedItem();

                        tempDbl = tempDbl.valueOf(tempVal);

                        step_size = tempDbl.doubleValue();

 

                        //Init the total energy

                        total_energy = 0;

                        jTextAreaSimOut.append("\n");

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        jTextAreaSimOut.append("Energy level at t\n");

                        jTextAreaSimOut.append("/-------------------------------------\n");

 

                        for (t = 0; t < 10000; t++)

{

                                    total_energy = 0;

                                    energyFirstTerm = 0;

                                    energySecondTerm = 0;

                                    //Calculate the all net input at t

                                    //jTextAreaSimOut.append("At t = ");

                                    jTextAreaSimOut.append(tempStr.valueOf(t));

                                    jTextAreaSimOut.append("\t");

 

                                    for (i = 0; i < N; i++)

{

                                                netInput[i] = 0.0;

                                                for (j = 0; j < N; j++)

{

                                                            //Calculate the net input

                                                            netInput[i] += a[j] * W[i][j];

                                                            //Calculate the first term of the total energy equation

                                                            energyFirstTerm += a[i] * a[j] * W[i][j];

                                                }

 

                                                //Calculate the second term of the total energy equation

                                                energySecondTerm += a[i] * e[i];

 

                                                //Calculate the net input

                                                netInput[i] += e[i];

 

                                                //Calculate the new activation

                                                a[i] = a[i] + step_size * (MAX - a[i]) * (a[i] - MIN) * netInput[i];

                                    }

 

                                    //Calculate the total energy

                                    total_energy = (-0.5) * energyFirstTerm - energySecondTerm;

                                    //jTextAreaSimOut.append("Total energy: ");

                                    jTextAreaSimOut.append(tempStr.valueOf(total_energy));

                                    jTextAreaSimOut.append("\n");

 

                                    //Verify if the network has converge,

                                    //if it did stop the simulation

                                    if(Math.abs(previousEnergy  - total_energy) < 0.0001)

                                    {

                                                jTextAreaSimOut.append("Stopped simulation after ");

                                                jTextAreaSimOut.append(tempStr.valueOf(t));

                                                jTextAreaSimOut.append(" iterations\n");

                                                jTextAreaSimOut.append("The network has converged at\nenergy level = ");

                                                jTextAreaSimOut.append(tempStr.valueOf(total_energy));

                                                jTextAreaSimOut.append("\n");

                                                jTextAreaSimOut.append("\n");

                                                break;

                                    }

                                    previousEnergy = total_energy;

                        }

 

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        jTextAreaSimOut.append("Final Activation\n");

                        jTextAreaSimOut.append("/-------------------------------------\n");

                        for(i = 0 ; i < N ; i++)

                        {

                                    jTextAreaSimOut.append(tempStr.valueOf(a[i]));

                                    jTextAreaSimOut.append("\n");

                        }

            }

 

}

 

class kWinnerApplet_jButtonRun_actionAdapter implements java.awt.event.ActionListener

{

            kWinnerApplet adaptee;

 

            kWinnerApplet_jButtonRun_actionAdapter(kWinnerApplet adaptee)

{

                        this.adaptee = adaptee;

            }

           

public void actionPerformed(ActionEvent e)

{

                        adaptee.jButtonRun_actionPerformed(e);

            }

}

 

class kWinnerApplet_jButtonRun1_actionAdapter implements java.awt.event.ActionListener

{

            kWinnerApplet adaptee;

 

            kWinnerApplet_jButtonRun1_actionAdapter(kWinnerApplet adaptee)

{

                        this.adaptee = adaptee;

            }

 

            public void actionPerformed(ActionEvent e)

{

                        adaptee.jButtonRun1_actionPerformed(e);

            }

}

Hosted by www.Geocities.ws

1