GeneThello Home

net.sf.genethello.gametheory
Class GameTheory

java.lang.Object
  extended by net.sf.genethello.gametheory.GameTheory
Direct Known Subclasses:
Brain

public abstract class GameTheory
extends java.lang.Object

A framework for running algorithms of game theory (minimax type) to search best move for a two players zero-sum game. It contains several famous minimax type algorithms.

Author:
Administrator

Field Summary
protected  Move bestmove
          Best move found so far.
protected  Board board
          Board used in the game.
protected  int depth
          Thinking depth.
protected  int[] historyTable
          Array of integer to hold history table.

To use this feature, the following should be done: Declares historyTable to be size of 2 * possible valid moves.
protected  int moveLength
          Number of valid moves.
protected  int step
          Current step of game.
protected  boolean timesUp
          Terminates searching process if time's up is true.
protected  long transpositionMaxIndex
          Maximum index of transposition table, should be (power of two) - 1, so that the size is power of two.
protected  TranspositionTable[] transpositionTable
          Array of TranspositionTable to hold transposition table.

To use this feature, the following should be done: Declares transpositionMaxIndex to be (power of two) - 1 somewhere in program initialization.
e.g.
protected  boolean unquiet
          Does quiescence search if true.
 
Constructor Summary
GameTheory()
          Creates a new instance of GameTheory
 
Method Summary
 int _AlphaBetaFailHard(int d, int alpha, int beta)
          Recursive portion of original AlphaBetaFailSoft algorithm.
 int _AlphaBetaFailHardHistory(int d, int alpha, int beta)
          Recursive portion of original AlphaBetaFailSoft algorithm.
 int _AlphaBetaFailHardTransposition(int d, int alpha, int beta)
          Recursive portion of original AlphaBetaFailSoft algorithm.
protected  int _AlphaBetaFailSoft(int d, int alpha, int beta)
          Recursive portion of AlphaBetaFailSoft algorithm.
protected  int _AlphaBetaFailSoftHistory(int d, int alpha, int beta)
          Recursive portion of AlphaBetaFailSoftHistory algorithm.
protected  int _AlphaBetaFailSoftHistoryTransposition(int d, int alpha, int beta)
          Recursive portion of AlphaBetaFailSoftHistoryTransposition algorithm.
protected  int _AlphaBetaFailSoftTransposition(int d, int alpha, int beta)
          Recursive portion of AlphaBetaFailSoftTransposition algorithm.
 int _NegaMax(int d)
          Recursive portion of original NegaMax algorithm.
 int _NegaScoutFailHard(int d, int alpha, int beta)
          Recursive portion of original NegaScoutFailSoft algorithm.
 int _NegaScoutFailHardTransposition(int d, int alpha, int beta)
          Recursive portion of original NegaScoutFailSoft algorithm.
protected  int _NegaScoutFailSoft(int d, int alpha, int beta)
          Recursive portion of NegaScoutFailSoft algorithm.
protected  int _NegaScoutFailSoftHistory(int d, int alpha, int beta)
          Recursive portion of NegaScoutFailSoftHistory algorithm.
protected  int _NegaScoutFailSoftHistoryTransposition(int d, int alpha, int beta)
          Recursive portion of NegaScoutFailSoftHistoryTransposition algorithm.
protected  int _NegaScoutFailSoftTransposition(int d, int alpha, int beta)
          Recursive portion of NegaScoutFailSoftTransposition algorithm.
 int AlphaBetaFailHard(int d, int alpha, int beta)
          Searches game tree using original AlphaBetaFailSoft algorithm.
 int AlphaBetaFailHardHistory(int d, int alpha, int beta)
          Searches game tree using original AlphaBetaFailSoft algorithm.
 int AlphaBetaFailHardTransposition(int d, int alpha, int beta)
          Searches game tree using original AlphaBetaFailSoft algorithm.
 int AlphaBetaFailSoft(int d, int alpha, int beta)
          Searches game tree using AlphaBetaFailSoft algorithm.
 int AlphaBetaFailSoftHistory(int d, int alpha, int beta)
          Searches game tree using AlphaBetaFailSoft algorithm and history table.
 int AlphaBetaFailSoftHistoryTransposition(int d, int alpha, int beta)
          Searches game tree using AlphaBetaFailSoft algorithm, history table and transposition table.
 int AlphaBetaFailSoftTransposition(int d, int alpha, int beta)
          Searches game tree using AlphaBetaFailSoft algorithm and transposition table.
abstract  void DoMove(int i)
          Does i-th move from valid moves.
protected abstract  void DoMove(Move mv)
          Does move mv.
abstract  int Eval()
          Evaluates leaf position from current player's standpoint.
abstract  void Generate()
          Generates valid moves of next board position.
 Move getBestmove()
          Gets best move.
abstract  Move getMove(int i)
          Gets i-th move from valid moves.
abstract  int HistoryIndex(int i)
          Searches index of an i-th move from array of history table.
abstract  int HistoryIndex(Move mv)
          Searches index of a move from array of history table.
 int IterativeMTD()
          Searches game tree using Memory-enhanced Test Driver algorithm and transposition table with iterative deepening.
 int IterativeNegascout()
          Searches game tree using NegaScoutFailSoft algorithm and transposition table with iterative deepening.
 int MTD(int d, int f)
          Searches game tree using Memory-enhanced Test Driver algorithm and transposition table.
 int NegaMax(int d)
          Searches game tree using original NegaMax algorithm.
 int NegaScoutFailHard(int d, int alpha, int beta)
          Searches game tree using original NegaScoutFailSoft algorithm.
 int NegaScoutFailHardTransposition(int d, int alpha, int beta)
          Searches game tree using original NegaScoutFailSoft algorithm.
 int NegaScoutFailSoft(int d, int alpha, int beta)
          Searches game tree using NegaScoutFailSoft algorithm.
 int NegaScoutFailSoftHistory(int d, int alpha, int beta)
          Searches game tree using NegaScoutFailSoft algorithm and history table.
 int NegaScoutFailSoftHistoryTransposition(int d, int alpha, int beta)
          Searches game tree using NegaScoutFailSoft algorithm, history table and transposition table.
 int NegaScoutFailSoftTransposition(int d, int alpha, int beta)
          Searches game tree using NegaScoutFailSoft algorithm and transposition table.
protected abstract  void PrintBoard()
          Prints board position to standard output.
protected abstract  void PrintMove(Move mv)
          Prints move to standard output.
 void setDepth(int depth)
          Sets the depth.
 void setStep(int step)
          Sets current step of game simulation.
abstract  void UndoGenerate()
          Retracts generated valid moves and puts board back to previous position.
abstract  void UndoMove()
          Undo last move.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

historyTable

protected int[] historyTable
Array of integer to hold history table.

To use this feature, the following should be done:


transpositionTable

protected TranspositionTable[] transpositionTable
Array of TranspositionTable to hold transposition table.

To use this feature, the following should be done:


transpositionMaxIndex

protected long transpositionMaxIndex
Maximum index of transposition table, should be (power of two) - 1, so that the size is power of two.


depth

protected int depth
Thinking depth.


board

protected Board board
Board used in the game.


bestmove

protected Move bestmove
Best move found so far.


moveLength

protected int moveLength
Number of valid moves.


step

protected int step
Current step of game.


unquiet

protected boolean unquiet
Does quiescence search if true.


timesUp

protected boolean timesUp
Terminates searching process if time's up is true.

Constructor Detail

GameTheory

public GameTheory()
Creates a new instance of GameTheory

Method Detail

NegaMax

public int NegaMax(int d)
Searches game tree using original NegaMax algorithm.
 function negamax(node, depth, color)
  if node is a terminal node or depth = 0
      return color * the heuristic value of node
  else
      foreach child of node
          α := max(α, -negamax(child, depth-1, -color))
      return α

 (* Initial call *)
 NegaMax(depth)
 

Parameters:
d - depth
Returns:
evaluation value

_NegaMax

public int _NegaMax(int d)
Recursive portion of original NegaMax algorithm.

Parameters:
d - depth
Returns:
evaluation value

AlphaBetaFailHard

public int AlphaBetaFailHard(int d,
                             int alpha,
                             int beta)
Searches game tree using original AlphaBetaFailSoft algorithm.
 function alphabeta(node, depth, α, β, Player)
  if  depth = 0 or node is a terminal node
      return the heuristic value of node
  if  Player = MaxPlayer
      for each child of node
          α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Beta cut-off *)
      return α
  else
      for each child of node
          β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Alpha cut-off *)
      return β

 (* Initial call *)
 AlphaBetaFailHard(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailHard

public int _AlphaBetaFailHard(int d,
                              int alpha,
                              int beta)
Recursive portion of original AlphaBetaFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailHardHistory

public int AlphaBetaFailHardHistory(int d,
                                    int alpha,
                                    int beta)
Searches game tree using original AlphaBetaFailSoft algorithm.
 function alphabeta(node, depth, α, β, Player)
  if  depth = 0 or node is a terminal node
      return the heuristic value of node
  if  Player = MaxPlayer
      for each child of node
          α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Beta cut-off *)
      return α
  else
      for each child of node
          β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Alpha cut-off *)
      return β

 (* Initial call *)
 AlphaBetaFailHardHistory(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailHardHistory

public int _AlphaBetaFailHardHistory(int d,
                                     int alpha,
                                     int beta)
Recursive portion of original AlphaBetaFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailHardTransposition

public int AlphaBetaFailHardTransposition(int d,
                                          int alpha,
                                          int beta)
Searches game tree using original AlphaBetaFailSoft algorithm.
 function alphabeta(node, depth, α, β, Player)
  if  depth = 0 or node is a terminal node
      return the heuristic value of node
  if  Player = MaxPlayer
      for each child of node
          α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Beta cut-off *)
      return α
  else
      for each child of node
          β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))
          if β≤α
              break                             (* Alpha cut-off *)
      return β

 (* Initial call *)
 AlphaBetaFailHardTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailHardTransposition

public int _AlphaBetaFailHardTransposition(int d,
                                           int alpha,
                                           int beta)
Recursive portion of original AlphaBetaFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailHard

public int NegaScoutFailHard(int d,
                             int alpha,
                             int beta)
Searches game tree using original NegaScoutFailSoft algorithm.
 function negascout(node, depth, α, β)
  if node is a terminal node or depth = 0
      return the heuristic value of node
  b := β                                             (* initial window is (-β, -α) *)
  foreach child of node
      a := -negascout (child, depth - 1, -b, -α)
      if α < a < β and child is not first child      (* check if null-window failed high *)
          a := -negascout(child, depth - 1, -β, -α)  (* full re-search *)
      α := max(α, a)
      if α ≥ β
          return α                                   (* Beta cut-off *)
      b := α + 1                                     (* set new null window *)
  return α

  (* Initial call *)
  NegaScoutFailHard(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailHard

public int _NegaScoutFailHard(int d,
                              int alpha,
                              int beta)
Recursive portion of original NegaScoutFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailHardTransposition

public int NegaScoutFailHardTransposition(int d,
                                          int alpha,
                                          int beta)
Searches game tree using original NegaScoutFailSoft algorithm.
 function negascout(node, depth, α, β)
  if node is a terminal node or depth = 0
      return the heuristic value of node
  b := β                                             (* initial window is (-β, -α) *)
  foreach child of node
      a := -negascout (child, depth - 1, -b, -α)
      if α < a < β and child is not first child      (* check if null-window failed high *)
          a := -negascout(child, depth - 1, -β, -α)  (* full re-search *)
      α := max(α, a)
      if α ≥ β
          return α                                   (* Beta cut-off *)
      b := α + 1                                     (* set new null window *)
  return α

  (* Initial call *)
  NegaScoutFailHardTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailHardTransposition

public int _NegaScoutFailHardTransposition(int d,
                                           int alpha,
                                           int beta)
Recursive portion of original NegaScoutFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailSoft

public int AlphaBetaFailSoft(int d,
                             int alpha,
                             int beta)
Searches game tree using AlphaBetaFailSoft algorithm.
  (* Initial call *)
  AlphaBetaFailSoft(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailSoft

protected int _AlphaBetaFailSoft(int d,
                                 int alpha,
                                 int beta)
Recursive portion of AlphaBetaFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailSoftHistory

public int AlphaBetaFailSoftHistory(int d,
                                    int alpha,
                                    int beta)
Searches game tree using AlphaBetaFailSoft algorithm and history table. Array history table need to be created beforehand.
  (* Initial call *)
  AlphaBetaFailSoftHistory(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailSoftHistory

protected int _AlphaBetaFailSoftHistory(int d,
                                        int alpha,
                                        int beta)
Recursive portion of AlphaBetaFailSoftHistory algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailSoftTransposition

public int AlphaBetaFailSoftTransposition(int d,
                                          int alpha,
                                          int beta)
Searches game tree using AlphaBetaFailSoft algorithm and transposition table. Array transposition table need to be created beforehand.
  (* Initial call *)
  AlphaBetaFailSoftTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailSoftTransposition

protected int _AlphaBetaFailSoftTransposition(int d,
                                              int alpha,
                                              int beta)
Recursive portion of AlphaBetaFailSoftTransposition algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

AlphaBetaFailSoftHistoryTransposition

public int AlphaBetaFailSoftHistoryTransposition(int d,
                                                 int alpha,
                                                 int beta)
Searches game tree using AlphaBetaFailSoft algorithm, history table and transposition table. Array history table and transposition table need to be created beforehand.
  (* Initial call *)
  AlphaBetaFailSoftHistoryTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_AlphaBetaFailSoftHistoryTransposition

protected int _AlphaBetaFailSoftHistoryTransposition(int d,
                                                     int alpha,
                                                     int beta)
Recursive portion of AlphaBetaFailSoftHistoryTransposition algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailSoft

public int NegaScoutFailSoft(int d,
                             int alpha,
                             int beta)
Searches game tree using NegaScoutFailSoft algorithm.
  (* Initial call *)
  NegaScoutFailSoft(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailSoft

protected int _NegaScoutFailSoft(int d,
                                 int alpha,
                                 int beta)
Recursive portion of NegaScoutFailSoft algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailSoftHistory

public int NegaScoutFailSoftHistory(int d,
                                    int alpha,
                                    int beta)
Searches game tree using NegaScoutFailSoft algorithm and history table. Array history table need to be created beforehand.
  (* Initial call *)
  NegaScoutFailSoftHistory(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailSoftHistory

protected int _NegaScoutFailSoftHistory(int d,
                                        int alpha,
                                        int beta)
Recursive portion of NegaScoutFailSoftHistory algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailSoftTransposition

public int NegaScoutFailSoftTransposition(int d,
                                          int alpha,
                                          int beta)
Searches game tree using NegaScoutFailSoft algorithm and transposition table. Array transposition table need to be created beforehand.
  (* Initial call *)
  NegaScoutFailSoftTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailSoftTransposition

protected int _NegaScoutFailSoftTransposition(int d,
                                              int alpha,
                                              int beta)
Recursive portion of NegaScoutFailSoftTransposition algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

NegaScoutFailSoftHistoryTransposition

public int NegaScoutFailSoftHistoryTransposition(int d,
                                                 int alpha,
                                                 int beta)
Searches game tree using NegaScoutFailSoft algorithm, history table and transposition table. Array history table and transposition table need to be created beforehand.
  (* Initial call *)
  NegaScoutFailSoftHistoryTransposition(depth, -infinity, +infinity)
 

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

_NegaScoutFailSoftHistoryTransposition

protected int _NegaScoutFailSoftHistoryTransposition(int d,
                                                     int alpha,
                                                     int beta)
Recursive portion of NegaScoutFailSoftHistoryTransposition algorithm.

Parameters:
d - depth
alpha - lower bound of expected value of the tree
beta - upper bound of expected value of the tree
Returns:
evaluation value

MTD

public int MTD(int d,
               int f)
Searches game tree using Memory-enhanced Test Driver algorithm and transposition table. Array transposition table need to be created beforehand.
  (* Initial call *)
  MTD(depth, 0)
 

Parameters:
d - depth
f - first guess of expected value of the tree
Returns:
evaluation value

IterativeMTD

public int IterativeMTD()
Searches game tree using Memory-enhanced Test Driver algorithm and transposition table with iterative deepening. Array transposition table need to be created beforehand.
  (* Initial call *)
  IterativeMTD()
 

Returns:
evaluation value

IterativeNegascout

public int IterativeNegascout()
Searches game tree using NegaScoutFailSoft algorithm and transposition table with iterative deepening. Array transpositoin table need to be created beforehand.
  (* Initial call *)
  IterativeNegascout()
 

Returns:
evaluation value

Eval

public abstract int Eval()
Evaluates leaf position from current player's standpoint.

Returns:
evaluation value

Generate

public abstract void Generate()
Generates valid moves of next board position.


UndoGenerate

public abstract void UndoGenerate()
Retracts generated valid moves and puts board back to previous position.


DoMove

public abstract void DoMove(int i)
Does i-th move from valid moves.

Parameters:
i - move index

DoMove

protected abstract void DoMove(Move mv)
Does move mv.

Parameters:
mv - move

UndoMove

public abstract void UndoMove()
Undo last move.


getMove

public abstract Move getMove(int i)
Gets i-th move from valid moves.

Parameters:
i - move index
Returns:
move

setDepth

public void setDepth(int depth)
Sets the depth.

Parameters:
depth - depth

getBestmove

public Move getBestmove()
Gets best move.

Returns:
best move

HistoryIndex

public abstract int HistoryIndex(int i)
Searches index of an i-th move from array of history table.

Parameters:
i - move index in array of valid moves
Returns:
index

HistoryIndex

public abstract int HistoryIndex(Move mv)
Searches index of a move from array of history table.

Parameters:
mv - move
Returns:
index

setStep

public void setStep(int step)
Sets current step of game simulation.

Parameters:
step - current step

PrintBoard

protected abstract void PrintBoard()
Prints board position to standard output. Useful for debugging.


PrintMove

protected abstract void PrintMove(Move mv)
Prints move to standard output. Useful for debugging.


GeneThello Home

Get GeneThello: Genetic Othello at SourceForge.net. Fast, secure and Free Open Source software downloads