📄 algorithmlbg.java_2
字号:
//----------------------------------------------------------------------
// AlgorithmLBG.java
//
// Created: 7/15/03
//
// Author: Phil Trasatti
//
// Last Edited by: Daniel May
//
// Class: AlgorithmLBG
//
//
//---------------------------------------------------------------------
//----------------------
// import java packages
//----------------------
import java.util.*;
import java.awt.*;
public class AlgorithmLBG extends Algorithm
{
//-----------------------------------------------------------------
//
// static data members
//
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//
// primitive data members
//
//-----------------------------------------------------------------
int output_canvas_d[][];
int iterations;
int currObject;
int clusters;
//-----------------------------------------------------------------
//
// instance data members
//
//-----------------------------------------------------------------
String algo_id = "AlgorithmLBG";
Vector support_vectors_d;
Vector data_pool_d;
Vector cbinary_d;
DecisionRegion decision_regions_d;
OutputPanel plain_data;
//---------------------------------------------------------------
//
// class methods
//
//---------------------------------------------------------------
//---------------------------------------------------------------
// method: initialize
//
// arguments: none
// return : boolean
//
// description:
// overrides the initialize() method in the base class. initializes
// member data and prepares for execution of first step. this method
// "resets" the algorithm.
//---------------------------------------------------------------
public boolean initialize()
{
// Debug
System.out.println(algo_id + ": initialize()");
scale = output_panel_d.disp_area_d.getDisplayScale();
data_pool_d = new Vector();
decision_regions_d = new DecisionRegion();
point_means_d = new Vector();
support_vectors_d = new Vector();
description_d = new Vector();
step_count = 3;
iterations = clusters;
System.out.println("" + iterations);
guesses_d = new Vector(2, 2);
cbinary_d = new Vector();
currObject = 0;
// add the process description for the LBG 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 for each class.");
description_d.addElement(str);
str = new String(" 3. Stepping through iterations.");
description_d.addElement(str);
str = new String("Iteration");
description_d.addElement(str);
}
// append message to process box
pro_box_d.appendMessage("LBG 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();
// advance to step 1
step_index_d = 0;
// append message to process box
pro_box_d.appendMessage((String)description_d.get(step_index_d));
// exit gracefully
return true;
}
//---------------------------------------------------------------
// method: step1
//
// arguments: none
// return : boolean
//
// description:
// displays data sets from input box in output box.
//
//---------------------------------------------------------------
boolean step1()
{
// Debug
System.out.println(algo_id + " : step1()");
// set up progress bar
pro_box_d.setProgressMin(0);
pro_box_d.setProgressMax(1);
pro_box_d.setProgressCurr(0);
scaleToFitData();
// Display original data
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);
plain_data = output_panel_d.copy();
// step 1 completed
pro_box_d.setProgressCurr(1);
output_panel_d.repaint();
return true;
}
//---------------------------------------------------------------
// method: step2
//
// arguments: none
// return : boolean
//
// description:
// computes the means of each data set and displays the means graphically
// and numerically
//
//---------------------------------------------------------------
boolean step2()
{
// Debug
System.out.println(algo_id + " : step2()");
// determine the within class scatter matrix
generatePool();
decision_regions_d.addRegion((Vector)data_pool_d.clone());
MyPoint mean = MathUtil.computeClusterMean(data_pool_d);
computeMeans();
guesses_d.addElement(new MyPoint(mean.x, mean.y));
decision_regions_d.setGuesses((Vector)guesses_d.clone());
output_panel_d.addOutput(decision_regions_d.getGuesses(),
Classify.PTYPE_OUTPUT_LARGE,
Color.black);
//cbinary_d.addElement(decision_regions_d);
pro_box_d.setProgressCurr(20);
output_panel_d.repaint();
return true;
}
//---------------------------------------------------------------
// method: step3
//
// arguments: none
// return : boolean
//
// description:
// Computes the Decision Regions and the associated errors.
//
//---------------------------------------------------------------
boolean step3()
{
// Debug
System.out.println(algo_id + " : step3()");
if (currObject == 0)
pro_box_d.appendMessage((String)description_d.get(step_index_d));
if (currObject < iterations)
{
step_index_d--;
output_panel_d.clear();
output_panel_d = plain_data.copy();
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);
output_panel_d.addOutput(decision_regions_d.getGuesses(),
Classify.PTYPE_OUTPUT_LARGE,
Color.black);
guesses_d = decision_regions_d.getGuesses();
System.out.println("Num Guesses" + guesses_d.size());
Vector guesses_d = decision_regions_d.;
Vector decisionRegions = decision_regions_d.getRegions();
//DecisionRegion region = new DecisionRegion(decision_regions_d);
decision_regions_d = new DecisionRegion();
// classify the data based on the guesses
//
classify(decision_regions_d);
// compute the new means of the classified data
//
computeBinaryDeviates(decision_regions_d);
// store the current iteration of the data set
//
//cbinary_d.addElement(decision_regions_d);
output_panel_d.repaint();
currObject++;
pro_box_d.appendMessage(" Displaying Iteration: " + currObject
+ "\n");
//DecisionRegion region;
//region = (DecisionRegion)cbinary_d.elementAt(currObject);
// System.out.println("Size of cbinary " + cbinary_d.size());
// determine the total number of clusters
//
int numclusters = decision_regions_d.getNumRegions();
// determine the classification error of each cluster
//
double error = 0.0;
double total = 0.0;
System.out.println("numclusters: " + numclusters);
for (int i=0; i<numclusters; i++) {
// get the cluster associated with the region
//
Vector cluster = decision_regions_d.getRegion(i);
// determine the mean of the current cluster
//
MyPoint mean = MathUtil.computeClusterMean(cluster);
System.out.println(mean.toString());
// display the mean for the current cluster
//
//double xval = setDecimal((mean.x) + Xmin, 2);
//double yval = setDecimal(Ymax - (mean.y), 2);
String message = new String(" Mean for cluster " +
i + ": "
+ mean.x + ", " + mean.y);
pro_box_d.appendMessage(message + "\n");
// determine the covariance matrix for the cluster
//
double x[] = new double[cluster.size()];
double y[] = new double[cluster.size()];
for (int j=0; j<cluster.size(); j++) {
MyPoint covarPoint = (MyPoint)cluster.elementAt(j);
x[j] = (covarPoint.x);
y[j] = (covarPoint.y);
}
// declare the covariance object
//
Covariance cov = new Covariance();
// declare the covariance matrix
//
Matrix covar = new Matrix();
covar.row = covar.col = 2;
covar.Elem = new double[2][2];
// compute the covariance matrix of the first data set
//
covar.Elem = cov.computeCovariance(x, y);
// display the covariance matrix for the cluster
//
double c11 = MathUtil.setDecimal(covar.Elem[0][0], 2);
double c12 = MathUtil.setDecimal(covar.Elem[0][1], 2);
double c21 = MathUtil.setDecimal(covar.Elem[1][0], 2);
double c22 = MathUtil.setDecimal(covar.Elem[1][1], 2);
message = new String(" Covariance matrix:\n" +
" " + c11
+ " " + c12 + "\n" +
" " + c21
+ " " + c22);
pro_box_d.appendMessage(message + "\n");
// determine the closest of the original data sets
//
int closest = getClosestSet(mean);
// compute the classification error
//
total += (double)cluster.size();
error += (double)displayClusterError(closest,
cluster, i);
}
double err = (error / total) * 100.0;
// display the clasification error
//
String message = new String(" Overall results:\n" +
" Total number of samples: " +
total + "\n" +
" Misclassified samples: " +
error + "\n" +
" Classification error: " +
MathUtil.setDecimal(err, 2) + "%");
pro_box_d.appendMessage(message + "\n");
// update the output canvas
//
output_panel_d.repaint();
}
return true;
}
//---------------------------------------------------------------
// method: run
//
// arguments: none
// return : none
//
// description:
// 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)
{
step1();
}
else if (step_index_d == 2)
{
step2();
}
else if (step_index_d == 3)
{
step3();
}
// exit gracefully
//
return;
}
public void generatePool() {
// determine the pool of points only once
//
if (data_pool_d.size() > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -