Lab Rat Example

This is certainly not the only kind of game you can make with AILIVE, but we think it is a good example of gameplay driven by a simple neural net.  This is just an overview of how we set it up, and what options there are to tweak.

To play Lab Rat, use the arrow keys to cheer on the rat as it makes it way through the maze.  This does not control the rat directly, but it applies a training step and teaches the rat to follow the direction the next time it is that area.

This means spamming the buttons is great for training!  But make sure you don’t give it any bad information!

labrat.png
compass_img.png
unnamed.png

Debug & Information

  • Row/Col: This is the information of which grid area the rat is currently located in.

    • This information is primarily what it bases it’s predictions off of.

  • Loss: This metric expresses if it gets new data it was not expecting.

    • You want this number to be as low as possible. Higher numbers are akin to ‘surprise’.

  • Updates: This is how many training updates the rat has received.

    • They stack round after round so it can improve.

  • Up/Right/Down/Left: These percentages represent how likely the rat thinks it should go in that direction.

  • You can also visualize which direction the rat is more likely to run in by enabling the “Compass” object.

AI Options

Rat Object

  • AI Controlled: When true, the rat is driven by machine learning, and player input provides training steps.  When false, the player directly controls the movement of the rat, like a traditional game.

  • Use Max Prediction: When true, the rat will always move in the most likely direction. When false, the rat will choose a random direction weighted by the prediction.

    • For example, it would have a 75% chance of going up, 10% chance of going left, 10% chance of going down and a 5% chance of going right.  Weighted sampling allows for more chance of ‘discovery’, and can possibly give the AI more organic behavior.

  • Prediction Update Time: This is how often the AI queries for a new prediction.

    • The longer this is, the slower “reaction time” it will have. The shorter it is, could cause jittering and “changing its mind”.

  • Correction Weight: This variable represents how many training steps there should be per input

    • This would be good if you want to give it a lot more data quickly, or reduce the need for button spamming.

  • Speed: This just determines how fast the rat moves.

    • It is not directly tied to machine learning, but should be balanced against the prediction update time.

  • INPUT_SIZE: This not be changed once initialized, otherwise it will likely invalidate whatever data you’ve generated so far. We set it at runtime, based on the size of the map (determined by Row Num and Col Num in the GameManager object). This is how large the input array for the AI is- it is what the AI can perceive.

  • HIDDEN_SIZE: This determines how complex the neural net is. The larger it is, the longer it will take to train, but the more complex/subtle it can be in making predictions. Tuning this number too high can impact performance.

 
rat_obj.png
gamemanager_obj.png

Game Manager

Most of the GameManager’s variables are just hooks for the UI

  • Row Num/Col Num: Specify how large you built your maze by number of grid units.

    • Since we just have the one map, these numbers are set by hand.

  • Grid Size: The interval that the grid is on.

    • The smaller the grid, the higher fidelity perception the AI has to know where it is located.

RatAIManager.cs

The content of these functions would be better investigated through DyNet’s documentation, if you are interested in changing the structure or function of the neural net.  I’ll give a brief explanation on how to use these functions in a live game environment, however.

 
  • OUTPUT_SIZE: This determines how many actions the AI can take. It is the size of the output array generated by the prediction.

    • Think of it as how many buttons it has access to.  In the case of Lab Rat, it can move Up, Down, Left and Right.

  • TrainStep(): This function takes an input array and an output array, and basically says “this input SHOULD yield this output”.

    • Do this enough times, and the neural net will eventually learn behavior!

  • GetPrediction(): The AI gives you it’s best guess based on what is learned. This function gives you an array of probabilities the size of OUTPUT_SIZE.  It takes an array the size of INPUT_SIZE.

    • In larger neural nets, this prediction can take a lot of computations, so we recommend only fetching a new prediction at longer intervals (not every tick).

 
debug.png

We call these functions based on gameplay, in the RatController class primarily, just like you would access functions for navigation, targeting, ect. You’ll see there is some algorithms to help translate the rat’s position into an input array and translate an output array into movement/animation.

It should serve a decent start to try modifying your own project to include a neural net!