📄 jgapclientgp.java
字号:
// -----------------------
JGAPClientGP client = new JGAPClientGP(config, clientClassName, inWAN,
receiveOnly, list, noComm, noEvolution, endless, max_fetch_results);
// Start the threaded process.
// ---------------------------
client.start();
client.join();
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
protected static Options makeOptions() {
Options options = new Options();
options.addOption("no_comm", false,
"Don't receive any results, don't send requests");
options.addOption("no_evolution", false,
"Don't perform genetic evolution");
options.addOption("receive_only", false,
"Only receive results, don't send requests");
options.addOption("endless", false, "Run endlessly");
options.addOption("config", true, "Grid configuration's class name");
options.addOption("list", false,
"List requests and results");
options.addOption("max_fetch_results", true,
"Maximum number of results to fetch at once");
options.addOption("help", false,
"Display all available options");
return options;
}
protected void removeEntries(Map a_cachedKeys, Map a_foundKeys) {
Iterator it = a_cachedKeys.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
if (!a_foundKeys.containsKey(key)) {
it.remove();
}
}
}
/**
* Override in sub classes.
*
* @param a_obj the object to get the key from
* @return the key of the object
*
* @throws Exception
*/
protected String getKeyFromObject(Object a_obj)
throws Exception {
return null;
}
/**
* New results has been received. Care that the best of them are stored
* in case it is a top 3 result.
*
* @param a_pop the fittest results received for a work request
* @return true: new top result
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected boolean resultReceived(GPPopulation a_pop)
throws Exception {
boolean isTopResult = false;
for (IGPProgram prog : a_pop.getGPPrograms()) {
if (prog != null) {
if (resultReceived(prog)) {
isTopResult = true;
}
}
}
return isTopResult;
}
/**
* A new result has been received. Care that it is stored to top list on disk
* in case it is a top 3 result. Also store it in other cases and if the
* result is not too bad to be able to mix it in when generating new work
* requests.
*
* @param a_fittest the fittest result received for a work request
*
* @return true: new top result
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected boolean resultReceived(IGPProgram a_fittest)
throws Exception {
if (a_fittest == null) {
return false;
}
/**@todo jeden Worker einer von n (rein logischen) Gruppen zuteilen.
* Pro logischer Gruppe top n Ergebnisse halten
*/
try {
Map<String, List> topAll = m_objects.getTopResults();
String appid = m_gridConfig.getContext().getAppId();
List<IGPProgram> topApp = topAll.get(appid);
if (topApp == null) {
topApp = new Vector();
topAll.put(appid, topApp);
}
int fitter = 0;
Iterator<IGPProgram> it = topApp.iterator();
Object worstEntry = null;
double worstFitness = -1;
String norm = a_fittest.toStringNorm(0);
int count = 0;
double a_fitness = a_fittest.getFitnessValue();
if (a_fitness > 12500) {
// Store online as a backup.
// -------------------------
log.info("Backup up good result");
String title = "fitness_" + NumberKit.niceDecimalNumber(a_fitness, 2);
m_gcmed.backupResult(a_fittest, "goodResults", title);
}
else {
if (a_fitness > 1750) {
// Store not too bad result for mixing it in to new work requests.
// ---------------------------------------------------------------
log.info("Storing not too bad result for later reusage");
String title = "ntb_fitness_"
+ DateKit.getNowAsString()
+ "_"
+ NumberKit.niceDecimalNumber(a_fitness, 2)
+ ".jgap";
// Store in separate subdir.
// -------------------------
saveResult(m_ntbResultsDir, title, a_fittest);
}
} while (it.hasNext()) {
IGPProgram prog = (IGPProgram) it.next();
// Don't allow identical results.
// ------------------------------
if (prog.toStringNorm(0).equals(norm)) {
fitter = 100;
break;
}
double fitness = prog.getFitnessValue();
if (Math.abs(fitness - a_fittest.getFitnessValue()) < 0.001) {
fitter = 100;
break;
}
else if (fitness >= a_fitness) {
fitter++;
}
// Determine the worst entry for later replacement.
// ------------------------------------------------
if (worstEntry == null ||
getConfiguration().getGPFitnessEvaluator().
isFitter(worstFitness, fitness)) {
worstEntry = prog;
worstFitness = fitness;
}
count++;
}
boolean result = true;
if (fitter < 3 || count > 3) { /**@todo make configurable*/
// Remove worst result yet and add new fit result.
// -----------------------------------------------
if (worstEntry != null && count >= 3) {
/**@todo compare with toStringNorm(0), use remove(int) instead of remove(Object)*/
if (!topApp.remove(worstEntry)) {
log.error("Removing of worst entry failed");
}
}
if (fitter < 3) {
try {
GPGenotype.checkErroneousProg(a_fittest, " add top fit", true, false);
} catch (Throwable t) {
log.warn("Received program not valid!");
result = false;
}
if (result) {
a_fitness = a_fittest.getFitnessValue();
if (a_fitness < 1000) { /**@todo ist nur test!*/
result = false;
}
else {
topApp.add(a_fittest);
log.info("Added fit program, fitness: " +
NumberKit.niceDecimalNumber(a_fitness, 2));
log.info("Solution: " + a_fittest.toStringNorm(0));
result = true;
}
}
}
else {
log.info(
"Result not better than top results received, removed obsolete top result");
result = false;
}
}
else {
log.info("Result not better than top results received");
result = false;
}
if (result) {
/**@todo skip unnecessary data, inject it after reload*/
//m_persister.save(true, JGAPClientGP.FIELDSTOSKIP);
m_persister.save();
}
return result;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
/**
* Saves a result to disk.
*
* @param a_dir the directory to put the result into
* @param a_filename name of the file to write
* @param a_obj the result object to write
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.4
*/
protected void saveResult(String a_dir, String a_filename,
IGPProgram a_obj)
throws Exception {
String filename = FileKit.addFilename(a_dir, a_filename);
PersistableObject po = new PersistableObject(filename);
po.setObject(a_obj);
po.save();
}
public String[] getFilenames(String a_dir)
throws Exception {
String[] files = FileKit.listFilesInDir(a_dir, null);
return files;
}
/**
* Presets initial population to be included for input to workers.
*
* @param a_workRequest the work request that is about to be sent.
*
* @throws Exception
*
* @author Klaus Meffert
* @since 3.3.3
*/
protected void presetPopulation(JGAPRequestGP a_workRequest)
throws Exception {
// Merge previously stored results with new requests.
// Often preset them as input for worker, sometimes give worker an
// empty population.
// -------------------------------------------------------------------
RandomGenerator randGen = getConfiguration().getRandomGenerator();
double d = randGen.nextDouble();
if (d > 0.15d) {
Map<String, List> topAll = m_objects.getTopResults();
String appid = m_gridConfig.getContext().getAppId();
List<IGPProgram> topApp = topAll.get(appid);
int added = 0;
// int index = 0;
GPPopulation pop = a_workRequest.getPopulation();
IGPProgram[] programs = pop.getGPPrograms();
// while (index < pop.getPopSize() && pop.getGPProgram(index) != null) {
// index++;
// }
List toAdd = new Vector();
if (topApp != null && topApp.size() > 0) {
// Merge top results in.
// ---------------------
for (IGPProgram prog : topApp) {
GPGenotype.checkErroneousProg(prog, " before add preset", true);
toAdd.add(prog);
added++;
if (added >= 3 || randGen.nextDouble() > 0.6d) {
break;
}
}
}
// Merge not too bad results in.
// -----------------------------
String[] results = getFilenames(m_ntbResultsDir);
if (results != null && results.length > 0) {
int count = randGen.nextInt(Math.min(5, results.length));
if (count > 0) {
if (count > results.length) {
count = results.length;
}
for (int i = 0; i < count; i++) {
int index = randGen.nextInt(results.length);
String filename = FileKit.addFilename(m_ntbResultsDir,
results[index]);
/**@todo remove results[index]*/
PersistableObject po = new PersistableObject(filename);
IGPProgram ntb = (IGPProgram) po.load();
log.info("Presetting with NTB result");
added++;
toAdd.add(ntb);
}
}
}
// Now merge old and new programs to one pool.
// -------------------------------------------
int len = programs.length;
if (len > 0) {
len = 0;
while (len < programs.length && programs[len] != null) {
len++;
}
IGPProgram[] programsNew = (IGPProgram[]) toAdd.toArray(new IGPProgram[] {});
int size = len + toAdd.size();
IGPProgram[] allPrograms = new IGPProgram[size];
if (len > 0) {
System.arraycopy(programs, 0, allPrograms, 0, len);
}
System.arraycopy(programsNew, 0, allPrograms, len, programsNew.length);
pop.setGPPrograms(allPrograms);
log.info("Population preset with " + added + " additional programs");
}
}
}
protected void showCurrentResults()
throws Exception {
String appid = m_gridConfig.getContext().getAppId();
Map<String, List> topAll = m_objects.getTopResults();
List<IGPProgram> topApp = topAll.get(appid);
if (topApp != null && topApp.size() > 0) {
log.info("Top evolved results yet:");
log.info("------------------------");
boolean changed = false;
Iterator<IGPProgram> it = topApp.iterator();
while (it.hasNext()) {
IGPProgram prog = it.next();
try {
GPGenotype.checkErroneousProg(prog, " as top result", false, true);
} catch (Throwable t) {
// Remove invalid program.
// -----------------------
it.remove();
changed = true;
continue;
}
double fitness = prog.getFitnessValue();
log.info("Fitness " +
NumberKit.niceDecimalNumber(fitness, 2));
if (fitness < 1000) {
log.info("Removing too bad result with fitness "
+ NumberKit.niceDecimalNumber(fitness, 2));
it.remove();
changed = true;
}
}
if (changed) {
m_persister.save();
}
log.info("");
}
else {
log.info("No top results yet.");
}
}
public int getMaxFetchResults() {
return m_max_fetch_results;
}
private boolean startsWith(String s, String a_prefix) {
if (s == null || a_prefix == null) {
return false;
}
return s.startsWith(a_prefix);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -