How to do it...

We will now implement two classes. The first one stores values in a dictionary for learning purposes and the second one actually holds the Q-learning algorithm. Go through the following steps to create the two classes:

  1. Create the QValueStore class:
using UnityEngine; 
using System.Collections.Generic; 
 
public class QValueStore : MonoBehaviour 
{ 
    private Dictionary<GameState, Dictionary<GameAction, float>> store;     
} 
  1. Implement the constructor:
public QValueStore() 
{ 
    store = new Dictionary<GameState, Dictionary<GameAction, float>>(); 
} 
  1. Define the function for getting the resulting value of taking an action in a game state. Carefully craft this so that an action cannot be taken in that particular state:
public virtual float ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.