1
0
mirror of https://github.com/fazo96/AIrium.git synced 2025-01-10 09:34:20 +01:00

optimization is back in

This commit is contained in:
Enrico Fasoli 2015-07-01 17:24:06 +02:00
parent 1cada40ab6
commit 5d9db86148
4 changed files with 72 additions and 4 deletions

View File

@ -15,10 +15,10 @@ public class Log {
public static final int INFO = 1; public static final int INFO = 1;
public static final int DEBUG = 2; public static final int DEBUG = 2;
private static int logLevel = 1; private static int logLevel = 0;
public static void log(int level, String msg) { public static void log(int level, String msg) {
if (level >= logLevel) { if (level <= logLevel) {
System.out.println(msg); System.out.println(msg);
} }
} }

View File

@ -54,6 +54,7 @@ public class Brain {
} }
public float[] compute() { public float[] compute() {
clearCache();
float[] res = new float[neurons[neurons.length - 1].length]; float[] res = new float[neurons[neurons.length - 1].length];
for (int i = 0; i < neurons[neurons.length - 1].length; i++) { for (int i = 0; i < neurons[neurons.length - 1].length; i++) {
Neuron n = neurons[neurons.length - 1][i]; Neuron n = neurons[neurons.length - 1][i];
@ -104,6 +105,16 @@ public class Brain {
return res; 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() { public Neuron[][] getNeurons() {
return neurons; return neurons;
} }

View File

@ -6,8 +6,6 @@
package logic.neural; package logic.neural;
import com.mygdx.game.Log; import com.mygdx.game.Log;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -18,6 +16,7 @@ import java.util.logging.Logger;
public class Neuron { public class Neuron {
private float[] weights; private float[] weights;
private NeuronCache cache;
private float bias, output; private float bias, output;
private boolean isInputNeuron; private boolean isInputNeuron;
private int layer; private int layer;
@ -35,6 +34,7 @@ public class Neuron {
} else { } else {
this.weights = weights; this.weights = weights;
} }
cache = new NeuronCache(this.weights.length);
} }
private void scramble() { private void scramble() {
@ -61,6 +61,14 @@ public class Neuron {
for (int i = 0; i < weights.length; i++) { 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"); //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())); //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]; Neuron n = brain.getNeurons()[layer - 1][i];
a += n.compute() * weights[i]; a += n.compute() * weights[i];
} }
@ -111,4 +119,8 @@ public class Neuron {
this.weights = weights; this.weights = weights;
} }
public void clearCache() {
cache.clear();
}
} }

View File

@ -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;
}
}
}