-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added brain stuff (probably all garbage)
- Loading branch information
1 parent
febc60a
commit f1ad08e
Showing
11 changed files
with
483 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/com/marginallyclever/ro3/node/nodes/odenode/brain/Neuron.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.marginallyclever.ro3.node.nodes.odenode.brain; | ||
|
||
/** | ||
* A neuron in a brain. | ||
* The neuron does not know how many inputs it has. | ||
* The neuron knows how many outputs it has. | ||
* An output might go to nothing. | ||
* An output might go to a synapse. | ||
* The synapse have bindings to "good" and "bad" feelings. | ||
*/ | ||
public class Neuron { | ||
|
||
} |
139 changes: 139 additions & 0 deletions
139
src/main/java/com/marginallyclever/ro3/node/nodes/odenode/brain/v2/Brain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.marginallyclever.ro3.node.nodes.odenode.brain.v2; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Brain { | ||
private List<Neuron> inputNeurons = new ArrayList<>(); | ||
private List<Neuron> outputNeurons = new ArrayList<>(); | ||
private List<Neuron> hiddenNeurons = new ArrayList<>(); | ||
private List<Connection> connections = new ArrayList<>(); | ||
private final DopamineSimulator dopamineSimulator; | ||
private final CortisolSimulator cortisolSimulator; | ||
|
||
public Brain(int numInputs, int numOutputs, DopamineSimulator dopamineSimulator, CortisolSimulator cortisolSimulator) { | ||
super(); | ||
this.dopamineSimulator = dopamineSimulator; | ||
this.cortisolSimulator = cortisolSimulator; | ||
|
||
// Initialize input neurons on the x=0 plane | ||
for (int i = 0; i < numInputs; i++) { | ||
inputNeurons.add(new Neuron(0, i, 0)); | ||
} | ||
|
||
// Initialize output neurons on the y=0 plane | ||
for (int i = 0; i < numOutputs; i++) { | ||
outputNeurons.add(new Neuron(i, 0, 0)); | ||
} | ||
|
||
// Create initial connections between input and output neurons | ||
for (Neuron inputNeuron : inputNeurons) { | ||
for (Neuron outputNeuron : outputNeurons) { | ||
Connection connection = new Connection(inputNeuron, outputNeuron, Math.random() * 0.2 - 0.1); | ||
addConnection(connection); | ||
} | ||
} | ||
} | ||
|
||
public void setInputs(List<Double> inputs) { | ||
if(inputs.size() != inputNeurons.size()) { | ||
throw new IllegalArgumentException("Number of inputs must match the number of input neurons"); | ||
} | ||
// Feed the input through the network | ||
for (int i = 0; i < inputs.size(); i++) { | ||
inputNeurons.get(i).setInputValue(inputs.get(i)); | ||
} | ||
} | ||
|
||
/** | ||
* Train the network with a set of inputs and expected outputs | ||
* @param inputs List of input values | ||
* @param expectedOutputs List of expected output values | ||
*/ | ||
public void train(List<Double> inputs, List<Double> expectedOutputs) { | ||
setInputs(inputs); | ||
|
||
propagate(); | ||
|
||
if(expectedOutputs.size() != outputNeurons.size()) { | ||
throw new IllegalArgumentException("Number of expected outputs must match the number of output neurons"); | ||
} | ||
double[] output = getOutputs(); | ||
for (int i = 0; i < expectedOutputs.size(); i++) { | ||
double d = outputNeurons.get(i).getOutputValue(); | ||
double error = expectedOutputs.get(i) - d; | ||
|
||
// what happens here? | ||
} | ||
} | ||
|
||
private void propagate() { | ||
// Activate input neurons | ||
for (Neuron neuron : inputNeurons) { | ||
neuron.activate(); | ||
} | ||
// Activate hidden and output neurons | ||
for (Neuron neuron : hiddenNeurons) { | ||
neuron.activate(); | ||
} | ||
for (Neuron neuron : outputNeurons) { | ||
neuron.activate(); | ||
} | ||
} | ||
|
||
public double[] getOutputs() { | ||
// Evaluate the network's output based on the input | ||
double[] output = new double[outputNeurons.size()]; | ||
for (int i = 0; i < outputNeurons.size(); i++) { | ||
output[i] = outputNeurons.get(i).getOutputValue(); | ||
} | ||
return output; | ||
} | ||
|
||
public List<Connection> findActiveConnections() { | ||
List<Connection> activeConnections = new ArrayList<>(); | ||
for (Connection connection : connections) { | ||
if (connection.isActive()) { | ||
activeConnections.add(connection); | ||
} | ||
} | ||
return activeConnections; | ||
} | ||
|
||
public void resetNetwork() { | ||
for (Neuron neuron : inputNeurons) { | ||
neuron.reset(); | ||
} | ||
for (Neuron neuron : hiddenNeurons) { | ||
neuron.reset(); | ||
} | ||
for (Neuron neuron : outputNeurons) { | ||
neuron.reset(); | ||
} | ||
for (Connection connection : connections) { | ||
connection.reset(); | ||
} | ||
} | ||
|
||
public void addNeuron(Neuron neuron) { | ||
hiddenNeurons.add(neuron); | ||
} | ||
|
||
public void addConnection(Connection connection) { | ||
connections.add(connection); | ||
connection.getFromNeuron().addOutgoingConnection(connection); | ||
} | ||
|
||
public List<Neuron> getInputNeurons() { | ||
return inputNeurons; | ||
} | ||
|
||
public List<Neuron> getOutputNeurons() { | ||
return outputNeurons; | ||
} | ||
|
||
public List<Connection> getConnections() { | ||
return connections; | ||
} | ||
} | ||
|
57 changes: 57 additions & 0 deletions
57
src/main/java/com/marginallyclever/ro3/node/nodes/odenode/brain/v2/Connection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.marginallyclever.ro3.node.nodes.odenode.brain.v2; | ||
|
||
/** | ||
* Positive weights will act as excitatory connections. | ||
* Negative weights will act as inhibitory connections. | ||
*/ | ||
public class Connection { | ||
private Neuron fromNeuron; | ||
private Neuron toNeuron; | ||
private double weight; | ||
private boolean active; | ||
|
||
public Connection(Neuron fromNeuron, Neuron toNeuron, double weight) { | ||
this.fromNeuron = fromNeuron; | ||
this.toNeuron = toNeuron; | ||
this.weight = weight; | ||
this.active = false; | ||
} | ||
|
||
public void propagate() { | ||
double propagatedValue = fromNeuron.getOutputValue() * weight; | ||
toNeuron.addInputValue(propagatedValue); | ||
active = true; | ||
} | ||
|
||
public void reset() { | ||
active = false; | ||
} | ||
|
||
public boolean isActive() { | ||
return active; | ||
} | ||
|
||
public Neuron getFromNeuron() { | ||
return fromNeuron; | ||
} | ||
|
||
public Neuron getToNeuron() { | ||
return toNeuron; | ||
} | ||
|
||
public void addWeight(double amount) { | ||
// Example: Increase the weight by a small factor | ||
weight += amount; | ||
//weight = Math.max(-1.0, Math.min(1.0, weight)); | ||
} | ||
|
||
public void scaleWeight(double scale) { | ||
// Example: Decrease the weight by a small factor | ||
weight *= scale; | ||
//weight = Math.max(-1.0, Math.min(1.0, weight)); | ||
} | ||
|
||
public double getWeight() { | ||
return weight; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/marginallyclever/ro3/node/nodes/odenode/brain/v2/CortisolSimulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.marginallyclever.ro3.node.nodes.odenode.brain.v2; | ||
|
||
import java.util.List; | ||
|
||
public class CortisolSimulator { | ||
double changeAmount = 1.0 - .01; | ||
public void releaseCortisol(List<Connection> activeConnections) { | ||
for (Connection connection : activeConnections) { | ||
// Weaken or prune the connection | ||
connection.scaleWeight(changeAmount); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/marginallyclever/ro3/node/nodes/odenode/brain/v2/DopamineSimulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.marginallyclever.ro3.node.nodes.odenode.brain.v2; | ||
|
||
import java.util.List; | ||
|
||
public class DopamineSimulator { | ||
double changeAmount = 1.0 + .01; | ||
|
||
public void releaseDopamine(List<Connection> activeConnections) { | ||
for (Connection connection : activeConnections) { | ||
// Strengthen the connection or add neurons along the pathway | ||
connection.scaleWeight(changeAmount); | ||
} | ||
// Optionally, add new neurons to reinforce successful pathways | ||
} | ||
} |
Oops, something went wrong.