Neural Network Football

Neural Network Football is an experiment of developing a football-playing artificial intelligence by utilizing neural networks that are trained using reinforcement learning and genetic algorithms.

Original Neural Network Football was published in summer 2012.

The environment

The goal of this project is to see if it is possible to teach a neural network AI to play a highly simplified version of football in an entertaining fashion.
“Entertaining” in this case does not necessarily mean “human smart” way of playing football but any kind of game play that is fun to observe.
To keep things simple, the game of football was reduced to few key elements: two teams, two opposite goals, one ball. The team that drives the ball to opponents goals more often is the winner.
If team scores, ball is moved back to the center of the pitch and teams are returned to their starting positions. There are no further rules, e.g. all ball out of play situations are omitted.
The edges of the pitch are solid so that the ball or the players can not accidentally get out of the pitch.

Screenshot of the match in progress:

Screen of a match in progress

Structure of the neural network

Each football player is controlled by neural network AI.
The same neural network is copied to each of the team members, which means that any member of the team would act similarly on same inputs.
The neural network is given the following input vector:

  • Player’s distance to ball along x- and y-axis
  • Player’s distance to opponent goal along x- and y-axis
  • Player’s distance to nearest team mate along x- and y-axis
  • Player’s distance to nearest opponent along x- and y-axis
  • Player’s distance to nearest field edge

The neural network consists of two 10-node hidden layers and connections between the nodes. Each connection has its own weight that is applied to the value of the node.
This means we have three matrices of weights with the following sizes: (number of inputs)x10, 10×10 and 10x(number of outputs).
Each node calculates a nonlinear weighted sum of its inputs.

Example of how nonlinear weighted sum is calculated

The four outputs of the neural network control four parameters of the player:

  • Player movement speed
  • Direction of player movement
  • Power of kick
  • Kick direction

Movement speed and kick power are limited certain ranges. Player can kick ball to any direction while moving to any other direction.
For simplicity, player tries to kick the ball on every game step, i.e. there is no decission making whatever or not player should try to kick.
This also allows player to run with the ball by applying small kicks to the ball on each game step.

The final structure of the neural network:
The structure of the neural network

Evolving the network

Initially there are 24 randomly generated neural networks – one for each team. Teams attend to a tournament in which all teams play twice against each other, a home game and an away game.
For each match the teams are awarded with tournament points and fitness points. For winning a match, team receives 3 tournament points, a tie is worth 1 point and losing a match does not give any tournament points.
Fitness points are based on the overall performance of the team and they accumulate during the tournament:

  • Distribution of the team’s players gives fitness points. The more distributed the better (to a certain extent)
  • Each players’ distance to the ball gives points. The closer the better (to a certain extent)
  • The distance of the ball from the opponent goal gives points.
  • Each successful kick of the ball gives points. Powerful kicks are more valuable
  • Each goal gives points.

Once the tournament is complete, the teams are ranked based on their tournament points, and if the points are equal, by their fitness points. The best five team are kept for the next tournament.
Rest of the teams are given new neural networks that are generated from the Top-5 teams’ neural networks by randomly selecting two of the top networks and mixing the connection weights of those networks.
The resulted network is then mutated by multiplying the weights with 1-mean normal distribution random value and by adding 0-mean normal distribution random value to them (standard deviation varies based on how “severe” mutation we want to apply).
This next generation of teams then starts a fresh tournament with all of their points resetted.

Creating new neural network from two winning networks:
Example of neural network crossover

Increasing the performance

As the neural networks require lots of matrix operations, resolving even a single match takes approximately 10-20 seconds on a modern CPU (using single core).
Each tournament has 552 matches and tens of tournaments need to be played before any progress in the evolved AI can be noticed.
In order to speed up the match resolving, a distributed solution was created. The Host initiates a tournament by creating a pool of all matches in that particular tournament.
An Agent queries the Host for a match that needs to be resolved. The Host serializes the match parameters (team names, their neural networks etc.) and sends them to the Agent.
The Agent then resolves the match results (team score and fitness points) and records the match events (player and ball movement) and sends the results back to the Host.
The Host collects the data and once all of the matches have been resolved, declares the tournament complete and ranks the teams based on their performance. Then the Host initiates the next tournament and the cycle continues.

Since the Host and the Agent communicate over network, Agents can be run on multiple PCs – and multiple Agents can be run in parallel on PCs with a multi-core CPU.
This gives a significant boost on the tournament resolving performance and therefore speeds up the evolution as more generations of neural networks can be tried in the same timeframe.

Random thoughts

  • The actual neural network football software is written in Python, and uses Numpy for math operations and Pygame for runtime visualization.
  • Each match record can be visualized via this web page’s HTML5 + JavaScript player. Currently, only the latest 2 tournaments are displayed completely in order to keep the loading times at bay.
  • The structure of the neural network is far from perfect. It would be interesting to change (or add) the inputs to something completely different (e.g. distance from center of own/opponent team).
    Endless possibilities but unfortunately changes to neural network pretty much mean scrapping the current networks and restarting from “generation 0”.
  • Currently there are 5 to 10 agents resolving the matches. If there’s enough interest, I might investigate a way to share the agent software.
  • Sometimes evolution can become stalled. Good example was tournaments from ~250 to 286 where certain playstyle was so dominant that there was practically no difference between the top-5 teams which meant that crossover produced similar networks
    and mutation itself was not able to produce enough variation to overcome the dominating playstyle as its standard deviation was ~0.1. This was solved by making the standard deviation of the addition component to have a relation to the mean value of the weights of connections between two layers.
    This ensures that the addition component has same scale (e.g. 1, 10, 100, 1000 etc.) as the weight it is being added to.

~315700 matches played.
Match with highest points: Tournament 449, match 0: AA 8 – BB 0 (8375530 – 280569)
Match with most goals: Tournament 449, match 93: EE 8 – BB 0 (8342491 – 287079)


Tournament 571 | Matches played: 552/552

TeamMatchesWinsTiesLossesPointsFitnessHome matches
1.VV462117880117881624AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU WW XX
2.KK462117880106050367AA BB CC DD EE FF GG HH II JJ LL MM NN OO PP QQ RR SS TT UU VV WW XX
3.AA462116979128120111BB LL MM NN OO PP QQ RR SS TT UU CC VV WW XX DD EE FF GG HH II JJ KK
4.BB462116979126164333AA CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
5.PP462116979111485675AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO QQ RR SS TT UU VV WW XX
6.II46211697979578078AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
7.HH46211697933912186AA BB CC DD EE FF GG II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
8.QQ462018878113516729AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP RR SS TT UU VV WW XX
9.DD462017977113468329AA BB CC EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
10.EE4692895521017864JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX AA BB CC DD FF GG HH II
11.JJ461450489182721AA BB CC DD EE FF GG HH II KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
12.NN4604334310494473AA BB CC DD EE FF GG HH II JJ KK LL MM OO PP QQ RR SS TT UU VV WW XX
13.SS460397397063589AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR TT UU VV WW XX
14.FF460397394303384AA BB CC DD EE GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
15.UU460388386924224AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT VV WW XX
16.RR4603793712183226AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ SS TT UU VV WW XX
17.CC4603793711807112AA BB DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
18.MM4603793711787784AA BB CC DD EE FF GG HH II JJ KK LL NN OO PP QQ RR SS TT UU VV WW XX
19.TT460379378999938AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS UU VV WW XX
20.OO460379378960468AA BB CC DD EE FF GG HH II JJ KK LL MM NN PP QQ RR SS TT UU VV WW XX
21.XX460379377850341AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW
22.LL4603610369433020AA BB CC DD EE FF GG HH II JJ KK MM NN OO PP QQ RR SS TT UU VV WW XX
23.GG4603610368019218AA BB CC DD EE FF HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
24.WW4603511358066536AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV XX

Tournament 570 | Matches played: 552/552

TeamMatchesWinsTiesLossesPointsFitnessHome matches
1.AA462120583125717647BB LL MM NN OO PP QQ RR SS TT UU CC VV WW XX DD EE FF GG HH II JJ KK
2.BB462120583120781593AA CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
3.QQ462120583117879570AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP RR SS TT UU VV WW XX
4.DD462120583116109751AA BB CC EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
5.PP462021581114000396AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO QQ RR SS TT UU VV WW XX
6.CC46172457558091278AA BB DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
7.JJ4604514510147989AA BB CC DD EE FF GG HH II KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
8.VV4604334311764158AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU WW XX
9.II460433438555428AA BB CC DD EE FF GG HH JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
10.OO4613964212252068AA BB CC DD EE FF GG HH II JJ KK LL MM NN PP QQ RR SS TT UU VV WW XX
11.RR461396428697657AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ SS TT UU VV WW XX
12.KK4604154111711902AA BB CC DD EE FF GG HH II JJ LL MM NN OO PP QQ RR SS TT UU VV WW XX
13.GG4604154110032954AA BB CC DD EE FF HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
14.XX460415419405774AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW
15.MM460415415761300AA BB CC DD EE FF GG HH II JJ KK LL NN OO PP QQ RR SS TT UU VV WW XX
16.LL4604064011511151AA BB CC DD EE FF GG HH II JJ KK MM NN OO PP QQ RR SS TT UU VV WW XX
17.SS4604064011243186AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR TT UU VV WW XX
18.NN4604064010205595AA BB CC DD EE FF GG HH II JJ KK LL MM OO PP QQ RR SS TT UU VV WW XX
19.HH460406409591577AA BB CC DD EE FF GG II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
20.FF460406409417566AA BB CC DD EE GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX
21.TT460406409392540AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS UU VV WW XX
22.UU460406408568211AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT VV WW XX
23.WW460406407445062AA BB CC DD EE FF GG HH II JJ KK LL MM NN OO PP QQ RR SS TT UU VV XX
24.EE460406407208849JJ KK LL MM NN OO PP QQ RR SS TT UU VV WW XX AA BB CC DD FF GG HH II

Random matches from older generations that show how the AI has improved over time
Tournament 286, match 258: LL 6 – FF 0 (6395939 – 237654)
Tournament 227, match 368: QQ 4 – AA 0 (4422505 – 288856)
Tournament 200, match 42: BB 0 – UU 0 (588763 – 205841)
Tournament 100, match 1: AA 0 – CC 0 (99621 – 179896)
Tournament 50, match 80: DD 0 – MM 0 (2271456 – 220127)
Tournament 10, match 24: BB 0 – CC 0 (256687 – 236597)
Tournament 0, match 13: AA 0 – OO 0 (347255 – 341457)


Leave a Reply

Your email address will not be published. Required fields are marked *

*