📄 tictactoemain.java
字号:
static final double GAME_LOST = 5000;
static final double MY_WORST_FITNESS_VALUE = 9999999;
static final double READ_VALUE = 10000;
static final double UNKNOWN_BUT_SOMETHING = MY_WORST_FITNESS_VALUE -
5000;
static final double ONE_MOVE = MY_WORST_FITNESS_VALUE /
(Board.HEIGHT * Board.WIDTH);
static final double ONE_MOVE2 = ONE_MOVE * 0.9;
public GameFitnessFunction(Board a_board, int a_color, GPGenotype a_other,
int a_otherColor) {
m_board = a_board;
m_color = a_color;
m_other = a_other;
firstTime = true;
}
public void setPlayer(GPGenotype a_player) {
m_player = a_player;
}
public void setOpponent(GPGenotype a_other) {
m_other = a_other;
}
protected double evaluate(final IGPProgram a_subject) {
return computeRawFitness(a_subject);
}
public double computeRawFitness(final IGPProgram a_program) {
double error = MY_WORST_FITNESS_VALUE;
double errorOpponent = MY_WORST_FITNESS_VALUE;
Object[] noargs = new Object[0];
// Determine opponent's program.
// -----------------------------
IGPProgram opponent;
if (firstTime) {
// First call.
// -----------
firstTime = false;
opponent = m_other.getGPPopulation().getGPProgram(0);
if (opponent == null) {
System.err.println("First time: opponent is null!");
}
}
else {
opponent = m_other.getFittestProgramComputed();
if (opponent == null) {
nullfound++;
if (nullfound == 100) {
System.err.println(
"---------- Consecutive calls: opponent is null!");
}
opponent = m_other.getGPPopulation().getGPProgram(0);
}
}
// Compute fitness for each program.
// ---------------------------------
int moves = 0;
// Set opponent's fitness value to a value to have it set at least here.
// ---------------------------------------------------------------------
opponent.setFitnessValue(UNKNOWN_BUT_SOMETHING);
try {
while (moves < Board.WIDTH * Board.HEIGHT) {
m_board.startNewRound();
Boolean var;
if (moves == 0) {
var = new Boolean(true);
}
else {
var = new Boolean(false);
}
Integer var2 = new Integer(moves);
Variable vb1 = m_player.getVariable("firstmove");
vb1.set(var);
Variable vb2 = m_other.getVariable("firstmove");
vb2.set(var);
Variable vx1 = m_player.getVariable("move");
vx1.set(var2);
Variable vx2 = m_other.getVariable("move");
vx2.set(var2);
// Initialize local stores.
// ------------------------
a_program.getGPConfiguration().clearStack();
a_program.getGPConfiguration().clearMemory();
try {
// First player.
// -------------
m_board.beginTurn();
for (int j = 0; j < a_program.size(); j++) {
if (j == a_program.size() - 1) {
a_program.execute_void(j, noargs);
}
else {
a_program.execute_void(j, noargs);
}
}
// Value the number of distinct read outs of the board by the
// player.
// -----------------------------------------------------------
int readCount = m_board.getReadPositionCount();
if (readCount > maxreads) {
maxreads = readCount;
if (maxreads > 1) {
System.out.println("**** Number of board reads reached: " +
maxreads);
}
}
error -= readCount * READ_VALUE;
m_board.endTurn();
moves++;
error -= ONE_MOVE2;
// Initialize local stores.
// ------------------------
a_program.getGPConfiguration().clearStack();
a_program.getGPConfiguration().clearMemory();
// Second player.
// --------------
m_board.beginTurn();
for (int j = 0; j < opponent.size(); j++) {
if (j == opponent.size() - 1) {
opponent.execute_void(j, noargs);
}
else {
opponent.execute_void(j, noargs);
}
}
// Value the number of distincts read outs of the board by the
// player.
// -----------------------------------------------------------
readCount = m_board.getReadPositionCount();
if (readCount > maxreads) {
maxreads = readCount;
if (maxreads > 1) {
System.out.println("**** Number of board reads reached: " +
maxreads);
}
}
errorOpponent -= readCount * READ_VALUE;
m_board.endTurn();
moves++;
errorOpponent -= ONE_MOVE2;
} catch (GameWonException gex) {
if (gex.getColor() == m_color) {
// Best case.
// ----------
error -= 0;
errorOpponent = GAME_LOST;
break;
}
else {
// Worse case: Player lost, but finished game correctly!
// -----------------------------------------------------
error -= GAME_LOST;
errorOpponent = 0.0d;
break;
}
}
m_board.endRound();
}
System.out.println("******************* SUPERB: WE MADE IT");
} catch (IllegalArgumentException iax) {
// Already cared about by not reducing error rate.
// -----------------------------------------------
;
} catch (IllegalStateException iex) {
// Already cared about by not reducing error rate.
// -----------------------------------------------
}
if (maxMoves < moves) {
maxMoves = moves;
System.out.println("**** Number of valid moves reached: " + maxMoves);
}
if (error < 0.000001) {
error = 0.0d;
}
else if (error < MY_WORST_FITNESS_VALUE * 0.8d) {
/**@todo add penalty for longer solutions*/
}
if (errorOpponent < 0.000001) {
errorOpponent = 0.0d;
}
else if (errorOpponent < MY_WORST_FITNESS_VALUE * 0.8d) {
/**@todo add penalty for longer solutions*/
}
opponent.setFitnessValue(errorOpponent);
return error;
}
}
}
class BestGeneticEventListener
implements GeneticEventListener {
private GPGenotype m_other;
public BestGeneticEventListener(GPGenotype a_other) {
m_other = a_other;
}
/**
* New best solution found.
*
* @param a_firedEvent GeneticEvent
*/
public void geneticEventFired(GeneticEvent a_firedEvent) {
GPGenotype genotype = (GPGenotype) a_firedEvent.getSource();
int evno = genotype.getGPConfiguration().getGenerationNr();
String indexString = "" + evno;
while (indexString.length() < 5) {
indexString = "0" + indexString;
}
IGPProgram best = genotype.getAllTimeBest();
IGPProgram fittest = genotype.getFittestProgram();
if (fittest != null) {
// Inject fittest program into opponent's population.
// --------------------------------------------------
m_other.addFittestProgram(fittest);
double bestFitness = fittest.getFitnessValue();
if (bestFitness < 0.5) {
genotype.outputSolution(best);
System.exit(0);
}
}
}
};
class MyGeneticEventListener
implements GeneticEventListener {
public MyGeneticEventListener() {
}
public void geneticEventFired(GeneticEvent a_firedEvent) {
GPGenotype genotype = (GPGenotype) a_firedEvent.getSource();
int evno = genotype.getGPConfiguration().getGenerationNr();
double freeMem = SystemKit.getFreeMemoryMB();
if (evno % 100 == 0) {
IGPProgram best = genotype.getAllTimeBest();
double allBestFitness = FitnessFunction.NO_FITNESS_VALUE;
if (best != null) {
allBestFitness = best.
getFitnessValue();
}
System.out.println("Evolving generation " + evno
+ ", all-time-best fitness: " +
allBestFitness
+ ", memory free: " + freeMem + " MB");
IGPProgram best1 = genotype.getFittestProgram();
System.out.println(" Best in current generation: " +
best1.getFitnessValue());
System.out.println(" " + best1.toStringNorm(0));
}
if (evno > 30000) {
System.exit(0);
}
else {
// Collect garbage if memory low.
// ------------------------------
if (freeMem < 50) {
System.gc();
}
}
}
}
class Coevolution {
private GPConfiguration m_conf;
private GPGenotype m_gp1;
private GPGenotype m_gp2;
public Coevolution(GPConfiguration a_conf, GPGenotype a_gp1, GPGenotype a_gp2) {
m_conf = a_conf;
m_gp1 = a_gp1;
m_gp2 = a_gp2;
}
public void start() {
try {
do {
m_gp1.evolve();
Thread.currentThread().sleep(5);
m_gp2.evolve();
Thread.currentThread().sleep(5);
m_gp1.calcFitness();
Thread.currentThread().sleep(5);
m_gp2.calcFitness();
Thread.currentThread().sleep(20);
} while (true);
} catch (InterruptedException iex) {
; //this is OK and means: end of evolution
iex.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -