📄 jgapthread.java
字号:
/*
* 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.multithread;
import org.jgap.*;
import org.jgap.impl.*;
/**
* A single thread working with its own Configuration object
*
* @author Klaus Meffert
* @since 3.2
*/
public class JGAPThread
implements Runnable {
/** String containing the CVS revision. Read out via reflection!*/
private final static String CVS_REVISION = "$Revision: 1.0 $";
private Configuration m_config;
private Genotype m_genotype;
private int m_index;
public JGAPThread(int i) {
m_index = i;
}
public void run() {
try {
String threadid = Thread.currentThread().getName();
System.err.println("Starting thread with ID " + threadid);
m_config = new DefaultConfiguration();
// m_config.setFitnessEvaluator(new DefaultFitnessEvaluator());
m_config.setFitnessFunction(new MaxFunction());
m_config.setPreservFittestIndividual(false);
m_config.setKeepPopulationSizeConstant(false);
IChromosome sampleChromosome;
if (m_index % 2 == 0) {
sampleChromosome = new Chromosome(m_config,
new IntegerGene(m_config, -50, +50), 20);
}
else {
sampleChromosome = new Chromosome(m_config,
new IntegerGene(m_config, -3, +3), 40);
}
m_config.setSampleChromosome(sampleChromosome);
m_config.setPopulationSize(5);
m_genotype = Genotype.randomInitialGenotype(m_config);
int evolution = 0;
IChromosome fittest;
while (!Thread.currentThread().interrupted()) {
doWork(m_genotype);
// Pause to avoid 100% CPU load.
// -----------------------------
Thread.sleep(100);
if (++evolution > 100) {
break;
}
if (evolution % 10 == 0) {
fittest = m_genotype.getFittestChromosome();
System.out.println("[" + threadid +
"][Evolution "+evolution+
"] Current best solution has fitness " +
fittest.getFitnessValue());
}
}
fittest = m_genotype.getFittestChromosome();
System.out.println("[" + threadid + "] Best solution found has fitness " +
fittest.getFitnessValue());
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
private void doWork(Genotype a_genotype) {
a_genotype.evolve();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -