예제 #1
0
  /**
   * Prints a single Chromosome
   *
   * @param cd the Chromosome
   * @return
   */
  public String print(Chromosome cd) {
    String s = "";

    s += "-----\n";
    s += "objectives: [";
    for (int i = 0; i < cd.getObjectives().length; i++) {
      s += cd.getObjectives()[i] + ",";
    }
    s += "]\n";

    s += "genes: [";
    for (int i = 0; i < cd.getGenes().size(); i++) {
      Gene curr = cd.getGene(i);
      if (curr instanceof DoubleGene) {
        DoubleGene double_curr = (DoubleGene) curr;
        s += double_curr.doubleValue() + ",";
      } else if (curr instanceof IntegerGene) {
        IntegerGene int_curr = (IntegerGene) curr;
        s += int_curr.intValue() + ",";
      } else {
        s += curr.toString() + ",";
      }
    }
    s += "]\n";

    return s;
  }
예제 #2
0
  public Object clone() {
    Chromosome clone = new Chromosome();
    clone.genes = new Vector<Gene>();

    for (Gene curr : genes) {
      clone.genes.add((Gene) curr.clone());
    }

    return clone;
  }
예제 #3
0
  /**
   * Initializes a chromosome based on the sample chromosome's gene set. The genes of this
   * chromosome are generated randomly in the range specified in the gene set.
   *
   * @param sample the sample chromosome.
   */
  public Chromosome(Chromosome sample) {
    genes = new Vector<Gene>();
    Vector<Gene> sample_genes = sample.getGenes();

    for (Gene curr : sample_genes) {
      genes.add(curr.generate());
    }
  }
예제 #4
0
  public boolean isDuplicate(Chromosome chrom1, Chromosome chrom2) {
    boolean duplicate = true;

    // check gene by gene
    for (int g = 0; g < chrom1.getGenes().size(); g++) {
      if (chrom1.getGene(g) instanceof IntegerGene) {
        IntegerGene i_gene = (IntegerGene) chrom1.getGene(g);
        IntegerGene j_gene = (IntegerGene) chrom2.getGene(g);
        if (i_gene.intValue() != j_gene.intValue()) duplicate = false;
      } else if (chrom1.getGene(g) instanceof DoubleGene) {
        DoubleGene i_gene = (DoubleGene) chrom1.getGene(g);
        DoubleGene j_gene = (DoubleGene) chrom2.getGene(g);
        // compute 1% of range in gene set
        double max_difference = (i_gene.upperBound() - i_gene.lowerBound()) * .01;

        if (Math.abs(i_gene.doubleValue() - j_gene.doubleValue()) > max_difference)
          duplicate = false;
      } else if (!chrom1.getGene(g).equals(chrom2.getGene(g))) {
        duplicate = false;
      }
    }

    return duplicate;
  }
예제 #5
0
  public Vector<Chromosome> select(Vector<Chromosome> from, int num) {
    // ** FIRST, evaluate any new chromosomes
    // check for duplicates from
    for (int i = 0; i < from.size(); i++) {
      for (int j = i + 1; j < from.size(); j++) {
        Chromosome i_chrom = from.get(i);
        Chromosome j_chrom = from.get(j);

        if (isDuplicate(i_chrom, j_chrom)) {
          // remove j_chrom
          from.remove(j);
          j--;
          continue;
        }
      }
    }

    for (Chromosome curr : from) {
      if (curr.getObjectives() == null) {
        curr.setObjectives(eval.getFitness(curr));
      }
    }

    // ** SECOND, we want to add chromosomes to elite set and swap

    // copy from from to clone_from
    Vector<ChromDominance> clone_from = new Vector<ChromDominance>();
    for (Chromosome curr : from) {
      clone_from.add(new ChromDominance(curr));
    }

    // remove bad chromosomes
    // duplicate-checking with elite set now included in goodChromosome()
    for (int i = 0; i < clone_from.size(); i++) {
      Chromosome chrom = clone_from.get(i).chrom;

      if (!goodChromosome(chrom)) {
        clone_from.remove(i);
        i--;
        continue;
      }
    }

    // check to see if elite set is not full
    if (elite.size() < elite_size) {
      // move from clone_from to elite
      for (int i = 0; i < clone_from.size(); i++) {
        elite.add(clone_from.get(i));
      }

      // check if its still less
      if (elite.size() < elite_size) {
        if (num_select != 0) {
          num = num_select;
        }

        return callSelector(from, num);
      } else {
        // now evaluate dominance counts within elite set
        for (int i = 0; i < elite.size(); i++) {
          for (int j = i + 1; j < elite.size(); j++) {
            ChromDominance i_chrom = elite.get(i);
            ChromDominance j_chrom = elite.get(j);

            if (eval.isFitter(i_chrom.chrom, j_chrom.chrom)) {
              j_chrom.dominance++;
            } else if (eval.isFitter(j_chrom.chrom, i_chrom.chrom)) {
              i_chrom.dominance++;
            }
          }
        }
      }
    }

    // compare for dominance counts within clone_from
    for (int i = 0; i < clone_from.size(); i++) {
      for (int j = i + 1; j < clone_from.size(); j++) {
        Chromosome i_chrom = clone_from.get(i).chrom;
        Chromosome j_chrom = clone_from.get(j).chrom;

        if (eval.isFitter(i_chrom, j_chrom)) {
          ChromDominance cd = clone_from.get(j);
          cd.dominance++;
        } else if (eval.isFitter(j_chrom, i_chrom)) {
          ChromDominance cd = clone_from.get(i);
          cd.dominance++;
        }
      }
    }

    // compare for dominance counts between clone_from and elite
    for (int i = 0; i < clone_from.size(); i++) {
      for (int j = 0; j < elite_size; j++) {
        Chromosome i_chrom = clone_from.get(i).chrom;
        Chromosome j_chrom = elite.get(j).chrom;

        if (eval.isEquivalent(i_chrom, j_chrom)) {
          ChromDominance cd = clone_from.get(i);
          cd.dominance++; // keep elite set individual if not comparable
        }

        if (eval.isFitter(i_chrom, j_chrom)) {
          ChromDominance cd = elite.get(j);
          cd.dominance++;
        } else if (eval.isFitter(j_chrom, i_chrom)) {
          ChromDominance cd = clone_from.get(i);
          cd.dominance++;
        }
      }
    }

    // switch between the sets, best -> elite set
    for (int i = 0; i < clone_from.size(); i++) {
      for (int j = 0; j < elite.size(); j++) {
        ChromDominance i_chrom = clone_from.get(i);
        ChromDominance j_chrom = elite.get(j);

        if (i_chrom.dominance < j_chrom.dominance) {
          clone_from.remove(i);
          elite.remove(j);
          clone_from.add(j_chrom);
          elite.add(i_chrom);
          i--;
          break;
        }
      }
    }

    // find num_changed
    int num_changed = 0; // number of chromosomes that are different now
    for (int i = 0; i < elite.size(); i++) {
      ChromDominance chrom = elite.get(i);
      if (chrom.changed) {
        num_changed++;
        chrom.changed = false;
      }
    }

    if (num_changed <= thresh_percent * elite_size) {
      // comparison of threshold evolutions to max
      //    is done in MultiConnector
      thresh_evolutions++;
    } else {
      thresh_evolutions = 0; // reset if not within threshold
    }

    // sort the elite set
    EliteSorter es = new EliteSorter();
    Collections.sort(elite, es);
    // calculate average objectives in the elite set
    double[] avg_objectives = new double[eval.getOptimal().length];
    for (int i = 0; i < elite.size(); i++) {
      double[] cd_objectives = elite.get(i).chrom.getObjectives();
      for (int j = 0; j < cd_objectives.length; j++) {
        avg_objectives[j] += cd_objectives[j];
      }
    }

    for (int i = 0; i < avg_objectives.length; i++) {
      avg_objectives[i] = avg_objectives[i] / elite.size();
    }
    // calculate change in average objectives
    if (last_avg_objectives != null) {
      for (int i = 0; i < avg_objectives.length; i++) {
        objective_change[i] = avg_objectives[i] / last_avg_objectives[i];
      }
    } else {
      objective_change = new double[avg_objectives.length];
    }
    last_avg_objectives = avg_objectives;

    // copy from new clone_from to a Vector<Chromosome>
    Vector<Chromosome> clone_chroms = new Vector<Chromosome>();
    for (int i = 0; i < clone_from.size(); i++) {
      clone_chroms.add(clone_from.get(i).chrom);
    }

    // copy from elite to clone_chroms
    for (int i = 0; i < elite.size(); i++) {
      clone_chroms.add(elite.get(i).chrom);
    }

    if (num_select != 0) {
      num = num_select;
    }

    if (merge) {
      return callSelector(clone_chroms, num);
    } else {
      return callSelector(from, num);
    }
  }