mirror of
https://github.com/fazo96/AIrium.git
synced 2025-03-31 22:38:38 +02:00
removed shitty workarounds
This commit is contained in:
parent
4195c08745
commit
a78f27627f
@ -11,22 +11,22 @@ public class Brain {
|
|||||||
|
|
||||||
public static final float bias = 0.5f;
|
public static final float bias = 0.5f;
|
||||||
private Neuron[][] neurons;
|
private Neuron[][] neurons;
|
||||||
private int nInputs;
|
|
||||||
|
|
||||||
public Brain(int nInputs, int nOutputs, int hiddenLayers, int neuronsPerHiddenLayer) {
|
public Brain(int nInputs, int nOutputs, int hiddenLayers, int neuronsPerHiddenLayer) {
|
||||||
this.nInputs = nInputs;
|
neurons = new Neuron[hiddenLayers + 2][];
|
||||||
neurons = new Neuron[hiddenLayers + 2][Math.max(nInputs, Math.max(nOutputs, neuronsPerHiddenLayer))];
|
|
||||||
populate(nInputs, nOutputs, hiddenLayers, neuronsPerHiddenLayer);
|
populate(nInputs, nOutputs, hiddenLayers, neuronsPerHiddenLayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populate(int nInputs, int nOutputs, int hiddenLayers, int neuronsPerHiddenLayer) {
|
private void populate(int nInputs, int nOutputs, int hiddenLayers, int neuronsPerHiddenLayer) {
|
||||||
// Create input neurons
|
// Create input neurons
|
||||||
|
neurons[0] = new Neuron[nInputs];
|
||||||
for (int i = 0; i < nInputs; i++) {
|
for (int i = 0; i < nInputs; i++) {
|
||||||
neurons[0][i] = new Neuron(0, bias, this);
|
neurons[0][i] = new Neuron(0, bias, this);
|
||||||
Log.log(Log.DEBUG, "Adding Input Layer Neuron " + (i + 1));
|
Log.log(Log.DEBUG, "Adding Input Layer Neuron " + (i + 1));
|
||||||
}
|
}
|
||||||
// popiulate hidden layers
|
// popiulate hidden layers
|
||||||
for (int i = 0; i < hiddenLayers; i++) {
|
for (int i = 0; i < hiddenLayers; i++) {
|
||||||
|
neurons[i + 1] = new Neuron[neuronsPerHiddenLayer];
|
||||||
for (int j = 0; j < neuronsPerHiddenLayer; j++) {
|
for (int j = 0; j < neuronsPerHiddenLayer; j++) {
|
||||||
// create neuron
|
// create neuron
|
||||||
Neuron n = new Neuron(i + 1, bias, this);
|
Neuron n = new Neuron(i + 1, bias, this);
|
||||||
@ -35,6 +35,7 @@ public class Brain {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// populate output layer
|
// populate output layer
|
||||||
|
neurons[hiddenLayers + 1] = new Neuron[nOutputs];
|
||||||
for (int i = 0; i < nOutputs; i++) {
|
for (int i = 0; i < nOutputs; i++) {
|
||||||
// add neuron
|
// add neuron
|
||||||
Neuron n = new Neuron(hiddenLayers + 1, bias, this);
|
Neuron n = new Neuron(hiddenLayers + 1, bias, this);
|
||||||
@ -100,30 +101,21 @@ public class Brain {
|
|||||||
res[i] = new float[neurons[i].length][];
|
res[i] = new float[neurons[i].length][];
|
||||||
for (int j = 0; j < neurons[i].length; j++) // neurons per layer
|
for (int j = 0; j < neurons[i].length; j++) // neurons per layer
|
||||||
{
|
{
|
||||||
if (neurons[i][j] != null) {
|
|
||||||
res[i][j] = neurons[i][j].mutate(mutationFactor);
|
res[i][j] = neurons[i][j].mutate(mutationFactor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearCache() {
|
private void clearCache() {
|
||||||
for (int i = 1; i < neurons.length; i++) {
|
for (int i = 1; i < neurons.length; i++) {
|
||||||
for (int j = 0; j < neurons[i].length; j++) {
|
for (int j = 0; j < neurons[i].length; j++) {
|
||||||
if (neurons[i][j] != null) {
|
|
||||||
neurons[i][j].clearCache();
|
neurons[i][j].clearCache();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public Neuron[][] getNeurons() {
|
public Neuron[][] getNeurons() {
|
||||||
return neurons;
|
return neurons;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int howManyInputNeurons() {
|
|
||||||
return nInputs;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -39,10 +39,8 @@ public class Neuron {
|
|||||||
|
|
||||||
private void scramble() {
|
private void scramble() {
|
||||||
// init weights
|
// init weights
|
||||||
if (layer > 1) {
|
if (layer > 0) {
|
||||||
weights = new float[brain.getNeurons()[layer - 1].length];
|
weights = new float[brain.getNeurons()[layer - 1].length];
|
||||||
} else if (layer == 1) {
|
|
||||||
weights = new float[brain.howManyInputNeurons()];
|
|
||||||
} else { // layer 0
|
} else { // layer 0
|
||||||
isInputNeuron = true;
|
isInputNeuron = true;
|
||||||
weights = new float[0];
|
weights = new float[0];
|
||||||
|
Loading…
Reference in New Issue
Block a user