예제 #1
0
파일: SEM.java 프로젝트: Navieclipse/KEEL
  /** Applies mutation in the new poblation */
  public void mutate() {
    int posiciones, i, j;
    double m;

    posiciones = n_genes * long_poblacion;

    if (prob_mutacion > 0)
      while (Mu_next < posiciones) {
        /* Se determina el cromosoma y el gen que corresponden a la posicion que
        se va a mutar */
        i = Mu_next / n_genes;
        j = Mu_next % n_genes;

        /* Se efectua la mutacion sobre ese gen */
        poblacion[i].mutate(j);

        /* Se marca el cromosoma mutado para su posterior evaluacion */
        poblacion[i].setEvaluated(false);

        /* Se calcula la siguiente posicion a mutar */
        if (prob_mutacion < 1) {
          m = Randomize.Rand();
          Mu_next += Math.ceil(Math.log(m) / Math.log(1.0 - prob_mutacion));
        } else Mu_next += 1;
      }

    Mu_next -= posiciones;
  }
예제 #2
0
  /**
   * Computes and stores the information gain of each attribute (variable) of the dataset
   *
   * @param Examples Set of instances of the dataset
   * @param nFile Name of the file
   */
  public void GainInit(TableDat Examples, String nFile) {

    int i, j, h, v;
    boolean encontrado;
    float info_gk, suma, suma1, suma2, p_clase, logaritmo;
    int num_clase[] = new int[n_clases];
    int n_vars = this.getNVars();
    int MaxVal = this.getMaxVal();
    float p[][] = new float[n_vars][MaxVal];
    float p_cond[][][] = new float[n_clases][n_vars][MaxVal];
    GI = new float[n_vars];
    intervalosGI = new float[n_vars][MaxVal];

    String contents;
    contents = "\n--------------------------------------------\n";
    contents += "|       Computation of the info gain       |\n";
    contents += "--------------------------------------------\n";
    contents += "Points for computation of the info gain:\n";

    // Loads the values for "intervalosGI"
    float marca, p_corte;
    for (int v1 = 0; v1 < n_vars; v1++) {
      if (this.getContinuous(v1) == true) {
        contents += "\tVariable " + var[v1].getName() + ": ";
        marca = (this.getMax(v1) - this.getMin(v1)) / ((float) (this.getNLabelVar(v1) - 1));
        p_corte = this.getMin(v1) + marca / 2;
        for (int et = 0; et < this.getNLabelVar(v1); et++) {
          intervalosGI[v1][et] = p_corte;
          contents += intervalosGI[v1][et] + "  ";
          p_corte += marca;
        }
        contents += "\n";
      }
    }

    // Structure initialization
    for (i = 0; i < n_clases; i++) num_clase[i] = 0;
    for (i = 0; i < n_vars; i++)
      for (j = 0; j < MaxVal; j++) {
        p[i][j] = 0; // Simple probabilities matrix
        for (h = 0; h < n_clases; h++) p_cond[h][i][j] = 0; // Conditional probabilities matrix
      }

    // Computation of the Simple and Conditional probabilities matrixs
    for (i = 0; i < Examples.getNEx(); i++) {
      num_clase[Examples.getClass(i)]++; // distribution by classes
      for (j = 0; j < n_vars; j++) { // distribution by values
        if (!this.getContinuous(j)) { // Discrete variable
          if (!Examples.getLost(this, i, j)) {
            // if the value is not a lost one
            p[j][(int) Examples.getDat(i, j)]++;
            p_cond[(int) Examples.getClass(i)][j][(int) Examples.getDat(i, j)]++;
          }
        } else { // Continuous variable
          encontrado = false;
          h = 0;
          while (!encontrado && h < this.getNLabelVar(j)) {
            if (Examples.getDat(i, j) <= intervalosGI[j][h]) encontrado = true;
            else h++;
          }
          if (encontrado == true) {
            p[j][h]++;
            p_cond[(int) Examples.getClass(i)][j][h]++;
          } else {
            if (!Examples.getLost(this, i, j)) {
              // Lost value
              System.out.println(
                  "Fallo al calcular la ganancia de infor, Variable " + j + " Ejemplo " + i);
              return;
            }
          }
        }
      }
    }
    for (h = 0; h < n_clases; h++)
      for (i = 0; i < n_vars; i++) {
        if (!this.getContinuous(i)) // Discrete variable
        for (j = (int) this.getMin(i); j <= (int) this.getMax(i); j++)
            p_cond[h][i][j] = p_cond[h][i][j] / Examples.getNEx();
        else // Continuous variable
        for (j = 0; j < this.getNLabelVar(i); j++)
            p_cond[h][i][j] = p_cond[h][i][j] / Examples.getNEx();
      }
    for (i = 0; i < n_vars; i++) {
      if (!this.getContinuous(i)) // Discrete variable
      for (j = (int) this.getMin(i); j <= (int) this.getMax(i); j++)
          p[i][j] = p[i][j] / Examples.getNEx();
      else // Continuous variable
      for (j = 0; j < this.getNLabelVar(i); j++) p[i][j] = p[i][j] / Examples.getNEx();
    }

    // Info Gk computation
    suma = 0;
    for (i = 0; i < n_clases; i++) {
      p_clase = ((float) num_clase[i]) / Examples.getNEx();
      if (p_clase > 0) {
        logaritmo = (float) (Math.log((double) p_clase) / Math.log(2));
        suma += p_clase * logaritmo;
      }
    }
    info_gk = (-1) * suma;

    // Information gain computation for each attibute
    for (v = 0; v < n_vars; v++) {
      suma = info_gk;
      suma1 = 0;
      if (!this.getContinuous(v)) { // Discrete Variable
        for (i = (int) this.getMin(v); i <= (int) this.getMax(v); i++) {
          suma2 = 0;
          for (j = 0; j < n_clases; j++)
            if (p_cond[j][v][i] > 0) {
              logaritmo = (float) (Math.log(p_cond[j][v][i]) / Math.log(2));
              suma2 += p_cond[j][v][i] * logaritmo;
            }
          suma1 += p[v][i] * (-1) * suma2;
        }
      } else { // Continuous Variable
        for (i = 0; i < this.getNLabelVar(v); i++) {
          suma2 = 0;
          for (j = 0; j < n_clases; j++)
            if (p_cond[j][v][i] > 0) {
              logaritmo = (float) (Math.log(p_cond[j][v][i]) / Math.log(2));
              suma2 += p_cond[j][v][i] * logaritmo;
            }
          suma1 += p[v][i] * (-1) * suma2;
        }
      }
      GI[v] = suma + (-1) * suma1;
    }

    contents += "Information Gain of the variables:\n";
    for (v = 0; v < n_vars; v++) {
      if (this.getContinuous(v) == true)
        contents += "\tVariable " + var[v].getName() + ": " + GI[v] + "\n";
    }

    if (nFile != "") Files.addToFile(nFile, contents);
  }