Demo of neural networks

Hi all,
This demo shows the capabilities of neural networks. The idea came up in another of my experiments in my version of the classic “Asteroids” :smile: . In another post I will show how I got the neural networks of this demo.

Live demo: https://jjcapellan.github.io/nn-demo1/
Repository: https://github.com/jjcapellan/nn-demo1

There is a population of 50 neural networks that was trained for 60 generations through a genetic algorithm (NeuroEvolution of Augmenting Topologies).

Each neural network controls a triangle and has its own behavior.

Triangle

Its objective is dodge the obstacles.

Each triangle captures data in a radius of 250 pixels.

For this it has 8 sensors spread around.

Sensors

Each sensor has a “viewing” angle of 360/8 degrees.

If in the area monitored by the sensor there is nothing, it will return the data 1.

But if there is one or more obstacles it will return:


sensorValue = distance of the nearest obstacle / detection radius

Input

The input is an array with all sensors data.

It is updated every frame.

The input is used by the network to decide if the triangle should turn to the left or to the right.

The input data must be normalized to values between 0 and 1.

Output

The triangle have 2 possible actions: Turn left or turn right.

With the input, the network determines the probability that each of these actions is correct.

Those probabilities are stored in an array (output).

Example


// One neural network stored in a JSON object.

let NN = {

"nodes": [/* here the nodes*/],

"connections": [/* here the connections*/],

"input": 8,

"output": 2,

"dropout":0

};

// Network creation from a JSON object

let myNetwork = neataptic.Network.fromJSON(NN);

// Takes the needed data

let input = captureData(); // returns [1, 1, 1, 1, 0.6, 1, 0.8, 1]

// The network calculates the output

let output = myNetwork.activate(input); // returns [0.9, 0.3]

// Analyzing the output, the correct action is executed

if(output[0] > 0.5 && output[0] > output[1]){

turnLeft();

} else if (output[1] > 0.5){

turnRight();

}

// The result will be "turn left"

Libraries used

  • Phaser 3 : I know you know it :grinning:.

  • Neataptic : Neuro-evolution on steroids, right in the browser. (unmaintained)

  • simplex-noise: a fast simplex noise implementation in Javascript.

Last update:
Added option to dodge triangles to other triangles. The triangles were not trained to dodge their congeners, but they do it quite well. :smile:

Regards.

7 Likes

That’s pretty cool. Just curious - you said 50 neural networks - was that for each triangle. How many nodes and layers in each neural network? And what kind of model do they run?

Great to see Phaser being used for applications beyond gaming.

Hi @npobbathi,
I’m glad you like it.

Each triangle has its own neural network.

To generate these networks I used the Neataptic library, which implements a genetic algorithm called NEAT ( neuroevolution of augmenting topologies) that in each generation not only changes its weights and biases but also its structure and activation functions, so there isn’t single type of network.
In this case the great majority are like a perceptron of a single layer, reason why they have 10 nodes (8 input, 2 output, 0 hidden), but with 2 additional connections between the outputs.

As a curiosity, in generation 70, the best network endured without colliding 779 seconds. You can see that network in the “bestGenome” property of this JSON object:

https://github.com/jjcapellan/nn-demo1/blob/master/js/population50.js
This network can be used in some type of game as an opponent.
Regards.

1 Like