KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > distr > Breeder


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.distr;
11
12 import org.jgap.*;
13
14 /**
15  * Breeds populations using a GA that will be breeded either on a single
16  * server or on multiple servers being whose results will be merged/synchronized
17  * later on.
18  * <p>
19  * A breeder is part of a fractal structure (fractal because each Breeder can
20  * be parent and/or child of other Breeder's).
21  *
22  * @author Klaus Meffert
23  * @since 2.0
24  */

25 public abstract class Breeder
26     implements Runnable JavaDoc {
27   /** String containing the CVS revision. Read out via reflection!*/
28   private final static String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
29
30   /**
31    * The parent Breeder to report to
32    */

33   private Breeder m_master; /**@todo use*/
34
35   /**
36    * The child Breeder's doing work for us and reporting to this Breeder.
37    */

38   private Breeder[] m_workers; /**@todo use*/
39
40   /**
41    * The Genotype this Breeder is responsible for
42    */

43   private Genotype m_genotype; /**@todo construct somewhere*/
44
45   /**
46    * Helper class for merging together two Populations into one.
47    */

48   private IPopulationMerger m_populationMerger; /**@todo use*/
49
50   private transient boolean m_running;
51
52   private transient boolean m_stopped = true;
53
54   private transient MeanBuffer m_meanBuffer = new MeanBuffer(40);
55
56   public Breeder(final IPopulationMerger a_populationMerger) {
57     super();
58     m_populationMerger = a_populationMerger;
59   }
60
61   /**
62    * Runs the evolution.
63    *
64    * @author Klaus Meffert
65    * @since 2.0
66    */

67   public void run() {
68     try {
69       m_stopped = false;
70       while (m_running) {
71         evalOneGeneration();
72         int sleepTime = m_meanBuffer.mean() / 100;
73         if (sleepTime <= 0) {
74           pause(1);
75         }
76         else {
77           pause(sleepTime);
78         }
79       }
80       m_stopped = true;
81     }
82     catch (Throwable JavaDoc t) {
83       m_stopped = true;
84       m_running = false;
85 // if (exHandler != null) {
86
// exHandler.handleThrowable(t);
87
// }
88
}
89   }
90
91   /**
92    * Evaluate one generation and memorize the time needed. This is important
93    * for load balancing and calculating the performance of a system
94    * @throws Exception
95    *
96    * @author Klaus Meffert
97    * @since 2.0
98    */

99   private void evalOneGeneration()
100       throws Exception JavaDoc {
101     long begin = System.currentTimeMillis();
102     m_genotype.evolve(1);
103     informParent();
104     m_meanBuffer.add( (int) (System.currentTimeMillis() - begin));
105   }
106
107   protected void informParent() {
108     /**@todo implement*/
109   }
110
111   /**
112    * Pauses the Breeder. This is important if a user providing a system for
113    * running the GA does not want to make available 100% of his CPU resources.
114    * @param a_milliSec number of milliseconds to wait
115    *
116    * @author Klaus Meffert
117    * @since 2.0
118    */

119   private synchronized void pause(final int a_milliSec) {
120     try {
121       wait(a_milliSec);
122     }
123     catch (InterruptedException JavaDoc e) {
124       ;
125     }
126   }
127
128   public void start() {
129     if (!m_running) {
130       m_running = true;
131       Thread JavaDoc thread = new Thread JavaDoc(this);
132       thread.start();
133     }
134   }
135
136   public void stop() {
137     if (m_running) {
138       m_running = false;
139       if (m_genotype != null) {
140         /**@todo implement*/
141 //: genAlgo.stop();
142
/*
143                 if (genAlgo.getRacer() != null) {
144                   genAlgo.getRacer().reset();
145                 }
146          */

147       }
148     }
149   }
150
151   public boolean isRunning() {
152     return m_running;
153   }
154
155   public boolean canBeStarted() {
156     return !m_running;
157   }
158
159   public boolean canBeStopped() {
160     return m_running;
161   }
162 }
163 /**
164  * A buffer that calculate the mean of a certain amount
165  * of values. Uses a fifo inside.
166  */

167 class MeanBuffer {
168   private int[] m_buf = null;
169
170   private int m_size;
171
172   private int m_index;
173
174   public MeanBuffer(final int a_size) {
175     m_size = a_size;
176     m_buf = new int[m_size];
177     for (int i = 0; i < m_size; i++) {
178       m_buf[i] = 0;
179     }
180   }
181
182   public void add(final int a_val) {
183     m_buf[m_index] = a_val;
184     m_index = (m_index + 1) % m_size;
185   }
186
187   public int mean() {
188     int sum = 0;
189     for (int i = 0; i < m_size; i++) {
190       sum += m_buf[i];
191     }
192     return sum / m_size;
193   }
194
195   public void reset() {
196     for (int i = 0; i < m_size; i++) {
197       m_buf[i] = 0;
198     }
199   }
200 }
201
Popular Tags