public JSONObject saveSystem() { try { fs.setData(data); return fs.saveAsJSON(); } catch (JSONException e) { System.out.println("could not create system JSON: " + e.getMessage()); return null; } }
public void loadSystem(JSONObject system, Instances data) { try { if (fs == null) fs = new FuzzySystem(); fs.setData(data); fs.loadFromJSON(system); } catch (JSONException e) { System.out.println("could not create system JSON: " + e.getMessage()); return; } }
public JSONObject getSystemJSON() { try { return fs.toJSON(); } catch (JSONException e) { System.out.println("could not create system JSON: " + e.getMessage()); return null; } }
/** * Name: createClassifier Goal: Evolves a fuzzy system according to given options and dataset * * @param train_data: the data used to compute performance of a system and provide data * information * @param options: the options used to control the genetic algorithm * @return boolean: indicates if creation was successful or not */ public boolean createClassifier(Object train_data, String options) { try { this.setOptions(weka.core.Utils.splitOptions(options)); } catch (Exception e) { this.appendError(this.getClass().getName(), "Option not recognised: " + e.getMessage()); System.out.println("option non reconnue:"); System.out.println(e.getMessage()); return false; } // construction du classificateur // les options sont separee pour plus de lisibilite en cas d'erreur Instances data = null; if (train_data instanceof String) data = DataLoader.loadData(null, null, (String) train_data, true, -1); else if (train_data instanceof Instances) data = (Instances) train_data; else { System.out.println("train data not supported!"); this.appendError(this.getClass().getName(), "Training data format not supported"); return false; } this.data = data; Coevolution ce = new Coevolution( data, mutation_rate, crossover_rate, selection_rate, pop_size, num_generations, is_binary, selection_algorithm, error_algorithm, elitism_rate, tournament_size, classification_weight, error_weight, rule_number_weight, var_per_rule_weight, rule_count); fs = ce.evolveSystem(); fs.setData(data); best_fitnesses = ce.getBestFitnesses(); if (getLogLevel() > 2) this.appendInfo(this.getClass().getName(), "Classifier was successfully created and trained"); return true; } /*end createClassifier*/
/** * Name: main Goal: example of JFuge's usage * * @param args: not used */ public static void main(String[] args) { Instances train_data = DataLoader.loadData( null, null, "/Users/numa/NUMA/Dossier Ecole/HEIG/Semestre 7/PDB/code/PDB_Trezzini/src/org/cheminfo/scripting/data/iris_train.arff", true, -1); Instances test_data = DataLoader.loadData( null, null, "/Users/numa/NUMA/Dossier Ecole/HEIG/Semestre 7/PDB/code/PDB_Trezzini/src/org/cheminfo/scripting/data/iris_test.arff", true, -1); double mutation_rate = 0.1; double crossover_rate = 0.2; double selection_rate = -1; int num_generations = 100; String selection_algorithm = TOURNAMENT_SELECTION; String error_algorithm = ERROR_MSE; double elitism_rate = 0.1; int tournament_size = 10; int pop = 200; double classification_weight = 1; double error_weight = 0; double rule_number_weight = 0; double var_per_rule_weight = 0.1; int rule_count = 5; FuzzySystem fs = null; double max = 0; for (int i = 0; i < 1; i++) { Coevolution ce = new Coevolution( train_data, mutation_rate, crossover_rate, selection_rate, pop, num_generations, false, selection_algorithm, error_algorithm, elitism_rate, tournament_size, classification_weight, error_weight, rule_number_weight, var_per_rule_weight, rule_count); FuzzySystem best = ce.evolveSystem(); System.out.println(best); if (best.getFitness() > max) { fs = best; max = best.getFitness(); } // System.out.println(best); System.out.println(i + ": " + best.getFitness()); } System.out.println(fs); System.out.println(max); double[] classif = fs.classifyInstances(test_data); double[][] distrib = fs.distributionForInstances(test_data); for (int i = 0; i < classif.length; i++) { System.out.println(classif[i]); } for (int i = 0; i < distrib.length; i++) { for (int j = 0; j < distrib[i].length; j++) { System.out.print(distrib[i][j] + " "); } System.out.println(); } } /*end main*/
/** * Name: distributionForInstances * * @param test_data: the data to classify * @return double[][]: the distribution for each instance */ public double[][] distributionForInstances(Instances test_data) { return fs.distributionForInstances(test_data); }
/** * Name: classifyInstances Goal: classifies each instance of the given dataset according to the * predictions made by the fuzzy system * * @param test_data: the data to classify * @return double[]: the classification for each instance */ public double[] classifyInstances(Instances test_data) { double[] result = fs.classifyInstances(test_data); return result; } /*end distributionForInstance*/