⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 algorithm.java

📁 包含了模式识别中常用的一些分类器设计算法
💻 JAVA
字号:
//--------------------------------------------------------------------------
// Algorithm.java   6.0 03/15/2005
// Created        : Daniel May.  
//                              Last Edited : Ryan Irwin 5/23/05
//
// Description    : This is the base class for all pattern recognition 
//                  algorithms. 
// Remarks        : Code unchanged since created. created 09/02/2003
//--------------------------------------------------------------------------


//----------------------
// import java packages
//----------------------
import java.util.*;
import java.awt.*;

/**
 * Base class for all pattern recognition algorithms
 */
public abstract class Algorithm implements Runnable
{	

    //-------------------------------------------------------------------
    // 
    // class variables
    //
    //-------------------------------------------------------------------

    /**
     * Step index tracks the indices of operation.  Initialized to -1.
     */
    int step_index_d = -1;

    // Data Members should be redefined by inheriting class
    //

   /**
    * Step count tracks the number of steps. Initialized to 0.
    */
    int step_count = 0;

    // Class Specific Data
    //

    /**
     * Data points of the input data
     */
    DataPoints data_points_d = null;
   
    /**
     * Panel display after algorithm is applied
     */
    OutputPanel output_panel_d = null;

    /**
     * Used in the textual display area of what is occuring.
     */
    ProcessBox pro_box_d = null;

    /**
     * Used for displaying coordinates of a point
     */
    DisplayScale scale;

    // Data Members should be redefined by inheriting class
    //

    /**
     * String initialized to "Algorithm" and is used for message appending
     */
    String algo_id = "Algorithm";

    /**
     * Stores appended messages of description
     */
    Vector<String> description_d = new Vector<String>();

    /**
     * Stores the means of the points
     */
    Vector<MyPoint> point_means_d = new Vector<MyPoint>();

    /**
     * Set 1 of points
     */
    Vector<MyPoint> set1_d = new Vector<MyPoint>();

    /**
     * Set 2 of points
     */
    Vector<MyPoint> set2_d = new Vector<MyPoint>();

    /**
     * Set 3 of points
     */
    Vector<MyPoint> set3_d = new Vector<MyPoint>();

    /**
     * Set 4 of points
     */
    Vector<MyPoint> set4_d = new Vector<MyPoint>();

    //-------------------------------------------------------------------
    // 
    // class public methods
    //
    //-------------------------------------------------------------------


    /**
     * Determines and executes next step of the algorithm in a new
     * thread of execution.
     *
     * @return  Returns true if being initialized or step_index_d is -1 as
     * initialized. Returns true if step_index_d does not equal step_count.
     * Returns false if they are equal.
     */
    public boolean nextStep()
    {
	if (step_index_d < 0)
	{
	    return initialize();
	}
	else if (step_index_d == step_count)
	{
	    return false;
	}
	else
	{
	    
	    // increment step index
	    //
	    step_index_d++;
	    
	    pro_box_d.appendMessage((String)description_d.get(step_index_d));
	    
	    Thread step = new Thread(this);
	    step.start();

	    // Auto scroll
	    //
	    pro_box_d.scrollPane.getVerticalScrollBar().setValue(100000);


	    
	    return true;
	}
    }

    /**
     * Determines and executes previous step of the algorithm in a new
     * thread of execution.
     *
     * @return Returns true if step_index_d is not equal to 0.
     */
    public boolean prevStep()
    {
	// Debug
	// System.out.println(algo_id + " : prevStep()");
	//

	if (step_index_d == 0)
	{
	    pro_box_d.appendMessage("No previous step");
	    return false;
	}

	// decrement step index
	//
	step_index_d--;

	pro_box_d.appendMessage((String)description_d.get(step_index_d));

	Thread step = new Thread(this);
	step.start();

	return true;
    }

    /**
     * Initializes the output panel. 
     *
     * @param  out_panel_a  output panel object
     * @return Returns true
     * @see    OutputPanel
     */
    public boolean setOutputPanel(OutputPanel out_panel_a)
    {
        // Debug
	//
	// System.out.println(algo_id 
	//                    + ": setOutputPanel(OutputPanel out_panel_a)");
	
	// set the output 
	output_panel_d = out_panel_a;

	return true;
    }
    
    /**
     * Initializes the process box. 
     *
     * @param   pro_box_a  process box object
     * @return  true
     * @see    ProcessBox
     */    
    public boolean setProcessBox(ProcessBox pro_box_a)
    {
	// Debug
	//
	// System.out.println(algo_id 
	//                    + ": setProcessBox(ProcessBox pro_box_a)");

	 // set the process box
	 pro_box_d = pro_box_a;

	 // Auto scroll
	 //
	 pro_box_d.scrollPane.getVerticalScrollBar().setValue(100000);


	 return true;
    }

    /**
     * Initializes the data points. 
     *
     * @param   data_a data points
     * @return  Returns true
     * @see     DataPoints
     */
    public boolean setDataPoints(DataPoints data_a)
    {
        // Debug
	//
	// System.out.println(algo_id + ": setDataPoints(DataPoints data_a)");

	// set the data points
	//
	data_points_d = data_a;


	return true;
    }

    /**
     * Computes the means for each existing data set 
     */
    public void computeMeans()
    {
        // Debug
	//
	// System.out.println(algo_id + ": computeMeans()");

	MyPoint numericalMean;
	MyPoint pointMean;
	String text;

	pro_box_d.setProgressMin(0);
	pro_box_d.setProgressMax(4);
	pro_box_d.setProgressCurr(0);
	
	pro_box_d.appendMessage("       Means:"); 

	// calculate point means
	//
	point_means_d.clear();
       	if (set1_d.size() > 0)
       	{
	    pointMean = new MyPoint(MathUtil.computePointMean(set1_d));
       	    point_means_d.add(pointMean);
	    pro_box_d.appendMessage("       Set 1: " + pointMean.toString()); 
       	}
		
	pro_box_d.setProgressCurr(1);

       	if (set2_d.size() > 0)
       	{
	    pointMean = new MyPoint(MathUtil.computePointMean(set2_d));
       	    point_means_d.add(pointMean);
	    pro_box_d.appendMessage("       Set 2: " + pointMean.toString()); 
       	}	

	pro_box_d.setProgressCurr(2);

       	if (set3_d.size() > 0)
       	{
	    pointMean = new MyPoint(MathUtil.computePointMean(set3_d));
       	    point_means_d.add(pointMean);
	    pro_box_d.appendMessage("       Set 3: " + pointMean.toString()); 
       	}	

	pro_box_d.setProgressCurr(3);
       	
	if (set4_d.size() > 0)
       	{
	    pointMean = new MyPoint(MathUtil.computePointMean(set4_d));
       	    point_means_d.add(pointMean);
	    pro_box_d.appendMessage("       Set 4: " + pointMean.toString()); 
       	}	
	
	pro_box_d.setProgressCurr(4);

	// Auto scroll
	//
	pro_box_d.scrollPane.getVerticalScrollBar().setValue(100000);

    }    

    /**
     * Scales the data axes to fit the data. 
    */
    public void scaleToFitData()
    {

	if(set1_d.size() < 2 && set2_d.size() < 2 && set3_d.size() < 2 &&
	   set4_d.size() < 2)
	    return;
	   
	double xmin = Double.MAX_VALUE;
	double xmax = Double.MIN_VALUE;
	double ymin = Double.MAX_VALUE;
	double ymax = Double.MIN_VALUE;

	for(int i = 0; i < set1_d.size(); i++)
	{
	    if(((MyPoint)set1_d.elementAt(i)).x < xmin)
		xmin = ((MyPoint)set1_d.elementAt(i)).x;
	    if(((MyPoint)set1_d.elementAt(i)).x > xmax)
		xmax = ((MyPoint)set1_d.elementAt(i)).x;
	    if(((MyPoint)set1_d.elementAt(i)).y < ymin)
		ymin = ((MyPoint)set1_d.elementAt(i)).y;
	    if(((MyPoint)set1_d.elementAt(i)).y > ymax)
		ymax = ((MyPoint)set1_d.elementAt(i)).y;	    
	}
	
	for(int i = 0; i < set2_d.size(); i++)
	{
	    if(((MyPoint)set2_d.elementAt(i)).x < xmin)
		xmin = ((MyPoint)set2_d.elementAt(i)).x;
	    if(((MyPoint)set2_d.elementAt(i)).x > xmax)
		xmax = ((MyPoint)set2_d.elementAt(i)).x;
	    if(((MyPoint)set2_d.elementAt(i)).y < ymin)
		ymin = ((MyPoint)set2_d.elementAt(i)).y;
	    if(((MyPoint)set2_d.elementAt(i)).y > ymax)
		ymax = ((MyPoint)set2_d.elementAt(i)).y;	    
	}

	for(int i = 0; i < set3_d.size(); i++)
	{
	    if(((MyPoint)set3_d.elementAt(i)).x < xmin)
		xmin = ((MyPoint)set3_d.elementAt(i)).x;
	    if(((MyPoint)set3_d.elementAt(i)).x > xmax)
		xmax = ((MyPoint)set3_d.elementAt(i)).x;
	    if(((MyPoint)set3_d.elementAt(i)).y < ymin)
		ymin = ((MyPoint)set3_d.elementAt(i)).y;
	    if(((MyPoint)set3_d.elementAt(i)).y > ymax)
		ymax = ((MyPoint)set3_d.elementAt(i)).y;	    
	}

	for(int i = 0; i < set4_d.size(); i++)
	{
	    if(((MyPoint)set4_d.elementAt(i)).x < xmin)
		xmin = ((MyPoint)set4_d.elementAt(i)).x;
	    if(((MyPoint)set4_d.elementAt(i)).x > xmax)
		xmax = ((MyPoint)set4_d.elementAt(i)).x;
	    if(((MyPoint)set4_d.elementAt(i)).y < ymin)
		ymin = ((MyPoint)set4_d.elementAt(i)).y;
	    if(((MyPoint)set4_d.elementAt(i)).y > ymax)
		ymax = ((MyPoint)set4_d.elementAt(i)).y;	    
	}

	scale = new DisplayScale(xmax + (xmax - xmin)* 0.05,
				 xmin - (xmax - xmin)* 0.05,
				 ymax + (ymax - ymin)* 0.05,
				 ymin - (xmax - xmin)* 0.05);

	Classify.output_panel_d.disp_area_d.setDisplayScale(scale);

    }

    /**
     * Disables the next and previous keys before beggining to execute
     * a step.
     */
    public void disableControl()
    {
	Classify.main_menu_d.prevMenuItem.setEnabled(false);
	Classify.main_menu_d.nextMenuItem.setEnabled(false);

	// Auto scroll
	//
	pro_box_d.scrollPane.getVerticalScrollBar().setValue(100000);

    }

    /**
     * Enables the next and previous keys after the execution of
     * a step.
     */
    public void enableControl()
    {
	Classify.main_menu_d.prevMenuItem.setEnabled(true);
	Classify.main_menu_d.nextMenuItem.setEnabled(true);

	// Auto scroll
	//
	pro_box_d.scrollPane.getVerticalScrollBar().setValue(100000);

    }


    /**
     * Implementation of run from the Runnable interface.  This method
     * should be overidden by inheriting classes
     */
    public abstract void run();

    /**
    * Initializes algorithm.  this method should be overidden by inheriting
    * classes
    *
    * @return  Returns true
    */    
    public abstract boolean initialize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -