jgapworkergp.java

来自「一个开源的用java开发的遗传算法的封装好的工程」· Java 代码 · 共 92 行

JAVA
92
字号
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licencing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */
package org.jgap.distr.grid.gp;

import org.homedns.dade.jcgrid.*;
import org.homedns.dade.jcgrid.worker.*;
import org.jgap.*;
import org.jgap.gp.impl.*;
import org.jgap.distr.grid.*;

/**
 * A GP worker receives work units from a JGAPServer and sends back computed
 * solutions to the same JGAPServer.
 *
 * @author Klaus Meffert
 * @since 3.2
 */
public class JGAPWorkerGP
    implements Worker {
  /** String containing the CVS revision. Read out via reflection!*/
  private final static String CVS_REVISION = "$Revision: 1.2 $";

  /**
   * Executes the evolution and returns the result.
   *
   * @param work WorkRequest
   * @param workDir String
   * @return WorkResult
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.01
   */
  public WorkResult doWork(WorkRequest work, String workDir)
      throws Exception {
    JGAPRequestGP req = ( (JGAPRequestGP) work);
    /**@todo set gridworkerfeedback in class GridWorker*/

    // Setup configuration.
    // --------------------
    GPConfiguration conf = req.getConfiguration();
    if (conf.getJGAPFactory() == null) {
      throw new IllegalStateException("JGAPFactory must not be null!");
    }
    conf = conf.newInstanceGP(conf.getId() + "_1", conf.getName() + "_1");
    // Important: Re-set the cloned configuration!
    // -------------------------------------------
    req.setConfiguration(conf);
    GPGenotype gen = null;
    // It is possible that no evolution happens at the worker.
    // -------------------------------------------------------
    if (req.getGenotypeInitializer() != null) {
      // Setup the genotype to evolve.
      // -----------------------------
      GPPopulation initialPop = req.getPopulation();
      gen = req.getGenotypeInitializer().setupGenotype(req, initialPop);
      if (req.getWorkerEvolveStrategy() != null) {
        // Execute evolution via registered strategy.
        // ------------------------------------------
        req.getWorkerEvolveStrategy().evolve(gen);
      }
    }
    // Assemble result according to registered strategy.
    // -------------------------------------------------
    WorkResult res = req.getWorkerReturnStrategy().assembleResult(req, gen);
    return res;
  }

  /**
   * Convenience method to start the worker.
   *
   * @param args command-line arguments, such as server address. See
   * @throws Exception
   *
   * @author Klaus Meffert
   * @since 3.01
   */
  public static void main(String[] args)
      throws Exception {
    // Start worker.
    // -------------
    new JGAPWorkersGP(args);
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?