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.mathProblemDistributed;
011:
012: import org.jgap.*;
013: import org.jgap.gp.*;
014: import org.jgap.gp.impl.*;
015: import org.jgap.distr.grid.gp.*;
016:
017: /**
018: * Sample implementation of IRequestSplitStrategy to split a single request
019: * into multiple requests for workers.
020: *
021: * @author Klaus Meffert
022: * @since 3.2
023: */
024: public class MyRequestSplitStrategy implements IRequestSplitStrategyGP {
025: /** String containing the CVS revision. Read out via reflection!*/
026: private final static String CVS_REVISION = "$Revision: 1.3 $";
027:
028: private GPConfiguration m_config;
029:
030: public MyRequestSplitStrategy(GPConfiguration a_config) {
031: m_config = a_config;
032: }
033:
034: public GPConfiguration getConfiguration() {
035: return m_config;
036: }
037:
038: /**
039: * Creates single requests to be sent to workers. Here, each request consists
040: * of a number of chromosome from the original population (determined here)
041: * plus the rest of the chromosomes to be initialized randomly at each worker.
042: *
043: * @param a_request the request to split
044: * @return single requests to be computed by workers
045: * @throws Exception
046: *
047: * @author Klaus Meffert
048: * @since 3.2
049: */
050: public JGAPRequestGP[] split(JGAPRequestGP a_request)
051: throws Exception {
052: GPPopulation pop = a_request.getPopulation();
053: // Is evolution started the first time?
054: // ------------------------------------
055: boolean firstTime;
056: if (pop == null || pop.size() < 1) {
057: firstTime = true;
058: } else {
059: firstTime = false;
060: }
061: if (!firstTime) {
062: // Sort population to have the fittest individuals at the beginning.
063: // -----------------------------------------------------------------
064: pop.sortByFitness();
065: }
066: // Generate 20 work requests.
067: // --------------------------
068: int requests = 20; // number of requests to create
069: JGAPRequestGP[] result = new JGAPRequestGP[requests];
070: // Only send 10% of the population to the workers.
071: // -----------------------------------------------
072: int count = getConfiguration().getPopulationSize() / 10;
073: for (int j = 0; j < requests; j++) {
074: result[j] = (JGAPRequestGP) a_request.newInstance(
075: "Population " + j, j);
076: // Setup JGAP configuration for worker.
077: // ------------------------------------
078: GPConfiguration config = getConfiguration().newInstanceGP(
079: j + "", "population " + j);
080: // Assemble population for one request.
081: // ------------------------------------
082: RandomGenerator rand = getConfiguration()
083: .getRandomGenerator();
084: GPPopulation workPop = new GPPopulation(config, count);
085: if (!firstTime) {
086: for (int i = 0; i < count; i++) {
087: IGPProgram chrom;
088: if (rand.nextDouble() > 0.2d) {
089: // Take one of the best chromosomes.
090: // ---------------------------------
091: chrom = pop.getGPProgram(i);
092: } else {
093: // Take one of the ordinary chromosomes.
094: // -------------------------------------
095: chrom = pop.getGPProgram(i + count);
096: }
097: workPop.setGPProgram(i, chrom);
098: }
099: }
100: result[j].setPopulation(workPop);
101: }
102: return result;
103: }
104: }
|