KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > eval > PopulationHistory


1 /*
2  * This file is part of JGAP.
3  *
4  * JGAP offers a dual license model containing the LGPL as well as the MPL.
5  *
6  * For licencing information please see the file license.txt included with JGAP
7  * or have a look at the top of class org.jgap.Chromosome which representatively
8  * includes the JGAP license policy applicable for any file delivered with JGAP.
9  */

10 package org.jgap.eval;
11
12 import java.util.*;
13 import org.jgap.*;
14
15 /**
16  * Container for holding a given number of populations. Serves as a history
17  * object for later evaluation
18  *
19  * @author Klaus Meffert
20  * @since 2.0
21  */

22 public class PopulationHistory {
23   /** String containing the CVS revision. Read out via reflection!*/
24   private final static String JavaDoc CVS_REVISION = "$Revision: 1.4 $";
25
26   private List m_populations;
27
28   private int m_maxSize;
29
30   /**
31    * Constructor.
32    * @param a_maxSize the maximum number of Population objects to hold, or
33    * zero if there is no limit.
34    * @author Klaus Meffert
35    * @since 2.0
36    */

37   public PopulationHistory(final int a_maxSize) {
38     m_populations = new Vector();
39     if (a_maxSize < 0) {
40       throw new IllegalArgumentException JavaDoc("Maximum size must be greater"
41                                          + " or equal to zero!");
42     }
43     m_maxSize = a_maxSize;
44   }
45
46   public Population getPopulation(final int a_count) {
47     if (a_count >= m_populations.size()) {
48       return null;
49     }
50     else {
51       return (Population) m_populations.get(a_count);
52     }
53   }
54
55   /**
56    * Adds a population to the history. If the maximum size of this container
57    * is exceeded after that then the oldest population added is removed
58    * @param a_population the population to be added
59    *
60    * @author Klaus Meffert
61    * @since 2.0
62    */

63   public void addPopulation(final Population a_population) {
64     m_populations.add(0, a_population);
65     int popSize = m_populations.size();
66     if (m_maxSize != 0 && popSize > m_maxSize) {
67       m_populations.remove(popSize - 1);
68     }
69   }
70
71   /**
72    * @author Klaus Meffert
73    * @since 2.0
74    */

75   public void removeAllPopulations() {
76     m_populations.removeAll(m_populations);
77   }
78
79   public int size() {
80     return m_populations.size();
81   }
82
83   public List getPopulations() {
84     return m_populations;
85   }
86
87   /**
88    * Sets the list of populations to the list provided.
89    * @param a_populations list of populations to be set
90    *
91    * @author Klaus Meffert
92    * @since 2.0
93    */

94   public void setPopulations(final List a_populations) {
95     m_populations = a_populations;
96     int popSize = m_populations.size();
97     if (m_maxSize != 0 && popSize > m_maxSize) {
98       for (int i = m_maxSize; i < popSize; i++) {
99         m_populations.remove(m_maxSize);
100       }
101     }
102   }
103 }
104
Popular Tags