Print

Print


Hello, 
I'm implementing a GP for the game Connect4. I use individuals (GPTrees) for
the evaluation of each choice a player can make. Since the game is played on
a 6x7 board and a player can only drop one coin at a time there are at most
7 possibilities. Each evaluation should return a value. The higher the value
the better the choice is.

Except for the functions, terminals and GPProblem class that I called
Connect4Problem I have 2 other relevant classes: Coin and Board. Coin just
keeps to which player the coin belongs to and Board keeps a 6x7 matrix of
Coins and has a lot of functionality. Connect4Problem also keeps a board to
work on. The board in this class represents the progress when a game is played.

Now the problem is that in my Connect4Problem class I have a method to find
all possibilities a player can play. And it's these possibilities that
should be evaluated! With possibility I mean a board (6x7 matrix) where the
current player has dropped a new coin.

This is my code, I hope it's clear enough. Is their an easier way to do this
or is my way the only way?

public void evaluate(EvolutionState state, Individual[] ind,
   boolean[] updateFitness, boolean countVictoriesOnly, int[] subpops,
   int threadnum) {
  if( ind.length != 2 || updateFitness.length != 2 )
            state.output.fatal( "Only 2 players (individuals) can play
against each other" );

  reset(); // input.value is put on zero and a board of Coins is initialized
(empty)
  
  // Player 0 can start first
  $player = 0;
  short[] score = new short[] {0,0}; // resulting score of the individuals
  int strokes = 0; // The number of coins put on the field. 42 is the maximum.
  do{
   strokes++;
   chooseBestPossibility(state, ind, threadnum); ***
   
   switchPlayers(); // It's the other individual's turn.
  }while(strokes != 42 && !victor(getBoard().getColumnLastDrop()));
  score = updateScore(score); // the winner gets +3
  
  
  reset();
  
  // Now player 1 can start first
  $player = 1;
  strokes = 0; // The number of coins put on the field. 42 is the maximum.
  do{
   strokes++;
   chooseBestPossibility(state, ind, threadnum);
   
   switchPlayers(); // It's the other individual's turn.
  }while(strokes != 42 && !victor(getBoard().getColumnLastDrop()));
  score = updateScore(score);
  
  // The fitness is updated
  for (int i = 0; i < 2; i++) // The fitness of both individuals must be updated
         if (updateFitness[i]){
          KozaFitness fitness = ((KozaFitness)ind[i].fitness);
          fitness.setStandardizedFitness(state,fitness.standardizedFitness()
+ score[i]);
         }

 } 



*** chooseBestPossibility looks like this:
private void chooseBestPossibility(EvolutionState state, Individual[] ind,
int threadnum) {
  double value = 0;
  Board bestBoardSoFar = $board;
  // This returns an generic of Boards with all possibilities the current
player can virtually play.
  ArrayList<Board> possibilities = virtuallyTryPossibilities(); 
  Iterator<Board> it = possibilities.iterator();    
  while(it.hasNext()){
   $board = it.next();
  
((GPIndividual)ind[getCurrentPlayer()]).trees[0].child.eval(state,threadnum,(GPData)input,

     stack, (GPIndividual)ind[getCurrentPlayer()], this);
   if(input.value >= value){ // The equality is there because a choice had
to be made when 2 possibilites have the same evaluation value
    value = input.value;
    bestBoardSoFar = $board;
   } // else the possibility has a lower evaluation than the best found so
far, ignore this possibility
  }  
  $board = bestBoardSoFar; // the best board in the end
 }