1 10 package org.jgap; 11 12 import java.io.*; 13 import java.lang.reflect.*; 14 import java.util.*; 15 import org.jgap.util.*; 16 import java.net.*; 17 18 25 public class Population 26 implements Serializable, ICloneable, IPersistentRepresentation { 27 28 private static final String CVS_REVISION = "$Revision: 1.56 $"; 29 30 33 private List m_chromosomes; 34 35 38 private IChromosome m_fittestChromosome; 39 40 44 private boolean m_changed; 45 46 49 private boolean m_sorted; 50 51 private 52 Configuration m_config; 53 54 public final static String CHROM_DELIMITER = "~"; 55 56 60 public final static String CHROM_DELIMITER_HEADING = "["; 61 62 66 public final static String CHROM_DELIMITER_CLOSING = "]"; 67 68 73 public Population(final Configuration a_config) 74 throws InvalidConfigurationException { 75 this(a_config, 100); 76 } 77 78 87 public Population(final Configuration a_config, 88 final IChromosome[] a_chromosomes) 89 throws InvalidConfigurationException { 90 this(a_config, a_chromosomes.length); 91 synchronized (m_chromosomes) { 92 for (int i = 0; i < a_chromosomes.length; i++) { 93 m_chromosomes.add(a_chromosomes[i]); 96 } 97 } 98 setChanged(true); 99 } 100 101 109 public Population(final Configuration a_config, 110 final IChromosome a_chromosome) 111 throws InvalidConfigurationException { 112 this(a_config, 1); 113 if (a_chromosome == null) { 114 throw new IllegalArgumentException ("Chromosome passed must not be null!"); 115 } 116 synchronized (m_chromosomes) { 117 m_chromosomes.add(a_chromosome); 118 } 119 setChanged(true); 120 } 121 122 131 public Population(final Configuration a_config, final int a_size) 132 throws InvalidConfigurationException { 133 if (a_config == null) { 134 throw new InvalidConfigurationException("Configuration must not be null!"); 135 } 136 m_config = a_config; 137 m_chromosomes = new Vector(a_size); 139 setChanged(true); 140 } 141 142 148 public Population() 149 throws InvalidConfigurationException { 150 this(Genotype.getStaticConfiguration()); 151 } 152 153 public Configuration getConfiguration() { 154 return m_config; 155 } 156 157 165 public void addChromosome(final IChromosome a_toAdd) { 166 if (a_toAdd != null) { 167 synchronized (m_chromosomes) { 168 m_chromosomes.add(a_toAdd); 169 } 170 setChanged(true); 171 } 172 } 173 174 183 public void addChromosomes(final Population a_population) { 184 if (a_population != null) { 185 synchronized (m_chromosomes) { 186 m_chromosomes.addAll(a_population.getChromosomes()); 187 } 188 setChanged(true); 197 } 198 } 199 200 208 public void setChromosomes(final List a_chromosomes) { 209 synchronized (m_chromosomes) { 210 m_chromosomes = a_chromosomes; 211 } 212 setChanged(true); 213 } 214 215 226 public void setChromosome(final int a_index, final IChromosome a_chromosome) { 227 if (m_chromosomes.size() == a_index) { 228 addChromosome(a_chromosome); 229 } 230 else { 231 synchronized (m_chromosomes) { 232 m_chromosomes.set(a_index, a_chromosome); 233 } 234 setChanged(true); 235 } 236 } 237 238 246 public List getChromosomes() { 247 return m_chromosomes; 248 } 249 250 257 public IChromosome getChromosome(final int a_index) { 258 return (IChromosome) m_chromosomes.get(a_index); 259 } 260 261 267 public int size() { 268 return m_chromosomes.size(); 269 } 270 271 278 public Iterator iterator() { 279 return m_chromosomes.iterator(); 280 } 281 282 288 public IChromosome[] toChromosomes() { 289 return (IChromosome[]) m_chromosomes.toArray( 290 new IChromosome[m_chromosomes.size()]); 291 } 292 293 303 public IChromosome determineFittestChromosome() { 304 if (!m_changed && m_fittestChromosome != null) { 305 return m_fittestChromosome; 306 } 307 Iterator it = m_chromosomes.iterator(); 308 FitnessEvaluator evaluator = getConfiguration().getFitnessEvaluator(); 309 double bestFitness; 310 if (evaluator.isFitter(2.0d, 1.0d)) { 311 bestFitness = -1.0d; 312 } 313 else { 314 bestFitness = Double.MAX_VALUE; 315 } 316 double fitness; 317 while (it.hasNext()) { 318 IChromosome chrom = (IChromosome) it.next(); 319 fitness = chrom.getFitnessValue(); 320 if (evaluator.isFitter(fitness, bestFitness) 321 || m_fittestChromosome == null) { 322 m_fittestChromosome = chrom; 323 bestFitness = fitness; 324 } 325 } 326 setChanged(false); 327 return m_fittestChromosome; 328 } 329 330 342 public IChromosome determineFittestChromosome(int a_startIndex, 343 int a_endIndex) { 344 double bestFitness = -1.0d; 345 FitnessEvaluator evaluator = getConfiguration().getFitnessEvaluator(); 346 double fitness; 347 int startIndex = Math.max(0, a_startIndex); 348 int endIndex = Math.min(m_chromosomes.size() - 1, a_endIndex); 349 for (int i = startIndex; i < endIndex; i++) { 350 IChromosome chrom = (IChromosome) m_chromosomes.get(i); 351 fitness = chrom.getFitnessValue(); 352 if (evaluator.isFitter(fitness, bestFitness) 353 || m_fittestChromosome == null) { 354 m_fittestChromosome = chrom; 355 bestFitness = fitness; 356 } 357 } 358 return m_fittestChromosome; 359 } 360 361 370 protected void setChanged(final boolean a_changed) { 371 m_changed = a_changed; 372 setSorted(false); 373 } 374 375 381 public boolean isChanged() { 382 return m_changed; 383 } 384 385 393 protected void setSorted(final boolean a_sorted) { 394 m_sorted = a_sorted; 395 } 396 397 405 public boolean contains(final IChromosome a_chromosome) { 406 return m_chromosomes.contains(a_chromosome); 407 } 408 409 420 IChromosome removeChromosome(final int a_index) { 421 if (a_index < 0 || a_index >= size()) { 422 throw new IllegalArgumentException ("Index must be within bounds!"); 423 } 424 setChanged(true); 425 return (IChromosome) m_chromosomes.remove(a_index); 426 } 427 428 440 public List determineFittestChromosomes(final int a_numberOfChromosomes) { 441 int numberOfChromosomes = Math.min(a_numberOfChromosomes, 442 getChromosomes().size()); 443 if (numberOfChromosomes <= 0) { 444 return null; 445 } 446 if (!m_changed && m_sorted) { 447 return getChromosomes().subList(0, numberOfChromosomes); 448 } 449 sortByFitness(); 451 return getChromosomes().subList(0, numberOfChromosomes); 453 } 454 455 463 public void sortByFitness() { 464 sort(new ChromosomeFitnessComparator(getConfiguration(). 468 getFitnessEvaluator())); 469 setChanged(false); 470 setSorted(true); 471 m_fittestChromosome = (IChromosome) m_chromosomes.get(0); 472 } 473 474 482 protected void sort(Comparator a_comparator) { 483 Collections.sort(getChromosomes(), a_comparator); 484 } 485 486 497 public List getGenome(final boolean a_resolveCompositeGenes) { 498 List result = new Vector(); 499 List chroms = getChromosomes(); 500 int len = chroms.size(); 501 for (int i = 0; i < len; i++) { 502 IChromosome chrom = (IChromosome) chroms.get(i); 503 Gene[] genes = chrom.getGenes(); 504 int len2 = genes.length; 505 for (int j = 0; j < len2; j++) { 506 Gene gene = genes[j]; 507 if (a_resolveCompositeGenes && gene instanceof ICompositeGene) { 508 addCompositeGene(result, (ICompositeGene) gene); 509 } 510 else { 511 addAtomicGene(result, gene); 512 } 513 } 514 } 515 return result; 516 } 517 518 528 private void addCompositeGene(final List a_result, final Gene a_gene) { 529 if (a_gene instanceof ICompositeGene) { 530 int len = a_gene.size(); 531 for (int i = 0; i < len; i++) { 532 addCompositeGene(a_result, ( (ICompositeGene) a_gene).geneAt(i)); 533 } 534 } 535 else { 536 addAtomicGene(a_result, a_gene); 537 } 538 } 539 540 549 private void addAtomicGene(final List a_result, final Gene a_gene) { 550 a_result.add(a_gene); 551 } 552 553 public boolean isSorted() { 554 return m_sorted; 555 } 556 557 566 public boolean equals(Object a_pop) { 567 try { 568 return compareTo(a_pop) == 0; 569 } catch (ClassCastException e) { 570 return false; 574 } 575 } 576 577 590 public int compareTo(Object a_pop) { 591 Population other = (Population) a_pop; 592 if (a_pop == null) { 593 return 1; 594 } 595 int size1 = size(); 596 int size2 = other.size(); 597 if (size1 != size2) { 598 if (size1 < size2) { 599 return -1; 600 } 601 else { 602 return 1; 603 } 604 } 605 List chroms2 = other.getChromosomes(); 606 for (int i = 0; i < size1; i++) { 607 if (!chroms2.contains(m_chromosomes.get(i))) { 608 return 1; 609 } 610 } 611 return 0; 612 } 613 614 620 public Object clone() { 621 try { 622 Population result = new Population(m_config); 623 result.m_changed = true; 626 result.m_sorted = false; 627 result.m_fittestChromosome = m_fittestChromosome; 628 int size = m_chromosomes.size(); 629 for (int i = 0; i < size; i++) { 630 IChromosome chrom = (IChromosome) m_chromosomes.get(i); 631 result.addChromosome( (IChromosome) chrom.clone()); 632 } 633 return result; 634 } catch (Exception ex) { 635 throw new CloneException(ex); 636 } 637 } 638 639 647 public void clear() { 648 m_chromosomes.clear(); 649 m_changed = true; 650 m_sorted = true; 651 m_fittestChromosome = null; 652 } 653 654 666 public String getPersistentRepresentation() { 667 StringBuffer b = new StringBuffer (); 668 IChromosome chrom; 671 for (int i = 0; i < m_chromosomes.size(); i++) { 672 chrom = (IChromosome) m_chromosomes.get(i); 673 if (! (chrom instanceof IPersistentRepresentation)) { 674 throw new RuntimeException ("Population contains a chromosome of type " 675 + chrom.getClass().getName() 676 + " which does not implement" 677 + " IPersistentRepresentation!"); 678 } 679 b.append(CHROM_DELIMITER_HEADING); 680 try { 681 b.append(URLEncoder.encode(chrom.getClass().getName() 682 + CHROM_DELIMITER 683 + ( (IPersistentRepresentation) chrom). 684 getPersistentRepresentation() 685 , "UTF-8")); 686 } catch (UnsupportedEncodingException uex) { 687 throw new RuntimeException ("UTF-8 should always be supported!", uex); 688 } 689 b.append(CHROM_DELIMITER_CLOSING); 690 } 691 return b.toString(); 692 } 693 694 705 public void setValueFromPersistentRepresentation(String a_representation) 706 throws UnsupportedRepresentationException { 707 if (a_representation != null) { 708 try { 709 List r = split(a_representation); 710 String g; 711 m_chromosomes = new Vector(); 712 Iterator iter = r.iterator(); 715 StringTokenizer st; 716 String clas; 717 String representation; 718 IChromosome chrom; 719 while (iter.hasNext()) { 720 g = URLDecoder.decode( (String ) iter.next(), "UTF-8"); 721 st = new StringTokenizer(g, CHROM_DELIMITER); 722 if (st.countTokens() != 2) 723 throw new UnsupportedRepresentationException("In " + g + ", " + 724 "expecting two tokens, separated by " + CHROM_DELIMITER); 725 clas = st.nextToken(); 726 representation = st.nextToken(); 727 chrom = createChromosome(clas, representation); 728 m_chromosomes.add(chrom); 729 } 730 setChanged(true); 731 } catch (Exception ex) { 732 throw new UnsupportedRepresentationException(ex.toString()); 733 } 734 } 735 } 736 737 751 protected IChromosome createChromosome(String a_chromClassName, 752 String a_persistentRepresentation) 753 throws Exception { 754 Class chromClass = Class.forName(a_chromClassName); 755 Constructor constr = chromClass.getConstructor(new Class [] {Configuration.class}); 756 IChromosome chrom = (IChromosome) constr.newInstance(new Object [] { 757 getConfiguration()}); 758 ( (IPersistentRepresentation) chrom).setValueFromPersistentRepresentation( 759 a_persistentRepresentation); 760 return chrom; 761 } 762 763 775 protected static final List split(String a_string) 776 throws UnsupportedRepresentationException { 777 List a = Collections.synchronizedList(new ArrayList()); 778 781 StringTokenizer st = new StringTokenizer 784 (a_string, CHROM_DELIMITER_HEADING + CHROM_DELIMITER_CLOSING, true); 785 while (st.hasMoreTokens()) { 786 if (!st.nextToken().equals(CHROM_DELIMITER_HEADING)) { 787 throw new UnsupportedRepresentationException(a_string + " no open tag"); 788 } 789 String n = st.nextToken(); 790 if (n.equals(CHROM_DELIMITER_CLOSING)) { 791 a.add(""); 792 } 793 else { 794 a.add(n); 795 if (!st.nextToken().equals(CHROM_DELIMITER_CLOSING)) { 796 throw new UnsupportedRepresentationException 797 (a_string + " no close tag"); 798 } 799 } 800 } 801 return a; 802 } 803 804 } 805 | Popular Tags |