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

📄 sigmaflnmap.java

📁 一个数据挖掘系统的源码
💻 JAVA
字号:

/**
 *
 *   AgentAcademy - an open source Data Mining framework for
 *   training intelligent agents
 *
 *   Copyright (C)   2001-2003 AA Consortium.
 *
 *   This library is open source software; you can redistribute it
 *   and/or modify it under the terms of the GNU Lesser General
 *   Public License as published by the Free Software Foundation;
 *   either version 2.0 of the License, or (at your option) any later
 *   version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *   MA  02111-1307 USA
 *
 */

package org.agentacademy.modules.dataminer.classifiers.flnmap;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002 Ioannis N. Athanasiadis</p>
 * <p>Company: </p>
 * @author Ioannis N. Athanasiadis
 * @version 1.2
 */
import org.agentacademy.modules.dataminer.classifiers.evaluation.*;
import org.agentacademy.modules.dataminer.core.*;
import org.agentacademy.modules.dataminer.core.Instances;
import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;

/**
 *  Class implementing sigma - FLNMAP classifier
 */

public class SigmaFlnmap extends Classifier implements OptionHandler{

 public static Logger                log = Logger.getLogger(SigmaFlnmap.class);
        public static final float EPSILON = 0.000001f;
	private Vector learnedCode;	// keeps the learned codes (hyperboxes)
	private double rhoa;	// keeps the vignilance parameter rhoa
        private Box bounds;
        private File bFile = new File("o");

	/**
	 * Builds the FLNMAP Classifier
	 */
	public void buildClassifier(Instances data) throws Exception {

      // Exceptions statements
          if (data == null ){
          throw new Exception("Dataset is null");
          }

          if (data.checkForStringAttributes()) {
             throw new Exception("Can't handle string attributes!");
          }
          if (data.classAttribute().isNumeric()) {
            throw new Exception("Class is numeric!");
          }

          if (data.numClasses() < 0) {
            throw new Exception ("Dataset has no class attribute");
          }
          if (!bFile.canRead()){
            try{
            setBounds(data);
            }
            catch(Exception e){
e.printStackTrace();
            }

          }
          if ( bounds.length()!= data.numAttributes()-1){
          throw new Exception ("Incompatible bounds file");
          }

        // BuildClassifier
          learnedCode = new Vector();

          int searching;
          Box  inputBuffer;

          Box Code = new Box(data.firstInstance());
          learnedCode.addElement(Code);


          for (int i = 1 ; i < data.numInstances() ; i++){
            inputBuffer = new Box ((Instance) data.instance(i));

            double[] sigma= new double[(learnedCode.size())];

            for (int j = 0; j < learnedCode.size(); j++){
              Box num = (Box) learnedCode.get(j);
             Box den = Box.join(inputBuffer, num);
              double numden = num.valuation(bounds)/den.valuation(bounds);
              sigma[j] = numden;

            } //for int j

            do{
            int winner = 0;
            double winnerf = sigma[0];
            for (int j = 1; j < learnedCode.size(); j++){
              if (winnerf < sigma[j]){
               winner = j;
               winnerf = sigma[j];
              } //if
            } //for

            Box num = inputBuffer;
            Box winnerBox = (Box) learnedCode.get(winner);
            Box den = Box.join(winnerBox,num);
            double numden = num.valuation(bounds)/den.valuation(bounds);


            if ((inputBuffer.getCateg()== winnerBox.getCateg())&&(rhoa < (numden))){
                 learnedCode.setElementAt(Box.join(winnerBox,inputBuffer), winner);
                 searching = 0;
              }
            else{
                sigma[winner] = 0;
                rhoa += EPSILON;
                searching =0;
                for (int j = 0; j<learnedCode.size(); j++){
                  if (sigma[j] != 0.0) {
                    searching = 1;
                  }//fi
                } //for

                if (searching==0){
                  learnedCode.addElement(inputBuffer);
                }//fi
              }//else
            } while (searching==1);
          }//for

       } //buildClassifier





	/**
	 * Classifies a given instance using the FLN Rules
	 */

  public double classifyInstance(Instance instance) {

    Box num, den, inputBuffer;
    inputBuffer = new Box (instance);

    // calculate excitations and winner
    double[] sigma= new double[(learnedCode.size())];
    for (int j = 0; j < learnedCode.size(); j++){
      num = (Box) learnedCode.get(j);
      den = Box.join(inputBuffer,num);

      sigma[j] =(num.valuation(bounds)/den.valuation(bounds));
    }//for j

    //find the winner Code (hyperbox)
    int winner = 0;
    double winnerf = sigma[0];
    for (int j=1; j<learnedCode.size(); j++){
      if (winnerf < sigma[j]){

        winner = j;
        winnerf = sigma[j];
      }//fi
    }//for j

   Box currentBox = (Box)learnedCode.get(winner);

  return (double )currentBox.getCateg();
  }

	/**
	 * Prints the hyperboxes using method showRules.
	 */
	public String toString(){
	String output = "";
        output = "sigma-FLNMAP classifier\n=======================\n Rhoa = "+rhoa +"\n Extracted Rules (HyperBoxes):\n\n";
        for (int i=0;i<learnedCode.size();i++){
           Box Code = (Box) learnedCode.get(i);
           output = output + Code.toString();
        }
        output = output + "\n\n Boundaries:\n" +bounds.toString();
        output = output + "\n Rules:    " +learnedCode.size();

       return output;
	}


       public void showRules(){
        for (int i=0;i<learnedCode.size();i++){
           Box Code = (Box) learnedCode.get(i);
           System.out.println("Rule:"+ i );
           Code.showbox();
        }

      }
	/**
	  * Returns an enumeration describing the available options.
	  *
	  * Valid options are: <p>
	  *
	  * -R rhoa<br>
	  *  Set vignilance parameter rhoa  (a float in range [0,1])<p>
	  *
	  */
	 public Enumeration listOptions() {
		 Vector newVector = new Vector(2);
		 newVector.addElement(new Option("\tSet vigilance parameter rhoa.", "R", 1, "-R"));
		 newVector.addElement(new Option("\tSet boundaries File", "B", 1, "-B"));
		 return newVector.elements();
	 }



	/**
	  * Parses a given list of options.
	  *
	  * @param options the list of options as an array of strings
	  * @exception Exception if an option is not supported
	  */
	  public void setOptions(String[] options) throws Exception{

	  // Other options

	  String rhoaString = Utils.getOption('R', options);
	  	if (rhoaString.length() != 0) {
		  rhoa = Double.parseDouble(rhoaString);
                  if (rhoa <0 || rhoa>1){
                    throw new Exception("Vigilance parameter (rhoa) should be a real number in range [0,1]");
                  }
                }
                else{
                 rhoa = 0.5;
                }
	  String boundsString = Utils.getOption('B', options);
	  	if (boundsString.length() != 0) {
		  bFile = new File(boundsString);
                  if (!bFile.canRead()){
                  throw new Exception("Boundaries File does not exist");
                  }
                  try{
                  BufferedReader in = new BufferedReader(new FileReader(bFile));
                  String line = in.readLine();
                  bounds = new Box(line);
                  }
                  catch(Exception e){
                  throw new Exception("Boundaries File structure error");

                  }
                }//fi

          Utils.checkForRemainingOptions(options);
	  }


/**
   * Gets the current settings of the Classifier.
   *
   * @return an array of strings suitable for passing to setOptions
   */
  public String [] getOptions() {

    String [] options = new String [2];
    int current = 0;

    options[current++] = "-R"; options[current++] = "" + rhoa;
    options[current++] = "-B"; options[current++] = "" + bFile.toString();
    while (current < options.length) {
      options[current++] = "";
    }
    return options;
  }


  public void setBounds(Instances data){
            // Initialize minmax stats

          bounds = new Box(data.numAttributes()-1);

          int k=0;
          for (int i=0; i<data.numAttributes();i++){

            if (i!=data.classIndex()) {

             // try{
              Instance inst  = data.firstInstance();
             double min = inst.value(i);
             double max = inst.value(i);

             for(int j=0; j<data.numInstances(); j++){
                 inst  = data.instance(j);
                 double val = inst.value(i);
                 min =  val <=min ? val : min ;
                 max =  val >=max ? val : max ;
             }
              bounds.setMin(k,min);
              bounds.setMax(k,max);
              k=k+1;
            } //if
          }//for

  }
/*
public sigmaFlnmap(){

}
/*
public static double getMinMax(int i, int j){
return minmax[i][j];
}
*/
	/**
	 * Main method
	 */
	public static void main(String[] args){

		try{
			System.out.println(Evaluation.evaluateModel(new SigmaFlnmap(), args));
                   }

		catch (Exception e){
		 log.error(e.getMessage());
		}

	}


}

⌨️ 快捷键说明

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