001: /*
002: * This file is part of JGAP.
003: *
004: * JGAP offers a dual license model containing the LGPL as well as the MPL.
005: *
006: * For licencing information please see the file license.txt included with JGAP
007: * or have a look at the top of class org.jgap.Chromosome which representatively
008: * includes the JGAP license policy applicable for any file delivered with JGAP.
009: */
010: package examples.grid.evolutionDistributed;
011:
012: import org.jgap.*;
013: import org.jgap.distr.grid.*;
014:
015: /**
016: * Sample implementation of IRequestSplitStrategy to split a single request
017: * into multiple requests for workers.
018: *
019: * @author Klaus Meffert
020: * @since 3.2
021: */
022: public class MyRequestSplitStrategy implements IRequestSplitStrategy {
023: /** String containing the CVS revision. Read out via reflection!*/
024: private final static String CVS_REVISION = "$Revision: 1.1 $";
025:
026: private Configuration m_config;
027:
028: public MyRequestSplitStrategy(Configuration a_config) {
029: m_config = a_config;
030: }
031:
032: public Configuration getConfiguration() {
033: return m_config;
034: }
035:
036: /**
037: * Creates single requests to be sent to workers. Here, each request consists
038: * of a number of chromosome from the original population (determined here)
039: * plus the rest of the chromosomes to be initialized randomly at each worker.
040: *
041: * @param a_request the request to split
042: * @return single requests to be computed by workers
043: * @throws Exception
044: *
045: * @author Klaus Meffert
046: * @since 3.2
047: */
048: public JGAPRequest[] split(JGAPRequest a_request) throws Exception {
049: Population pop = a_request.getPopulation();
050: // Is evolution started the first time?
051: // ------------------------------------
052: boolean firstTime;
053: if (pop == null || pop.size() < 1) {
054: firstTime = true;
055: } else {
056: firstTime = false;
057: }
058: if (!firstTime) {
059: // Sort Population to have the fittest ones at the beginning.
060: // ----------------------------------------------------------
061: pop.sortByFitness();
062: }
063: // Generate 20 work requests.
064: // --------------------------
065: int requests = 20; // number of requests to create
066: JGAPRequest[] result = new JGAPRequest[requests];
067: // Only send 10% of the population to the workers.
068: // -----------------------------------------------
069: int count = getConfiguration().getPopulationSize() / 10;
070: for (int j = 0; j < requests; j++) {
071: result[j] = (JGAPRequest) a_request.newInstance(
072: "Population " + j, j);
073: // Setup JGAP configuration for worker.
074: // ------------------------------------
075: Configuration config = getConfiguration().newInstance(
076: j + "", "population " + j);
077: // Assemble population for one request.
078: // ------------------------------------
079: RandomGenerator rand = getConfiguration()
080: .getRandomGenerator();
081: Population workPop = new Population(config, count);
082: if (!firstTime) {
083: for (int i = 0; i < count; i++) {
084: IChromosome chrom;
085: if (rand.nextDouble() > 0.2d) {
086: // Take one of the best chromosomes.
087: // ---------------------------------
088: chrom = pop.getChromosome(i);
089: } else {
090: // Take one of the ordinary chromosomes.
091: // -------------------------------------
092: chrom = pop.getChromosome(i + count);
093: }
094: workPop.addChromosome(chrom);
095: }
096: }
097: result[j].setPopulation(workPop);
098: }
099: return result;
100: }
101: }
|