Generic alpha beta search with C ++

I am trying to create a function template that looks for the best move for any game - of course, the user of this function template must implement some game functions. What I'm trying to do is generalize alpha beta search with function template.

The declaration of this function template is as follows:

template<class GameState, class Move,
         class EndGame, class Evaluate, class GetMoves, class MakeMove)
int alphaBetaMax(GameState g, int alpha, int beta, int depthleft);

By the way, the function should:

  • Determine if the game has ended: bool EndGame(g)
  • Rate the game: int Evaluate(g)
  • Get possible moves: std::vector<Move> moves = GetMoves(g)
  • Make a move: Gamestate gnew = MakeMove(g, moves[i])

Do you think a function has many template arguments? Is there a way to reduce the number of arguments? One idea is to extend the GameState class with elements that evaluate the gamestate or decide whether the game is over. But the alpha beta search tree contains many instances of Gamestate, which can lead to unnecessary memory requirements, so I like to keep Gamestate small. In general, is the correct function template really the right one?

+3
source share
6 answers

You can define an abstract interface, say, game_traits and have a specialized implementation of game_traits for each game:

template<typename Game>
class game_traits {
  ...
};

class Chess {
  ...
};

template<>
class game_traits<Chess> {
  static bool endGame(Chess game);
  ...
};

template <typename Game, typename traits = game_traits<Game> >
int alphaBetaMax(Game game, int alpha, int beta, int depthleft) {
  ended = traits::endGame(game);
  ...
}

See char_traits in the C ++ Standard Library for how it is used there.

Game, - , . , , , , , game.has_ended(), . .

btw, ; :

auto concept GameType<typename Game>
{
  bool has_ended(Game&);
  ...
};

template<typename Game> requires GameType<Game>
int alphaBetaMax(Game game, int alpha, int beta, int depthleft) {
  bool ended = game.has_ended();
  ...
}

, ++ 0x: (

+5

, :

  • GameState, EndGame, GetMoves, Evaluate - GameStateTraits
  • MakeMove , GameMovePolicy

. Boost , , . , . - MakeMove .

template<typename GameStateTraits, typename GameMovePolicy>
int alphaBetaMax(GameStateTraits const& state, int alpha, int beta, int depthleft);
+2

. . GameState , .

GameState , .

:

template<class GameState>
int alphaBetaMax(GameState g, int alpha, int beta, int depthleft) {
   if (endGame(g)) {
     return 1;
   }
   std::vector<Move> moves = getMoves(g);
   // ...   
}

endGame getMoves - , ( g ). ( , , GameState ).

, , :

struct MyGameState {};

bool endGame(const MyGameState &st) {
  return false;
}

std::vector<Move> getMoves(const MyGameState &st) {
  // ...
}

void tst() {
  MyGameState s;
  alphaBetaMax(s, 1, 1, 1); // uses the new functions
}

GameState , . , .

, , .

+2

:

int alphaBetaMax(GameState *g, Move *move, EndGame *endgame, 
    Evaluate *evaluate, GetMoves* getmoves, MakeMove* makemove, 
    int alpha, int beta, int depthleft);

:

GameState gs;
alphaBetaMax(&gs, new ChessMove(), new ChessEndGame(), new ChessEvaluate(),
    new ChessGetMoves(), new ChessMakeMove(), a, b, 40);

, ( , , , ?).

- , , " ", " ", "", . 5 , ++ .

+1

? ? , " ", . - , , .

+1

: gamestate " " - ( ) gamestate , , ? ...

0

Source: https://habr.com/ru/post/1729730/


All Articles