Skip to content

Creating an AI Player

James Goodman edited this page Dec 28, 2020 · 10 revisions

Basics

Every agent in the framework has to extend the AbstractPlayer class and implement the getAction method. The Random Player can serve as a template for agents.

public class RandomPlayer extends AbstractPlayer {
    private final Random rnd;

    public RandomPlayer()
    {
        this(new Random());
    }

    @Override
    public AbstractAction getAction(List<AbstractAction> availableActions, AbstractGameState observation) {
        int randomAction = rnd.nextInt(availableActions.size());
        return availableActions.get(randomAction);
    }
}

The observation argument gives the observation to the agent that the agent might use for making better decisions. The agent gets a forward model from the game, which can be accessed by calling player.getForwardModel(). To advance the game state player.getForwardModel().next(gameState, action) can be used.

The AbstractPlayer class provides some optional functions: initializePlayer and finalizePlayer that can be used for loading the initial parameters (weights) and saving them after training. In some cases the player only has a single or no actions to choose from, in these cases instead of getAction the registerUpdatedObservation method gets called. This function lets the player know the gameState, so it can update its belief accordingly. An example when this function gets called: In UNO when the player has a single card to play.

Heuristics

All the games have a basic scoring function implemented which can be accessed by calling observation.getScore(this.getPlayerID()). If desired, the game heuristics can be overwritten, and the game state evaluated using a custom heuristic. The new heuristic has to implement the IStateHeuristic interface along with its evaluateState(AbstractGameState gs, int playerId) function. The agents implemented in the framework (OSLA, RMHC, MCTS) all support custom heuristics.

To use one of these agents with a custom heuristics, just supply it in the constructor: players.add(new OSLAPlayer(new CustomHeuristics()), or as part of the Parameters (for MCTS and RMHC). If no heuristic is provided, then the agent uses the default getHeuristicScore function from the game. When creating a new player the developer should take care of handling the custom Heuristics as the AbstractPlayer does not enforce its usage.

All heuristics should ideally support the ITunableParameters interface, as this allows them to be easily tuned as described in Optimising an AI. See any of the existing Heuristics for examples to follow.

Clone this wiki locally