Reader small image

You're reading from  Unity 5.x Game AI Programming Cookbook

Product typeBook
Published inMar 2016
PublisherPackt
ISBN-139781783553570
Edition1st Edition
Tools
Right arrow
Author (1)
Jorge Palacios
Jorge Palacios
author image
Jorge Palacios

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.
Read more about Jorge Palacios

Right arrow

Chapter 7. Learning Techniques

In this chapter, we will explore the world of machine learning through the following topics:

  • Predicting actions with an N-Gram predictor

  • Improving the predictor: Hierarchical N-Gram

  • Learning to use a Naïve Bayes classifier

  • Learning to use decision trees

  • Learning to use reinforcement

  • Learning to use artificial neural networks

.Introduction


In this chapter, we will explore the field of machine learning. This is a very extensive and intrinsic field in which even AAA titles have a hard time due to the amount of time that the techniques require for fine-tuning and experimentation.

However, the recipes that are contained in this chapter will give us a great head start in our endeavor to learn and apply machine-learning techniques to our games. They are used in several different ways, but the one we usually appreciate the most is difficulty adjustment.

Finally, you are advised to complement the recipes with the reading of more formal books on the subject, in order to gain theoretical insights that lie beyond the scope of this chapter.

Predicting actions with an N-Gram predictor


Predicting actions is a great way to give players a challenge by going from random selection to selection based on past actions. One way to implement learning is by using probabilities in order to predict what the player will do next, and that's what an N-Gram predictor does.

To predict the next choice, N-Gram predictors hold a record of the probabilities of making a decision (which is usually a move), given all combinations of choices for the previous n moves.

Getting ready…

This recipe makes use of general types. It is recommended that we have at least a basic understanding of how they work because it's critical that we use them well.

The first thing to do is implement a data type for holding the actions and their probabilities; we'll call it KeyDataRecord.

The KeyDataReconrd.cs file should look like this:

using System.Collections;
using System.Collections.Generic;

public class KeyDataRecord<T>
{
    public Dictionary<T, int> counts;
...

Improving the predictor: Hierarchical N-Gram


The N-Gram predictor can be improved by having a handler with several other predictors ranging from 1 to n, and obtaining the best possible action after comparing the best guess from each one of them.

Getting ready…

We need to make some adjustments prior to implementing the hierarchical N-Gram predictor.

Add the following member function to the NGramPredictor class:

public int GetActionsNum(ref T[] actions)
{
    string key = ArrToStrKey(ref actions);
    if (!data.ContainsKey(key))
        return 0;
    return data[key].total;
}

How to do it…

Just like the N-Gram predictor, building the hierarchical version takes a few steps:

  1. Create the new class:

    using System;
    using System.Collections;
    using System.Text;
    
    public class HierarchicalNGramP<T>
    {
        
        public int threshold;
        public NGramPredictor<T>[] predictors;
        private int nValue;
    }
  2. Implement the constructor for initializing member values:

    public HierarchicalNGramP(int windowSize)
    ...

Learning to use Naïve Bayes classifiers


Learning to use examples could be hard even for humans. For example, given a list of examples for two sets of values, it's not always easy to see the connection between them. One way of solving this problem would be to classify one set of values and then give it a try, and that's where classifier algorithms come in handy.

Naïve Bayes classifiers are prediction algorithms for assigning labels to problem instances; they apply probability and Bayes' theorem with a strong-independence assumption between the variables to analyze. One of the key advantages of Bayes' classifiers is scalability.

Getting ready…

Since it is hard to build a general classifier, we will build ours assuming that the inputs are positive- and negative-labeled examples. So, the first thing that we need to address is defining the labels that our classifier will handle using an enum data structure called NBCLabel:

public enum NBCLabel
{
    POSITIVE,
    NEGATIVE
}

How to do it…

The classifier...

Learning to use decision trees


We already learned the power and flexibility of decision trees for adding a decision-making component to our game. Furthermore, we can also build them dynamically through supervised learning. That's why we're revisiting them in this chapter.

There are several algorithms for building decision trees that are suited for different uses such as prediction and classification. In our case, we'll explore decision-tree learning by implementing the ID3 algorithm.

Getting ready…

Despite having built decision trees in a previous chapter, and the fact that they're based on the same principles as the ones that we will implement now, we will use different data types for our implementation needs in spite of the learning algorithm.

We will need two data types: one for the decision nodes and one for storing the examples to be learned.

The code for the DecisionNode data type is as follows:

using System.Collections.Generic;

public class DecisionNode
{
    public string testValue;
...

Learning to use reinforcement


Imagine that we need to come up with an enemy that needs to select different actions over time as the player progresses through the game and his or her patterns change, or a game for training different types of pets that have free will to some extent.

For these types of tasks, we can use a series of techniques aimed at modeling learning based on experience. One of these algorithms is Q-learning, which will be implemented in this recipe.

Getting ready…

Before delving into the main algorithm, it is necessary to have certain data structures implemented. We need to define a structure for game state, another for game actions, and a class for defining an instance of the problem. They can coexist in the same file.

The following is an example of the data structure for defining a game state:

public struct GameState
{
    // TODO
    // your state definition here
}

Next is an example of the data structure for defining a game action:

public struct GameAction
{
    // TODO
  ...

Learning to use artificial neural networks


Imagine a way to make an enemy or game system emulate the way the brain works. That's how neural networks operate. They are based on a neuron, we call it Perceptron, and the sum of several neurons; its inputs and outputs are what makes a neural network.

In this recipe, we will learn how to build a neural system, starting from Perceptron, all the way to joining them in order to create a network.

Getting ready…

We will need a data type for handling raw input; this is called InputPerceptron:

public class InputPerceptron
{
    public float input;
    public float weight;
}

How to do it…

We will implement two big classes. The first one is the implementation for the Perceptron data type, and the second one is the data type handling the neural network:

  1. Implement a Perceptron class derived from the InputPerceptron class that was previously defined:

    public class Perceptron : InputPerceptron
    {
        public InputPerceptron[] inputList;
        public delegate float Threshold...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 5.x Game AI Programming Cookbook
Published in: Mar 2016Publisher: PacktISBN-13: 9781783553570
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jorge Palacios

Jorge Palacios is a software and game developer with a BS in computer science and eight years of professional experience. He's been developing games for the last five years in different roles, from tool developer to lead programmer. Mainly focused on artificial intelligence and gameplay programming, he is currently working with Unity and HTML5. He's also a game-programming instructor, speaker, and game-jam organizer.
Read more about Jorge Palacios