粗糙集属性约简的一些关键步骤(第一次写)

为了大家能够更好的实现粗糙集属性约简以及了解相关知识,在这里给出了构造决策表部分的代码,希望读者通过阅读代码自己实现一些论文中的简单算法,代码中有什么不懂得可以与我交流,论文代码实现也可以与我一起探讨,互相学习。

import java.util.ArrayList;
import java.util.Iterator;
public class MyElement implements Cloneable
{
	public MyElement(){}
	public MyElement(ArrayList<Float> condition, ArrayList<Float> decision, int iNo)
	{
		this.valueOfCondition = condition;
		this.valueOfDecision = decision;
		this.u = iNo;
	}
	public MyElement(MySet condition, MySet decision, int iNo)
	{
		this.valueOfCondition = condition.toIntArrayList();
		this.valueOfDecision = condition.toIntArrayList();
		this.u = iNo;
	}
	public MyElement clone() throws CloneNotSupportedException
	{
		MyElement cloned = new MyElement();
		cloned.u = this.getU();
		Iterator<Float> itCondition = this.getCondition().iterator();
		while(itCondition.hasNext())
		{
			cloned.valueOfCondition.add(itCondition.next());
		}
		Iterator<Float> itDecision = this.getDecision().iterator();
		while(itDecision.hasNext())
		{
			cloned.valueOfDecision.add(itDecision.next());
		}
		return cloned;
	}
	public ArrayList<Float> getCondition()
	{
		return this.valueOfCondition;
	}
	public ArrayList<Float> getDecision()
	{
		return this.valueOfDecision;
	}
	public int getU()
	{
		return this.u;
	}
	void setCondition(ArrayList<Float> condition)
	{
		this.valueOfCondition = condition;
	}
	void setDecision(ArrayList<Float> decision)
	{
		this.valueOfDecision = decision;
	}
	void setU(int u)
	{
		this.u = u;
	}
	boolean isConditionEqual(MyElement another)
	{
		Iterator<Float> it1 = this.valueOfCondition.iterator();
		Iterator<Float> it2 = another.valueOfCondition.iterator();
		
		while(it1.hasNext() && it2.hasNext())
		{
			if(it1.next() != it2.next())
				return false;
		}
		return true;
	}
	boolean isDecisionEqual(MyElement another)
	{
		Iterator<Float> it1 = this.getDecision().iterator();
		Iterator<Float> it2 = another.getDecision().iterator();
		
		while(it1.hasNext() && it2.hasNext())
		{
			if(it1.next() != it2.next())
				return false;
		}
		return true;
	}
	boolean isUEqual(MyElement another)
	{
		if(this.getU() != another.getU())
			return false;
		else 
			return true;
	}
	boolean isEqual(MyElement another)
	{
		if(this.isConditionEqual(another) && this.isDecisionEqual(another))
			return true;
		else 
			return false;
	}
	void removeConditionAttribute(int it)
	{
		this.valueOfCondition.remove(it);
	}
	void addConditionAttribute(Float a)
	{
		this.valueOfCondition.add(a);
	}
	void print()
	{
		if(!this.valueOfCondition.isEmpty() && !this.valueOfDecision.isEmpty())
		{
			System.out.print("(");
			Iterator<Float> it = this.valueOfCondition.iterator();
			System.out.print(it.next());
			while(it.hasNext())
			{
				System.out.print(",\t" + it.next());
			}
			System.out.print(")\t->\t(");
			it = this.valueOfDecision.iterator();
			System.out.print(it.next());
			while(it.hasNext())
			{
				System.out.print(",\t" + it.next());
			}
			System.out.println(")\tu" + u);
		}
		else
			System.out.println("该样本不符合条件,无意义");
	}
	
	private ArrayList<Float> valueOfCondition = new ArrayList<Float>(0);
	private ArrayList<Float> valueOfDecision = new ArrayList<Float>(0);
	private int u;
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
import java.io.*;
import java.math.*;
public class DecisionTable implements Cloneable
{
	private static final Float Interger = null;
	public static int iNo;
	public DecisionTable()
	{
		
	}
	public DecisionTable(String fileName)
	{
		try
		{
			File file = new File(fileName);
			Scanner in = new Scanner(file);
			String[] temp;
			while(in.hasNextLine())
			{
				temp = in.nextLine().split(" |,");
				
				if(temp[0].equals( "@conditionAttribute"))
				{
//					System.out.println("#####"+temp.length);
					for(int i=1; i<temp.length; i++)
					{
//						System.out.print(" "+(temp[i]));
						this.conditionSet.add( Integer.parseInt(temp[i]));
						
					}
				}
				else if(temp[0].equals("@decisionAttribute"))
				{
					for(int i=1; i<temp.length; i++)
					{
						this.decisionSet.add(Integer.parseInt(temp[i]));
					}
				}
				else if(temp[0].equals("%") || temp[0].equals("@data"))
				{
					continue;
				}
				else if(temp[0].equals("@end"))
				{
					break;
				}
		
				else if(Float.parseFloat(temp[0]) < Float.MAX_VALUE)
				{
					MyElement e = new MyElement();
					int i = 0;
					ArrayList<Float> C = new ArrayList<Float>(0);
					ArrayList<Float> D = new ArrayList<Float>(0);
					for(; i< this.conditionSet.size(); i++)
					{
						C.add(Float.parseFloat(temp[i]));
						
					}
					for(int j=0; j<this.decisionSet.size(); j++)
					{
						D.add(Float.parseFloat(temp[i+j]));
					}
					e.setCondition(C);
					e.setDecision(D);
					this.addElement(e);
				}
				else
				{
					System.out.print(temp[0]+"!!!");
					System.out.println("数据数目有误,请检查");
					break;
				}
			}
	
		}

		catch(IOException e)
		{
			
			System.out.print("不能打开文件" + fileName + " " + e.getMessage());
		}
	}
	public DecisionTable clone() throws CloneNotSupportedException
	{
		DecisionTable cloned = new DecisionTable();
		
		cloned.conditionSet = (ArrayList<Integer>)this.conditionSet.clone();
		cloned.decisionSet = (ArrayList<Integer>)this.decisionSet.clone();
		ArrayList<MyElement> arrayElements = new ArrayList<MyElement>(0);
		MyElement tempElement = new MyElement();
		Iterator<MyElement> itElements = this.DT.iterator();
		while(itElements.hasNext())
		{
			tempElement = itElements.next().clone();
			arrayElements.add(tempElement);
		}
		cloned.DT = arrayElements;
		
		return cloned;
	}
	public float [][]  to_Condition_table(){
		ArrayList<MyElement> temp = this.getDT();
		float table[][] = new float[temp.size()][this.conditionSet.size()];
		Iterator<MyElement> itElements = this.DT.iterator();
		for(int i = 0 ; i < this.numberOfElements() ;i++){
			for(int j = 0 ; j < this.conditionSet.size() ; j++ ){
				table[i][j] = temp.get(i).getCondition().get(j);
			}
		}
		return table;
		
	}	

	public float [][]  to_Decision_table(){
		ArrayList<MyElement> temp = this.getDT();
		float table[][] = new float[temp.size()][this.decisionSet.size()];
		Iterator<MyElement> itElements = this.DT.iterator();
		for(int i = 0 ; i < this.numberOfElements() ;i++){
			for(int j = 0 ; j < this.decisionSet.size() ; j++ ){
				table[i][j] = temp.get(i).getDecision().get(j);
			}
		}
		return table;
		
	}	
	void setConditionSet(ArrayList<Integer> condition)
	{
		this.conditionSet = condition;
	}
	void setDecisionSet(ArrayList<Integer> decision)
	{
		this.decisionSet = decision;
	}
	void setU(int index, int newU)
	{
		MyElement temp = DT.get(index);
		temp.setU(newU);
		DT.set(index,temp);
	}
	ArrayList<Integer> getConditionSet()
	{
		return this.conditionSet;
	}
	ArrayList<Integer> getDecisionSet()
	{
		return this.decisionSet;
	}
	ArrayList<MyElement> getDT()
	{
		return DT;
	}
	MyElement getElement(float f)
	{
		MyElement temp = new MyElement();
		Iterator<MyElement> it = this.DT.iterator();
		while(it.hasNext())
		{
			temp = it.next();
			if(temp.getU() == f)
			{
				return temp;
			}
		}
		System.out.println("不存在你所指定的样本");
		return null;
	}
	
	//返回决策表DT中的样本数
	int numberOfElements()
	{
		return DT.size();	
	}
	void addElement(MyElement e)
	{
		this.serialNumberOfElement++;
		if(DecisionTable.iNo < Integer.MAX_VALUE)
		{
			e.setU(this.serialNumberOfElement);
			DT.add(e);
		}
		else
		{
			System.out.println("可用编号已耗尽,添加样本失败");
		}
	}
	void addElement(MyElement e, boolean flag)
	{
		if(flag)
		{
			this.addElement(e);
		}
		else
		{
			DT.add(e);
		}
	}
	int findElement(MyElement e)
	{
		Iterator<MyElement> it = this.DT.iterator();
		while(it.hasNext())
		{
			MyElement temp = it.next();
			if(temp.equals(e))
			{
				return temp.getU();
			}
		}
		return -1;
	}
	void removeElement(int iNo)
	{
		if(iNo >= 1 && iNo <= this.serialNumberOfElement)
		{
			Iterator<MyElement> it = this.DT.iterator();
			while(it.hasNext())
			{
				if(it.next().getU() == iNo)
				{
					this.DT.remove(iNo);
					break;
				}
			}
		}
		else
		{
			System.out.println("越界,不存在这样的样本");
		}
	}
	boolean containsConditionAttribute(int c )
	{
		Iterator<Integer> itConditionAttr = this.getConditionSet().iterator();
		while(itConditionAttr.hasNext())
		{
			if(itConditionAttr.next() == c)
				return true;
		}
		return false;
	}
	boolean containsDecisionAttribute(int d)
	{
		Iterator<Integer> itConditionAttr = this.getDecisionSet().iterator();
		while(itConditionAttr.hasNext())
		{
			if(itConditionAttr.next() == d)
				return true;
		}
		return false;
	}
	DecisionTable removeConditionAttribute(int c)
	{
		int index =0;
		Iterator<Integer> it = this.conditionSet.iterator();
		int temp;
		while(it.hasNext())
		{
			index++;
			temp = it.next();
			if(temp == c)
			{//有可能位置有问题;
				it.remove();
				break;
			}
		}
		index--;
		Iterator<MyElement> itEle = this.DT.iterator();
		while(itEle.hasNext())
		{
			itEle.next().removeConditionAttribute(index);
		}
		return this;
	}
	MySet POS()
	{
		boolean flag;
		MySet pos = new MySet();
		Iterator<MyElement> it1 = this.DT.iterator();
		Iterator<MyElement> it2;
		MyElement temp1 = new MyElement(),temp2 = new MyElement();
		while(it1.hasNext())
		{
			temp1 = it1.next();
			flag = true;
			it2 = this.DT.iterator();
			while(it2.hasNext())
			{
				temp2 = it2.next();
				if(temp1.isConditionEqual(temp2) && !temp1.isDecisionEqual(temp2))
				{
					flag = false;
					break;
				}
			}
			if(flag)
			{
				pos.add(temp1.getU());
			}
		}
		return pos;
	}
	double relyDegreeOfDToC()
	{
		double result = 0.0d;
		MySet pos = this.POS();
		
		result = (double) pos.card() / this.numberOfElements();
		
		return result;
	}
	float relyDegreeOfDToc(float x)
	{
		float result = 0.0f;
		
		result = (float) x / this.numberOfElements();
			
		return result;
	}
	double relyDegreeOfDToAttribute(int c) throws CloneNotSupportedException
	{
		DecisionTable temp = (DecisionTable)this.clone();
		Iterator<Integer> it = temp.conditionSet.iterator();
		double result = 0.0d;
		int nowCondition;
		while(it.hasNext())
		{
			nowCondition = it.next();
			if(c != nowCondition)
			{
				temp.removeConditionAttribute(nowCondition);
			}
		}
		MySet pos = temp.POS();
		result = (double)pos.card() / this.numberOfElements();
		return result;
	}
	double sigOfAttribute(int c) throws CloneNotSupportedException
	{
		double sig = 0.0d;
		double x,y;
		DecisionTable temp = (DecisionTable)this.clone();
		temp.removeConditionAttribute(c);
		MySet posx = this.POS();
		x = (double) posx.card() / this.numberOfElements();
		MySet posy = temp.POS();
		y = (double) posy.card() / temp.numberOfElements();
		sig = x- y;
		return sig;
	}
	float sigOfC(ArrayList<Integer> alChar) throws CloneNotSupportedException
	{
		float sig_C ;
		sig_C = 0;
		DecisionTable temp = (DecisionTable)this.clone();

		Iterator<MySet> equClass = temp.equivalenceClass2(alChar).iterator();
		while(equClass.hasNext())
		{
			MySet tt=equClass.next();
			sig_C += ((float)tt.card()/temp.numberOfElements())*
					(    (float)Math.log(  (double)tt.card()/temp.numberOfElements()    )/ (float)Math.log ((double)2)  );	
		}
		return  (float) (sig_C*(-1.0)); 
	}
	ArrayList<Integer> core() throws CloneNotSupportedException
	{
		ArrayList<Integer> temp = new ArrayList<Integer>(0);
		Iterator<Integer> it = this.getConditionSet().iterator();
		int tmpChar;
		while(it.hasNext())
		{
			tmpChar = it.next();
			if(this.sigOfAttribute(tmpChar) != 0)
			{
				temp.add(tmpChar);
			}
		}
		return temp;
	}
	MySet lowerApproximation(ArrayList<Integer> alChar) throws CloneNotSupportedException{
		Iterator<Integer> itPara = alChar.iterator();
		ArrayList<MySet> equClass = new ArrayList<MySet>(0);
		boolean availablePara_C = true;
		while(itPara.hasNext()){
			int temp_At=itPara.next();
		
			
			if(!this.containsConditionAttribute(temp_At)){
//				System.out.println(temp_At+" ###");
				availablePara_C = false;
			}
		}
		if(availablePara_C){
			MySet lowAppSet = new MySet();
			ArrayList<Integer> d = new ArrayList<Integer>(0);
			d.addAll(this.getDecisionSet());
			ArrayList<MySet> equClass_D = this.equivalenceClass2(d);
			ArrayList<MySet> equClass_C = this.equivalenceClass2(alChar);
			Iterator<MySet> itSet_D = equClass_D.iterator();
			while(itSet_D.hasNext()){
				Iterator<MySet> itSet_C = equClass_C.iterator();
				MySet temp_C = new MySet();
				MySet temp_D = new MySet();
				temp_D = itSet_D.next();
				while(itSet_C.hasNext()){
					temp_C=itSet_C.next();		
					if(temp_C.belongTo(temp_D)){
						lowAppSet.union(temp_C);
					}
				}
			}
			lowAppSet.sort();
			return lowAppSet;
		}
		else{
			System.out.println("您输入的属性不存在,不能为您求下近似");
			return null;
		}

	}
	//H(D|C)
	float sigOfC_D(ArrayList<Integer> alChar) throws CloneNotSupportedException
	{
		float sig_C ;
		sig_C = 0;
		DecisionTable temp = (DecisionTable)this.clone();      
		Iterator<MySet> equClass_c = temp.equivalenceClass2(alChar).iterator();
		while(equClass_c.hasNext())
		{
			MySet C_i=equClass_c.next();
			Iterator<Integer> itAllDecisionAttr= this.getDecisionSet().iterator();
			DecisionTable tempDT = (DecisionTable)this.clone();
			int U = C_i.card();
			ArrayList<Boolean> flag = new ArrayList<Boolean>(0);
			
			for(int i=0; i < U ; i++){
				flag.add(false);
			}

			ArrayList<MySet> equClass = new ArrayList<MySet>(0);
			
			for(int i=0 ; i< U ; i++){	
				int s = (int)C_i.keyAt(i);
				if(flag.get(i)!= true ){
					MySet temp1 = new MySet();
					temp1.add(s);
					flag.set(i,true);
					for(int j=i+1; j < U ; j++){
						int s2 = (int)C_i.keyAt(j);			
						if(flag.get(j) !=  true){							
							if(tempDT.getElement(s).getDecision().equals(tempDT.getElement(s2).getDecision())){				
								temp1.add(s2);
								flag.set(j, true);
							}
						}
					}
					equClass.add(temp1);
				}
			}
			float sig_t = 0;
			Iterator<MySet> fff = equClass.iterator();
			while(fff.hasNext()){
				float  P_x = (float)C_i.card();
				float  p_xy = (float)fff.next().toIntArrayList().size();
				sig_t += (p_xy/P_x)*(   (float)  Math.log((double)(p_xy / P_x) ) / Math.log((double)2) );

			}
			sig_t = sig_t*((float)C_i.card()/temp.numberOfElements());
			sig_C += sig_t;
		}
		return  (float) (sig_C*(-1.0)); 
	}	
	ArrayList<MySet> equivalenceClass2(ArrayList<Integer> alChar) throws CloneNotSupportedException{
		boolean availablePara_C = true,availablePara_D=true;
		Iterator<Integer> itPara = alChar.iterator();
		while(itPara.hasNext()){
			int temp_At=itPara.next();
			if(!this.containsConditionAttribute(temp_At))
				availablePara_C = false;
			if(!this.containsDecisionAttribute(temp_At))
				availablePara_D = false;
		}
		if(availablePara_C && !availablePara_D){
			ArrayList<Integer> conditionsToBeDeleted = new ArrayList<Integer>(0);
			Iterator<Integer> itAllConditionAttr= this.getConditionSet().iterator();
			int tempChar;
			while(itAllConditionAttr.hasNext()){
				tempChar = itAllConditionAttr.next();
				//判断是否为其余属性
				if(!alChar.contains(tempChar)){
					conditionsToBeDeleted.add(tempChar);
				}
			}
			Iterator<Integer> itconditionsToBeDeleted = conditionsToBeDeleted.iterator();
			DecisionTable tempDT = (DecisionTable)this.clone();
			while(itconditionsToBeDeleted.hasNext()){
				tempDT.removeConditionAttribute(itconditionsToBeDeleted.next());		
			}
			int U = tempDT.numberOfElements();
			int length = tempDT.getConditionSet().size();	
			float ss[][] = tempDT.to_Condition_table();
			float M_m[][] = new float [length][2];
             
			for(int j = 0 ; j < length;j++){
				M_m[j][0]=-1f;
        		M_m[j][1]=1000000f;
			}
        
			for(int j = 0 ; j < length;j++){
				for(int i = 0 ; i < U ; i++){
					M_m[j][0]=Math.max(M_m[j][0], ss[i][j]);
					M_m[j][1]=Math.min(M_m[j][1], ss[i][j]);
				}
			}
        
			ArrayList<Integer> List = new ArrayList<Integer>(0);
			Iterator<MyElement> it = tempDT.getDT().iterator();
    	
			while(it.hasNext()){
				List.add(it.next().getU());			}	

			for(int i = 0 ; i < length ; i++){		//3,for(i =1 ; i 
				Map<Integer,LinkedList> que = new LinkedHashMap<Integer,LinkedList>();
				int tem =  (int) ((int)M_m[i][0]-(int)M_m[i][1]+1);
				for(int k  = 0 ; k < tem ; k++){   //j建立Mi-mi+1空队列
					LinkedList lk = new LinkedList();
					que.put(k,lk);
				}
				for(int j = 0 ; j < List.size() ; j++){
					int num = List.get(j);        
					Float e = tempDT.getElement(num).getCondition().get(i);	
					LinkedList lk = (LinkedList) que.get((int)(e - M_m[i][1]));
					lk.add(num);
					que.put((int)(e - M_m[i][1]),lk);
				}

				List.clear();
				for(int j = 0 ; j < que.size(); j++){//第i趟收集结果
					List.addAll(que.get(j));
				}
			}
		
			ArrayList<MySet> equClass = new ArrayList<MySet>(0);
			MySet temp = new MySet();	
			temp.add(List.get(0));
		
			for(int i = 1 ; i < List.size() ; i++){//第i条记录
				boolean  flag =  true;
				for(int j = 0 ;  j < length ; j++){//第j个属性
					float  f_j_i=tempDT.getElement(List.get(i)).getCondition().get(j);  //第i条记录第j个属性的取值
					float  f_j2_i = tempDT.getElement(List.get(i-1)).getCondition().get(j);  //第i-1条记录第j个属性的取值
//					System.out.println(List.get(i)+" : "+f_j_i+"   "+List.get(i-1)+" :"+f_j2_i+" "+j);
					if(f_j_i != f_j2_i){
						flag = false;
						break;
					}
				}	
				if(flag){
					temp.add(List.get(i));
					if(i==List.size()-1){
						MySet temp3 = new MySet();	
						for(int  k = 0 ; k < temp.card()  ; k++){
							temp3.add(temp.keyAt(k));
						}
					equClass.add(temp3);
					}
				}
				else{
//					System.out.println(temp.toIntArrayList()+" 第 "+i+" ci");
					MySet temp2 = new MySet();	
					for(int  k = 0 ; k < temp.card()  ; k++){
						temp2.add(temp.keyAt(k));
					}
					equClass.add(temp2);
//					System.out.println("#### "+temp2.toIntArrayList());
					temp.clear();
					temp.add(List.get(i));
//					System.out.println("#### "+temp.toIntArrayList()+" "+List.get(i)+" "+i);
					if(i==List.size()-1){
						MySet temp3 = new MySet();	
//						System.out.println("*** "+temp.toIntArrayList()+" "+List.get(i)+" "+i);
						temp3.add(temp.keyAt(0));
						equClass.add(temp3);
					}
				}
			}
			return equClass;
		}
		else if(!availablePara_C && availablePara_D){	
			Iterator<Integer> itAllDecisionAttr= this.getDecisionSet().iterator();
			DecisionTable tempDT = (DecisionTable)this.clone();
			int U = tempDT.numberOfElements();
			int length = tempDT.getDecisionSet().size();	
			float ss[][] = tempDT.to_Decision_table();
			float M_m[][] = new float [length][2];
			
			for(int j = 0 ; j < length;j++){
				M_m[j][0]=-1f;
        		M_m[j][1]=1000000f;
			}
        
			for(int j = 0 ; j < length;j++){
				for(int i = 0 ; i < U ; i++){
					M_m[j][0]=Math.max(M_m[j][0], ss[i][j]);
					M_m[j][1]=Math.min(M_m[j][1], ss[i][j]);
				}
			}
        
			ArrayList<Integer> List = new ArrayList<Integer>(0);
			Iterator<MyElement> it = tempDT.getDT().iterator();
    	
			while(it.hasNext()){
				List.add(it.next().getU());		//保存x1,x2,xn编号
			}	

			for(int i = 0 ; i < length ; i++){		//3,for(i =1 ; i 
				Map<Integer,LinkedList> que = new LinkedHashMap<Integer,LinkedList>();
				int tem =  (int) ((int)M_m[i][0]-(int)M_m[i][1]+1);
				for(int k  = 0 ; k < tem ; k++){   //j建立Mi-mi+1空队列
					LinkedList lk = new LinkedList();
					que.put(k,lk);
				}
				for(int j = 0 ; j < List.size() ; j++){
					int num = List.get(j);        
					Float e = tempDT.getElement(num).getDecision().get(i);	
					LinkedList lk = (LinkedList) que.get((int)(e - M_m[i][1]));
					lk.add(num);
					que.put((int)(e - M_m[i][1]),lk);
				}

				List.clear();
				for(int j = 0 ; j < que.size(); j++){//第i趟收集结果
					List.addAll(que.get(j));
				}
			}
		
			ArrayList<MySet> equClass = new ArrayList<MySet>(0);
			MySet temp = new MySet();	
			temp.add(List.get(0));
		
			for(int i = 1 ; i < List.size() ; i++){//第i条记录
				boolean  flag =  true;
				for(int j = 0 ;  j < length ; j++){//第j个属性
					float  f_j_i=tempDT.getElement(List.get(i)).getDecision().get(j);  //第i条记录第j个属性的取值
					float  f_j2_i = tempDT.getElement(List.get(i-1)).getDecision().get(j);  //第i-1条记录第j个属性的取值
//					System.out.println(List.get(i)+" : "+f_j_i+"   "+List.get(i-1)+" :"+f_j2_i+" "+j);
					if(f_j_i != f_j2_i){
						flag = false;
						break;
					}
				}	
				if(flag){
					temp.add(List.get(i));
					if(i==List.size()-1){
						MySet temp3 = new MySet();	
						for(int  k = 0 ; k < temp.card()  ; k++){
							temp3.add(temp.keyAt(k));
						}
					equClass.add(temp3);
					}
				}
				else{
//					System.out.println(temp.toIntArrayList()+" 第 "+i+" ci");
					MySet temp2 = new MySet();	
					for(int  k = 0 ; k < temp.card()  ; k++){
						temp2.add(temp.keyAt(k));
					}
					equClass.add(temp2);
//					System.out.println("#### "+temp2.toIntArrayList());
					temp.clear();
					temp.add(List.get(i));
//					System.out.println("#### "+temp.toIntArrayList()+" "+List.get(i)+" "+i);
					if(i==List.size()-1){
						MySet temp3 = new MySet();	
//						System.out.println("*** "+temp.toIntArrayList()+" "+List.get(i)+" "+i);
						temp3.add(temp.keyAt(0));
						equClass.add(temp3);
					}
				}
			}
			return equClass;
		}
		else{
			System.out.println("您输入的属性不存在,不能为您求相应等价类");
			ArrayList<MySet> equClass = new ArrayList<MySet>(0);
			return equClass;
		}
	}
	DecisionTable attributeReduct() throws CloneNotSupportedException
	{
		DecisionTable reduction = (DecisionTable)this.clone();
		DecisionTable temp = (DecisionTable)this.clone();
		ArrayList<Integer> C = reduction.getConditionSet();
		Iterator<Integer> it = C.iterator();
		MySet pos = this.POS();//原决策表中D的C正域
		
		while(it.hasNext())
		{
			if(temp.removeConditionAttribute(it.next()).POS().isEqual(pos))
			{
				reduction = temp.clone();
			}
			else
			{
				temp = reduction.clone();
			}
		}
		return reduction;
	}
	DecisionTable attributeReductByRely() throws CloneNotSupportedException
	{
		DecisionTable reduction = this.clone();
		DecisionTable tempDT;
		double r = this.relyDegreeOfDToC();
		
		while(reduction.relyDegreeOfDToC() == r)
		{
			double sig = 1.0d;
			int index = 0;
			tempDT = reduction.clone();
			ArrayList<Integer> condition = reduction.getConditionSet();
			for(int i=0; i<condition.size(); i++)
			{
				if(reduction.sigOfAttribute(condition.get(i)) <sig)
				{
					sig = reduction.sigOfAttribute(condition.get(i));
					index = i;
				}
			}
			if(reduction.removeConditionAttribute(condition.get(index)).relyDegreeOfDToC() == r)
			{
				continue;
			}
			else
			{
				reduction = tempDT.clone();
				break;
			}
		}
		return reduction;
	}
	void print()
	{
		Iterator<Integer> itAttribute;
		itAttribute = this.getConditionSet().iterator();
		while(itAttribute.hasNext())
		{
			System.out.print(itAttribute.next() + "  ");
		}
		System.out.print("\t->");
		itAttribute = this.getDecisionSet().iterator();
		while(itAttribute.hasNext())
		{
			System.out.print("  " + itAttribute.next());
		}
		System.out.println();
	}
	
	private ArrayList<Integer> conditionSet = new ArrayList<Integer>(0);
	private ArrayList<Integer> decisionSet = new ArrayList<Integer>(0);
	private ArrayList<MyElement> DT = new ArrayList<MyElement>(0);
	int serialNumberOfElement;

	
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class MySet implements Cloneable
{
	public MySet(){}
	//用数组初始化集合
	public MySet(float A[])
	{
		for(float e : A)
		{
			arrayList.add(e);
		}
	}
	public MySet(MySet s)
	{
		arrayList = s.arrayList;
	}
	public MySet(ArrayList<Float> al)
	{
		arrayList = al;
	}
	public MySet clone() throws CloneNotSupportedException
	{
		MySet cloned = new MySet();
				
		Iterator<Float> itArrayList = this.arrayList.iterator();
		while(itArrayList.hasNext())
		{
			cloned.arrayList.add(itArrayList.next());
		}
		
		return cloned;
	}
	boolean isEmpty()
	{
		return arrayList.isEmpty();
	}
	int card()
	{
		return arrayList.size();
	}
	void clear()
	{
		arrayList.clear();
	}
	int find(float a)
	{
		for(int i=0; i<this.card();i++)
		{
			if(a == this.keyAt(i))
			{
				return i;
			}
		}
		return -1;
	}
	float keyAt(int loc)
	{
		return arrayList.get(loc);
	}
	void setKey(int loc,float val)
	{
		arrayList.set(loc, val);
	}
	void sort()
	{
		Collections.sort(arrayList);
	}
	void remove(float a)
	{
		if(-1 != find(a))
		{
			ArrayList<Float> temp = new ArrayList<Float>(0);
			for(int i=0;i<arrayList.size();i++)
			{
				if(arrayList.get(i) != a)
				{
					temp.add(arrayList.get(i));
				}
			}
			arrayList = temp;
		}
	}
	void add(float i)
	{
		arrayList.add(i);
	}
	boolean belongTo(MySet s)
	{
		for(float e : arrayList)
		{
			if(-1 == s.find(e))
				return false;
		}
		return true;
	}
	boolean isEqual(MySet s)
	{
		if(this.belongTo(s) && s.belongTo(this))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	MySet sub(MySet s)
	{
		if(s.belongTo(this))
		{
			MySet temp = new MySet(arrayList);
			for(int i=0; i<s.card(); i++)
			{
				temp.remove(s.keyAt(i));
			}
			return temp;
		}
		else
		{
			System.out.println("参数中集合并非当前集的子集,不能进行该运算");
			return null;
		}
	}
	MySet union(MySet s)
	{
		MySet temp = new MySet(arrayList);
		for(int i=0;i<s.card();i++)
			if(-1 == temp.find(s.keyAt(i)))
			{
				temp.add(s.keyAt(i));
			}
		return temp;
	}
	MySet interSect(MySet s)
	{
		MySet temp = new MySet(arrayList);
		for(int i=0; i<s.card();i++)
			if(-1 == s.find(s.keyAt(i)))
			{
				temp.remove(s.keyAt(i));
			}
		MySet VOfA = temp.valueOfAttribute();
		return VOfA;
	}
	MySet valueOfAttribute()
	{
		MySet temp = new MySet();
		for(int i=0; i<this.card();i++)
		{
			if(this.keyAt(i) != Integer.MAX_VALUE)
			{
				temp.add(this.keyAt(i));
				for(int j=i+1; j<this.card();j++)
				{
					if(this.keyAt(i) == this.keyAt(j))
					{
						this.setKey(j, Integer.MAX_VALUE);
					}
				}
			}
			else
				continue;
		}
		return temp;
	}
	ArrayList<Float> toIntArrayList()
	{
		ArrayList<Float> temp = new ArrayList<Float>(0);
		for(int i=0; i<this.card(); i++)
		{
			temp.add(this.keyAt(i));
		}
		return temp;
	}
	void print()
	{
		int i=0;
		System.out.print("{");
		if(!arrayList.isEmpty())
		{
			System.out.print(arrayList.get(i));
		}
		i++;
		for(; i<arrayList.size(); ++i)
		{
			System.out.print(","+arrayList.get(i));
		}
		System.out.print("}");
	}
	
	private ArrayList<Float> arrayList = new ArrayList<Float>(0);
}

上面最基本的三个类,根据最基本的3个类添加自己代码部分,实现功能。

你可能感兴趣的:(属性约简,属性约简论文实现局部框架)