Deck of Cards: (Pattern Recognition Using Backpropagation Algorithm)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

c

DECK OF CARDS
(Pattern Recognition Using Backpropagation Algorithm)

In Partial Fulfillment of the Requirements in


ECE 178

Miquiabas, Cheryl L.
Talatagod, Girlie K.

February 6, 2012

Cristina P. Dadula
Adviser

Class MathUtil
import java.util.Random;
public class MathUtil {
private static Random random = new Random();
public static synchronized double boundedRandom(double lower, double upper) {
double range = upper - lower;
double result = random.nextDouble() * range + lower;
return(result);
}
public static double[] initialize(double[] y){
for(int i = 0; i<y.length; i++){
y[i] = 0;
}
return y;
}
}

Class DeckOfCards
import java.awt.Color;
import javax.swing.JOptionPane;

public class DeckOfCards extends javax.swing.JFrame {


int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22, x23,
x24,x25, x26, x27, x28, x29, x30, x31, x32, x33, x34, x35, x36, x37, x38, x39, x40, x41, x42, x43, x44, x45,
x46, x47, x48, x49;
String[] outputText1 = new String[]{"", "Ace of", "2 of","3 of","4 of","5 of","6 of","7 of","8 of","9 of","10
of","Jack of","Queen of","King of"};
String outputText2;
int inputN = 50; // number of input neurons
int hiddenN = 5; // number of hidden neurons
int outputN = 14; // number of output neurons
int sampleN = 14; // number of training sets
// for actual character recognition process
double[] input = new double[inputN]; // the actual input of the user
double[] output = new double[outputN]; // actual output of of the input pattern entered by the user
double max; // the maximum value from output[]
int maxIndex; // the index of the maximum value from output[]
//for training
double[][] inPattern = new double[sampleN][inputN]; // input pattern of every training set
double[][] targetOut = new double[sampleN][outputN]; // the expected outputs for each training sample
double[][] wInToHid = new double[hiddenN][inputN]; // weights from input neurons to hidden neurons
double[] hidIn = new double[hiddenN]; // the net input to the hidden neurons
double[] hidOut = new double[hiddenN]; // the outputs of the hidden neurons

^c
c

double[][] wHidToOut = new double[outputN][hiddenN]; // weights fro hidden neurons to output neurons
double[] outIn = new double[outputN]; // the net input to the output neurons
double[][] outOut = new double[sampleN][outputN]; // the outputs of the output neurons
double[] outError = new double[outputN]; // errors of the output neurons
double[] hidError = new double[hiddenN]; // errors of the hidden neurons
double[][] new_wHidToOut = new double[outputN][hiddenN]; // updated weights of wHidToOut
double[][] new_wInToHid = new double[hiddenN][inputN]; // updated weights of wInToHid
double lr = 0.3; // learning rate
double e = 0.001; //maximum desired error
double error; // network total error
double epoch; // number of iterations during training
public DeckOfCards() {
initComponents();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
//<Generated code for GUI here>
// </editor-fold>
private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
b1.setBackground(Color.LIGHT_GRAY);x1=0;b2.setBackground(Color.LIGHT_GRAY);x2=0;
b3.setBackground(Color.LIGHT_GRAY);x3=0;b4.setBackground(Color.LIGHT_GRAY);x4=0;
b5.setBackground(Color.LIGHT_GRAY);x5=0;b6.setBackground(Color.LIGHT_GRAY);x6=0;
b7.setBackground(Color.LIGHT_GRAY);x7=0;b8.setBackground(Color.LIGHT_GRAY);x8=0;
b9.setBackground(Color.LIGHT_GRAY);x9=0;b10.setBackground(Color.LIGHT_GRAY);x10=0;
b11.setBackground(Color.LIGHT_GRAY);x11=0;b12.setBackground(Color.LIGHT_GRAY);x12=0;
b13.setBackground(Color.LIGHT_GRAY);x13=0;b14.setBackground(Color.LIGHT_GRAY);x14=0;
b15.setBackground(Color.LIGHT_GRAY);x15=0;b16.setBackground(Color.LIGHT_GRAY);x16=0;
b17.setBackground(Color.LIGHT_GRAY);x17=0;b18.setBackground(Color.LIGHT_GRAY);x18=0;
b19.setBackground(Color.LIGHT_GRAY);x19=0;b20.setBackground(Color.LIGHT_GRAY);x20=0;
b21.setBackground(Color.LIGHT_GRAY);x21=0;b22.setBackground(Color.LIGHT_GRAY);x22=0;
b23.setBackground(Color.LIGHT_GRAY);x23=0;b24.setBackground(Color.LIGHT_GRAY);x24=0;
b25.setBackground(Color.LIGHT_GRAY);x25=0;b26.setBackground(Color.LIGHT_GRAY);x26=0;
b27.setBackground(Color.LIGHT_GRAY);x27=0;b28.setBackground(Color.LIGHT_GRAY);x28=0;
b29.setBackground(Color.LIGHT_GRAY);x29=0;b30.setBackground(Color.LIGHT_GRAY);x30=0;
b31.setBackground(Color.LIGHT_GRAY);x31=0;b32.setBackground(Color.LIGHT_GRAY);x32=0;
b33.setBackground(Color.LIGHT_GRAY);x33=0;b34.setBackground(Color.LIGHT_GRAY);x34=0;
b35.setBackground(Color.LIGHT_GRAY);x35=0;b36.setBackground(Color.LIGHT_GRAY);x36=0;
b37.setBackground(Color.LIGHT_GRAY);x37=0;b38.setBackground(Color.LIGHT_GRAY);x38=0;
b39.setBackground(Color.LIGHT_GRAY);x39=0;b40.setBackground(Color.LIGHT_GRAY);x40=0;
b41.setBackground(Color.LIGHT_GRAY);x41=0;b42.setBackground(Color.LIGHT_GRAY);x42=0;
b43.setBackground(Color.LIGHT_GRAY);x43=0;b44.setBackground(Color.LIGHT_GRAY);x44=0;
b45.setBackground(Color.LIGHT_GRAY);x45=0;b46.setBackground(Color.LIGHT_GRAY);x46=0;
b47.setBackground(Color.LIGHT_GRAY);x47=0;b48.setBackground(Color.LIGHT_GRAY);x48=0;
b49.setBackground(Color.LIGHT_GRAY);x49=0; outputTextfield1.setText(""); outputTextfield2.setText("");
}
private void heartsButtonActionPerformed(java.awt.event.ActionEvent evt) {
outputText2 = "Hearts";
}

c
c

private void diamondsButtonActionPerformed(java.awt.event.ActionEvent evt) {


outputText2 = "Diamonds";
}
private void clubsButtonActionPerformed(java.awt.event.ActionEvent evt) {
outputText2 = "Clubs";
}
private void spadesButtonActionPerformed(java.awt.event.ActionEvent evt) {
outputText2 = "Spades";
}
private void trainButtonActionPerformed(java.awt.event.ActionEvent evt) {
// card Ace
inPattern[1][1]=0;
inPattern[1][2]=0;
inPattern[1][3]=0;
inPattern[1][4]=1;
inPattern[1][6]=0;
inPattern[1][7]=0;
inPattern[1][8]=0;
inPattern[1][9]=0;
inPattern[1][11]=0;
inPattern[1][12]=1;
inPattern[1][13]=0;
inPattern[1][14]=0;
inPattern[1][16]=1;
inPattern[1][17]=0;
inPattern[1][18]=0;
inPattern[1][19]=0;
inPattern[1][21]=0;
inPattern[1][22]=0;
inPattern[1][23]=1;
inPattern[1][24]=1;
inPattern[1][26]=1;
inPattern[1][27]=1;
inPattern[1][28]=0;
inPattern[1][29]=0;
inPattern[1][31]=0;
inPattern[1][32]=0;
inPattern[1][33]=0;
inPattern[1][34]=1;
inPattern[1][36]=0;
inPattern[1][37]=1;
inPattern[1][38]=0;
inPattern[1][39]=0;
inPattern[1][41]=1;
inPattern[1][42]=0;
inPattern[1][43]=0;
inPattern[1][44]=1;
inPattern[1][46]=0; inPattern[1][47]=0;
inPattern[1][48]=1;
inPattern[1][49]=0;
//card 2
inPattern[2][1]=0;
inPattern[2][2]=0;
inPattern[2][3]=1;
inPattern[2][4]=1;
inPattern[2][6]=0;
inPattern[2][7]=0;
inPattern[2][8]=0;
inPattern[2][9]=1;
inPattern[2][11]=0;
inPattern[2][12]=0;
inPattern[2][13]=1;
inPattern[2][14]=0;
inPattern[2][16]=0;
inPattern[2][17]=0;
inPattern[2][18]=0;
inPattern[2][19]=0;
inPattern[2][21]=0;
inPattern[2][22]=0;
inPattern[2][23]=0;
inPattern[2][24]=0;
inPattern[2][26]=1;
inPattern[2][27]=0;
inPattern[2][28]=0;
inPattern[2][29]=0;
inPattern[2][31]=0;
inPattern[2][32]=1;
inPattern[2][33]=0;
inPattern[2][34]=0;
inPattern[2][36]=0;
inPattern[2][37]=0;
inPattern[2][38]=1;
inPattern[2][39]=0;
inPattern[2][41]=0;
inPattern[2][42]=0;
inPattern[2][43]=0;
inPattern[2][44]=1;
inPattern[2][46]=1;
inPattern[2][47]=1;
inPattern[2][48]=1;
inPattern[2][49]=0;
//card 3
inPattern[3][1]=0;
inPattern[3][2]=0;
inPattern[3][3]=1;
inPattern[3][4]=1;
inPattern[3][6]=0;
inPattern[3][7]=0;
inPattern[3][8]=0;
inPattern[3][9]=1;
inPattern[3][11]=0;
inPattern[3][12]=0;
inPattern[3][13]=1;
inPattern[3][14]=0;
inPattern[3][16]=0;
inPattern[3][17]=0;
inPattern[3][18]=0;
inPattern[3][19]=0;
inPattern[3][21]=0;
inPattern[3][22]=0;
inPattern[3][23]=0;
inPattern[3][24]=0;
inPattern[3][26]=1;
inPattern[3][27]=0;
inPattern[3][28]=0;
inPattern[3][29]=0;
inPattern[3][31]=0;
inPattern[3][32]=0;
inPattern[3][33]=0;
inPattern[3][34]=1;
inPattern[3][36]=0;
inPattern[3][37]=1;
inPattern[3][38]=0;
inPattern[3][39]=0;
inPattern[3][41]=1;
inPattern[3][42]=0;
inPattern[3][43]=0;
inPattern[3][44]=0;
inPattern[3][46]=1;
inPattern[3][47]=1;
inPattern[3][48]=0;
inPattern[3][49]=0;
//card 4
inPattern[4][1]=0;
inPattern[4][2]=1;
inPattern[4][3]=0;
inPattern[4][4]=0;
inPattern[4][6]=1;
inPattern[4][7]=0;
inPattern[4][8]=0;
inPattern[4][9]=1;
inPattern[4][11]=0;
inPattern[4][12]=0;
inPattern[4][13]=1;
inPattern[4][14]=0;
inPattern[4][16]=1;
inPattern[4][17]=0;
inPattern[4][18]=0;
inPattern[4][19]=0;
inPattern[4][21]=0;
inPattern[4][22]=0;
inPattern[4][23]=1;
inPattern[4][24]=1;

inPattern[1][5]=0;
inPattern[1][10]=1;
inPattern[1][15]=0;
inPattern[1][20]=1;
inPattern[1][25]=1;
inPattern[1][30]=1;
inPattern[1][35]=0;
inPattern[1][40]=0;
inPattern[1][45]=0;

inPattern[2][5]=1;
inPattern[2][10]=0;
inPattern[2][15]=0;
inPattern[2][20]=1;
inPattern[2][25]=0;
inPattern[2][30]=0;
inPattern[2][35]=0;
inPattern[2][40]=0;
inPattern[2][45]=1;

inPattern[3][5]=1;
inPattern[3][10]=0;
inPattern[3][15]=0;
inPattern[3][20]=1;
inPattern[3][25]=1;
inPattern[3][30]=0;
inPattern[3][35]=0;
inPattern[3][40]=0;
inPattern[3][45]=1;

inPattern[4][5]=0;
inPattern[4][10]=0;
inPattern[4][15]=0;
inPattern[4][20]=1;
inPattern[4][25]=1;

*c
c

inPattern[4][26]=1;
inPattern[4][31]=0;
inPattern[4][36]=0;
inPattern[4][41]=1;
inPattern[4][46]=0;
//card 5
inPattern[5][1]=0;
inPattern[5][6]=1;
inPattern[5][11]=0;
inPattern[5][16]=1;
inPattern[5][21]=0;
inPattern[5][26]=0;
inPattern[5][31]=0;
inPattern[5][36]=0;
inPattern[5][41]=1;
inPattern[5][46]=1;
//card 6
inPattern[6][1]=0;
inPattern[6][6]=1;
inPattern[6][11]=0;
inPattern[6][16]=1;
inPattern[6][21]=0;
inPattern[6][26]=1;
inPattern[6][31]=0;
inPattern[6][36]=0;
inPattern[6][41]=1;
inPattern[6][46]=1;
//card 7
inPattern[7][1]=0;
inPattern[7][6]=1;
inPattern[7][11]=0;
inPattern[7][16]=0;
inPattern[7][21]=0;
inPattern[7][26]=1;
inPattern[7][31]=0;
inPattern[7][36]=0;
inPattern[7][41]=0;
inPattern[7][46]=0;
//card 8
inPattern[8][1]=0;
inPattern[8][6]=0;
inPattern[8][11]=0;
inPattern[8][16]=1;
inPattern[8][21]=0;
inPattern[8][26]=1;
inPattern[8][31]=0;
inPattern[8][36]=0;
inPattern[8][41]=1;
inPattern[8][46]=1;

inPattern[4][27]=1;
inPattern[4][32]=0;
inPattern[4][37]=0;
inPattern[4][42]=0;
inPattern[4][47]=0;

inPattern[4][28]=0;
inPattern[4][33]=0;
inPattern[4][38]=0;
inPattern[4][43]=0;
inPattern[4][48]=1;

inPattern[4][29]=0;
inPattern[4][34]=1;
inPattern[4][39]=0;
inPattern[4][44]=0;
inPattern[4][49]=0;

inPattern[4][30]=0;
inPattern[4][35]=0;
inPattern[4][40]=0;
inPattern[4][45]=0;

inPattern[5][2]=1;
inPattern[5][7]=0;
inPattern[5][12]=0;
inPattern[5][17]=1;
inPattern[5][22]=0;
inPattern[5][27]=1;
inPattern[5][32]=0;
inPattern[5][37]=1;
inPattern[5][42]=0;
inPattern[5][47]=1;

inPattern[5][3]=1;
inPattern[5][8]=0;
inPattern[5][13]=0;
inPattern[5][18]=1;
inPattern[5][23]=0;
inPattern[5][28]=0;
inPattern[5][33]=0;
inPattern[5][38]=0;
inPattern[5][43]=0;
inPattern[5][48]=0;

inPattern[5][4]=1;
inPattern[5][9]=1;
inPattern[5][14]=0;
inPattern[5][19]=1;
inPattern[5][24]=0;
inPattern[5][29]=0;
inPattern[5][34]=1;
inPattern[5][39]=0;
inPattern[5][44]=0;
inPattern[5][49]=0;

inPattern[5][5]=1;
inPattern[5][10]=0;
inPattern[5][15]=0;
inPattern[5][20]=0;
inPattern[5][25]=0;
inPattern[5][30]=0;
inPattern[5][35]=0;
inPattern[5][40]=0;
inPattern[5][45]=1;

inPattern[6][2]=1;
inPattern[6][7]=0;
inPattern[6][12]=0;
inPattern[6][17]=0;
inPattern[6][22]=0;
inPattern[6][27]=1;
inPattern[6][32]=0;
inPattern[6][37]=1;
inPattern[6][42]=0;
inPattern[6][47]=1;

inPattern[6][3]=1;
inPattern[6][8]=0;
inPattern[6][13]=0;
inPattern[6][18]=0;
inPattern[6][23]=1;
inPattern[6][28]=0;
inPattern[6][33]=0;
inPattern[6][38]=0;
inPattern[6][43]=0;
inPattern[6][48]=1;

inPattern[6][4]=1;
inPattern[6][9]=1;
inPattern[6][14]=0;
inPattern[6][19]=0;
inPattern[6][24]=1;
inPattern[6][29]=0;
inPattern[6][34]=1;
inPattern[6][39]=0;
inPattern[6][44]=1;
inPattern[6][49]=0;

inPattern[6][5]=1;
inPattern[6][10]=0;
inPattern[6][15]=0;
inPattern[6][20]=0;
inPattern[6][25]=1;
inPattern[6][30]=1;
inPattern[6][35]=0;
inPattern[6][40]=0;
inPattern[6][45]=1;

inPattern[7][2]=1;
inPattern[7][7]=0;
inPattern[7][12]=0;
inPattern[7][17]=0;
inPattern[7][22]=0;
inPattern[7][27]=0;
inPattern[7][32]=1;
inPattern[7][37]=0;
inPattern[7][42]=0;
inPattern[7][47]=0;

inPattern[7][3]=1;
inPattern[7][8]=0;
inPattern[7][13]=1;
inPattern[7][18]=0;
inPattern[7][23]=0;
inPattern[7][28]=0;
inPattern[7][33]=0;
inPattern[7][38]=1;
inPattern[7][43]=0;
inPattern[7][48]=0;

inPattern[7][4]=1;
inPattern[7][9]=0;
inPattern[7][14]=0;
inPattern[7][19]=0;
inPattern[7][24]=0;
inPattern[7][29]=0;
inPattern[7][34]=0;
inPattern[7][39]=0;
inPattern[7][44]=0;
inPattern[7][49]=0;

inPattern[7][5]=1;
inPattern[7][10]=0;
inPattern[7][15]=0;
inPattern[7][20]=1;
inPattern[7][25]=0;
inPattern[7][30]=0;
inPattern[7][35]=0;
inPattern[7][40]=0;
inPattern[7][45]=1;

inPattern[8][2]=0;
inPattern[8][7]=0;
inPattern[8][12]=0;
inPattern[8][17]=0;
inPattern[8][22]=0;
inPattern[8][27]=0;
inPattern[8][32]=0;
inPattern[8][37]=1;
inPattern[8][42]=0;
inPattern[8][47]=1;

inPattern[8][3]=1;
inPattern[8][8]=0;
inPattern[8][13]=1;
inPattern[8][18]=0;
inPattern[8][23]=0;
inPattern[8][28]=0;
inPattern[8][33]=0;
inPattern[8][38]=0;
inPattern[8][43]=0;
inPattern[8][48]=0;

inPattern[8][4]=1;
inPattern[8][9]=1;
inPattern[8][14]=0;
inPattern[8][19]=0;
inPattern[8][24]=1;
inPattern[8][29]=0;
inPattern[8][34]=1;
inPattern[8][39]=0;
inPattern[8][44]=0;
inPattern[8][49]=0;

inPattern[8][5]=1;
inPattern[8][10]=0;
inPattern[8][15]=0;
inPattern[8][20]=1;
inPattern[8][25]=1;
inPattern[8][30]=1;
inPattern[8][35]=0;
inPattern[8][40]=0;
inPattern[8][45]=1;

Ac
c

//card 9
inPattern[9][1]=0;
inPattern[9][6]=0;
inPattern[9][11]=0;
inPattern[9][16]=1;
inPattern[9][21]=0;
inPattern[9][26]=1;
inPattern[9][31]=0;
inPattern[9][36]=0;
inPattern[9][41]=1;
inPattern[9][46]=1;
//card 10
inPattern[10][1]=0;
inPattern[10][6]=1;
inPattern[10][11]=1;
inPattern[10][16]=1;
inPattern[10][21]=1;
inPattern[10][26]=0;
inPattern[10][31]=0;
inPattern[10][36]=0;
inPattern[10][41]=0;
inPattern[10][46]=0;
//card Jack
inPattern[11][1]=0;
inPattern[11][6]=1;
inPattern[11][11]=1;
inPattern[11][16]=0;
inPattern[11][21]=0;
inPattern[11][26]=0;
inPattern[11][31]=0;
inPattern[11][36]=0;
inPattern[11][41]=0;
inPattern[11][46]=0;
//card Queen
inPattern[12][1]=0;
inPattern[12][6]=0;
inPattern[12][11]=0;
inPattern[12][16]=1;
inPattern[12][21]=0;
inPattern[12][26]=0;
inPattern[12][31]=0;
inPattern[12][36]=0;
inPattern[12][41]=1;
inPattern[12][46]=1;
//card King
inPattern[13][1]=0;
inPattern[13][6]=1;
inPattern[13][11]=0;
inPattern[13][16]=1;

inPattern[9][2]=0;
inPattern[9][7]=0;
inPattern[9][12]=0;
inPattern[9][17]=0;
inPattern[9][22]=0;
inPattern[9][27]=1;
inPattern[9][32]=0;
inPattern[9][37]=1;
inPattern[9][42]=0;
inPattern[9][47]=1;

inPattern[9][3]=1;
inPattern[9][8]=0;
inPattern[9][13]=1;
inPattern[9][18]=0;
inPattern[9][23]=0;
inPattern[9][28]=0;
inPattern[9][33]=0;
inPattern[9][38]=0;
inPattern[9][43]=0;
inPattern[9][48]=0;

inPattern[9][4]=1;
inPattern[9][9]=1;
inPattern[9][14]=0;
inPattern[9][19]=0;
inPattern[9][24]=1;
inPattern[9][29]=0;
inPattern[9][34]=1;
inPattern[9][39]=0;
inPattern[9][44]=0;
inPattern[9][49]=0;

inPattern[9][5]=1;
inPattern[9][10]=0;
inPattern[9][15]=0;
inPattern[9][20]=1;
inPattern[9][25]=1;
inPattern[9][30]=0;
inPattern[9][35]=0;
inPattern[9][40]=0;
inPattern[9][45]=1;

inPattern[10][2]=1;
inPattern[10][7]=0;
inPattern[10][12]=0;
inPattern[10][17]=0;
inPattern[10][22]=0;
inPattern[10][27]=0;
inPattern[10][32]=1;
inPattern[10][37]=1;
inPattern[10][42]=1;
inPattern[10][47]=1;

inPattern[10][3]=0;
inPattern[10][8]=1;
inPattern[10][13]=0;
inPattern[10][18]=1;
inPattern[10][23]=1;
inPattern[10][28]=1;
inPattern[10][33]=0;
inPattern[10][38]=0;
inPattern[10][43]=0;
inPattern[10][48]=1;

inPattern[10][4]=0;
inPattern[10][9]=1;
inPattern[10][14]=1;
inPattern[10][19]=0;
inPattern[10][24]=0;
inPattern[10][29]=0;
inPattern[10][34]=0;
inPattern[10][39]=1;
inPattern[10][44]=1;
inPattern[10][49]=0;

inPattern[10][5]=1;
inPattern[10][10]=0;
inPattern[10][15]=0;
inPattern[10][20]=0;
inPattern[10][25]=1;
inPattern[10][30]=1;
inPattern[10][35]=1;
inPattern[10][40]=0;
inPattern[10][45]=0;

inPattern[11][2]=1;
inPattern[11][7]=0;
inPattern[11][12]=0;
inPattern[11][17]=0;
inPattern[11][22]=0;
inPattern[11][27]=0;
inPattern[11][32]=1;
inPattern[11][37]=1;
inPattern[11][42]=0;
inPattern[11][47]=0;

inPattern[11][3]=1;
inPattern[11][8]=0;
inPattern[11][13]=0;
inPattern[11][18]=1;
inPattern[11][23]=0;
inPattern[11][28]=0;
inPattern[11][33]=0;
inPattern[11][38]=0;
inPattern[11][43]=0;
inPattern[11][48]=0;

inPattern[11][4]=1; inPattern[11][5]=1;
inPattern[11][9]=0; inPattern[11][10]=0;
inPattern[11][14]=0; inPattern[11][15]=0;
inPattern[11][19]=0; inPattern[11][20]=0;
inPattern[11][24]=0; inPattern[11][25]=1;
inPattern[11][29]=0; inPattern[11][30]=1;
inPattern[11][34]=0; inPattern[11][35]=0;
inPattern[11][39]=1; inPattern[11][40]=0;
inPattern[11][44]=0; inPattern[11][45]=1;
inPattern[11][49]=0;

inPattern[12][2]=0;
inPattern[12][7]=0;
inPattern[12][12]=0;
inPattern[12][17]=0;
inPattern[12][22]=0;
inPattern[12][27]=1;
inPattern[12][32]=1;
inPattern[12][37]=1;
inPattern[12][42]=0;
inPattern[12][47]=1;

inPattern[12][3]=1;
inPattern[12][8]=0;
inPattern[12][13]=1;
inPattern[12][18]=0;
inPattern[12][23]=1;
inPattern[12][28]=0;
inPattern[12][33]=0;
inPattern[12][38]=0;
inPattern[12][43]=0;
inPattern[12][48]=1;

inPattern[12][4]=1;
inPattern[12][9]=1;
inPattern[12][14]=0;
inPattern[12][19]=0;
inPattern[12][24]=0;
inPattern[12][29]=0;
inPattern[12][34]=1;
inPattern[12][39]=0;
inPattern[12][44]=0;
inPattern[12][49]=0;

inPattern[13][2]=1;
inPattern[13][7]=0;
inPattern[13][12]=1;
inPattern[13][17]=0;

inPattern[13][3]=0;
inPattern[13][8]=0;
inPattern[13][13]=0;
inPattern[13][18]=1;

inPattern[13][4]=0;
inPattern[13][9]=1;
inPattern[13][14]=0;
inPattern[13][19]=0;

inPattern[12][5]=1;
inPattern[12][10]=0;
inPattern[12][15]=0;
inPattern[12][20]=1;
inPattern[12][25]=0;
inPattern[12][30]=1;
inPattern[12][35]=0;
inPattern[12][40]=1;
inPattern[12][45]=1;

inPattern[13][5]=0;
inPattern[13][10]=0;
inPattern[13][15]=0;
inPattern[13][20]=0;

Gc
c

inPattern[13][21]=0;
inPattern[13][26]=0;
inPattern[13][31]=0;
inPattern[13][36]=0;
inPattern[13][41]=0;
inPattern[13][46]=0;

inPattern[13][22]=0;
inPattern[13][27]=0;
inPattern[13][32]=1;
inPattern[13][37]=1;
inPattern[13][42]=0;
inPattern[13][47]=0;

inPattern[13][23]=1;
inPattern[13][28]=0;
inPattern[13][33]=0;
inPattern[13][38]=0;
inPattern[13][43]=0;
inPattern[13][48]=1;

inPattern[13][24]=1;
inPattern[13][29]=0;
inPattern[13][34]=0;
inPattern[13][39]=0;
inPattern[13][44]=1;
inPattern[13][49]=0;

inPattern[13][25]=0;
inPattern[13][30]=1;
inPattern[13][35]=0;
inPattern[13][40]=1;
inPattern[13][45]=0;

// the training target output patterns


//card Ace
targetOut[1][1]=1;targetOut[1][2]=0;targetOut[1][3]=0;targetOut[1][4]=0;targetOut[1][5]=0;
targetOut[1][6]=0;targetOut[1][7]=0;targetOut[1][8]=0;targetOut[1][9]=0;targetOut[1][10]=0;
targetOut[1][11]=0;targetOut[1][12]=0;targetOut[1][13]=0;
//card 2
targetOut[2][1]=0;targetOut[2][2]=1;targetOut[2][3]=0;targetOut[2][4]=0;targetOut[2][5]=0;
targetOut[2][6]=0;targetOut[2][7]=0;targetOut[2][8]=0;targetOut[2][9]=0;targetOut[2][10]=0;
targetOut[2][11]=0;targetOut[2][12]=0;targetOut[2][13]=0;
//card 3
targetOut[3][1]=0;targetOut[3][2]=0;targetOut[3][3]=1;targetOut[3][4]=0;targetOut[3][5]=0;
targetOut[3][6]=0;targetOut[3][7]=0;targetOut[3][8]=0;targetOut[3][9]=0;targetOut[3][10]=0;
targetOut[3][11]=0;targetOut[3][12]=0;targetOut[3][13]=0;
//card 4
targetOut[4][1]=0;targetOut[4][2]=0;targetOut[4][3]=0;targetOut[4][4]=1;targetOut[4][5]=0;
targetOut[4][6]=0;targetOut[4][7]=0;targetOut[4][8]=0;targetOut[4][9]=0;targetOut[4][10]=0;
targetOut[4][11]=0;targetOut[4][12]=0;targetOut[4][13]=0;
//card 5
targetOut[5][1]=0;targetOut[5][2]=0;targetOut[5][3]=0;targetOut[5][4]=0;targetOut[5][5]=1;
targetOut[5][6]=0;targetOut[5][7]=0;targetOut[5][8]=0;targetOut[5][9]=0;targetOut[5][10]=0;
targetOut[5][11]=0;targetOut[5][12]=0;targetOut[5][13]=0;
//card 6
targetOut[6][1]=0;targetOut[6][2]=0;targetOut[6][3]=0;targetOut[6][4]=0;targetOut[6][5]=0;
targetOut[6][6]=1;targetOut[6][7]=0;targetOut[6][8]=0;targetOut[6][9]=0;targetOut[6][10]=0;
targetOut[6][11]=0;targetOut[6][12]=0;targetOut[6][13]=0;
//card 7
targetOut[7][1]=0;targetOut[7][2]=0;targetOut[7][3]=0;targetOut[7][4]=0;targetOut[7][5]=0;
targetOut[7][6]=0;targetOut[7][7]=1;targetOut[7][8]=0;targetOut[7][9]=0;targetOut[7][10]=0;
targetOut[7][11]=0;targetOut[7][12]=0;targetOut[7][13]=0;
//card 8
targetOut[8][1]=0;targetOut[8][2]=0;targetOut[8][3]=0;targetOut[8][4]=0;targetOut[8][5]=0;
targetOut[8][6]=0;targetOut[8][7]=0;targetOut[8][8]=1;targetOut[8][9]=0;targetOut[8][10]=0;
targetOut[8][11]=0;targetOut[8][12]=0;targetOut[8][13]=0;
//card 9
targetOut[9][1]=0;targetOut[9][2]=0;targetOut[9][3]=0;targetOut[9][4]=0;targetOut[9][5]=0;
targetOut[9][6]=0;targetOut[9][7]=0;targetOut[9][8]=0;targetOut[9][9]=1;targetOut[9][10]=0;
targetOut[9][11]=0;targetOut[9][12]=0;targetOut[9][13]=0;
//card 10
targetOut[10][1]=0;targetOut[10][2]=0;targetOut[10][3]=0;targetOut[10][4]=0;targetOut[10][5]=0;
targetOut[10][6]=0;targetOut[10][7]=0;targetOut[10][8]=0;targetOut[10][9]=0;targetOut[10][10]=1;
targetOut[10][11]=0;targetOut[10][12]=0;targetOut[10][13]=0;
//card Jack

Jc
c

targetOut[11][1]=0;targetOut[11][2]=0;targetOut[11][3]=0;targetOut[11][4]=0;targetOut[11][5]=0;
targetOut[11][6]=0;targetOut[11][7]=0;targetOut[11][8]=0;targetOut[11][9]=0;targetOut[11][10]=0;
targetOut[11][11]=1;targetOut[11][12]=0;targetOut[11][13]=0;
//card Queen
targetOut[12][1]=0;targetOut[12][2]=0;targetOut[12][3]=0;targetOut[12][4]=0;targetOut[12][5]=0;
targetOut[12][6]=0;targetOut[12][7]=0;targetOut[12][8]=0;targetOut[12][9]=0;targetOut[12][10]=0;
targetOut[12][11]=0;targetOut[12][12]=1;targetOut[12][13]=0;
//card King
targetOut[13][1]=0;targetOut[13][2]=0;targetOut[13][3]=0;targetOut[13][4]=0;targetOut[13][5]=0;
targetOut[13][6]=0;targetOut[13][7]=0;targetOut[13][8]=0;targetOut[13][9]=0;targetOut[13][10]=0;
targetOut[13][11]=0;targetOut[13][12]=0;targetOut[13][13]=1;
for(int i=1;i<hiddenN;i++){
for(int j=1;j<inputN;j++){
wInToHid[i][j]=(MathUtil.boundedRandom(-1,1));
}
}
for(int i=1;i<outputN;i++){
for(int j=1;j<hiddenN;j++){
wHidToOut[i][j]=(MathUtil.boundedRandom(-1,1));
}
}
do{
epoch++;
for(int n=1;n<sampleN;n++){
//net input to hidden neurons
hidIn=MathUtil.initialize(hidIn);
for(int i=1;i<hiddenN;i++){
for(int j=1;j<inputN;j++){
hidIn[i]+=inPattern[n][j]*wInToHid[i][j];
}
}
//outputs of hidden neurons
for(int i = 1; i<hiddenN;i++){
hidOut[i]=1/(1+Math.exp(-hidIn[i]));
}
//net input to output neurons
outIn=MathUtil.initialize(outIn);
for(int i=1;i<outputN;i++){
for(int j=1;j<hiddenN;j++){
outIn[i]+=hidOut[j]*wHidToOut[i][j];
}
}
//output of output neurons
for(int i = 1; i<outputN;i++){
outOut[n][i]=1/(1+Math.exp(-outIn[i]));
}
//error of outputs from output neurons
outError=MathUtil.initialize(outError);

ac
c

for(int i =1;i<outputN;i++){
outError[i] = (targetOut[n][i]-outOut[n][i])*(1-outOut[n][i])*outOut[n][i];
}
//error of outputs from hidden neurons
hidError=MathUtil.initialize(hidError);
for(int i=1; i<hiddenN;i++){
for(int j=1;j<outputN;j++){
hidError[i]+=hidOut[i]*(1-hidOut[i])*outError[j]*wHidToOut[j][i];
}
}
//total error
for(int i=1; i<outError.length;i++){
error+=outError[i];
}
//change the weights in output layer
for(int i=1; i<outputN; i++){
for(int j=1; j<hiddenN; j++){
new_wHidToOut[i][j]=lr*outError[i]*hidOut[j];
wHidToOut[i][j]+=new_wHidToOut[i][j];
}
}
//change the weights in hidden layer
for(int i=1; i<hiddenN; i++){
for(int j=1; j<inputN; j++){
new_wInToHid[i][j]=lr*hidError[i]*inPattern[n][j];
wInToHid[i][j]+=new_wInToHid[i][j];
}
}
}
epochsLabel.setText("Epochs: "+epoch);
errorLabel.setText("Error: "+error);
System.out.println("Epoch: "+epoch+"\nError: "+error);
}while(error<e);
JOptionPane.showMessageDialog(null, "Training successful!", "", JOptionPane.INFORMATION_MESSAGE);
recognizeButton.setEnabled(true);
}
private void recognizeButtonActionPerformed(java.awt.event.ActionEvent evt) {
input[1]=x1;input[2]=x2;input[3]=x3;input[4]=x4;input[5]=x5;input[6]=x6;input[7]=x7;
input[8]=x8;input[9]=x9;input[10]=x10;input[11]=x11;input[12]=x12;input[13]=x13;input[14]=x14;
input[15]=x15;input[16]=x16;input[17]=x17;input[18]=x18;input[19]=x19;input[20]=x20;input[21]=x21;
input[22]=x22;input[23]=x23;input[24]=x24;input[25]=x25;input[26]=x26;input[27]=x27;input[28]=x28;
input[29]=x29;input[30]=x30;input[31]=x31;input[32]=x32;input[33]=x33;input[34]=x34;input[35]=x35;
input[36]=x36;input[37]=x37;input[38]=x38;input[39]=x39;input[40]=x40;input[41]=x41;input[42]=x42;
input[43]=x43;input[44]=x44;input[45]=x45;input[46]=x46;input[47]=x47;input[48]=x48;input[49]=x49;
// get the actual network output
hidIn=MathUtil.initialize(hidIn);
outIn=MathUtil.initialize(outIn);
for(int i=1;i<hiddenN;i++){

Xc
c

for(int j=1;j<inputN;j++){
hidIn[i]+=input[j]*wInToHid[i][j];
}
}
for(int i = 1; i<hiddenN;i++){
hidOut[i]=1/(1+Math.exp(-hidIn[i]));
}
for(int i=1;i<outputN;i++){
for(int j=1;j<hiddenN;j++){
outIn[i]+=hidOut[j]*wHidToOut[i][j];
}
}
for(int i = 1; i<outputN;i++){
output[i]=1/(1+Math.exp(-outIn[i]));
}
// find the maximum from output
maxIndex = 1;
max = output[0];
for (int i = 1; i<output.length; i++){
if (output[i] > max){
max = output[i];
maxIndex = i;
outputTextfield1.setText(outputText1[i]);
}
}
outputTextfield2.setText(outputText2);
}
private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
b1.setBackground(Color.CYAN);
x1 = 0;
if (x1==0){x1=1;}
else{x1=0;}
}
private void b2ActionPerformed(java.awt.event.ActionEvent evt) {
b2.setBackground(Color.CYAN);
x2 = 0;
if (x2==0){x2=1;}
else{x2=0;}
}
private void b3ActionPerformed(java.awt.event.ActionEvent evt) {
b3.setBackground(Color.CYAN);
x3 = 0;
if (x3==0){x3=1;}
else{x3=0;}
}
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
b4.setBackground(Color.CYAN);
x4 = 0;
if (x4==0){x4=1;}

3 c
c

else{x4=0;}
}
private void b5ActionPerformed(java.awt.event.ActionEvent evt) {
b5.setBackground(Color.CYAN);
x5 = 0;
if (x5==0){x5=1;}
else{x5=0;}
}
private void b6ActionPerformed(java.awt.event.ActionEvent evt) {
b6.setBackground(Color.CYAN);
x6 = 0;
if (x6==0){x6=1;}
else{x6=0;}
}
private void b7ActionPerformed(java.awt.event.ActionEvent evt) {
b7.setBackground(Color.CYAN);
x7 = 0;
if (x7==0){x7=1;}
else{x7=0;}
}
private void b8ActionPerformed(java.awt.event.ActionEvent evt) {
b8.setBackground(Color.CYAN);
x8 = 0;
if (x8==0){x8=1;}
else{x8=0;}
}
private void b9ActionPerformed(java.awt.event.ActionEvent evt) {
b9.setBackground(Color.CYAN);
x9 = 0;
if (x9==0){x9=1;}
else{x9=0;}
}
private void b10ActionPerformed(java.awt.event.ActionEvent evt) {
b10.setBackground(Color.CYAN);
x10 = 0;
if (x10==0){x10=1;}
else{x10=0;}
}
private void b11ActionPerformed(java.awt.event.ActionEvent evt) {
b11.setBackground(Color.CYAN);
x11 = 0;
if (x11==0){x11=1;}
else{x11=0;}
}
private void b12ActionPerformed(java.awt.event.ActionEvent evt) {
b12.setBackground(Color.CYAN);
x12 = 0;
if (x12==0){x12=1;}
else{x12=0;}

33c
c

}
private void b13ActionPerformed(java.awt.event.ActionEvent evt) {
b13.setBackground(Color.CYAN);
x13 = 0;
if (x13==0){x13=1;}
else{x13=0;}
}
private void b14ActionPerformed(java.awt.event.ActionEvent evt) {
b14.setBackground(Color.CYAN);
x14 = 0;
if (x14==0){x14=1;}
else{x14=0;}
}
private void b15ActionPerformed(java.awt.event.ActionEvent evt) {
b15.setBackground(Color.CYAN);
x15 = 0;
if (x15==0){x15=1;}
else{x15=0;}
}
private void b16ActionPerformed(java.awt.event.ActionEvent evt) {
b16.setBackground(Color.CYAN);
x16 = 0;
if (x16==0){x16=1;}
else{x16=0;}
}
private void b17ActionPerformed(java.awt.event.ActionEvent evt) {
b17.setBackground(Color.CYAN);
x17 = 0;
if (x17==0){x17=1;}
else{x17=0;}
}
private void b18ActionPerformed(java.awt.event.ActionEvent evt) {
b18.setBackground(Color.CYAN);
x18 = 0;
if (x18==0){x18=1;}
else{x18=0;}
}
private void b19ActionPerformed(java.awt.event.ActionEvent evt) {
b19.setBackground(Color.CYAN);
x19 = 0;
if (x19==0){x19=1;}
else{x19=0;}
}
private void b20ActionPerformed(java.awt.event.ActionEvent evt) {
b20.setBackground(Color.CYAN);
x20 = 0;
if (x20==0){x20=1;}
else{x20=0;}
}

3^c
c

private void b21ActionPerformed(java.awt.event.ActionEvent evt) {


b21.setBackground(Color.CYAN);
x21 = 0;
if (x21==0){x21=1;}
else{x21=0;}
}
private void b22ActionPerformed(java.awt.event.ActionEvent evt) {
b22.setBackground(Color.CYAN);
x22 = 0;
if (x22==0){x22=1;}
else{x22=0;}
}
private void b23ActionPerformed(java.awt.event.ActionEvent evt) {
b23.setBackground(Color.CYAN);
x23 = 0;
if (x23==0){x23=1;}
else{x23=0;}
}
private void b24ActionPerformed(java.awt.event.ActionEvent evt) {
b24.setBackground(Color.CYAN);
x24 = 0;
if (x24==0){x24=1;}
else{x24=0;}
}
private void b25ActionPerformed(java.awt.event.ActionEvent evt) {
b25.setBackground(Color.CYAN);
x25 = 0;
if (x25==0){x25=1;}
else{x25=0;}
}
private void b26ActionPerformed(java.awt.event.ActionEvent evt) {
b26.setBackground(Color.CYAN);
x26 = 0;
if (x26==0){x26=1;}
else{x26=0;}
}
private void b27ActionPerformed(java.awt.event.ActionEvent evt) {
b27.setBackground(Color.CYAN);
x27 = 0;
if (x27==0){x27=1;}
else{x27=0;}
}
private void b28ActionPerformed(java.awt.event.ActionEvent evt) {
b28.setBackground(Color.CYAN);
x28 = 0;
if (x28==0){x28=1;}
else{x28=0;}
}

3c
c

private void b29ActionPerformed(java.awt.event.ActionEvent evt) {


b29.setBackground(Color.CYAN);
x29 = 0;
if (x29==0){x29=1;}
else{x29=0;}
}
private void b30ActionPerformed(java.awt.event.ActionEvent evt) {
b30.setBackground(Color.CYAN);
x30 = 0;
if (x30==0){x30=1;}
else{x30=0;}
}
private void b31ActionPerformed(java.awt.event.ActionEvent evt) {
b31.setBackground(Color.CYAN);
x31 = 0;
if (x31==0){x31=1;}
else{x31=0;}
}
private void b32ActionPerformed(java.awt.event.ActionEvent evt) {
b32.setBackground(Color.CYAN);
x32 = 0;
if (x32==0){x32=1;}
else{x32=0;}
}
private void b33ActionPerformed(java.awt.event.ActionEvent evt) {
b33.setBackground(Color.CYAN);
x33 = 0;
if (x33==0){x33=1;}
else{x33=0;}
}
private void b34ActionPerformed(java.awt.event.ActionEvent evt) {
b34.setBackground(Color.CYAN);
x34 = 0;
if (x34==0){x34=1;}
else{x34=0;}
}
private void b35ActionPerformed(java.awt.event.ActionEvent evt) {
b35.setBackground(Color.CYAN);
x35 = 0;
if (x35==0){x35=1;}
else{x35=0;}
}
private void b36ActionPerformed(java.awt.event.ActionEvent evt) {
b36.setBackground(Color.CYAN);
x36 = 0;
if (x36==0){x36=1;}
else{x36=0;}
}
private void b37ActionPerformed(java.awt.event.ActionEvent evt) {

3*c
c

b37.setBackground(Color.CYAN);
x37 = 0;
if (x37==0){x37=1;}
else{x37=0;}
}
private void b38ActionPerformed(java.awt.event.ActionEvent evt) {
b38.setBackground(Color.CYAN);
x38 = 0;
if (x38==0){x38=1;}
else{x38=0;}
}
private void b39ActionPerformed(java.awt.event.ActionEvent evt) {
b39.setBackground(Color.CYAN);
x39 = 0;
if (x39==0){x39=1;}
else{x39=0;}
}
private void b40ActionPerformed(java.awt.event.ActionEvent evt) {
b40.setBackground(Color.CYAN);
x40 = 0;
if (x40==0){x40=1;}
else{x40=0;}
}
private void b41ActionPerformed(java.awt.event.ActionEvent evt) {
b41.setBackground(Color.CYAN);
x41 = 0;
if (x41==0){x41=1;}
else{x41=0;}
}
private void b42ActionPerformed(java.awt.event.ActionEvent evt) {
b42.setBackground(Color.CYAN);
x42 = 0;
if (x42==0){x42=1;}
else{x42=0;}
}
private void b43ActionPerformed(java.awt.event.ActionEvent evt) {
b43.setBackground(Color.CYAN);
x43 = 0;
if (x43==0){x43=1;}
else{x43=0;}
}
private void b44ActionPerformed(java.awt.event.ActionEvent evt) {
b44.setBackground(Color.CYAN);
x44 = 0;
if (x44==0){x44=1;}
else{x44=0;}
}
private void b45ActionPerformed(java.awt.event.ActionEvent evt) {
b45.setBackground(Color.CYAN);

3Ac
c

x45 = 0;
if (x45==0){x45=1;}
else{x45=0;}
}
private void b46ActionPerformed(java.awt.event.ActionEvent evt) {
b46.setBackground(Color.CYAN);
x46 = 0;
if (x46==0){x46=1;}
else{x46=0;}
}
private void b47ActionPerformed(java.awt.event.ActionEvent evt) {
b47.setBackground(Color.CYAN);
x47 = 0;
if (x47==0){x47=1;}
else{x47=0;}
}
private void b48ActionPerformed(java.awt.event.ActionEvent evt) {
b48.setBackground(Color.CYAN);
x48 = 0;
if (x48==0){x48=1;}
else{x48=0;}
}
private void b49ActionPerformed(java.awt.event.ActionEvent evt) {
b49.setBackground(Color.CYAN);
x49 = 0;
if (x49==0){x49=1;}
else{x49=0;}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DeckOfCards().setVisible(true);
}
});
}
// Variables declaration - do not modify
private java.awt.Button b1; private java.awt.Button b10; private java.awt.Button b11; private java.awt.Button b12;
private java.awt.Button b13; private java.awt.Button b14; private java.awt.Button b15; private java.awt.Button b16;
private java.awt.Button b17; private java.awt.Button b18; private java.awt.Button b19; private java.awt.Button b2;
private java.awt.Button b20; private java.awt.Button b21; private java.awt.Button b22; private java.awt.Button b23;
private java.awt.Button b24; private java.awt.Button b25; private java.awt.Button b26; private java.awt.Button b27;
private java.awt.Button b28; private java.awt.Button b29; private java.awt.Button b3; private java.awt.Button b30;
private java.awt.Button b31; private java.awt.Button b32; private java.awt.Button b33; private java.awt.Button b34;
private java.awt.Button b35; private java.awt.Button b36; private java.awt.Button b37; private java.awt.Button b38;
private java.awt.Button b39; private java.awt.Button b4; private java.awt.Button b40; private java.awt.Button b41;
private java.awt.Button b42; private java.awt.Button b43; private java.awt.Button b44; private java.awt.Button b45;
private java.awt.Button b46; private java.awt.Button b47; private java.awt.Button b48; private java.awt.Button b49;
private java.awt.Button b5; private java.awt.Button b6; private java.awt.Button b7; private java.awt.Button b8;

3Gc
c

private java.awt.Button b9; private javax.swing.JToggleButton button16;


private javax.swing.JToggleButton button17; private javax.swing.JToggleButton button18;
private javax.swing.JToggleButton button19; private javax.swing.JToggleButton button20;
private java.awt.Button clearButton; private javax.swing.JToggleButton clubButton;
private javax.swing.JButton clubsButton; private javax.swing.JToggleButton diamondButton;
private javax.swing.JButton diamondsButton; private javax.swing.JLabel epochsLabel;
private javax.swing.JLabel errorLabel; private javax.swing.JToggleButton heartButton;
private javax.swing.JButton heartsButton; private javax.swing.JLabel jLabel1; private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5;
private javax.swing.JTextField outputTextfield1; private javax.swing.JTextField outputTextfield2;
private java.awt.Button recognizeButton; private javax.swing.JToggleButton spadeButton;
private javax.swing.JButton spadesButton; private java.awt.Button trainButton;
// End of variables declaration

3Jc
c

You might also like