文档介绍:遗传蚂蚁算法【Java】
【】
// ic Ant Algorithm
// Carl E. Bredlau
// from Koza, ic Programming_, MIT Press
// programmed modeled after sga. Thanks to the author.
package icAnt;
public class Ant implements Cloneable {
// An ant
// From the ant's point of view it only sees food. It does not know
// where on the board it is.
// For counting ants:
private static int antCount = 0;
int antID = 0; // which ant
Chromosome chromo;
int state = 0; // internal state;
int fitness = 1; // How good the ant is
int parent1 = -1; // "Parents" of the ant
int parent2 = -1;
int age = 0; // age of ant
int gene = -1; // where we swap or mutate
public Ant(int states) {
chromo = new Chromosome(states);
antID = antCount++;
}
// Ant moves:
public static final int MOVE = ; // move foward
public static final int LEFT = ; // turn left
public static final int RIGHT = ; // move right
public static final int NOP = ; // don't move
public int move(boolean sees_food) {
// depending upon the state and whether it sees food, change
// state and return what it will do.
int mem_loc = 2*state;
if (sees_food) { // move down one state if food is there...
mem_loc++;
}
state = [mem_loc].state;
return [mem_loc].action;
}
public void incFitness() {fitness++;}
public void walk (Board board, int numMoves) {
// Ant walks on the board to determine fitness
fitness = 1;
state = 0;
// Put ant on board
();
// ("Move: " + 0 + " State: " + state +
// " Fitness is " + fitness);
// (); // for testing.
for (int moves = 1; moves <= numMoves; moves++) {
int ant_move = move(()); // Ant moves or turns
int foodEaten = (ant_move);
if (foodEaten == ) incFitness();
// for testing...
// ("Move: " + moves + " State: " + state +
// " Fitness is " + fitn