📄 jgapclientgp.java
字号:
/*
* This file is part of JGAP.
*
* JGAP offers a dual license model containing the LGPL as well as the MPL.
*
* For licensing 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 java.io.*;
import java.net.*;
import java.util.*;
import org.apache.commons.cli.*;
import org.homedns.dade.jcgrid.client.*;
import org.homedns.dade.jcgrid.cmd.*;
import org.homedns.dade.jcgrid.message.*;
import org.jgap.*;
import org.jgap.distr.*;
import org.jgap.distr.grid.*;
import org.jgap.distr.grid.common.*;
import org.jgap.distr.grid.util.*;
import org.jgap.distr.grid.wan.*;
import org.jgap.gp.*;
import org.jgap.gp.impl.*;
import org.jgap.util.*;
import com.thoughtworks.xstream.io.xml.*;
/**
* A client defines work for the grid and sends it to the JGAPServer.
* Use this class as base class for your grid client implementations.
*
* @author Klaus Meffert
* @since 3.2
*/
public class JGAPClientGP
extends Thread {
/**@todo in dateiname requester/worker kodieren*/
/**@todo auch schlechtere ergebnisse einmixen: die direkt empfangenen
* gleich wieder in einen request reinstecken --> aber mischen verschiedener
* results in einen request!*/
/**@todo small, medium, large work requests*/
/**@todo re-evaluate each result on behalf of another worker: keep separate
* lookup-table for all requests --> m_resultsVerified, m_resultsPersister */
/**@todo remove old requests from online store automatically*/
/**@todo info when work request has been taken*/
/**@todo info when worker logs on --> evaluate logon files*/
/**@todo top results in eigener datei speichern,
* komprimierung durch weglassen 黚erfl. infos, siehe xml --> injection after reload*/
/**@todo copy good results to online folder*/
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.18 $";
public static final String APP_VERSION = "1.02a";
/**@todo store version in external file*/
public static final String MODULE_CS = "CS";
public static final String CLIENT_DATABASE = "clientdbGP.jgap";
public static final String RESULTS_DATABASE = "results.jgap";
/**@todo das ist nicht module, sondern sender-receiver*/
public static final String MODULE_SC = "SC";
public static final String MODULE_SW = "SW";
public static final String MODULE_WS = "WS";
public static final String MODULE_ANY = "*";
public static final String CONTEXT_WORK_REQUEST = "WREQ";
public static final String CONTEXT_WORK_RESULT = "WRES";
public static final String CONTEXT_ANY = "W*";
public static final String CONTEXT_ID_EMPTY = "0";
public static final String CONTEXT_ID_ANY = "*";
public static final int TIMEOUT_SECONDS = 20;
public static final int WAITTIME_SECONDS = 5;
public static final Object[][] FIELDSTOSKIP = new Object[][] { {GPPopulation.class,
"m_fitnessRank"}, {GPPopulation.class, "m_fittestProgram"}, {GPPopulation.class,
"m_changed"}, {GPPopulation.class, "m_sorted"}, {GPPopulation.class,
"m_fittestToAdd"}, {BaseGPChromosome.class,
"m_ind"},
};
private static transient org.apache.log4j.Logger log
= org.apache.log4j.Logger.getLogger(JGAPClientGP.class);
protected GridNodeClientConfig m_gridconfig;
protected JGAPRequestGP m_workReq;
private IGridClientMediator m_gcmed;
private IGridConfigurationGP m_gridConfig;
/**
* Is the client operating in a WAN or in a LAN?
* TRUE: WAN --> Do not use JCGrid architecture
* FALSE: LAN --> Do use JCGrid architecture
*/
private boolean m_WANMode;
/**
* Only received results or send requests beforehand?
*/
private boolean m_receiveOnly;
private boolean m_list;
private String m_workDir;
/**
* Dir for not too bad results
*/
private String m_ntbResultsDir;
private String m_runID;
private boolean m_endless;
private ClientStatus m_objects;
private PersistableObject m_persister;
private ResultVerification m_resultsVerified;
private PersistableObject m_resultsPersister;
private int m_requestIdx;
private boolean m_no_comm;
private boolean m_no_evolution;
private int m_max_fetch_results;
public JGAPClientGP(GridNodeClientConfig a_gridconfig,
String a_clientClassName,
boolean a_WANMode,
boolean a_receiveOnly, boolean a_list, boolean a_no_comm,
boolean a_no_evolution, boolean a_endless,
int a_max_fetch_results)
throws Exception {
this(null, a_gridconfig, a_clientClassName, a_WANMode, a_receiveOnly,
a_list, a_no_comm, a_no_evolution, a_endless, a_max_fetch_results);
m_gcmed = new DummyGridClientMediator(m_gridconfig);
}
public JGAPClientGP(IGridClientMediator a_gcmed,
GridNodeClientConfig a_gridconfig,
String a_clientClassName, boolean a_WANMode,
boolean a_receiveOnly, boolean a_list, boolean a_no_comm,
boolean a_no_evolution, boolean a_endless,
int a_max_fetch_results)
throws Exception {
log.info("This is JGAP Grid version " + APP_VERSION);
m_runID = getRunID();
log.info("ID of this run: " + m_runID);
if (a_clientClassName == null || a_clientClassName.length() < 1) {
throw new IllegalArgumentException(
"Please specify a class name of the configuration!");
}
m_WANMode = a_WANMode;
m_receiveOnly = a_receiveOnly;
if (m_receiveOnly) {
log.info("Only receive results");
}
m_list = a_list;
if (m_list) {
log.info("List requests and results");
}
m_endless = a_endless;
if (m_endless) {
log.info("Endless run");
}
m_no_comm = a_no_comm;
if (m_no_comm) {
log.info("Don't send or receive anything");
}
m_no_evolution = a_no_evolution;
if (m_no_evolution) {
log.info("Don't execute genetic evolution");
}
m_max_fetch_results = a_max_fetch_results;
if (m_max_fetch_results <= 0) {
m_max_fetch_results = 500;
}
log.info("Maximum number of results to fetch at once: " +
m_max_fetch_results);
m_gridconfig = a_gridconfig;
Class client = Class.forName(a_clientClassName);
m_gridConfig = (IGridConfigurationGP) client.getConstructor(new
Class[] {}).newInstance(new Object[] {});
m_gridConfig.initialize(m_gridconfig);
if (m_gridConfig.getClientFeedback() == null) {
m_gridConfig.setClientFeedback(new NullClientFeedbackGP());
}
// Setup work request.
// -------------------
/**@todo ab 2. zyklus ist pop.gr鰏se nur 1 !*/
JGAPRequestGP req = new JGAPRequestGP(m_gridconfig.getSessionName(),
m_runID + "_" + m_requestIdx, 0,
m_gridConfig);
m_requestIdx++;
req.setWorkerReturnStrategy(m_gridConfig.getWorkerReturnStrategy());
req.setGenotypeInitializer(m_gridConfig.getGenotypeInitializer());
req.setEvolveStrategy(m_gridConfig.getWorkerEvolveStrategy());
MasterInfo requester = new MasterInfo(true);
req.setRequesterInfo(requester);
req.setRequestDate(DateKit.now());
// If evolution takes place on client only:
// ----------------------------------------
// req.setEvolveStrategy(null);
//
setWorkRequest(req);
m_gcmed = a_gcmed;
init();
}
private void init()
throws Exception {
if (getWorkDirectory() == null) {
String workDir = FileKit.getCurrentDir() + "/work/" + "storage";
workDir = FileKit.getConformPath(workDir);
setWorkDirectory(workDir);
}
m_ntbResultsDir = FileKit.addSubDir(getWorkDirectory(), "ntb", true);
FileKit.createDirectory(m_ntbResultsDir);
log.info("NTB dir: " + m_ntbResultsDir);
// m_workDirResults =FileKit.getCurrentDir() + "/work/" + "results";
// m_workDirResults = FileKit.getConformPath(workDir);
// Try to load previous object information.
// ----------------------------------------
File f = new File(getWorkDirectory(), CLIENT_DATABASE);
m_persister = new PersistableObject(f);
m_objects = (ClientStatus) m_persister.load();
if (m_objects == null) {
m_objects = new ClientStatus();
m_persister.setObject(m_objects);
}
// Try to load previous request information.
// -----------------------------------------
f = new File(getWorkDirectory(), RESULTS_DATABASE);
m_resultsPersister = new PersistableObject(f);
m_resultsVerified = (ResultVerification) m_resultsPersister.load();
if (m_resultsVerified == null) {
m_resultsVerified = new ResultVerification();
m_resultsPersister.setObject(m_resultsVerified);
}
}
/**
* @return the most possibly unique ID of a single program execution
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected String getRunID() {
if (m_runID == null) {
return "RJGrid" + DateKit.getNowAsString();
}
else {
return m_runID;
}
}
public void setWorkRequest(JGAPRequestGP a_request) {
m_workReq = a_request;
}
/**
* Called at start of run().
* Override in sub classes if needed.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected void onBeginOfRunning()
throws Exception {
}
/**
* Called in run() before sending work requests.
* Override in sub classes if needed.
*
* @param a_workRequests work requests pending to be sent
*
* @return true: do send work requests, false: don't send any work request
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected boolean beforeSendWorkRequests(JGAPRequestGP[] a_workRequests)
throws Exception {
return true;
}
/**
* Called in run() before generating work requests for sending.
* Override in sub classes if needed.
*
* @return true: do generate work requests, false: don't generate any work
* request
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected boolean beforeGenerateWorkRequests()
throws Exception {
return true;
}
/**
* Called in run() after sending work requests successfully.
* Override in sub classes if needed.
*
* @param a_workRequests the sent requests
* @return true: process further, false: stop processing the rest
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected boolean afterSendWorkRequests(JGAPRequestGP[] a_workRequests)
throws Exception {
return true;
}
protected void errorOnSendWorkRequests(Throwable uex,
JGAPRequestGP[] a_workRequests)
throws Exception {
}
/**
* Called in run() before one evolution step is executed.
* Override in sub classes if needed.
*
* @param a_gcmed the GridClient mediator
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected void beforeEvolve(IGridClientMediator a_gcmed)
throws Exception {
}
/**
* Called in run() after one evolution step is executed.
* Override in sub classes if needed.
*
* @param a_gcmed the GridClient mediator
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected void afterEvolve(IGridClientMediator a_gcmed)
throws Exception {
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -