📄 algorithmlda2.java,v
字号:
head 1.8;access;symbols;locks; strict;comment @# @;1.8date 2005.06.10.16.50.15; author rirwin; state Exp;branches;next 1.7;1.7date 2005.05.23.18.42.34; author rirwin; state Exp;branches;next 1.6;1.6date 2005.04.18.20.38.07; author patil; state Exp;branches;next 1.5;1.5date 2005.03.16.20.16.22; author patil; state Exp;branches;next 1.4;1.4date 2005.03.16.19.25.54; author patil; state Exp;branches;next 1.3;1.3date 2005.03.08.02.24.43; author patil; state Exp;branches;next 1.2;1.2date 2005.01.19.22.30.44; author patil; state Exp;branches;next 1.1;1.1date 2004.12.28.00.04.32; author patil; state Exp;branches;next ;desc@No changes made.@1.8log@establishing version in rcs.@text@//--------------------------------------------------------------------------
// AlgorithmLBG.java 6.0 03/15/2005
// Created : Phil Trasatti Edited : Daniel May
// Edited : Sanjay Patil
// Last Edited : Ryan Irwin
//
// Description : Describes the LBG algorithms
// Remarks : Code unchanged since created. Created 07/15/2003
//--------------------------------------------------------------------------
//----------------------
// import java packages
//----------------------
import java.util.*;
import java.awt.*;
/**
* Implements the class dependent Linear Discriminant Algorithm
*/
public class AlgorithmLDA2 extends Algorithm
{
Vector<MyPoint> decision_regions_d;
Vector<MyPoint> support_vectors_d;
int output_canvas_d[][];
// declare local matrix objects
Matrix W;
Matrix B1;
Matrix B2;
Matrix B3;
Matrix B4;
Matrix S1;
Matrix S2;
Matrix S3;
Matrix S4;
Matrix invW;
Matrix LDA1;
Matrix LDA2;
Matrix LDA3;
Matrix LDA4;
Matrix CLDA1; // covariance matrix for CLDA1
Matrix CLDA2; // covariance matrix for CLDA2
Matrix CLDA3; // covariance matrix for CLDA3
Matrix CLDA4; // covariance matrix for CLDA4
Matrix CLDA; // covariance matrix for CLDA
/**
* Overrides the initialize() method in the base class. Initializes
* member data and prepares for execution of first step. This method
* "resets" the algorithm.
*
* @@return Returns true
*/
public boolean initialize()
{
algo_id = "AlgorithmLDA2";
// Debug
//
// System.out.println(algo_id + ": initialize()");
step_count = 3;
point_means_d = new Vector<MyPoint>();
decision_regions_d = new Vector<MyPoint>();
support_vectors_d = new Vector<MyPoint>();
description_d = new Vector<String>();
// create local matrix objects
//
W = new Matrix();
B1 = new Matrix();
B2 = new Matrix();
B3 = new Matrix();
B4 = new Matrix();
S1 = new Matrix();
S2 = new Matrix();
S3 = new Matrix();
S4 = new Matrix();
CLDA = new Matrix();
CLDA1 = new Matrix();
CLDA2 = new Matrix();
CLDA3 = new Matrix();
CLDA4 = new Matrix();
invW = new Matrix();
// Add the process description for the LDA algorithm
//
if (description_d.size() == 0)
{
String str = new String(" 0. Initialize the original data.");
description_d.addElement(str);
str = new String(" 1. Displaying the original data.");
description_d.addElement(str);
str = new String(" 2. Computing the means and covariance.");
description_d.addElement(str);
str = new String(" 3. Computing the decision regions based on the class dependent LDA algorithm.");
description_d.addElement(str);
}
// append message to process box
//
pro_box_d.appendMessage("Class Dependent LDA Analysis:" + "\n");
// set the data points for this algorithm
//
// set1_d = (Vector)data_points_d.dset1.clone();
// set2_d = (Vector)data_points_d.dset2.clone();
// set3_d = (Vector)data_points_d.dset3.clone();
// set4_d = (Vector)data_points_d.dset4.clone();
//
set1_d = data_points_d.dset1;
set2_d = data_points_d.dset2;
set3_d = data_points_d.dset3;
set4_d = data_points_d.dset4;
// set the step index
//
step_index_d = 0;
// append message to process box
//
pro_box_d.appendMessage((String)description_d.get(step_index_d));
// exit gracefully
//
return true;
}
/**
* Displays data sets from input box in output box.
*
* @@return Returns true
*/
boolean step1()
{
// Debug
//
// System.out.println(algo_id + ": step1()");
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(20);
pro_box_d.setProgressCurr(0);
// append message to process box
//
output_panel_d.addOutput(set1_d, Classify.PTYPE_INPUT,
data_points_d.color_dset1);
output_panel_d.addOutput(set2_d, Classify.PTYPE_INPUT,
data_points_d.color_dset2);
output_panel_d.addOutput(set3_d, Classify.PTYPE_INPUT,
data_points_d.color_dset3);
output_panel_d.addOutput(set4_d, Classify.PTYPE_INPUT,
data_points_d.color_dset4);
// step 1 completed
//
pro_box_d.setProgressCurr(20);
output_panel_d.repaint();
return true;
}
/**
* Calculates the within class and between class scatter matrix,
* transforms the data sets ans displays the mean graphically
* and numerically
*
* @@return Returns true
*/
boolean step2()
{
// Debug
//
// System.out.println(algo_id + ": step2()");
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(20);
pro_box_d.setProgressCurr(0);
computeMeans();
// determine the within class scatter matrix
//
withinClass(W);
// determine the between class scatter matrix
//
betweenClass1(B1, B2, B3, B4);
// determine the ratio of the between class scatter matrix
// to the within class scatter matrix
//
W.invertMatrix(invW);
if (set1_d.size() > 0)
{
invW.multMatrix(B1, S1);
}
if (set2_d.size() > 0)
{
invW.multMatrix(B2, S2);
}
if (set3_d.size() > 0)
{
invW.multMatrix(B3, S3);
}
if (set4_d.size() > 0)
{
invW.multMatrix(B4, S4);
}
// transform the samples from all data sets
//
transformLDA1(S1, S2, S3, S4);
printMatrices();
// display means
//
output_panel_d.addOutput(point_means_d,
Classify.PTYPE_OUTPUT_LARGE, Color.black);
// display support vectors
//
output_panel_d.addOutput(support_vectors_d,
Classify.PTYPE_INPUT, Color.cyan);
// display support vectors
//
pro_box_d.setProgressCurr(20);
output_panel_d.repaint();
return true;
}
/**
* Computes the decision regions and totals the data points in error,
* as well displays the decision region
*
* @@return Returns true
*/
boolean step3()
{
// Debug
//System.out.println(algo_id + ": step3()");
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(20);
pro_box_d.setProgressCurr(0);
// compute the decision regisions
//
computeDecisionRegions();
// compute errors
//
computeErrors();
// display support vectors
//
output_panel_d.addOutput( decision_regions_d,
Classify.PTYPE_INPUT,
new Color(255, 200, 0));
output_panel_d.repaint();
return true;
}
/**
* Implementation of the run function from the Runnable interface.
* Determines what the current step is and calls the appropriate method.
*/
public void run()
{
// Debug
//
// System.out.println(algo_id + " run()");
if (step_index_d == 1)
{
disableControl();
step1();
enableControl();
}
else if (step_index_d == 2)
{
disableControl();
step2();
enableControl();
}
else if (step_index_d == 3)
{
disableControl();
step3();
pro_box_d.appendMessage(" Algorithm Complete");
enableControl();
}
return;
}
/**
* Transforms a given set of points to a new space
* using the class dependent linear discrimination analysis algorithm
*
* @@param S1 covariance matrix of the first class
* @@param S2 covariance matrix of the second class
* @@param S3 covariance matrix of the third class
* @@param S4 covariance matrix of the fourth class
* @@see Matrix
*/
public void transformLDA1(Matrix S1, Matrix S2, Matrix S3, Matrix S4)
{
// declare local variables
int size = 0;
int xsize1 = 0;
int ysize1 = 0;
int xsize2 = 0;
int ysize2 = 0;
int xsize3 = 0;
int ysize3 = 0;
int xsize4 = 0;
int ysize4 = 0;
double xval1 = 0.0;
double yval1 = 0.0;
double xval2 = 0.0;
double yval2 = 0.0;
double xval3 = 0.0;
double yval3 = 0.0;
double xval4 = 0.0;
double yval4 = 0.0;
double xmean1 = 0.0;
double ymean1 = 0.0;
double xmean2 = 0.0;
double ymean2 = 0.0;
double xmean3 = 0.0;
double ymean3 = 0.0;
double xmean4 = 0.0;
double ymean4 = 0.0;
double xval = 0.0;
double yval = 0.0;
// declare arrays for the eigenvalues
double eigVal1[] = null;
double eigVal2[] = null;
double eigVal3[] = null;
double eigVal4[] = null;
// declare an array to store the eigen vectors
//
double eigVec[] = new double[2];
// compute the propabilities of each data set
//
double maxsamples = set1_d.size() + set2_d.size()
+ set3_d.size() + set4_d.size();
double p1 = set1_d.size() / maxsamples;
double p2 = set2_d.size() / maxsamples;
double p3 = set3_d.size() / maxsamples;
double p4 = set4_d.size() / maxsamples;
size = set1_d.size();
xsize1 += size;
ysize1 += size;
for (int i = 0; i < size; i++)
{
MyPoint p = (MyPoint)set1_d.elementAt(i);
xval1 += p.x;
yval1 += p.y;
}
// compute the transformation matrix for the first data set
//
if (size > 0)
{
// declare matrix objects
//
Matrix T = new Matrix();
Matrix M = new Matrix();
Matrix W = new Matrix();
// allocate memory for the matrix elements
//
T.Elem = new double[2][2];
M.Elem = new double[2][2];
W.Elem = new double[2][2];
// initialize the transformation matrix dimensions
//
W.row = 2;
W.col = 2;
// reset the matrices
//
W.resetMatrix();
// initialize the matrix needed to compute the eigenvalues
//
T.initMatrix(S1.Elem, 2, 2);
// make a copy of the original matrix
//
M.copyMatrix(T);
// compute the eigen values
//
eigVal1 = Eigen.compEigenVal(T);
// compute the eigen vectors
//
for (int i = 0; i < 2; i++)
{
Eigen.calcEigVec(M, eigVal1[i], eigVec);
for (int j = 0; j < 2; j++)
{
W.Elem[j][i] = eigVec[j];
}
}
// save the transformation matrix
//
LDA1 = W;
}
size = set2_d.size();
xsize2 += size;
ysize2 += size;
for (int i = 0; i < size; i++)
{
MyPoint p = (MyPoint)set2_d.elementAt(i);
xval2 += p.x;
yval2 += p.y;
}
// compute the transformation matrix for the second data set
//
if (size > 0)
{
// declare matrix objects
//
Matrix T = new Matrix();
Matrix M = new Matrix();
Matrix W = new Matrix();
// allocate memory for the matrix elements
//
T.Elem = new double[2][2];
M.Elem = new double[2][2];
W.Elem = new double[2][2];
// initialize the transformation matrix dimensions
//
W.row = 2;
W.col = 2;
// reset the matrices
//
W.resetMatrix();
// initialize the matrix needed to compute the eigenvalues
//
T.initMatrix(S2.Elem, 2, 2);
// make a copy of the original matrix
//
M.copyMatrix(T);
// compute the eigen values
//
eigVal2 = Eigen.compEigenVal(T);
// compute the eigen vectors
//
for (int i = 0; i < 2; i++)
{
Eigen.calcEigVec(M, eigVal2[i], eigVec);
for (int j = 0; j < 2; j++)
{
W.Elem[j][i] = eigVec[j];
}
}
// save the transformation matrix
//
LDA2 = W;
}
size = set3_d.size();
xsize3 += size;
ysize3 += size;
for (int i = 0; i < size; i++)
{
MyPoint p = (MyPoint)set3_d.elementAt(i);
xval3 += p.x;
yval3 += p.y;
}
// compute the transformation matrix for the third data set
//
if (size > 0)
{
// declare matrix objects
//
Matrix T = new Matrix();
Matrix M = new Matrix();
Matrix W = new Matrix();
// allocate memory for the matrix elements
//
T.Elem = new double[2][2];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -