Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/public/wp-includes/functions-formatting.php on line 76
Here is a fun little game I wrote using Python and pygame.
You’re a knight and your job is to kill as many dragons as you can. The twist is that the dragons use Genetic Programming to learn from every encounter. (You can optionally have them use Reinforcement learning instead too.)
You can download the source code and all files needed to run the game here (8.6 MB, GPL).
To run it, you just need Python and pygame installed.
Kill as many dragons as fast as you can.
The arrow keys move ye.
The space bar swings your sword.
ctrl+c to quit.
When using Genetic Programming, the dragons basically learn like this. Every dragon gets assigned a randomly created program tree. Here’s an example in LISP like syntax:
Which means this:
if knight is facing me: if 20< =distance to knight: Breath Fire else: move away from knight else: move towards knight
Dragons get points the longer they survive in a level, and they get a whole lot of points for killing the knight. After the end of a level, the dragons’ program trees are mixed up with each other to create new programs. program trees associated with the most points, get chosen more frequently to reproduce.
Sadly this scheme turned out not to work too well, though it does work once in a while. If you play say 20 games (not levels), you’ll probably see an instance of the dragons learning to chase you. But normally they don’t learn much of anything.
Here are some of the reasons I think the learning doesn’t go better:
1. The population is too small for the search space.
2. Even good dragons can get killed, just by standing too close to another dragon. They have no means to learn not to do this.
3. Fitness isn’t fair. If a dragon starts out far away from the knight (placement is random) he will probably live longer than a dragon closer to the knight.
If anyone wants to fix these things, please send me your patches.
I also tried Reinforcement learning for the dragons. This actually made them good at turning around when the knight comes near, and shooting fire at him. But they could never learn how to move.
Please try it out and let me know your feedback. Do you have any ideas on how to scale up this idea and make it into a fun game? Any ideas how to improve the AI?