/** * 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; }
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; }