


|
|

(Commander Class Code Sample - 2010)
Download it here: Code Sample (.cs file, open in any text editor)
This code sample is a C# class written as part of a University project in an Advanced AI class. The goal of the project was to see if reinforcement learning, specifically Q-Learning could be used to create an AI adversary in a game. In this project, the AI adversary takes the role of a "Commander" whose job is to spend resources (money) on units (ships) which will automatically go and attack the player. As such, the game can be likened to a Real-Time Strategy Game, with the Commander having to make decisions on when to spend his money, when to save and which units to buy.
The Q-Learning algorithm used here is a form of reinforcement learning, which means that the Commander is trying to maximize "rewards". In this game, rewards are given when Commander controls many units at once and when the player's health is low, as such states are more likely to result in a win for the Commander. The Commander will start by trying actions at random, note which ones give the biggest reward and then prefer those actions over others, giving the impression that the Commander is "learning" to play the game.
This specific class represents the Commander's behavior and learning. This class is the most interesting part of the Q-Learning implementation, since it is where the "magic" happens. Everything is done in the Play() function; The Commander looks at what is currently going on in the game, takes the action which he thinks is best for this situation and then examines the result, adding it to his knowledge base.
This project received perfect marks and won a prize, awarded by Ubisoft.
|
|