Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MOEA_GP;
- import org.moeaframework.Executor;
- import org.moeaframework.core.NondominatedPopulation;
- import org.moeaframework.core.Solution;
- import org.moeaframework.util.tree.Node;
- import core.ConfusionMatrix;
- import core.CsvDataFrame;
- import core.DataFrame;
- import core.TreeView;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Properties;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.plot.PlotOrientation;
- import org.jfree.data.xy.XYDataset;
- import org.jfree.data.xy.XYSeries;
- import org.jfree.data.xy.XYSeriesCollection;
- import org.jfree.ui.ApplicationFrame;
- import org.jfree.ui.RefineryUtilities;
- public class GP_exp {
- private NondominatedPopulation solutions ;
- private ArrayList<ConfusionMatrix> train_confusion_matrices ;
- private ArrayList<ConfusionMatrix> test_confusion_matrices ;
- private HashMap<String,String> run_configs ;
- 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) {
- //saving run parameters
- this.run_configs = new HashMap<String,String>() ;
- this.run_configs.put("algorithm", algo);
- this.run_configs.put("populationsize", String.valueOf(pop_size));
- this.run_configs.put("mutation_rate", String.valueOf(mut_prob));
- this.run_configs.put("crossover_rate", String.valueOf(cross_prob));
- this.run_configs.put("max_evaluation_number", String.valueOf(max_eval));
- this.run_configs.put("algorithm", algo);
- Object[] true_train_labels = train_data.get_column_data(topredict) ;
- Object[] true_test_labels = test_data.get_column_data(topredict) ;
- this.solutions = new Executor()
- .withProblemClass(Train_MOGP.class, train_data,true_train_labels,features,objectives)
- .withAlgorithm(algo)
- .withProperty("populationSize", pop_size)
- .withProperty("operator", "bx")
- .withProperty("operator", "ptm")
- .withProperty("bx.rate",cross_prob)
- .withProperty("ptm.rate", mut_prob)
- //.distributeOnAllCores()
- .withMaxEvaluations(max_eval)
- .run();
- this.train_confusion_matrices = new ArrayList<ConfusionMatrix>() ;
- this.test_confusion_matrices = new ArrayList<ConfusionMatrix>() ;
- for (Solution sol : this.solutions)
- {
- Boolean [] train_predictions = Gp_try.compute_predictions(sol,train_data.getData(),features.keySet()) ;
- Boolean [] test_predictions = Gp_try.compute_predictions(sol,test_data.getData(),features.keySet()) ;
- ConfusionMatrix train_confusion_matrix = new ConfusionMatrix(true_train_labels,train_predictions) ;
- ConfusionMatrix test_confusion_matrix = new ConfusionMatrix(true_test_labels,test_predictions) ;
- train_confusion_matrices.add(train_confusion_matrix) ;
- test_confusion_matrices.add(test_confusion_matrix) ;
- }
- }
- public NondominatedPopulation getSolutions()
- {
- return this.solutions ;
- }
- public ArrayList<ConfusionMatrix> getrain_confusion_matrices()
- {
- return this.train_confusion_matrices ;
- }
- public ArrayList<ConfusionMatrix> getest_confusion_matrices()
- {
- return this.test_confusion_matrices ;
- }
- public void save_data(String path)
- {
- try {
- //System.out.println("lol" + path);
- ObjectOutputStream train_confusion_matrix = new ObjectOutputStream(new FileOutputStream(path+"/train_confusion_matrix.txt"));
- ObjectOutputStream test_confusion_matrix = new ObjectOutputStream(new FileOutputStream(path+"/test_confusion_matrix.txt"));
- ObjectOutputStream parameters = new ObjectOutputStream(new FileOutputStream(path+"/parameters.txt"));
- int i = 0 ;
- for (ConfusionMatrix m : this.train_confusion_matrices)
- {
- train_confusion_matrix.writeChars(m.get_statistics().toString());
- i++ ;
- }
- i = 0 ;
- train_confusion_matrix.close();
- for (ConfusionMatrix m : this.test_confusion_matrices)
- {
- test_confusion_matrix.writeChars(m.get_statistics().toString());
- i++ ;
- }
- test_confusion_matrix.close();
- parameters.writeChars(this.run_configs.toString());
- parameters.close();
- i = 0 ;
- for (Solution sol : this.solutions)
- {
- TreeView btv = new TreeView(((Node)sol.getVariable(0)).getNodeAt(1), 1000, 1000);
- btv.save(path+"/model"+(i+1)+".png");
- i++ ;
- }
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static void main(String[] args) throws InterruptedException
- {
- HashMap<String,Integer> cols = new HashMap<String,Integer>() ;
- cols.put("dit",0) ;
- cols.put("noc",0) ;
- cols.put("cbo",0) ;
- cols.put("rfc",0) ;
- cols.put("lcom",0) ;
- cols.put("ca",0) ;
- cols.put("ce",0) ;
- cols.put("npm",0) ;
- cols.put("lcom3",1) ;
- cols.put("loc",0) ;
- cols.put("dam",0) ;
- cols.put("moa",0) ;
- cols.put("mfa",1) ;
- cols.put("cam",1) ;
- cols.put("ic",0) ;
- cols.put("cbm",0) ;
- cols.put("amc",1) ;
- cols.put("max_cc",0) ;
- cols.put("avg_cc",1) ;
- String path = "C:/Users/moataz/Desktop/data_folds" ;
- String result_path1 = "C:/Users/moataz/Desktop/mvmthtry" ;
- /*String result_path2 = "C:/Users/moataz/Desktop/output_redunduncy" ;
- String result_path3 = "C:/Users/moataz/Desktop/output_lost" ;*/
- File folder = new File(path);
- File[] listOfFiles = folder.listFiles();
- Boolean success1 ;
- Boolean success2 ;
- Boolean success3 ;
- String[] multi_metrics = {"tpr","tnr"} ;
- String[] single_metric = {"G"} ;
- String Folder_name1 ;
- String Folder_name2 ;
- String Folder_name3 ;
- for (int i = 0; i < listOfFiles.length; i++)
- {
- if (listOfFiles[i].getName().contains("train"))
- {
- CsvDataFrame train = new CsvDataFrame(",");
- CsvDataFrame test = new CsvDataFrame(",");
- try {
- train.read_data_from_file(path+"/"+listOfFiles[i].getName());
- System.out.println(train.count());
- test.read_data_from_file(path+"/"+listOfFiles[i].getName().replace("train", "test"));
- Folder_name1 = result_path1+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");
- /*Folder_name2 = result_path2+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");
- Folder_name3 = result_path3+"/"+listOfFiles[i].getName().replace("train_", "").replace(".csv","");*/
- System.out.println(Folder_name1) ;
- /*System.out.println(Folder_name2) ;
- System.out.println(Folder_name3) ; */
- success1 = (new File(Folder_name1).mkdirs());
- System.out.println(success1);
- /*success2 = (new File(Folder_name2).mkdirs());
- System.out.println(success2);
- success3 = (new File(Folder_name3).mkdirs());
- System.out.println(success3);*/
- if (success1 == true ||(success1 == false && (new File(Folder_name1)).list().length==0))
- {
- Run_MOGP GP_run1 = new Run_MOGP(train,cols,"bug","spea2",multi_metrics,3000,300,0.65,0.35) ;
- Run_MOGP GP_run2 = new Run_MOGP(train,cols,"bug","nsga3",multi_metrics,3000,300,0.65,0.35) ;
- Run_MOGP GP_run3 = new Run_MOGP(train,cols,"bug","GA",single_metric,3000,300,0.65,0.35) ;
- success2 = (new File(Folder_name1+"/spea2_algorithm").mkdirs()) ;
- if (success2)
- {
- GP_run1.start();
- GP_run1.join() ;
- }
- success2 = (new File(Folder_name1+"/nsga3_algorithm").mkdirs()) ;
- if (success2)
- {
- GP_run2.start();
- GP_run2.join() ;
- }
- success2 = (new File(Folder_name1+"/Single_objective").mkdirs()) ;
- if (success2)
- {
- GP_run3.start();
- GP_run3.join() ;
- }
- System.out.println("finish!") ;
- Test_Single_Model gp_exp = new Test_Single_Model(test,GP_run1) ;
- gp_exp.save(Folder_name1+"/spea2_algorithm");
- gp_exp = new Test_Single_Model(test,GP_run2) ;
- gp_exp.save(Folder_name1+"/nsga3_algorithm");
- gp_exp = new Test_Single_Model(test,GP_run3) ;
- gp_exp.save(Folder_name1+"/Single_objective");
- //GP_exp gp_exp = new GP_exp(train,test,cols,"bug","GA",metrics,10000,10000,0.35,0.65) ;
- }
- else
- {
- System.out.println(Folder_name1 + " done!") ;
- }
- /* if (success2 == true ||(success2 == false && (new File(Folder_name2)).list().length==0))
- {
- GP_exp gp_exp = new GP_exp(train,test,cols,"output lost","nsga3",metrics,4000,8000000,0.35,0.65) ;
- gp_exp.save_data(Folder_name2);
- }
- else
- {
- System.out.println(Folder_name2 + " done!") ;
- }
- if (success3 == true ||(success3 == false && (new File(Folder_name3)).list().length==0))
- {
- GP_exp gp_exp = new GP_exp(train,test,cols,"output redundancy","nsga3",metrics,4000,8000000,0.35,0.65) ;
- gp_exp.save_data(Folder_name3);
- }*/
- }
- catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement