Qujianji概念算法

来源:互联网 发布:张家港软件动漫产业园 编辑:程序博客网 时间:2024/05/22 16:43
package cn.edu.xidian.w3ang.qt;import java.io.File;import java.io.FileWriter;import java.io.IOException;public class Combination{private static MyList<Integer> objects;private static MyList<Integer> attributes;private static boolean flagHasSons;private static int countNum=0;private static File outFile;private static FileWriter fileWriter;public static void main(String[] args){//形式背景文件//System.out.println(System.getProperty("user.dir"));String currentDir=System.getProperty("user.dir");//系统当前目录String contextName=currentDir+"/src/contexts/example1.cxt";//example1、2、3、4ContextReader contextReader = new ContextReader(new File(contextName));QTContext qtContext = contextReader.getContext();objects = qtContext.objects;attributes = qtContext.attributes;int endIndex=contextName.lastIndexOf('.');int startIndex=contextName.lastIndexOf('/');outFile=new File(contextName.substring(0,endIndex)+"_result.txt");//结果文件,和形势背景在同一目录下System.out.println(contextName.substring(startIndex+1));System.out.println(qtContext.oNum + " X " + qtContext.aNum + "\n" + qtContext.density);try{fileWriter=new FileWriter(outFile);} catch (IOException e){e.printStackTrace();}long starTime=System.currentTimeMillis();listCombineAll(qtContext.objects, new MyList<Integer>(), 0, qtContext.oNum);long endTime=System.currentTimeMillis();System.out.print("规模:"+qtContext.oNum + " X " + qtContext.aNum + ", 密度:" + qtContext.density);System.out.println(", 时间:"+(endTime-starTime)/1000.0+", 区间集概念个数:"+countNum);try{fileWriter.write("规模:"+qtContext.oNum + " X " + qtContext.aNum + ", 密度:" + qtContext.density);fileWriter.write(", 时间:"+(endTime-starTime)/1000.0+", 区间集概念个数:"+countNum);fileWriter.close();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}    /*    example1.cxt4 X 50.55规模:4 X 5, 密度:0.55, 时间:0.004, 区间集概念个数:18    example2.cxt10 X 300.57规模:10 X 30, 密度:0.57, 时间:0.508, 区间集概念个数:2171       example3.cxt12 X 300.575规模:12 X 30, 密度:0.575, 时间:2.989, 区间集概念个数:3132     example4.cxt15 X 300.5666666666666667规模:15 X 30, 密度:0.5666666666666667, 时间:60.665, 区间集概念个数:843*/static void listCombineAll(MyList<Integer> ar, MyList<Integer> result, int s, int t){// #对应的对MyList<Integer> intent1 = new MyList<>();MyList<Integer> intent2 = new MyList<>();if (IsDefinition(new MyList<>(), new MyList<>(), intent1, intent2))print(new MyList<>(), new MyList<>(), intent1, intent2);listSonCombine(ar, new MyList<>(), result, 0, ar.size());//System.out.println();try{fileWriter.write("\n");} catch (IOException e){e.printStackTrace();}// 非空组合对应的对listCombine(ar, result, s, t);}static void listCombine(MyList<Integer> ar, MyList<Integer> result, int s, int t){for (int i = s; i < t; i++){@SuppressWarnings("unchecked")MyList<Integer> new_result = (MyList<Integer>) result.clone();new_result.add(ar.get(i));// 列出包含result+ar.get(i)的所有组合@SuppressWarnings("unchecked")MyList<Integer> arSon = (MyList<Integer>) ar.clone();MyList<Integer> intent1 = new MyList<>();MyList<Integer> intent2 = new MyList<>();flagHasSons = false;if (IsDefinition(new_result, new_result, intent1, intent2)){flagHasSons=true;print(new_result, new_result, intent1, intent2);}for (int j = 0; j < new_result.size(); j++){Integer integer = new_result.get(j);arSon.remove(integer);}listSonCombine(arSon, new_result, new_result, 0, arSon.size());if(flagHasSons){try{fileWriter.write("\n");} catch (IOException e){e.printStackTrace();}//System.out.println();}listCombine(ar, new_result, i + 1, t);}}static void listSonCombine(MyList<Integer> ar, MyList<Integer> originResult, MyList<Integer> result, int s, int t){for (int i = s; i < t; i++){@SuppressWarnings("unchecked")MyList<Integer> new_result = (MyList<Integer>) result.clone();new_result.add(ar.get(i));MyList<Integer> intent1 = new MyList<>();MyList<Integer> intent2 = new MyList<>();if (IsDefinition(originResult, new_result, intent1, intent2)){flagHasSons=true;print(originResult, new_result, intent1, intent2);}listSonCombine(ar, originResult, new_result, i + 1, t);}}static void print(MyList<Integer> extent1, MyList<Integer> extent2, MyList<Integer> intent1,MyList<Integer> intent2){try{fileWriter.write("([");for (int obj : extent1)fileWriter.write(QTContext.objHashTable.get(obj)+" ");fileWriter.write(",");for (int obj : extent2)fileWriter.write(QTContext.objHashTable.get(obj)+" ");fileWriter.write("],[");for (int att : intent1)fileWriter.write(QTContext.attHashTable.get(att)+" ");fileWriter.write(",");for (int att : intent2)fileWriter.write(QTContext.attHashTable.get(att)+" ");fileWriter.write("]) ");} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}/*static void print(MyList<Integer> extent1, MyList<Integer> extent2, MyList<Integer> intent1,MyList<Integer> intent2){System.out.print("([");for (int obj : extent1)System.out.print(QTContext.objHashTable.get(obj));System.out.print(",");for (int obj : extent2)System.out.print(QTContext.objHashTable.get(obj));System.out.print("],[");for (int att : intent1)System.out.print(QTContext.attHashTable.get(att));System.out.print(",");for (int att : intent2)System.out.print(QTContext.attHashTable.get(att));System.out.print("]) ");}*/static boolean IsDefinition(MyList<Integer> obj1, MyList<Integer> obj2, MyList<Integer> intent1,MyList<Integer> intent2){// obj1 op1=>att1, obj2 op1=>att2 eg:(1,13)=>(e,de)// att1 op2=>obj3, att2 op2=>obj4 eg:(e,de)=>(1,13)// (obj1,obj2)=?=(att1,att2) 如果等于,则满足定义// return true;/*if(obj1.toString().equals("13")&&obj2.toString().equals("1324")){System.out.println("###");System.out.println(obj1+"->"+op1(obj1)+"->"+op2(op1(obj1)));System.out.println(obj2+"->"+op1(obj2)+"->"+op2(op1(obj2)));//13->45->13//1324->12345->1234}*/MyList<Integer> atts1 = op1(obj1);MyList<Integer> atts2 = op1(obj2);for (int i : atts1)intent1.add(i);for (int j : atts2)intent2.add(j);MyList<Integer> obj3=op2(atts1);MyList<Integer> obj4=op2(atts2);if(obj3.size()==obj1.size()&&obj4.size()==obj2.size()){obj3.removeAll(obj1);obj4.removeAll(obj2);if (obj3.isEmpty()&&obj4.isEmpty()){countNum++;return true;}elsereturn false;}else{return false;}}static MyList<Integer> op1(MyList<Integer> obj){@SuppressWarnings("unchecked")MyList<Integer> objCopy = (MyList<Integer>) obj.clone();// 遍历属性,如果当前属性*运算后=》new_obj被obj包含,则满足 eg:对象1=》属性e(5)MyList<Integer> att_result = new MyList<>();for (int att : attributes){MyList<Integer> extent = QTContext.attToExtent.get(att);if (objCopy.containsAll(extent)) // 不改变objCopy,所有不用单独clone个副本{att_result.add(att);}}return att_result;}static MyList<Integer> op2(MyList<Integer> att)// 定义给错了。。。{MyList<Integer> obj_result = new MyList<>();for (int obj : objects){MyList<Integer> intent = QTContext.objToIntent.get(obj);@SuppressWarnings("unchecked")MyList<Integer> attCopy2 = (MyList<Integer>) att.clone();attCopy2.retainAll(intent);if (attCopy2.size() > 0) // 交集不为空{obj_result.add(obj);}}return obj_result;}static MyList<Integer> intersectionOP(MyList<Integer> arrayList, MyList<Integer> arrayList2){arrayList.retainAll(arrayList2);return arrayList;}static MyList<Integer> unionOP(MyList<Integer> arrayList, MyList<Integer> arrayList2){arrayList.removeAll(arrayList2);arrayList.addAll(arrayList2);return arrayList;}}


MyList.java:

package cn.edu.xidian.w3ang.qt;import java.util.ArrayList;@SuppressWarnings("hiding")public class MyList<Object> extends ArrayList<Object>{private static final long serialVersionUID = 1L;@Overridepublic String toString(){// TODO Auto-generated method stub//return super.toString();String str ="";for(Object i:this){str+=i;}return str;}}

QTContext.java:

package cn.edu.xidian.w3ang.qt;import java.util.Hashtable;public class QTContext{public int oNum;public int aNum;public int countTrue;public double density;public MyList<MyList<Character>> values;public static Hashtable<Integer,String> objHashTable;public static Hashtable<Integer,String> attHashTable;public MyList<Integer> objects;public MyList<Integer> attributes;public static Hashtable<Integer,MyList<Integer>>objToIntent;public static Hashtable<Integer,MyList<Integer>>attToExtent;public QTContext(){values=new MyList<>();objHashTable=new Hashtable<>();attHashTable=new Hashtable<>();objects=new MyList<>();attributes=new MyList<>();objToIntent=new Hashtable<>();attToExtent=new Hashtable<>();}}


ContextReader.java:
package cn.edu.xidian.w3ang.qt;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class ContextReader{private QTContext qtContext = new QTContext();public QTContext getContext(){return qtContext;}public ContextReader(File file){BufferedReader bReader = null;try{bReader = new BufferedReader(new FileReader(file));bReader.readLine();// type:BbReader.readLine();qtContext.oNum = Integer.parseUnsignedInt(bReader.readLine());qtContext.aNum = Integer.parseUnsignedInt(bReader.readLine());bReader.readLine();for (int i = 0; i < qtContext.oNum; i++){QTContext.objHashTable.put(i + 1,bReader.readLine().trim());qtContext.objects.add(i + 1);}for (int i = 0; i < qtContext.aNum; i++){QTContext.attHashTable.put(i + 1,bReader.readLine().trim());qtContext.attributes.add(i + 1);}for (int i = 0; i < qtContext.oNum; i++)//形式背景矩阵{String relations = bReader.readLine();MyList<Character> arrayList = new MyList<>();MyList<Integer>intent=new MyList<>();for (int j = 0; j < relations.length(); j++)//XX.XX{if (relations.charAt(j) == 'X' || relations.charAt(j) == '1'){intent.add(j+1);//内涵。。。qtContext.countTrue++;arrayList.add('X');} else{arrayList.add('.');}}QTContext.objToIntent.put(i+1,intent);////qtContext.values.add(arrayList);}for(int j=0;j<qtContext.aNum;j++){MyList<Integer>extent=new MyList<>();for(int i=0;i<qtContext.oNum;i++)//对象{if(qtContext.values.get(i).get(j).equals('X'))extent.add(i+1);}QTContext.attToExtent.put(j+1, extent);}qtContext.density = qtContext.countTrue * 1.0 / (qtContext.aNum * qtContext.oNum);// while(bReader.readLine())} catch (IOException e){e.printStackTrace();}}}


0 0
原创粉丝点击