Exemplo n.º 1
0
  /**
   * Function that determines the cromosomes who have to be crossed and the other ones who have to
   * be removed It returns the number of remaining cromosomes in the poblation
   */
  private int recombinar(Chromosome C[], int d, int nNeg, int nPos, boolean majSelection) {

    int i, j;
    int distHamming;
    int tamC = 0;
    int n;

    if (majSelection) n = nNeg;
    else n = nNeg + nPos;

    for (i = 0; i < C.length / 2; i++) {
      distHamming = 0;
      for (j = 0; j < n; j++) if (C[i * 2].getGen(j) != C[i * 2 + 1].getGen(j)) distHamming++;
      if ((distHamming / 2) > d) {
        for (j = 0; j < n; j++) {
          if ((C[i * 2].getGen(j) != C[i * 2 + 1].getGen(j)) && Randomize.Rand() < 0.5) {
            if (C[i * 2].getGen(j)) C[i * 2].setGen(j, false);
            else if (Randomize.Rand() < prob0to1Rec) C[i * 2].setGen(j, true);
            if (C[i * 2 + 1].getGen(j)) C[i * 2 + 1].setGen(j, false);
            else if (Randomize.Rand() < prob0to1Rec) C[i * 2 + 1].setGen(j, true);
          }
        }
        tamC += 2;
      } else {
        C[i * 2].borrar();
        C[i * 2 + 1].borrar();
      }
    }

    return tamC;
  }
Exemplo n.º 2
0
  private void interpola(
      double ra[],
      double rb[],
      int na[],
      int nb[],
      boolean ma[],
      boolean mb[],
      double resS[],
      double resR[],
      int resN[],
      boolean resM[]) {

    int i;
    double diff;
    double gap;
    int suerte;

    for (i = 0; i < ra.length; i++) {
      if (ma[i] == true && mb[i] == true) {
        resM[i] = true;
        resS[i] = 0;
      } else {
        resM[i] = false;
        if (entradas[i].getType() == Attribute.REAL) {
          diff = rb[i] - ra[i];
          gap = Randomize.Rand();
          resR[i] = ra[i] + gap * diff;
          resS[i] =
              (ra[i] + entradas[i].getMinAttribute())
                  / (entradas[i].getMaxAttribute() - entradas[i].getMinAttribute());
        } else if (entradas[i].getType() == Attribute.INTEGER) {
          diff = rb[i] - ra[i];
          gap = Randomize.Rand();
          resR[i] = Math.round(ra[i] + gap * diff);
          resS[i] =
              (ra[i] + entradas[i].getMinAttribute())
                  / (entradas[i].getMaxAttribute() - entradas[i].getMinAttribute());
        } else {
          suerte = Randomize.Randint(0, 2);
          if (suerte == 0) {
            resN[i] = na[i];
          } else {
            resN[i] = nb[i];
          }
          resS[i] = (double) resN[i] / (double) (entradas[i].getNominalValuesList().size() - 1);
        }
      }
    }
  }
Exemplo n.º 3
0
  /** Applies a roulette wheel selection */
  public void selection() {
    RuleSet temp[];
    double probability[] = new double[long_poblacion];
    double total;
    double prob;
    int sel;

    temp = new RuleSet[long_poblacion];
    // sort the poblation in order of fitness
    Arrays.sort(poblacion, Collections.reverseOrder());

    probability[0] = poblacion[0].getFitness();
    for (int i = 1; i < long_poblacion; i++) {
      probability[i] = probability[i - 1] + poblacion[i].getFitness();
    }
    total = probability[long_poblacion - 1];
    for (int i = 0; i < long_poblacion; i++) {
      probability[i] /= total;
    }

    for (int i = 0; i < long_poblacion; i++) {
      prob = Randomize.Rand();
      sel = -1;
      for (int j = 0; j < long_poblacion && sel == -1; j++) {
        if (probability[j] > prob) sel = j;
      }
      temp[i] = new RuleSet(poblacion[sel]);
    }

    previousPob = poblacion;
    poblacion = temp;
  }
Exemplo n.º 4
0
  /** 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;
  }
Exemplo n.º 5
0
 /**
  * It performs a one point crossover in the new poblation, using adjacent chromosomes as parents
  */
 public void crossOver() {
   int parentspreserved = (int) (long_poblacion * survivorsPercent);
   for (int i = 0; i < long_poblacion; i = i + 2) {
     if (Randomize.Rand() < this.crossoverRate && i + 1 < long_poblacion)
       onePointCrossover(i, i + 1);
   }
 }
Exemplo n.º 6
0
  /**
   * KMeans constructor: the cluster centroids are obtained for the given dataset. Firstly, the
   * cluster's centroids are randomly chosen. Then the centroids are updated as the mean vlaue of
   * nearest examples in the dataset. The updating is carried out until no changes in the centroids
   * is achieved.
   *
   * @param X The dataset to be clusterized
   * @param nclusters The desired number of clusters
   * @param vrand The Randomize object to be used
   */
  public KMeans(double[][] X, int nclusters, Randomize vrand) {

    rand = vrand;
    train = X;
    clusters = nclusters;
    cclusters = new double[nclusters][X[0].length];

    for (int i = 0; i < nclusters; i++) {
      int pos = (int) (rand.Rand() * X.length);
      for (int j = 0; j < cclusters[i].length; j++) cclusters[i][j] = X[pos][j];
    }

    int[] C = new int[X.length];
    int[] C_old = new int[X.length];
    for (int i = 0; i < X.length; i++) {
      C_old[i] = nearestCentroid(X[i]);
    }
    centroidsUpdating(C_old);

    int cambios = 0, iteracion = 0;
    do {

      iteracion++;
      System.out.println("Iter=" + iteracion + " changes=" + cambios);
      cambios = 0;
      for (int i = 0; i < X.length; i++) {
        C[i] = nearestCentroid(X[i]);
        if (C[i] != C_old[i]) cambios++;
        C_old[i] = C[i];
      }
      centroidsUpdating(C);
    } while (cambios > 0);
  }
Exemplo n.º 7
0
  /* Selection based on the Baker's Estocastic Universal Sampling */
  void Select() {
    double expected, factor, perf, ptr, sum, rank_max, rank_min;
    int i, j, k, best, temp;

    rank_min = 0.75;

    /* we assign the ranking to each element:
       The best: ranking = long_poblacion-1
    The worse: ranking = 0 */
    for (i = 0; i < long_poblacion; i++) Old[i].n_e = 0;

    /* we look for the best ordered non element */
    for (i = 0; i < long_poblacion - 1; i++) {
      best = -1;
      perf = 0.0;
      for (j = 0; j < long_poblacion; j++) {
        if ((Old[j].n_e == 0) && (best == -1 || Old[j].Perf < perf)) {
          perf = Old[j].Perf;
          best = j;
        }
      }

      /* we assign the ranking */
      Old[best].n_e = long_poblacion - 1 - i;
    }

    /* we normalize the ranking */
    rank_max = 2.0 - rank_min;
    factor = (rank_max - rank_min) / (double) (long_poblacion - 1);

    /* we assign the number of replicas of each chormosome according to the select probability */
    k = 0;
    ptr = Randomize.Rand();
    for (sum = i = 0; i < long_poblacion; i++) {
      expected = rank_min + Old[i].n_e * factor;
      for (sum += expected; sum >= ptr; ptr++) sample[k++] = i;
    }

    /* we complete the population if necessary */
    if (k != long_poblacion) {
      for (; k < long_poblacion; k++) sample[k] = Randomize.RandintClosed(0, long_poblacion - 1);
    }

    /* we shuffle the selected chromosomes */
    for (i = 0; i < long_poblacion; i++) {
      j = Randomize.RandintClosed(i, long_poblacion - 1);
      temp = sample[j];
      sample[j] = sample[i];
      sample[i] = temp;
    }

    /* we create the new population */
    for (i = 0; i < long_poblacion; i++) {
      k = sample[i];
      for (j = 0; j < n_genes; j++) New[i].Gene[j] = Old[k].Gene[j];

      New[i].Perf = Old[k].Perf;
      New[i].n_e = 0;
    }
  }
Exemplo n.º 8
0
  /* Operador de Mutacion Uniforme */
  void Mutacion_Uniforme() {
    int posiciones, i, j;
    double m;

    posiciones = n_genes * long_poblacion;

    if (prob_mutacion > 0) {
      while (Mu_next < posiciones) {
        /* we determinate the chromosome and the gene */
        i = Mu_next / n_genes;
        j = Mu_next % n_genes;

        /* we mutate the gene */
        if (New[i].Gene[j] == '0') New[i].Gene[j] = '1';
        else New[i].Gene[j] = '0';

        New[i].n_e = 1;

        /* we calculate the next position */
        if (prob_mutacion < 1) {
          m = Randomize.Rand();
          Mu_next += (int) (Math.log(m) / Math.log(1.0 - prob_mutacion)) + 1;
        } else Mu_next += 1;
      }
    }

    Mu_next -= posiciones;
  }
Exemplo n.º 9
0
  /** Inicialization of the population */
  public void Initialize() {
    int i, j;

    last = (int) ((prob_cruce * long_poblacion) - 0.5);

    Trials = 0;

    if (prob_mutacion < 1.0) {
      Mu_next = (int) (Math.log(Randomize.Rand()) / Math.log(1.0 - prob_mutacion));
      Mu_next++;
    } else {
      Mu_next = 1;
    }

    for (j = 0; j < n_genes; j++) {
      New[0].GeneSel[j] = '1';
    }
    New[0].n_e = 1;

    for (i = 1; i < long_poblacion; i++) {
      for (j = 0; j < n_genes; j++) {
        if (Randomize.RandintClosed(0, 1) == 0) {
          New[i].GeneSel[j] = '0';
        } else {
          New[i].GeneSel[j] = '1';
        }
      }

      New[i].n_e = 1;
    }
  }
Exemplo n.º 10
0
  public void crossover() {

    int j, countCross = 0;

    Sampling samp = new Sampling(popSize);
    int p1 = -1;

    for (j = 0; j < popSize; j++) {

      if (Randomize.Rand() < Parameters.crossoverProbability) {

        if (p1 == -1) {
          p1 = samp.getSample();
        } else {
          int p2 = samp.getSample();
          crossTwoParents(p1, p2, countCross, countCross + 1);
          countCross += 2;
          p1 = -1;
        }
      } else {
        crossOneParent(samp.getSample(), countCross++);
      }
    }

    if (p1 != -1) {
      crossOneParent(p1, countCross++);
    }
  }
Exemplo n.º 11
0
 public void individualMutation() {
   int i;
   for (i = 0; i < popSize; i++) {
     if (Randomize.Rand() < Parameters.mutationProbability) {
       offspringPopulation[i].mutation();
     }
   }
 }
Exemplo n.º 12
0
  /**
   * Reinitializes the chromosome by using CHC diverge procedure
   *
   * @param r R factor of diverge
   * @param mejor Best chromosome found so far
   * @param prob Probability of setting a gen to 1
   */
  public void divergeCHC(double r, Cromosoma mejor, double prob) {

    int i;

    for (i = 0; i < cuerpo.length; i++) {
      if (Randomize.Rand() < r) {
        if (Randomize.Rand() < prob) {
          cuerpo[i] = true;
        } else {
          cuerpo[i] = false;
        }
      } else {
        cuerpo[i] = mejor.getGen(i);
      }
    }
    cruzado = true;
  } // end-method
Exemplo n.º 13
0
  /**
   * Builder. Generates a new subpopulation from a subset of the entire training set
   *
   * @param id Identifier of the population
   * @param train Subset of training data
   * @param out Output attribute of the subset of training data
   */
  public Subpopulation(int id, double train[][], int out[]) {

    // identify it
    ID = id;

    IDs = new int[size];

    for (int i = 0; i < size; i++) {
      IDs[i] = i;
    }

    // set data
    trainData = new double[train.length][train[0].length];
    trainOutput = new int[out.length];

    for (int i = 0; i < trainData.length; i++) {

      for (int j = 0; j < trainData[0].length; j++) {
        trainData[i][j] = train[i][j];
      }
      trainOutput[i] = out[i];
    }

    // Getting the number of different classes
    nClasses = 0;
    for (int i = 0; i < trainOutput.length; i++) {
      if (trainOutput[i] > nClasses) {
        nClasses = trainOutput[i];
      }
    }
    nClasses++;

    // create population
    population = new int[size][trainData.length];

    for (int i = 0; i < size; i++) {
      for (int j = 0; j < trainData.length; j++) {
        if (Randomize.Rand() < 0.5) {
          population[i][j] = 1;
        } else {
          population[i][j] = 0;
        }
      }
    }

    fitness = new double[size];
    Arrays.fill(fitness, -1.0);

    // initialize cache of distances
    generateCache();

    // initialize structures
    ISSelection = new int[trainData.length];
    selectedClasses = new int[nClasses];
    nearestN = new int[K];
    minDist = new double[K];
  } // end-method
Exemplo n.º 14
0
  /**
   * Mutation operator
   *
   * @param pMutacion1to0 Probability of change 1 to 0
   * @param pMutacion0to1 Probability of change 0 to 1
   */
  public void mutacion(double pMutacion1to0, double pMutacion0to1) {

    int i;

    for (i = 0; i < cuerpo.length; i++) {
      if (cuerpo[i]) {
        if (Randomize.Rand() < pMutacion1to0) {
          cuerpo[i] = false;
          cruzado = true;
        }
      } else {
        if (Randomize.Rand() < pMutacion0to1) {
          cuerpo[i] = true;
          cruzado = true;
        }
      }
    }
  } // end-method
  /**
   * Returns a vector with the discretized values.
   *
   * @param attribute
   * @param values
   * @param begin
   * @param end
   * @return vector with the discretized values
   */
  protected Vector discretizeAttribute(int attribute, int[] values, int begin, int end) {
    double quota = (end - begin + 1) / numInt;
    double dBound = 0.0;
    int i;
    int oldBound = 0;

    Vector cp = new Vector();

    for (i = 0; i < numInt - 1; i++) {
      dBound += quota;
      int iBound = (int) Math.round(dBound);
      if (iBound <= oldBound) continue;
      if (realValues[attribute][values[iBound - 1]] != realValues[attribute][values[iBound]]) {
        double cutPoint =
            (realValues[attribute][values[iBound - 1]] + realValues[attribute][values[iBound]])
                / 2.0;
        cp.addElement(new Double(cutPoint));
      } else {
        double val = realValues[attribute][values[iBound]];
        int numFW = 1;
        while (iBound + numFW <= end && realValues[attribute][values[iBound + numFW]] == val)
          numFW++;
        if (iBound + numFW > end) numFW = end - begin + 2;
        int numBW = 1;
        while (iBound - numBW > oldBound && realValues[attribute][values[iBound - numBW]] == val)
          numBW++;
        if (iBound - numBW == oldBound) numBW = end - begin + 2;

        if (numFW < numBW) {
          iBound += numFW;
        } else if (numBW < numFW) {
          iBound -= numBW;
        } else {
          if (numFW == end - begin + 2) {
            return cp;
          }
          if (Randomize.Rand() < 0.5) {
            iBound += numFW;
          } else {
            iBound -= numBW;
            iBound++;
          }
        }
        double cutPoint =
            (realValues[attribute][values[iBound - 1]] + realValues[attribute][values[iBound]])
                / 2.0;
        cp.addElement(new Double(cutPoint));
      }
      oldBound = iBound;
    }

    return cp;
  }
Exemplo n.º 16
0
  /**
   * It calculates PCBLX
   *
   * @param d it multiplies the module of the difference of the parents
   * @param P1 it is a father to cross
   * @param P2 it is the other father to do the cross
   * @param Hijo1 the son obtained
   * @param Hijo2 the other obtained
   * @param gens the size of the vector
   */
  private void xPC_BLX(
      double d, double[] P1, double[] P2, double[] Hijo1, double[] Hijo2, int gens) {

    double I, A1, C1;
    int i;

    for (i = 0; i < gens; i++) {

      I = d * Math.abs(P1[i] - P2[i]);
      A1 = P1[i] - I;
      if (A1 < Gene[i].min()) A1 = Gene[i].min();
      C1 = P1[i] + I;
      if (C1 > Gene[i].max()) C1 = Gene[i].max();
      Hijo1[i] = A1 + Randomize.Rand() * (C1 - A1);

      A1 = P2[i] - I;
      if (A1 < Gene[i].min()) A1 = Gene[i].min();
      C1 = P2[i] + I;
      if (C1 > Gene[i].max()) C1 = Gene[i].max();
      Hijo2[i] = A1 + Randomize.Rand() * (C1 - A1);
    }
  }
Exemplo n.º 17
0
 private void crossover() {
   boolean[] mascara = new boolean[train.getnInputs() + 1];
   for (int i = 0; i < selectos.length / 2; i++) {
     for (int j = 0; j < mascara.length; j++) {
       mascara[j] = (Randomize.Rand() > 0.5);
     }
     Individuo padre = cromosomas.get(selectos[i]);
     Individuo madre = cromosomas.get(selectos[i + 1]);
     Individuo hijo1 = new Individuo(padre, madre, mascara);
     Individuo hijo2 = new Individuo(madre, padre, mascara);
     hijos.add(hijo1);
     hijos.add(hijo2);
   }
 }
Exemplo n.º 18
0
  /**
   * Builder. Construct a random chromosome of specified size
   *
   * @param size Size of the chromosome
   */
  public Cromosoma(int size) {

    double u;
    int i;

    cuerpo = new boolean[size];
    for (i = 0; i < size; i++) {
      u = Randomize.Rand();
      if (u < 0.5) {
        cuerpo[i] = false;
      } else {
        cuerpo[i] = true;
      }
    }
    cruzado = true;
    valido = true;
  } // end-method
Exemplo n.º 19
0
  /** Process the training and test files provided in the parameters file to the constructor. */
  public void process() {
    // declarations
    double[] outputs;
    double[] outputs2;
    Instance neighbor;
    double dist, mean;
    int actual;
    Randomize rnd = new Randomize();
    Instance ex;
    gCenter kmeans = null;
    int iterations = 0;
    double E;
    double prevE;
    int totalMissing = 0;
    boolean allMissing = true;

    rnd.setSeed(semilla);
    // PROCESS
    try {

      // Load in memory a dataset that contains a classification problem
      IS.readSet(input_train_name, true);
      int in = 0;
      int out = 0;

      ndatos = IS.getNumInstances();
      nvariables = Attributes.getNumAttributes();
      nentradas = Attributes.getInputNumAttributes();
      nsalidas = Attributes.getOutputNumAttributes();

      X = new String[ndatos][nvariables]; // matrix with transformed data
      kmeans = new gCenter(K, ndatos, nvariables);

      timesSeen = new FreqList[nvariables];
      mostCommon = new String[nvariables];

      // first, we choose k 'means' randomly from all
      // instances
      totalMissing = 0;
      for (int i = 0; i < ndatos; i++) {
        Instance inst = IS.getInstance(i);
        if (inst.existsAnyMissingValue()) totalMissing++;
      }
      if (totalMissing == ndatos) allMissing = true;
      else allMissing = false;
      for (int numMeans = 0; numMeans < K; numMeans++) {
        do {
          actual = (int) (ndatos * rnd.Rand());
          ex = IS.getInstance(actual);
        } while (ex.existsAnyMissingValue() && !allMissing);

        kmeans.copyCenter(ex, numMeans);
      }

      // now, iterate adjusting clusters' centers and
      // instances to them
      prevE = 0;
      iterations = 0;
      do {
        for (int i = 0; i < ndatos; i++) {
          Instance inst = IS.getInstance(i);

          kmeans.setClusterOf(inst, i);
        }
        // set new centers
        kmeans.recalculateCenters(IS);
        // compute RMSE
        E = 0;
        for (int i = 0; i < ndatos; i++) {
          Instance inst = IS.getInstance(i);

          E += kmeans.distance(inst, kmeans.getClusterOf(i));
        }
        iterations++;
        // System.out.println(iterations+"\t"+E);
        if (Math.abs(prevE - E) == 0) iterations = maxIter;
        else prevE = E;
      } while (E > minError && iterations < maxIter);
      for (int i = 0; i < ndatos; i++) {
        Instance inst = IS.getInstance(i);

        in = 0;
        out = 0;

        for (int j = 0; j < nvariables; j++) {
          Attribute a = Attributes.getAttribute(j);

          direccion = a.getDirectionAttribute();
          tipo = a.getType();

          if (direccion == Attribute.INPUT) {
            if (tipo != Attribute.NOMINAL && !inst.getInputMissingValues(in)) {
              X[i][j] = new String(String.valueOf(inst.getInputRealValues(in)));
            } else {
              if (!inst.getInputMissingValues(in)) X[i][j] = inst.getInputNominalValues(in);
              else {
                actual = kmeans.getClusterOf(i);
                X[i][j] = new String(kmeans.valueAt(actual, j));
              }
            }
            in++;
          } else {
            if (direccion == Attribute.OUTPUT) {
              if (tipo != Attribute.NOMINAL && !inst.getOutputMissingValues(out)) {
                X[i][j] = new String(String.valueOf(inst.getOutputRealValues(out)));
              } else {
                if (!inst.getOutputMissingValues(out)) X[i][j] = inst.getOutputNominalValues(out);
                else {
                  actual = kmeans.getClusterOf(i);
                  X[i][j] = new String(kmeans.valueAt(actual, j));
                }
              }
              out++;
            }
          }
        }
      }
    } catch (Exception e) {
      System.out.println("Dataset exception = " + e);
      e.printStackTrace();
      System.exit(-1);
    }
    write_results(output_train_name);
    /** ************************************************************************************ */
    // does a test file associated exist?
    if (input_train_name.compareTo(input_test_name) != 0) {
      try {

        // Load in memory a dataset that contains a classification problem
        IStest.readSet(input_test_name, false);
        int in = 0;
        int out = 0;

        ndatos = IStest.getNumInstances();
        nvariables = Attributes.getNumAttributes();
        nentradas = Attributes.getInputNumAttributes();
        nsalidas = Attributes.getOutputNumAttributes();

        for (int i = 0; i < ndatos; i++) {
          Instance inst = IStest.getInstance(i);

          in = 0;
          out = 0;

          for (int j = 0; j < nvariables; j++) {
            Attribute a = Attributes.getAttribute(j);

            direccion = a.getDirectionAttribute();
            tipo = a.getType();

            if (direccion == Attribute.INPUT) {
              if (tipo != Attribute.NOMINAL && !inst.getInputMissingValues(in)) {
                X[i][j] = new String(String.valueOf(inst.getInputRealValues(in)));
              } else {
                if (!inst.getInputMissingValues(in)) X[i][j] = inst.getInputNominalValues(in);
                else {
                  actual = kmeans.getClusterOf(i);
                  X[i][j] = new String(kmeans.valueAt(actual, j));
                }
              }
              in++;
            } else {
              if (direccion == Attribute.OUTPUT) {
                if (tipo != Attribute.NOMINAL && !inst.getOutputMissingValues(out)) {
                  X[i][j] = new String(String.valueOf(inst.getOutputRealValues(out)));
                } else {
                  if (!inst.getOutputMissingValues(out)) X[i][j] = inst.getOutputNominalValues(out);
                  else {
                    actual = kmeans.getClusterOf(i);
                    X[i][j] = new String(kmeans.valueAt(actual, j));
                  }
                }
                out++;
              }
            }
          }
        }
      } catch (Exception e) {
        System.out.println("Dataset exception = " + e);
        e.printStackTrace();
        System.exit(-1);
      }
      write_results(output_test_name);
    }
  }
Exemplo n.º 20
0
  /** Function which restart the population */
  public void ReStart() {

    int i, j, i_mejor;

    if (Ajuste == 1) {
      if (BEST_CROM.entrado() == 1) {
        /* BUSCAR EL MEJOR ELEMENTO */
        for (i = i_mejor = 0; i < Popsize; i++)
          if (Better(Poblacion[i].perf(), Poblacion[i_mejor].perf())) i_mejor = i;

        for (j = 0; j < Genes; j++) Poblacion[0].set_gene(j, BEST_CROM.gene(j));
        for (j = 0; j < GenesA; j++) Poblacion[0].set_geneA(j, BEST_CROM.geneA(j));
        for (j = 0; j < n_reglas_total; j++) Poblacion[0].set_geneR(j, (char) 1);
        Poblacion[0].set_perf(BEST_CROM.perf());

        i = 1;
      } else {
        i = 0;
      }

      /* REINICIALIZAR TODOS MENOS EL PRIMERO */

      for (; i < Popsize; i++) {
        for (j = 0; j < Genes; j++) {
          Poblacion[i].set_gene(j, BEST_CROM.gene(j) + ((Randomize.Rand() - 0.5) / 4.0));
          if (Poblacion[i].gene(j) > Gene[j].max()) Poblacion[i].set_gene(j, Gene[j].max());
          if (Poblacion[i].gene(j) < Gene[j].min()) Poblacion[i].set_gene(j, Gene[j].min());
        }
        for (j = 0; j < GenesA; j++) {
          Poblacion[i].set_geneA(j, BEST_CROM.geneA(j) + ((Randomize.Rand() - 0.5) / 4.0));
          if (Poblacion[i].geneA(j) > Gene[j].max()) Poblacion[i].set_geneA(j, Gene[j].max());
          if (Poblacion[i].geneA(j) < Gene[j].min()) Poblacion[i].set_geneA(j, Gene[j].min());
        }
        for (j = 0; j < n_reglas_total; j++) Poblacion[i].set_geneR(j, (char) 1);
      }
      THRESHOLD = (double) ((Genes + GenesA) * BITS_GEN / 4.0);
      reduccionIni = THRESHOLD * 0.001;
    } else {
      if (BEST_CROM.entrado() == 1) {
        /* BUSCAR EL MEJOR ELEMENTO */
        for (i = i_mejor = 0; i < Popsize; i++)
          if (Better(Poblacion[i].perf(), Poblacion[i_mejor].perf())) i_mejor = i;
        /* COLOCAR EL MEJOR EN LA PRIMERA POSICION */
        for (j = 0; j < Genes; j++) Poblacion[0].set_gene(j, BEST_CROM.gene(j));
        for (j = 0; j < GenesA; j++) Poblacion[0].set_geneA(j, BEST_CROM.geneA(j));
        for (j = 0; j < n_reglas_total; j++) Poblacion[0].set_geneR(j, (char) 1);
        Poblacion[0].set_perf(BEST_CROM.perf());

        i = 1;
      } else i = 0;

      /* REINICIALIZAR TODOS MENOS EL PRIMERO */
      for (; i < Popsize; i++) {
        for (j = 0; j < Genes; j++) Poblacion[i].set_gene(j, BEST_CROM.gene(j));
        for (j = 0; j < GenesA; j++)
          Poblacion[i].set_geneA(
              j, Gene[j].min() + (Gene[j].max() - Gene[j].min()) * Randomize.Rand());
        for (j = 0; j < n_reglas_total; j++) Poblacion[i].set_geneR(j, '1');
      }
      THRESHOLD = (double) (GenesA / 4.5);
      reduccionIni = THRESHOLD * 0.001;
    }

    Reiniciado = 1;
  }
Exemplo n.º 21
0
  /** Performs a new generation of the subpopulation */
  public void doGeneration() {

    int notSave;
    int father, mother;

    // evaluate population
    for (int i = 0; i < size; i++) {

      fitness[i] = evaluateFitness(i);
    }

    sortPopulation();

    // apply elitism
    notSave = size - ((int) ((double) size * Elitism));

    newPopulation = new int[size][trainData.length];

    // generate new population
    for (int i = 0; i < notSave; i += 2) {

      // selection of parents

      father = Randomize.RandintClosed(0, size - notSave - 1);
      do {
        mother = Randomize.RandintClosed(0, size - notSave - 1);
      } while (mother == father);

      // crossover
      HUX(father, mother, i);
    }

    // merge new population
    int basis = size - notSave;
    for (int i = 0; i < notSave; i++) {

      for (int j = 0; j < population[i].length; j++) {
        population[basis + i][j] = newPopulation[i][j];
      }
      fitness[basis + i] = -1.0;
      CoCoIS.RequestReevaluation(ID, getKey(basis + i));
    }

    // apply random mutation
    for (int i = 0; i < size; i++) {

      if (Randomize.Rand() < PRandom) {

        for (int j = 0; j < population[i].length; j++) {

          if (Randomize.Rand() < PBit) {
            population[i][j] = (population[i][j] + 1) % 2;
          }
        }
        fitness[i] = -1.0;
        CoCoIS.RequestReevaluation(ID, getKey(i));
      }
    }

    // apply Rnn mutation
    for (int i = 0; i < size; i++) {

      if (Randomize.Rand() < PRnn) {

        rnnMutation(i);

        fitness[i] = -1.0;
        CoCoIS.RequestReevaluation(ID, getKey(i));
      }
    }
  } // end-method
Exemplo n.º 22
0
  /** It launches the algorithm */
  public void execute() {

    int i, j, k, l;
    int t;
    int ele;
    double prob[];
    double aux;
    double NUmax = 1.5; // used for lineal ranking
    double NUmin = 0.5; // used for lineal ranking
    double pos1, pos2;
    int sel1, sel2;
    int data[][];
    int infoAttr[];
    int classData[];
    Vector<Rule> contenedor = new Vector<Rule>();
    Vector<Rule> conjR = new Vector<Rule>();
    Rule tmpRule;
    Condition tmpCondition[] = new Condition[1];
    RuleSet population[];
    RuleSet hijo1, hijo2;

    if (somethingWrong) { // We do not execute the program
      System.err.println("An error was found, the data-set has numerical values.");
      System.err.println("Aborting the program");
      // We should not use the statement: System.exit(-1);
    } else {
      Randomize.setSeed(seed);

      nClasses = train.getnClasses();

      /*Build the nominal data information*/
      infoAttr = new int[train.getnInputs()];
      for (i = 0; i < infoAttr.length; i++) {
        infoAttr[i] = train.numberValues(i);
      }

      data = new int[train.getnData()][train.getnInputs()];
      for (i = 0; i < data.length; i++) {
        for (j = 0; j < data[i].length; j++) {
          if (train.isMissing(i, j)) data[i][j] = -1;
          else data[i][j] = train.valueExample(i, j);
        }
      }

      classData = new int[train.getnData()];
      for (i = 0; i < classData.length; i++) {
        classData[i] = train.getOutputAsInteger(i);
      }

      /*Find first-order rules which result interesting*/

      for (i = 0; i < nClasses; i++) {
        for (j = 0; j < infoAttr.length; j++) {
          for (k = 0; k < infoAttr[j]; k++) {
            tmpCondition[0] = new Condition(j, k);
            tmpRule = new Rule(tmpCondition);
            if (Math.abs(computeAdjustedResidual(data, classData, tmpRule, i)) > 1.96) {
              if (!contenedor.contains(tmpRule)) {
                contenedor.add(tmpRule);
                conjR.add(tmpRule);
              }
            }
          }
        }
      }

      // Construct the Baker selection roulette
      prob = new double[popSize];
      for (j = 0; j < popSize; j++) {
        aux = (double) (NUmax - NUmin) * ((double) j / (popSize - 1));
        prob[j] = (double) (1.0 / (popSize)) * (NUmax - aux);
      }
      for (j = 1; j < popSize; j++) prob[j] = prob[j] + prob[j - 1];

      /*Steady-State Genetic Algorithm*/
      ele = 2;
      population = new RuleSet[popSize];
      while (conjR.size() >= 2) {
        t = 0;

        System.out.println("Producing rules of level " + ele);

        for (i = 0; i < population.length; i++) {
          population[i] = new RuleSet(conjR);
          population[i].computeFitness(data, classData, infoAttr, contenedor, nClasses);
        }

        Arrays.sort(population);

        while (t < numGenerations && !population[0].equals(population[popSize - 1])) {
          System.out.println("Generation " + t);
          t++;

          /*Baker's selection*/
          pos1 = Randomize.Rand();
          pos2 = Randomize.Rand();
          for (l = 0; l < popSize && prob[l] < pos1; l++) ;
          sel1 = l;
          for (l = 0; l < popSize && prob[l] < pos2; l++) ;
          sel2 = l;

          hijo1 = new RuleSet(population[sel1]);
          hijo2 = new RuleSet(population[sel2]);

          if (Randomize.Rand() < pCross) {
            RuleSet.crossover1(hijo1, hijo2);
          } else {
            RuleSet.crossover2(hijo1, hijo2);
          }

          RuleSet.mutation(hijo1, conjR, pMut, data, classData, infoAttr, contenedor, nClasses);
          RuleSet.mutation(hijo2, conjR, pMut, data, classData, infoAttr, contenedor, nClasses);

          hijo1.computeFitness(data, classData, infoAttr, contenedor, nClasses);
          hijo2.computeFitness(data, classData, infoAttr, contenedor, nClasses);

          population[popSize - 2] = new RuleSet(hijo1);
          population[popSize - 1] = new RuleSet(hijo2);

          Arrays.sort(population);
        }

        /*Decode function*/
        ele++;
        conjR.removeAllElements();
        System.out.println(
            "Fitness of the best chromosome in rule level " + ele + ": " + population[0].fitness);
        for (i = 0; i < population[0].getRuleSet().length; i++) {
          if (Math.abs(computeAdjustedResidual(data, classData, population[0].getRule(i), i))
              > 1.96) {
            if (validarRegla(population[0].getRule(i))
                && !contenedor.contains(population[0].getRule(i))) {
              contenedor.add(population[0].getRule(i));
              conjR.add(population[0].getRule(i));
            }
          }
        }
      }

      // Finally we should fill the training and test output files
      doOutput(this.val, this.outputTr, data, classData, infoAttr, contenedor, nClasses);
      doOutput(this.test, this.outputTst, data, classData, infoAttr, contenedor, nClasses);

      /*Print the rule obtained*/
      for (i = contenedor.size() - 1; i >= 0; i--) {
        if (reglaPositiva(
            this.train, data, classData, infoAttr, nClasses, contenedor.elementAt(i))) {
          Fichero.AnadirtoFichero(outputRule, contenedor.elementAt(i).toString(train));
          Fichero.AnadirtoFichero(
              outputRule,
              " -> "
                  + consecuente(
                      this.train, data, classData, infoAttr, nClasses, contenedor.elementAt(i))
                  + "\n");
        }
      }
      System.out.println("Algorithm Finished");
    }
  }
Exemplo n.º 23
0
  /** Executes the algorithm */
  public void ejecutar() {

    int i, j, l;
    int nClases;
    double conjS[][];
    double conjR[][];
    int conjN[][];
    boolean conjM[][];
    int clasesS[];
    int nSel = 0;
    Cromosoma poblacion[];
    int ev = 0;
    double prob[];
    double NUmax = 1.5;
    double NUmin = 0.5; // used for lineal ranking
    double aux;
    double pos1, pos2;
    int sel1, sel2, comp1, comp2;
    Cromosoma newPob[];

    long tiempo = System.currentTimeMillis();

    /*Getting the number of different clases*/
    nClases = 0;
    for (i = 0; i < clasesTrain.length; i++) if (clasesTrain[i] > nClases) nClases = clasesTrain[i];
    nClases++;

    /*Random inicialization of the population*/
    Randomize.setSeed(semilla);
    poblacion = new Cromosoma[tamPoblacion];
    for (i = 0; i < tamPoblacion; i++) poblacion[i] = new Cromosoma(datosTrain.length);

    /*Initial evaluation of the population*/
    for (i = 0; i < tamPoblacion; i++)
      poblacion[i].evalua(
          datosTrain,
          realTrain,
          nominalTrain,
          nulosTrain,
          clasesTrain,
          alfa,
          kNeigh,
          nClases,
          distanceEu);

    if (torneo) {
      while (ev < nEval) {
        newPob = new Cromosoma[2];

        /*Binary tournament selection*/
        comp1 = Randomize.Randint(0, tamPoblacion - 1);
        do {
          comp2 = Randomize.Randint(0, tamPoblacion - 1);
        } while (comp2 == comp1);
        if (poblacion[comp1].getCalidad() > poblacion[comp2].getCalidad()) sel1 = comp1;
        else sel1 = comp2;
        comp1 = Randomize.Randint(0, tamPoblacion - 1);
        do {
          comp2 = Randomize.Randint(0, tamPoblacion - 1);
        } while (comp2 == comp1);
        if (poblacion[comp1].getCalidad() > poblacion[comp2].getCalidad()) sel2 = comp1;
        else sel2 = comp2;

        if (Randomize.Rand() < pCruce) { // there is cross
          crucePMX(poblacion, newPob, sel1, sel2);
        } else { // there is not cross
          newPob[0] = new Cromosoma(datosTrain.length, poblacion[sel1]);
          newPob[1] = new Cromosoma(datosTrain.length, poblacion[sel2]);
        }

        /*Mutation of the cromosomes*/
        for (i = 0; i < 2; i++) newPob[i].mutacion(pMutacion1to0, pMutacion0to1);

        /*Evaluation of the population*/
        for (i = 0; i < 2; i++)
          if (!(newPob[i].estaEvaluado())) {
            newPob[i].evalua(
                datosTrain,
                realTrain,
                nominalTrain,
                nulosTrain,
                clasesTrain,
                alfa,
                kNeigh,
                nClases,
                distanceEu);
            ev++;
          }

        /*Replace the two worst*/
        Arrays.sort(poblacion);
        poblacion[tamPoblacion - 2] = new Cromosoma(datosTrain.length, newPob[0]);
        poblacion[tamPoblacion - 1] = new Cromosoma(datosTrain.length, newPob[1]);
      }
    } else {
      /*Get the probabilities of lineal ranking in case of not use binary tournament*/
      prob = new double[tamPoblacion];
      for (i = 0; i < tamPoblacion; i++) {
        aux = (double) (NUmax - NUmin) * ((double) i / (tamPoblacion - 1));
        prob[i] = (double) (1.0 / (tamPoblacion)) * (NUmax - aux);
      }
      for (i = 1; i < tamPoblacion; i++) prob[i] = prob[i] + prob[i - 1];

      while (ev < nEval) {
        /*Sort the population by quality criterion*/
        Arrays.sort(poblacion);

        newPob = new Cromosoma[2];
        pos1 = Randomize.Rand();
        pos2 = Randomize.Rand();
        for (j = 0; j < tamPoblacion && prob[j] < pos1; j++) ;
        sel1 = j;
        for (j = 0; j < tamPoblacion && prob[j] < pos2; j++) ;
        sel2 = j;

        if (Randomize.Rand() < pCruce) { // there is cross
          crucePMX(poblacion, newPob, sel1, sel2);
        } else { // there is not cross
          newPob[0] = new Cromosoma(datosTrain.length, poblacion[sel1]);
          newPob[1] = new Cromosoma(datosTrain.length, poblacion[sel2]);
        }

        /*Mutation of the cromosomes*/
        for (i = 0; i < 2; i++) newPob[i].mutacion(pMutacion1to0, pMutacion0to1);

        /*Evaluation of the population*/
        for (i = 0; i < 2; i++)
          if (!(newPob[i].estaEvaluado())) {
            newPob[i].evalua(
                datosTrain,
                realTrain,
                nominalTrain,
                nulosTrain,
                clasesTrain,
                alfa,
                kNeigh,
                nClases,
                distanceEu);
            ev++;
          }

        /*Replace the two worst*/
        poblacion[tamPoblacion - 2] = new Cromosoma(datosTrain.length, newPob[0]);
        poblacion[tamPoblacion - 1] = new Cromosoma(datosTrain.length, newPob[1]);
      }
    }

    nSel = poblacion[0].genesActivos();

    /*Building of S set from the best cromosome obtained*/
    conjS = new double[nSel][datosTrain[0].length];
    conjR = new double[nSel][datosTrain[0].length];
    conjN = new int[nSel][datosTrain[0].length];
    conjM = new boolean[nSel][datosTrain[0].length];
    clasesS = new int[nSel];
    for (i = 0, l = 0; i < datosTrain.length; i++) {
      if (poblacion[0].getGen(i)) { // the instance must be copied to the solution
        for (j = 0; j < datosTrain[0].length; j++) {
          conjS[l][j] = datosTrain[i][j];
          conjR[l][j] = realTrain[i][j];
          conjN[l][j] = nominalTrain[i][j];
          conjM[l][j] = nulosTrain[i][j];
        }
        clasesS[l] = clasesTrain[i];
        l++;
      }
    }

    System.out.println(
        "SGA " + relation + " " + (double) (System.currentTimeMillis() - tiempo) / 1000.0 + "s");

    OutputIS.escribeSalida(
        ficheroSalida[0], conjR, conjN, conjM, clasesS, entradas, salida, nEntradas, relation);
    OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida, nEntradas, relation);
  } // end-method
Exemplo n.º 24
0
  /**
   * It initialize the population
   *
   * @param tam1 It contains the size of the table of training
   * @param v1 It contains the training input values
   * @param s1 It contains the training output values
   * @param tam2 It contains the size of the table of test
   * @param v2 It contains the test input values
   * @param s2 It contains the test output values
   * @param n_variables It contains the number of variables
   * @param reglas It contains the number of rules
   * @param var It contains the number of state variables
   * @param sal It contains the exit value
   * @param v It contains the values of data base
   * @param semilla It contains the value of the seed
   */
  public void Initialize(
      int tam1,
      double[][] v1,
      double[] s1,
      int tam2,
      double[][] v2,
      double[] s2,
      int n_variables,
      int reglas,
      int var,
      double sal,
      double[] v,
      long semilla) {

    /* INICIALIZAR VARIABLES */

    int i, j;
    Genes = 0;
    contador = contador2 = 0;
    Reiniciado = 0;
    Randomize.setSeed(semilla);
    E = new Ecm(tam1, v1, s1, tam2, v2, s2, n_variables, reglas, var, sal, v);
    Poblacion = new Cromosoma[2 * Popsize];
    Trials = 0;

    for (i = 0; i < n_variables; i++) {
      Genes += E.base().getN_etiquetas(i);
    }
    Ajuste = 1;
    Gene = new Gen[Genes];
    GenesA = Genes - E.base().getN_etiquetas(E.base().getN_var_estado());
    THRESHOLD = (double) ((Genes + GenesA) * BITS_GEN / 4.0);
    reduccionIni = THRESHOLD * 0.001;
    n_reglas_total = reglas;
    Gen aux = new Gen();
    aux.set_min(0.0);
    aux.set_max(1.0);

    for (i = 0; i < Genes; i++) {
      Gene[i] = aux;
    }
    for (i = 0; i < 2 * Popsize; i++) {
      Poblacion[i] = new Cromosoma(Genes, GenesA, n_reglas_total);
    }
    sample = new int[Popsize];
    BEST_CROM = new Cromosoma(Genes, Genes, n_reglas_total);

    for (j = 0; j < Genes; j++) {
      BEST_CROM.set_gene(j, Gene[j].min() + (Gene[j].max() - Gene[j].min()) / 2.);
      Poblacion[0].set_gene(j, BEST_CROM.gene(j));
    }
    for (j = 0; j < GenesA; j++) {
      BEST_CROM.set_geneA(j, Gene[j].min() + (Gene[j].max() - Gene[j].min()) / 2.);
      Poblacion[0].set_geneA(j, BEST_CROM.gene(j));
    }
    for (j = 0; j < n_reglas_total; j++) {
      BEST_CROM.set_geneR(j, (char) 1);
      Poblacion[0].set_geneR(j, (char) 1);
    }
    for (i = 1; i < Popsize; i++) {
      for (j = 0; j < Genes; j++)
        Poblacion[i].set_gene(
            j, Gene[j].min() + (Gene[j].max() - Gene[j].min()) * Randomize.Rand());
      for (j = 0; j < GenesA; j++)
        Poblacion[i].set_geneA(
            j, Gene[j].min() + (Gene[j].max() - Gene[j].min()) * Randomize.Rand());
      for (j = 0; j < n_reglas_total; j++) Poblacion[i].set_geneR(j, (char) 1);
    }
    F = new Funciones();
  }