How to do it...

We will implement a Bandit class for building the logic behind the algorithm:

  1. Create a new class named Bandit:
using UnityEngine;public class Bandit : MonoBehaviour{  // next steps}
  1. Define the required member variables:
bool init;int totalActions;int[] count;float[] score;int numActions;RPSAction lastAction;int lastStrategy;
  1. Define the function for initializing the UCB1 algorithm:
public void InitUCB1(){  if (init)    return; // next step}
  1. Define the local variables and initialize them:
  totalActions = 0;  numActions = System.Enum.GetNames(typeof(RPSAction)).Length;  count = new int[numActions];  score = new float[numActions];  int i;  for (i = 0; i < numActions; i++)  {    count[i] = 0;    score[i] = 0f;  }  init = true;
  1. Define ...

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.