This article will demonstrate the ability to easily write your neural network in Java. In order not to reinvent the wheel, take the already well-developed library of the Fast Artificial Neural Network. Using neural networks in their Java projects is real. You can often hear accusations against Java regarding execution speed. Although the difference is not so great - you can learn more about this in the publication
“C ++ vs. Performance. Java vs. PHP vs. Python. Test "in the forehead." We will use a wrapper around the FANN library.
Task
You need to write a system that can make decisions for a character who can meet one or more enemies. The system may be aware of:
- character's health percentage;
- the presence of a pistol;
- number of enemies.
The answer should be in the form of one of the actions:
- Attack;
- to run;
- hide (for a surprise attack);
- nothing to do.
For learning we will make a table of "lessons":
Health | pistol | The enemies | Act |
---|
50% | one | one | Attack |
90% | one | 2 | Attack |
80% | 0 | one | Attack |
thirty% | one | one | Hide |
60% | one | 2 | Hide |
40% | 0 | one | Hide |
90% | one | 7 | To run |
60% | one | four | To run |
ten% | 0 | one | To run |
60% | one | 0 | Nothing |
100% | 0 | 0 | Nothing |
Training
The first thing to do is build and install
libfann .
Then download
fannj and
jna .
Make a file that will contain a set of "lessons":
11 3 4 0.5 1 1 1 0 0 0 0.9 1 2 1 0 0 0 0.8 0 1 1 0 0 0 0.3 1 1 0 1 0 0 0.6 1 2 0 1 0 0 0.4 0 1 0 1 0 0 0.9 1 7 0 0 1 0 0.5 1 4 0 0 1 0 0.1 0 1 0 0 1 0 0.6 1 0 0 0 0 1 1.0 0 0 0 0 0 1
Now we will train our ANN and save it to a file:
')
public static void main(String[] args) {
Explanation
Layer
ANN consists of layers of neurons. The first layer is the “receptor” neurons or input neurons. The last layer of neuron output. All the others are hidden layers. In our case, the first layer has 3 neurons:
health level (0.1-1.0);
the presence of weapons (1-there, 0-no);
number of enemies.
Fann
The object of the class Fann is the neural network, which is created on the basis of the layers created earlier.
Trainer
The class object trainer encapsulates the neural network learning algorithms transmitted during the creation of the trainer. After learning, do not forget to save it to a file.
Checking results
To test our training, we use the following code:
public static void main(String[] args) { Fann fann = new Fann("ann"); float[][] tests = { {1.0f, 0, 1}, {0.9f, 1, 3}, {0.3f, 0, 8}, {1, 1, 8}, {0.1f, 0, 0}, }; for (float[] test:tests){ System.out.println(getAction(fann.run(test))); } } private static String getAction(float[] out){ int i = 0; for (int j = 1; j < 4; j++) { if(out[i]<out[j]){ i = j; } } switch (i){ case 0:return ""; case 1:return ""; case 2:return ""; case 3:return " "; } return ""; }
I got the following results:
Health | pistol | The enemies | Act |
---|
100% | Not | one | Attack |
90% | there is | 3 | Hide |
thirty% | not | eight | To run |
100% | there is | eight | To run |
ten% | Not | 0 | Nothing to do |
I will be glad to hear constructive criticism.