Exemplo n.º 1
0
  public void evaluateBestIndividual(IntegerVectorIndividual ind, EvolutionState state)
      throws Exception {
    int[] genome = ((IntegerVectorIndividual) ind).genome;
    // ajustar com parametros "globais"
    // ClusWrapper.initialization(Dataset.getPath()+Dataset.getFileName() +
    // "-train.arff",Dataset.getPath()+Dataset.getFileName() + "-train.arff", Main.targets,false);

    int currentFold = Dataset.getCurrentFold();

    String trainSet =
        Dataset.getPath() + Dataset.getFileName() + "_fold" + (currentFold) + ".train";
    String trainValidSet =
        Dataset.getPath() + Dataset.getFileName() + "_fold" + (currentFold) + ".trainvalid";
    String valSet = Dataset.getPath() + Dataset.getFileName() + "_fold" + (currentFold) + ".valid";
    String testSet = Dataset.getPath() + Dataset.getFileName() + "_fold" + (currentFold) + ".test";

    // String trainSet = Dataset.getPath()+Dataset.getFileName() +
    // "_train_fold_"+(currentFold+1)+".arff";
    // String testSet = Dataset.getPath()+Dataset.getFileName() +
    // "_test_fold_"+(currentFold+1)+".arff";

    // ClusWrapper.initialization(Dataset.getPath()+Dataset.getFileName() + "-train.arff",
    // Dataset.getPath()+Dataset.getFileName() + "-test.arff", Main.targets,Main.randomForest);
    myMeasures measures = new myMeasures();

    if (Main.mlTask > 0) {

      double auroc[] = new double[3];
      double auprc[] = new double[3];

      /*if (Main.mlTask == 2) { // multi-threads)

      	ClusWrapperNonStatic object = new ClusWrapperNonStatic();
      	object.initialization(trainSet,valSet, Main.targets,Main.randomForest,true);
      	measures = object.evaluateIndividualClassification(genome,true);
      }
      else { // ==1, single thread
      	// true = classification or false = regression
      	*/

      ClusWrapper.clus = null;
      ClusWrapper.initialization(trainSet, valSet, Main.targets, Main.randomForest, true);
      measures = ClusWrapper.evaluateIndividualClassification(genome, true);
      //	}
      auroc[1] = measures.getAUROC()[1];
      auprc[1] = measures.getAUPRC()[1];

      /*if (Main.mlTask == 2) { // multi-threads)
      	ClusWrapperNonStatic object = new ClusWrapperNonStatic();

      	object.initialization(trainValidSet,testSet, Main.targets,Main.randomForest,true);
      	measures = object.evaluateIndividualClassification(genome,true);
      }
      else { // ==1, single thread
      	*/
      // true = classification or false = regression

      ClusWrapper.clus =
          null; // we really need to do this! otherwise, cluswrapper does not re-read the
                // corresponding partition.

      ClusWrapper.initialization(trainValidSet, testSet, Main.targets, Main.randomForest, true);
      measures = ClusWrapper.evaluateIndividualClassification(genome, true);
      //		}

      auroc[0] = measures.getAUROC()[0];
      auprc[0] = measures.getAUPRC()[0];
      auroc[2] = measures.getAUROC()[1];
      auprc[2] = measures.getAUPRC()[1];
      ;

      /*	System.err.println("trainValidSet: "+ trainValidSet);
      			System.err.println("testSet: "+ testSet);

      			System.err.println("auroc[0]: "+ auroc[0]);
      			System.err.println("auroc[2]: "+ auroc[2]);
      */

      for (int i = 0; i < 3; i++) {
        Main.measuresSingle[i][0][Dataset.getCurrentFold()][(Integer) state.job[0]] = auroc[i];
        Main.measuresSingle[i][1][Dataset.getCurrentFold()][(Integer) state.job[0]] = auprc[i];
      }

      // Printing results
      String test = new String();
      for (int i = 0; i < 2; i++)
        test =
            test
                + Main.measuresSingle[2][i][Dataset.getCurrentFold()][(Integer) state.job[0]]
                + ",";
      Main.pwTest.println(test);

      String full = new String();
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
          full =
              full
                  + Main.measuresSingle[j][i][Dataset.getCurrentFold()][(Integer) state.job[0]]
                  + ",";
        }
      }
      Main.pwAll.println(full);
    } else {
      double mae[] = new double[3];
      double mse[] = new double[3];
      double rmse[] = new double[3];

      // true = classification or false = regression
      ClusWrapper.initialization(trainSet, valSet, Main.targets, Main.randomForest, false);

      measures = ClusWrapper.evaluateIndividual(genome, true);
      mae[1] = measures.getMAE()[1];
      mse[1] = measures.getMSE()[1];
      rmse[1] = measures.getRMSE()[1]; // wrmse[1] = measures.getWRMSE()[0];

      ClusWrapper.initialization(trainValidSet, testSet, Main.targets, Main.randomForest, false);

      measures = ClusWrapper.evaluateIndividual(genome, true);
      mae[0] = measures.getMAE()[0];
      mse[0] = measures.getMSE()[0];
      rmse[0] = measures.getRMSE()[0]; // wrmse[0] = measures.getWRMSE()[0];
      mae[2] = measures.getMAE()[1];
      mse[2] = measures.getMSE()[1];
      rmse[2] = measures.getRMSE()[1]; // wrmse[2] = measures.getWRMSE()[1];

      for (int i = 0; i < 3; i++) {
        Main.measuresSingle[i][0][Dataset.getCurrentFold()][(Integer) state.job[0]] = mae[i];
        Main.measuresSingle[i][1][Dataset.getCurrentFold()][(Integer) state.job[0]] = mse[i];
        Main.measuresSingle[i][2][Dataset.getCurrentFold()][(Integer) state.job[0]] = rmse[i];
      }

      // Printing results
      String test = new String();
      for (int i = 0; i < 3; i++)
        test =
            test
                + Main.measuresSingle[2][i][Dataset.getCurrentFold()][(Integer) state.job[0]]
                + ",";
      Main.pwTest.println(test);

      String full = new String();
      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
          full =
              full
                  + Main.measuresSingle[j][i][Dataset.getCurrentFold()][(Integer) state.job[0]]
                  + ",";
        }
      }
      Main.pwAll.println(full);
    }

    // Main.pEvolution.println(mae[0] +","+ mse[0] +","+ rmse[0] +","+ mae[1] +","+ mse[1] +","+
    // rmse[1]);

  }
Exemplo n.º 2
0
  /** Method for evaluating individuals * */
  @Override
  public void evaluate(
      EvolutionState state,
      Individual ind,
      int subpopulation,
      int threadnum,
      ClusWrapperNonStatic objectClusWrapperNonStatic) {
    if (ind.evaluated) return;
    if (!(ind instanceof IntegerVectorIndividual))
      state.output.fatal("Whoa!  It's not an IntegerVectorIndividual!!!", null);
    boolean isIdeal;
    try {

      ((IntegerVectorIndividual) ind).genome =
          genomeAdjustment(((IntegerVectorIndividual) ind).genome);
      int[] genome = ((IntegerVectorIndividual) ind).genome;

      /*
      System.out.print("Genome = ["+genome[0]);
      for (int i = 1; i < genome.length; i++)
      	System.out.print(","+genome[i]);
      System.out.println("]");
       */

      // int currentFold = Dataset.getCurrentFold();
      // String trainSet = Dataset.getPath()+Dataset.getFileName() +
      // "_train_fold_"+(currentFold+1)+".arff";
      // String testSet = Dataset.getPath()+Dataset.getFileName() +
      // "_test_fold_"+(currentFold+1)+".arff";
      // ClusWrapper.initialization(trainSet,trainSet, Main.targets,Main.randomForest);

      // System.out.println("train set = "+trainSet);

      myMeasures measures = new myMeasures();

      if (Main.mlTask > 0) { // classification

        if (Main.mlTask == 2) { // multi-threads)
          /*
          int currentFold = Dataset.getCurrentFold();
          String trainSet = Dataset.getPath()+Dataset.getFileName() + "_fold"+(currentFold)+".train";
          String valSet = Dataset.getPath()+Dataset.getFileName() + "_fold"+(currentFold)+".valid";


          if(objectClusWrapperNonStatic.initialisationRun){
          	System.out.println("I've been intialised already");
          }
          /*else{
          	objectClusWrapperNonStatic.initialization(trainSet,valSet, Main.targets,Main.randomForest,true);
          }*/

          measures = objectClusWrapperNonStatic.evaluateIndividualClassification(genome, true);

          // System.out.println("AUROC = " + measures.getAUROC()[1]);
        } else { // == 1 (single thread)
          measures = ClusWrapper.evaluateIndividualClassification(genome, true);
          // System.out.println("static AUROC = " + measures.getAUROC()[1]);
        }

        double auroc = measures.getAUROC()[1];
        double auprc = measures.getAUPRC()[1];

        if (Main.fitnessType == 0) { // AUROC fitness
          if (Utils.eq(auroc, 1)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, (float) auroc, isIdeal);
        } else if (Main.fitnessType == 1) { // AUPRC fitness
          if (Utils.eq(auprc, 1)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, (float) auprc, isIdeal);
        } else {
          if (Utils.eq(auroc, 1)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, (float) auroc, isIdeal);
        }

        ind.evaluated = true;

        int currentGen = (int) state.generation;
        // System.out.println("current generation = "+currentGen);
        // System.out.println("number of generations = "+state.numGenerations);
        // disable for allowing multi-thread
        if ((currentGen == 0) || (currentGen == (state.numGenerations - 1))) { // first job
          // ClusWrapper.initialization(trainSet, testSet, Main.targets,Main.randomForest);
          // measures = new myMeasures();
          // measures = ClusWrapper.evaluateIndividual(genome,true);

          if (currentGen == 0)
            Main.pFirstGen.println(
                measures.getAUROC()[0]
                    + ","
                    + measures.getAUPRC()[0]
                    + ","
                    + measures.getAUROC()[1]
                    + ","
                    + measures.getAUPRC()[1]);
          else {
            Main.pLastGen.println(
                measures.getAUROC()[0]
                    + ","
                    + measures.getAUPRC()[0]
                    + ","
                    + measures.getAUROC()[1]
                    + ","
                    + measures.getAUPRC()[1]);
          }
        }

      } else { // regression

        // System.out.println("Main.targes = " + Main.targets);
        measures = ClusWrapper.evaluateIndividual(genome, true);

        double mae = measures.getMAE()[1];
        double mse = measures.getMSE()[1];
        double rmse = measures.getRMSE()[1];
        // double wrmse = measures.getWRMSE()[1];

        if (Main.fitnessType == 0) { // MAE fitness
          if (Utils.eq(mae, 0)) isIdeal = true;
          else isIdeal = false;
          // f.setStandardizedFitness(state, mae);
          ((SimpleFitness) ind.fitness).setFitness(state, ((float) 10000 - mae), isIdeal);
        } else if (Main.fitnessType == 1) { // MSE fitness
          if (Utils.eq(mse, 0)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, ((float) 10000 - mse), isIdeal);
        } else if (Main.fitnessType == 2) { // RMSE fitness
          if (Utils.eq(rmse, 0)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, ((float) 10000 - rmse), isIdeal);
        } else {
          if (Utils.eq(mae, 0)) isIdeal = true;
          else isIdeal = false;
          ((SimpleFitness) ind.fitness).setFitness(state, ((float) 10000 - mae), isIdeal);
        }

        ind.evaluated = true;

        int currentGen = (int) state.generation;
        // System.out.println("current generation = "+currentGen);
        // System.out.println("number of generations = "+state.numGenerations);
        // disable for allowing multi-thread
        if ((currentGen == 0) || (currentGen == (state.numGenerations - 1))) { // first job
          // ClusWrapper.initialization(trainSet, testSet, Main.targets,Main.randomForest);
          // measures = new myMeasures();
          // measures = ClusWrapper.evaluateIndividual(genome,true);

          if (currentGen == 0)
            Main.pFirstGen.println(
                measures.getMAE()[0]
                    + ","
                    + measures.getMSE()[0]
                    + ","
                    + measures.getRMSE()[0]
                    + ","
                    + measures.getMAE()[1]
                    + ","
                    + measures.getMSE()[1]
                    + ","
                    + measures.getRMSE()[1]);
          else {
            Main.pLastGen.println(
                measures.getMAE()[0]
                    + ","
                    + measures.getMSE()[0]
                    + ","
                    + measures.getRMSE()[0]
                    + ","
                    + measures.getMAE()[1]
                    + ","
                    + measures.getMSE()[1]
                    + ","
                    + measures.getRMSE()[1]);
          }
        }
        //
      }

    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  }