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:
parent
1cada40ab6
commit
5d9db86148
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
45
core/src/logic/neural/NeuronCache.java
Normal file
45
core/src/logic/neural/NeuronCache.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user