Using Decision Tree

"A decision tree is a decision support recursive partitioning structure" Wikipedia. It can be used to partitioning states, i.e. to groups similar states together into super-states.

Grouped states should be close in terms of behavior: expected action, value and transition. If such a structure can be built over the state space, then it is possible to exploit it into reinforcement learning.

Simple version With Py421

Py421 advantages the combinations 4-2-1, 1-1-1 or X-1-1. A first idea could be to focus state definition to target those combinations.

This idea modeled as a tree will lookalike:
DT for 421

This tree can be implemented with a "if-then-else" procedure into a QLearner Bot, to learn a good behavior on Py421 game only over 7 states.

As a result, learning should be very fast but with a final behavior less performant than learning applied over all the 168 game states.

Expert state definition for Risky

The goal is to apply state factorization/clustering to a game where the number of states and actions prevents from using QLearning over all the states. That for, the exercise consists of applying QLearning to risky.

1. As a first, action you should get familiar with the game mechanisms: tuto risky

2. Then, you have to trace (ie. print) a complete definition of crossed states.

QUESTION: How many states describe risky configuration (considering the default BOARD-4) ?

3. Finally, try to learn a policy using QLearning over meta actions.

QUESTION: Which action has the worst branching number ?

A simple score over the game can be computed as the difference between armies total size. This score feeds appropriately the reward computation.

    def totalArmies(self) :
        myArmy= 0
        opponentArmy= 0
        for ic in self._game.cellIds() :
            army= self._game.armyOn(ic)
            if army :
                if self._playerId == army.status() :
                    myArmy+= army.digit(2)
                else :
                    opponentArmy+= army.digit(2)
        return myArmy, opponentArmy

4. Speed-up the learning process by reducing the state definition (for instance by considering only player armies (ignoring opponent details).

5. Its working well ? try your solution on board-6 then board-10 and board-12. The board can be set on the GameRisky constructor, in launcher script:

from hacka.games.risky import GameRisky as Game
game= Game( map="board-6" )

Generate Decision-Tree in Risky

In this next step, the goal is to learn a decision tree from data. A good course introducing decision tree learning is proposed on the w3schools.

In our case the data can be generated from our first QLearner Bot. The CSV (ComaSeparetedValue) file is built at sleep step to trace visited states and actions associated with the final game status.

states variables, action, winner
states variables, action, winner
states variables, action, winner
states variables, action, winner
states variables, action, winner
...
  • states variables: coma separated values of all state variables.
  • action: selected action (defend, expend-1, expend-2, ... fight-1, ...).
  • winner: a boolean ( positive \(1\) if the player win, negative \(0\) else).

The model should be trained to predict if the player is on a winning trajectory. In other terms, if the current state and the applied action can be labeled as good or bad considering a predicted end of the game.

The generated Decision-Tree model can be analyzed regarding \(2\) aspects:

  • Prediction accuracy: The capability the model has to predict good and bad situation and responses.
  • Model readability: The size of the decision tree (the smallest the best)

Naturally, the \(2\) aspects are in contradiction, but generally the reduction of the decision tree is welcomed while the consequences of prediction accuracy is low.

Tricks:

  • It is generally possible to fix an expected size for the Decision-Trees.
  • While we are not dependent anymore to the number of state and action variables (i.e. fixed decision-tree size), it is possible to generate as many new descriptive variables we want (for instance total size of the army, number occupied territories...).

(An alternative to the solution proposed by the w3schools exits with scikit-learn).

Going further

The solution should perform on a more complex board (from board-6 to board-12). You can apply a classical reinforcement learning approach consisting of a loop:

  1. Use the learned Decition-Tree into QLearner Bot.
  2. Generate data from learned new QValues
    (the behavior should be increased, so different than previously used data set).
  3. Regenerate a new Decition-Tree.
  4. Return to step \(1.\)