Genetic Algorithms and Machine Learning in wargames

Started by Pakz, January 17, 2021, 09:14:52

Previous topic - Next topic

Pakz

I was looking for programming books and found a book titled "Genetic Algorithms and Machine Learning for Programmers: Create AI Models and Evolve Solutions" It was in a list of bought by others who bought "Mazes for programmers" This is a book I have and thought was readable.

When I was waiting for it to get delivered I decided to experiment with what I had learned from previous books. This a Genetic algorithm.

I wanted to see if I could put the algorithm in a strategy war game setting. This were it moves units to capture a city and take out as many enemy units as possible.  I had coded it so it has a small map set up in a multidimensional array. 4 vs 4 units and a city. The code picks random spots on the map and gives a movement instruction at the same time. All this gets stored. This gets done a x amount of times and than gets reviewed if anything worth while happens. Will any units get destroyed or any units get close to goal.
A number of the best scoring sets get copied and mutated. The mutating is done by redoing a percentage of the instuctions from %..end of the instructions. A number of new instruction sets get added also.

The results after a x amount of runs was that they were able to capture the objective and take out most or all of the enemy fortified units. Though when modding map later on it appeared the system did not seem to work on more complex maps. Even when I added a dijkstra map(a map with the distance on each grid to objective)

Here a video of the first working version :



I now have been reading the book for a day or two. It is a fun book I think. Not a academic book. You learn to understand what machine learning and Genetic algorithms are by coding particles/bees/ants/cannon balls etc. out of a paper bag. The first chapter is in the python language and it's turtle library. You learn to make the turtle escape out of a paper bag(2d rectangle on the canvas) Different programming languages are used like javascript, c++, python.

The chapters I liked most so far are the ones about creating a "ant colony optimization" Here you code ants and pheromones and their paths that eventually lead out of the paper bag. The chapter on bees is nice also. Here you make bees teach themselfs to find food sources and exchange knowledge. Inactive bees in their nests store locations and remember these and share this information with other bees.

There is a chapter about Cellular Automata and Conways game of life. You learn about it's history and how to code it.

The book is not thick. There is not a lot of overly complex code and math formula's. Though I think the code could be explained more as I have spent quite some time trying to understand it.

I have now been thinking of how to code ai that learns to optimize a map. Making roads and irrigation and mines and other cities.

I wish there were more books like this!

Derron

You already did the basics: give scores to achieved results. So each iteration ("simulation of a game") the score defines how good it reached the desired results.

If you want to take different tiles (slow movement etc) into consideration, then just give the movement a kind of "effectiveness" value - similar to your map and the distances. This way your AI can optimize for "effectiveness" too - and learns to move to a certain goal in less amount of time. BUT ... it also learns that it sometimes needs to move through a swam as it still is faster than cirvumenting it (more moves = less effective, more moves might allow the enemy to move too).
Give tiles bonus or malus on attacks to other tiles (mountain -> grassland) and the AI will effectively use that too.

At the end you get your neural network and you can use it to do movement and attacks.

Of course it surely misses a lot of steps but in theory this sounds as if it should work (I never simulated AI stuff for games, just for other purposes).


bye
Ron

Matty

This was my first introduction to artificial intelligence programming.  I would have been in high school. Attached.

Dabz

Ohhhh, I havent got that one!

I like to collect old Amstrad books, I have quite a few, probably worth something now... probably, maybe not! :D
Intel Core i5 6400 2.7GHz, NVIDIA GeForce GTX 1070 (8GB), 16Gig DDR4 RAM, 256GB SSD, 1TB HDD, Windows 10 64bit