diff --git a/core/src/com/mygdx/game/Log.java b/core/src/com/mygdx/game/Log.java index 35fbccc..5b1f62b 100644 --- a/core/src/com/mygdx/game/Log.java +++ b/core/src/com/mygdx/game/Log.java @@ -15,10 +15,10 @@ public class Log { public static final int INFO = 1; public static final int DEBUG = 2; - private static int logLevel = 1; + private static int logLevel = 0; public static void log(int level, String msg) { - if (level >= logLevel) { + if (level <= logLevel) { System.out.println(msg); } } diff --git a/core/src/logic/neural/Brain.java b/core/src/logic/neural/Brain.java index 172610b..7c20a85 100644 --- a/core/src/logic/neural/Brain.java +++ b/core/src/logic/neural/Brain.java @@ -54,6 +54,7 @@ public class Brain { } public float[] compute() { + clearCache(); float[] res = new float[neurons[neurons.length - 1].length]; for (int i = 0; i < neurons[neurons.length - 1].length; i++) { Neuron n = neurons[neurons.length - 1][i]; @@ -104,6 +105,16 @@ public class Brain { return res; } + private void clearCache() { + for (int i = 1; i < neurons.length; i++) { + for (int j = 0; j < neurons[i].length; j++) { + if (neurons[i][j] != null) { + neurons[i][j].clearCache(); + } + } + } + } + public Neuron[][] getNeurons() { return neurons; } diff --git a/core/src/logic/neural/Neuron.java b/core/src/logic/neural/Neuron.java index 3fc6a40..a7f5c80 100644 --- a/core/src/logic/neural/Neuron.java +++ b/core/src/logic/neural/Neuron.java @@ -6,8 +6,6 @@ package logic.neural; import com.mygdx.game.Log; -import java.util.ArrayList; -import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -18,6 +16,7 @@ import java.util.logging.Logger; public class Neuron { private float[] weights; + private NeuronCache cache; private float bias, output; private boolean isInputNeuron; private int layer; @@ -35,6 +34,7 @@ public class Neuron { } else { this.weights = weights; } + cache = new NeuronCache(this.weights.length); } private void scramble() { @@ -61,6 +61,14 @@ public class Neuron { for (int i = 0; i < weights.length; i++) { //if(brain == null) System.out.println("BRAINS NULL"); else if(brain.getNeurons() == null) System.out.println("NEURONS NULL"); //System.out.println(Arrays.toString(brain.getNeurons())); + if (cache.has(i)) { + try { + return cache.get(i); + } catch (Exception ex) { + // This should never happen + Logger.getLogger(Neuron.class.getName()).log(Level.SEVERE, null, ex); + } + } Neuron n = brain.getNeurons()[layer - 1][i]; a += n.compute() * weights[i]; } @@ -111,4 +119,8 @@ public class Neuron { this.weights = weights; } + public void clearCache() { + cache.clear(); + } + } diff --git a/core/src/logic/neural/NeuronCache.java b/core/src/logic/neural/NeuronCache.java new file mode 100644 index 0000000..456b195 --- /dev/null +++ b/core/src/logic/neural/NeuronCache.java @@ -0,0 +1,45 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package logic.neural; + +/** + * + * @author fazo + */ +public class NeuronCache { + + private float[] cache; + private boolean[] validity; + + public NeuronCache(int size) { + cache = new float[size]; + validity = new boolean[size]; + clear(); + } + + public void put(int index, float value) { + validity[index] = true; + cache[index] = value; + } + + public float get(int index) throws Exception { + if (validity[index]) { + return cache[index]; + } else { + throw new Exception("Value not present"); + } + } + + public boolean has(int index) { + return validity[index]; + } + + public void clear() { + for (int i = 0; i < cache.length; i++) { + validity[i] = false; + } + } +}