Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.53 KB | None | 0 0
  1. package MOEA_GP;
  2.  
  3. import org.moeaframework.Executor;
  4. import org.moeaframework.core.NondominatedPopulation;
  5. import org.moeaframework.core.Solution;
  6. import org.moeaframework.util.tree.Node;
  7.  
  8. import core.ConfusionMatrix;
  9. import core.CsvDataFrame;
  10. import core.DataFrame;
  11. import core.TreeView;
  12.  
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.io.ObjectOutputStream;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.Properties;
  21.  
  22. import org.jfree.chart.ChartFactory;
  23. import org.jfree.chart.ChartPanel;
  24. import org.jfree.chart.JFreeChart;
  25. import org.jfree.chart.plot.PlotOrientation;
  26. import org.jfree.data.xy.XYDataset;
  27. import org.jfree.data.xy.XYSeries;
  28. import org.jfree.data.xy.XYSeriesCollection;
  29. import org.jfree.ui.ApplicationFrame;
  30. import org.jfree.ui.RefineryUtilities;
  31.  
  32. public class GP_exp {
  33.     private NondominatedPopulation solutions ;
  34.     private  ArrayList<ConfusionMatrix> train_confusion_matrices ;
  35.     private  ArrayList<ConfusionMatrix> test_confusion_matrices ;
  36.     private HashMap<String,String> run_configs ;
  37.     public GP_exp(DataFrame train_data,DataFrame test_data, HashMap<String,Integer> features,String topredict,String algo,String[] objectives,int pop_size,int max_eval,Double mut_prob,Double cross_prob) {
  38.        
  39.         //saving run parameters
  40.         this.run_configs = new HashMap<String,String>() ;
  41.        
  42.         this.run_configs.put("algorithm", algo);
  43.         this.run_configs.put("populationsize",  String.valueOf(pop_size));
  44.         this.run_configs.put("mutation_rate",  String.valueOf(mut_prob));
  45.         this.run_configs.put("crossover_rate",  String.valueOf(cross_prob));
  46.         this.run_configs.put("max_evaluation_number",  String.valueOf(max_eval));
  47.         this.run_configs.put("algorithm", algo);
  48.        
  49.         Object[] true_train_labels = train_data.get_column_data(topredict) ;
  50.         Object[] true_test_labels = test_data.get_column_data(topredict) ;
  51.        
  52.         this.solutions = new Executor()
  53.                   .withProblemClass(Train_MOGP.class, train_data,true_train_labels,features,objectives)
  54.                   .withAlgorithm(algo)
  55.                   .withProperty("populationSize", pop_size)
  56.                   .withProperty("operator", "bx")
  57.                   .withProperty("operator", "ptm")
  58.                   .withProperty("bx.rate",cross_prob)
  59.                   .withProperty("ptm.rate", mut_prob)
  60.                   //.distributeOnAllCores()
  61.                   .withMaxEvaluations(max_eval)
  62.                   .run();  
  63.        
  64.         this.train_confusion_matrices = new  ArrayList<ConfusionMatrix>() ;
  65.         this.test_confusion_matrices = new  ArrayList<ConfusionMatrix>() ;
  66.        
  67.        
  68.         for (Solution sol : this.solutions)
  69.         {
  70.              Boolean [] train_predictions = Gp_try.compute_predictions(sol,train_data.getData(),features.keySet()) ;
  71.              Boolean [] test_predictions = Gp_try.compute_predictions(sol,test_data.getData(),features.keySet()) ;
  72.              
  73.              ConfusionMatrix train_confusion_matrix = new ConfusionMatrix(true_train_labels,train_predictions) ;
  74.              ConfusionMatrix test_confusion_matrix = new ConfusionMatrix(true_test_labels,test_predictions) ;
  75.              
  76.              train_confusion_matrices.add(train_confusion_matrix) ;
  77.              test_confusion_matrices.add(test_confusion_matrix) ;
  78.              
  79.              
  80.         }
  81.    
  82.        
  83.     }
  84.     public NondominatedPopulation getSolutions()
  85.     {
  86.         return this.solutions ;
  87.     }
  88.     public ArrayList<ConfusionMatrix> getrain_confusion_matrices()
  89.     {
  90.         return this.train_confusion_matrices ;
  91.     }
  92.     public ArrayList<ConfusionMatrix> getest_confusion_matrices()
  93.     {
  94.         return this.test_confusion_matrices ;
  95.     }
  96.     public void save_data(String path)
  97.     {
  98.         try {
  99.             //System.out.println("lol" + path);
  100.             ObjectOutputStream train_confusion_matrix = new ObjectOutputStream(new FileOutputStream(path+"/train_confusion_matrix.txt"));
  101.             ObjectOutputStream test_confusion_matrix = new ObjectOutputStream(new FileOutputStream(path+"/test_confusion_matrix.txt"));
  102.             ObjectOutputStream parameters = new ObjectOutputStream(new FileOutputStream(path+"/parameters.txt"));
  103.            
  104.             int i = 0 ;
  105.             for (ConfusionMatrix m : this.train_confusion_matrices)
  106.             {
  107.                 train_confusion_matrix.writeChars(m.get_statistics().toString());
  108.                 i++ ;
  109.             }
  110.             i = 0 ;
  111.             train_confusion_matrix.close();
  112.             for (ConfusionMatrix m : this.test_confusion_matrices)
  113.             {
  114.                 test_confusion_matrix.writeChars(m.get_statistics().toString());
  115.                 i++ ;
  116.             }
  117.             test_confusion_matrix.close();
  118.             parameters.writeChars(this.run_configs.toString());
  119.             parameters.close();
  120.             i = 0 ;
  121.             for (Solution sol : this.solutions)
  122.             {
  123.                 TreeView btv = new TreeView(((Node)sol.getVariable(0)).getNodeAt(1), 1000, 1000);
  124.                 btv.save(path+"/model"+(i+1)+".png");
  125.                 i++ ;
  126.             }
  127.            
  128.         } catch (FileNotFoundException e) {
  129.             // TODO Auto-generated catch block
  130.             e.printStackTrace();
  131.         } catch (IOException e) {
  132.             // TODO Auto-generated catch block
  133.             e.printStackTrace();
  134.         }
  135.     }
  136.     public static void main(String[] args) throws InterruptedException
  137.     {
  138.        
  139.         HashMap<String,Integer> cols = new HashMap<String,Integer>() ;  
  140.         cols.put("dit",0) ;
  141.         cols.put("noc",0) ;
  142.         cols.put("cbo",0) ;
  143.         cols.put("rfc",0) ;
  144.         cols.put("lcom",0) ;
  145.         cols.put("ca",0) ;
  146.         cols.put("ce",0) ;
  147.         cols.put("npm",0) ;
  148.         cols.put("lcom3",1) ;
  149.         cols.put("loc",0) ;
  150.         cols.put("dam",0) ;
  151.         cols.put("moa",0) ;
  152.         cols.put("mfa",1) ;
  153.         cols.put("cam",1) ;
  154.         cols.put("ic",0) ;
  155.         cols.put("cbm",0) ;
  156.         cols.put("amc",1) ;
  157.         cols.put("max_cc",0) ;
  158.         cols.put("avg_cc",1) ;
  159.  
  160.         String path = "C:/Users/moataz/Desktop/data_folds" ;
  161.         String result_path1 = "C:/Users/moataz/Desktop/mvmthtry" ;
  162.         /*String result_path2 = "C:/Users/moataz/Desktop/output_redunduncy" ;
  163.         String result_path3 = "C:/Users/moataz/Desktop/output_lost" ;*/
  164.         File folder = new File(path);
  165.         File[] listOfFiles = folder.listFiles();
  166.         Boolean success1 ;
  167.         Boolean success2 ;
  168.         Boolean success3 ;
  169.         String[] multi_metrics = {"tpr","tnr"} ;
  170.         String[] single_metric = {"G"} ;
  171.         String Folder_name1 ;
  172.         String Folder_name2 ;
  173.         String Folder_name3 ;
  174.         for (int i = 0; i < listOfFiles.length; i++)
  175.         {
  176.             if (listOfFiles[i].getName().contains("train"))
  177.             {
  178.                 CsvDataFrame train = new CsvDataFrame(",");
  179.                 CsvDataFrame test = new CsvDataFrame(",");
  180.                 try {
  181.                     train.read_data_from_file(path+"/"+listOfFiles[i].getName());
  182.                     System.out.println(train.count());
  183.                     test.read_data_from_file(path+"/"+listOfFiles[i].getName().replace("train", "test"));
  184.                     Folder_name1 = result_path1+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");
  185.                     /*Folder_name2 = result_path2+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");
  186.                     Folder_name3 = result_path3+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");*/
  187.                     System.out.println(Folder_name1) ;
  188.                     /*System.out.println(Folder_name2) ;
  189.                     System.out.println(Folder_name3) ; */
  190.                     success1 = (new File(Folder_name1).mkdirs());
  191.                     System.out.println(success1);
  192.                     /*success2 = (new File(Folder_name2).mkdirs());
  193.                     System.out.println(success2);
  194.                     success3 = (new File(Folder_name3).mkdirs());
  195.                     System.out.println(success3);*/
  196.                    
  197.                        if (success1 == true ||(success1 == false && (new File(Folder_name1)).list().length==0))
  198.                        {
  199.                           Run_MOGP GP_run1 = new Run_MOGP(train,cols,"bug","spea2",multi_metrics,3000,300,0.65,0.35) ;
  200.                           Run_MOGP GP_run2 = new Run_MOGP(train,cols,"bug","nsga3",multi_metrics,3000,300,0.65,0.35) ;
  201.                           Run_MOGP GP_run3 = new Run_MOGP(train,cols,"bug","GA",single_metric,3000,300,0.65,0.35) ;
  202.                          
  203.                           success2 = (new File(Folder_name1+"/spea2_algorithm").mkdirs()) ;
  204.                           if (success2)
  205.                           {
  206.                               GP_run1.start();
  207.                               GP_run1.join() ;
  208.                           }
  209.                           success2 = (new File(Folder_name1+"/nsga3_algorithm").mkdirs()) ;
  210.                           if (success2)
  211.                           {
  212.                               GP_run2.start();
  213.                               GP_run2.join() ;
  214.                           }
  215.                           success2 = (new File(Folder_name1+"/Single_objective").mkdirs()) ;
  216.                           if (success2)
  217.                           {
  218.                               GP_run3.start();
  219.                               GP_run3.join() ;
  220.                           }
  221.                          
  222.                           System.out.println("finish!") ;
  223.                         Test_Single_Model gp_exp = new Test_Single_Model(test,GP_run1) ;
  224.                         gp_exp.save(Folder_name1+"/spea2_algorithm");
  225.                         gp_exp = new Test_Single_Model(test,GP_run2) ;
  226.                         gp_exp.save(Folder_name1+"/nsga3_algorithm");
  227.                         gp_exp = new Test_Single_Model(test,GP_run3) ;
  228.                         gp_exp.save(Folder_name1+"/Single_objective");
  229.                          
  230.                          
  231.                          
  232.                            //GP_exp gp_exp = new GP_exp(train,test,cols,"bug","GA",metrics,10000,10000,0.35,0.65) ;
  233.                        }
  234.                        else
  235.                        {
  236.                            System.out.println(Folder_name1 + " done!") ;
  237.                        }
  238.                       /* if (success2 == true ||(success2 == false && (new File(Folder_name2)).list().length==0))
  239.                        {
  240.                           GP_exp gp_exp = new GP_exp(train,test,cols,"output lost","nsga3",metrics,4000,8000000,0.35,0.65) ;
  241.                           gp_exp.save_data(Folder_name2);
  242.                        }
  243.                        else
  244.                        {
  245.                            System.out.println(Folder_name2 + " done!") ;
  246.                        }
  247.                        if (success3 == true ||(success3 == false && (new File(Folder_name3)).list().length==0))
  248.                        {
  249.                            GP_exp gp_exp = new GP_exp(train,test,cols,"output redundancy","nsga3",metrics,4000,8000000,0.35,0.65) ;
  250.                            gp_exp.save_data(Folder_name3);
  251.                        }*/
  252.                        
  253.                        }
  254.                    
  255.                    
  256.                  catch (FileNotFoundException e) {
  257.                    
  258.                     // TODO Auto-generated catch block
  259.                     e.printStackTrace();
  260.                 }
  261.                
  262.                
  263.             }
  264.         }
  265.  
  266.     }
  267.    
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement