Advertisement
lamiastella

no run button

Jul 6th, 2016
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.48 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.sql.*;
  4.  
  5.  
  6. public class PF {
  7.    
  8.     private static boolean neato = false;
  9.     private static boolean showTransitionCount = true;
  10.    
  11.    
  12.    
  13.     public static void main(String[] Args){
  14.                 System.out.println("I am here");
  15.  
  16.         (new PF()).go();    
  17.     }      
  18.    
  19.     private void goX() {
  20.        
  21.         String students[] = { "class", "studentA", "studentB", "studentC" };
  22.         int n = students.length;
  23.        
  24.         Pathfinder pf[] = new Pathfinder[n];
  25.  
  26.         pf[0] = new Pathfinder();
  27.              
  28.         System.out.println("reading log file");
  29.         pf[0].readLogfile("example/log.txt");    
  30.  
  31.         for (int i = 1; i < n; i++) pf[i] = new Pathfinder(pf[0]);
  32.         for (int i = 1; i < n; i++) pf[i].restrictTo(students[i]);
  33.         for (int i = 0; i < n; i++) pf[i].setFrequencies();
  34.         for (int i = 0; i < n; i++) pf[i].MCN();
  35.  
  36.         for (int i = 0; i < n; i++) pf[i].outputGraph("example\\" + students[i]);
  37.  
  38. }
  39.  
  40.      
  41.     private void go1() {
  42.         Pathfinder pf = new Pathfinder();
  43.        
  44.         System.out.println("starting...");
  45.         pf.readLogfile("iisdata/visit.txt");
  46.         System.out.println("data read");
  47.         pf.setFrequencies();
  48.         System.out.println("frequencies set");
  49.         pf.MCN();
  50.         System.out.println("MCN computed");
  51.         pf.outputGraph("iisdata/visit");
  52.         System.out.println("XML written");
  53.     }
  54.    
  55.  
  56.     private void go() {
  57.         int[] classNumber = { 4 };
  58.         String[] date =     {"2001-10-19"};
  59.        
  60.         for (int i = 0; i < classNumber.length; i++) {
  61.             doClass(classNumber[i],date[i]);
  62.         }
  63.     }
  64.        
  65.         private void doClass(int classNumber, String dateRestriction) {        
  66.         RawData rd = new RawData();
  67.  
  68.         String[] nodeNames = rd.getNodeNames("%",classNumber, dateRestriction);
  69.         String[] studentNames = rd.getStudentNames("%", classNumber, dateRestriction);
  70.         int n = studentNames.length;
  71.        
  72.         System.out.print("Class " + classNumber + ", " + dateRestriction + " has " + n + " students");
  73.         System.out.println(" and " + nodeNames.length + " nodes.");
  74.  
  75.         Pathfinder[] pf = new Pathfinder[n + 1]; // for all students and the class
  76.  
  77.         Hashtable invNodeNames = new Hashtable();
  78.  
  79.         for (int i = 0; i < nodeNames.length; i++) {
  80.             invNodeNames.put(nodeNames[i].toLowerCase(),new Integer(i));
  81.         }
  82.  
  83.         for (int i = 0; i < n+1; i++) {
  84.             double[][] s;
  85.            
  86.             if (i < n) {
  87.                 System.out.print(studentNames[i] + " ");
  88.                 s = rd.getTransitionMatrix(classNumber, dateRestriction, studentNames[i], invNodeNames);
  89.             } else {
  90.                 System.out.println("all students");
  91.                 s = rd.getTransitionMatrix(classNumber, dateRestriction, "%", invNodeNames);
  92.             }
  93.  
  94.             pf[i] = new Pathfinder();
  95.             pf[i].setFrequencies(s);
  96.             pf[i].setNames(nodeNames);
  97.             pf[i].MCN();   
  98.  
  99.             if (i < n) {
  100.                 pf[i].outputGraph("data/" + "/" + classNumber + "_" + studentNames[i] + "_" + dateRestriction,showTransitionCount,neato);
  101.             } else {
  102.                 pf[i].outputGraph("data/" + "/" + classNumber + "_" + "CLASS_" + dateRestriction,showTransitionCount,neato);
  103.             }
  104.  
  105.         }      
  106.         System.out.println();
  107.        
  108.         try {
  109.  
  110.             //finally generating the comaprison output into a tab delimited file
  111.             System.out.println("Writing comparison file");
  112.  
  113.             String filename = "data/" + "/" + classNumber + "_" + "CLASS_" + dateRestriction + ".txt";
  114.             PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
  115.  
  116.             outFile.write("Comparing each student to the whole class\n\n");
  117.  
  118.             for (int i = 0; i < n; i++) {
  119.                 double sim = pf[i].sim(pf[n]);
  120.                 outFile.write(studentNames[i] + "\t" + sim + "\n");
  121.             }
  122.        
  123.             outFile.write("\nComparing all student to each other\n\n");
  124.        
  125.             outFile.write("\t");
  126.             for (int i = 0; i < n; i++) {
  127.                 outFile.write(studentNames[i] + "\t");
  128.             }
  129.             outFile.write("\n");
  130.  
  131.             double[][] matrix = new double[n][n];
  132.            
  133.             for (int i = 0; i < n; i++) {
  134.                 outFile.write(studentNames[i] + "\t");
  135.                 for (int j = 0; j < n; j++) {
  136.                     double sim = pf[i].sim(pf[j]);
  137.                     outFile.write(sim + "\t");
  138.                     matrix[i][j] = sim;
  139.                     if (sim <= 0) matrix[i][j] = -1;
  140.                 }
  141.                 outFile.write("\n");
  142.             }
  143.             outFile.close();
  144.            
  145.            
  146.             //now we treat similarities as frequencies and use pathfinder again
  147.             //that's probably silly, but...
  148.             /* ling uncomments   */
  149.             System.out.println("doing the pathfinder for the big thing");
  150.             Pathfinder p = new Pathfinder();
  151.             System.out.println("setting frequencies");
  152.             p.setFrequencies(matrix);
  153.             System.out.println("setting names");
  154.             p.setNames(studentNames);
  155.             System.out.println("MCN...");
  156.             p.MCN();   
  157.             System.out.println("MCN done!");
  158.  
  159.  
  160.             p.outputGraph("data/" + "/" + classNumber + "_allstudents_" + dateRestriction,showTransitionCount, neato, 10);
  161.  
  162.            
  163.             //now we do the real clustering
  164. //ling          for (int k = 1; k <= 3; k++) {  
  165.                
  166.                 int k=3;
  167.                 System.out.println("clustering with k = " + k);
  168.                 ClusterAlg clusterAlg = new ClusterAlg(k,studentNames);
  169.                 for(int i = 0; i < n; i++) {
  170.                     clusterAlg.addPNF(pf[i]);
  171.                 }
  172.                
  173.                 float v = 0, worstV = 0;
  174.                 v = clusterAlg.cluster();
  175.                 worstV += clusterAlg.worstClusterValue();
  176.  
  177.                 Pathfinder centroid[] = clusterAlg.getCentroids();
  178.                 for (int i = 0; i < k; i++) {
  179.                     centroid[i].outputGraph("data/" + "/" + classNumber +"_C_"+k+"_"+(i+1)+"_"+ dateRestriction,showTransitionCount, neato, 10);
  180.                 }
  181.                 System.out.println("k = " + k + " -> " + v + " -- " + worstV);
  182.                 System.out.println(clusterAlg);
  183. //          }
  184. /*ling end          */
  185.            
  186.            
  187.         } catch (Exception e) {
  188.             System.out.println(e);
  189.         }
  190.     }
  191.  
  192.  
  193.  
  194.  
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement