Пример #1
0
  /**
   * Builder. Copies a chromosome of specified size
   *
   * @param size Size of the chromosome
   * @param a Chromosome to copy
   */
  public Cromosoma(int size, Cromosoma a) {

    int i;

    cuerpo = new boolean[size];
    for (i = 0; i < cuerpo.length; i++) cuerpo[i] = a.getGen(i);
    calidad = a.getCalidad();
    cruzado = false;
    valido = true;
  } // end-method
Пример #2
0
  /**
   * Test if two chromosome differ in only one gene
   *
   * @param a Chromosome to compare
   * @return Position of the difference, if only one is found. Otherwise, -1
   */
  public int differenceAtOne(Cromosoma a) {

    int i;
    int cont = 0, pos = -1;

    for (i = 0; i < cuerpo.length && cont < 2; i++)
      if (cuerpo[i] != a.getGen(i)) {
        pos = i;
        cont++;
      }

    if (cont >= 2) return -1;
    else return pos;
  } // end-method
Пример #3
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