Black Rose
An unbreakable bond
Please indent your code.
you need a comment every few lines to explain what each part does.
Java:
/*
* This java program simulates two sheets of cortex as found in the brain.
* Input creates oscillatory effects in the two cortices because input is
* limited to one sheet. Neurons on the other sheet connect to other neurons
* that will have the same value as the input neuron on the adjacent sheet.
* The degree to which connections change is based on the error between the
* input neuron and the adjacent neuron summing its connections.
* All together a nonlocal hierarchy of oscillatory connection cycles are
* produced from neurons trying to find connections that brings each neurons
* corresponding neuron on the other sheet to an equilibrium.
* This current program uses random input so the hierarchy is not complex.
* With non random input a complex oscillatory hierarchy should emerge.
* In the brain the thalamus is the base oscillatory pacemaker.
* Cortical structures like the thalamus has not been added.
* Output neurons have not been designated in this program.
*/
package javaapplication1;
import java.util.Random;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
// Initiate core program
public class JavaApplication1 extends JComponent implements Runnable {
// Initiate variables
int ws = 100; //window_size = ws = x and y coordinates size
int synapses = 100; // number of connections per node
int input_num = 100; // indicates the number of nodes used for input
int[][] difference_node = new int[ws][ws]; // holds difference between sheets
int[][] Data1 = new int[ws][ws]; // temp variable for accumulated node values
int[][] buffer1 = new int[ws][ws]; // temp variable
int[][] buffer2 = new int[ws][ws]; // temp variable
int[][] Cortex1 = new int[ws][ws]; // holds values of sheet 1
int[][] Cortex2 = new int[ws][ws]; // holds values of sheet 1
int[][] memory1 = new int[ws*ws][synapses]; // x coordinate values of sheet 1
int[][] memory2 = new int[ws*ws][synapses]; // y coordinate values of sheet 1
int[][] memory3 = new int[ws*ws][synapses]; // x coordinate values of sheet 2
int[][] memory4 = new int[ws*ws][synapses]; // y coordinate values of sheet 2
int[] input_values1 = new int[input_num]; // x coordinates of input nodes
int[] input_values2 = new int[input_num]; // y coordinates of input nodes
// End initiate variables
// Initiate core function
public JavaApplication1() {
initialData();
new Thread(this).start();
}
// End function
// Initiate core thread
public void run() {
while (true) {
try {
changeData();
repaint();
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// End function
// Set values of all data structures to random.
private void initialData() {
Random r = new Random();
synchronized (Cortex1) { // lock paint
int accumulate = 0;
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
Cortex1[x][y] = r.nextInt(255);
Cortex2[x][y] = r.nextInt(255);
accumulate ++;
if (accumulate >= (ws*ws)-1){
accumulate = 0;
}
// Memory holds the neural connections.
for (int z = 0; z < synapses; z++) {
// memory1 holds the x coordinate values of sheet 1
memory1[accumulate][z] = r.nextInt(ws);
// memory2 holds the y coordinate values of sheet 1
memory2[accumulate][z] = r.nextInt(ws);
// memory3 holds the x coordinate values of sheet 2
memory3[accumulate][z] = r.nextInt(ws);
// memory4 holds the y coordinate values of sheet 2
memory4[accumulate][z] = r.nextInt(ws);
}
}
}
// The number of inputs is selected here.
for (int x = 0; x < input_num; x++) {
input_values1[x] = r.nextInt(ws); // The x values for input are selected.
input_values2[x] = r.nextInt(ws); // The y values for input are selected.
}
}
}
// End function
// This is the core function for using temp variable to change the sheet values.
private void changeData() {
synchronized (Cortex1) { // lock paint
Random r = new Random();
// This transfers accumulated node changes to Data1.
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
difference_node[x][y] = Math.abs(Cortex1[x][y] - Cortex2[x][y]);
Data1[x][y] += difference_node[x][y];
}
}
// Here is where input data can be added to the cortex.
for (int x = 0; x < input_num; x++) {
Cortex1[input_values1[x]][input_values2[x]] = r.nextInt(255);
}
// keeps temp buffer values empty.
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
buffer1[x][y] = 0;
buffer2[x][y] = 0;
}
}
int p1 = 0; // temp value
int p2 = 0; // temp value
int accumulate = 0; // temp value
// keeps memory from overflowing
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
p1 = 0;
p2 = 0;
accumulate++;
if (accumulate >= (ws*ws)-1){
accumulate = 0;
}
// adds up all memory values to one value per node.
for (int z = 0; z < synapses; z++) {
p1 += Cortex1[memory1[accumulate][z]][memory2[accumulate][z]];
p2 += Cortex2[memory3[accumulate][z]][memory4[accumulate][z]];
}
buffer1[x][y] = p1 / synapses; // sets bufffer by memory average sheet 1
buffer2[x][y] = p2 / synapses; // sets bufffer by memory average sheet 2
}
}
// data is transfered.
// sheets are set to new values
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
Cortex1[x][y] = buffer1[x][y];
Cortex2[x][y] = buffer2[x][y];
}
}
int pnum = 0;
// temp variable for finding corresponding difference between sheet nodes.
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
pnum += difference_node[x][y];
accumulate ++;
if (accumulate >= (ws*ws)-1){
accumulate = 0;
}
// This is where connection are randomized depending on the diffrence between
// cortex1 and cortex2.
for (int z = 0; z < Math.abs(difference_node[x][y]/2); z++) {
memory1[accumulate][r.nextInt(synapses)] = r.nextInt(ws);
memory2[accumulate][r.nextInt(synapses)] = r.nextInt(ws);
memory3[accumulate][r.nextInt(synapses)] = r.nextInt(ws);
memory4[accumulate][r.nextInt(synapses)] = r.nextInt(ws);
}
}
}
// prints to consol pnum
System.out.println("Average number of randomized connections " + pnum / (ws*ws));
// Keeps color in bounds
int k = r.nextInt(255);
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
if (Data1[x][y] >= 255 || Data1[x][y] <= 0) {
Data1[x][y] = 1;
}
}
}
// Keeps sheets in bounds
for (int x = 0; x < ws; x++) {
for (int y = 0; y < ws; y++) {
if (Cortex1[x][y] >= 255 || Cortex1[x][y] <= 0) {
Cortex1[x][y] = k;
}
if (Cortex2[x][y] >= 255 || Cortex2[x][y] <= 0) {
Cortex2[x][y] = k;
}
}
}
}
}
// End function
// Sets all data to a window
public void paint(Graphics g) {
synchronized (Cortex1) { // do not paint while recalculating
int resolution = 5; // Change the 5 to any number to change resolution.
g.fillRect(0 + 00, 0 + 00, ws*resolution, ws*resolution);
for (int x = 1; x < ws-1; x += 1) {
for (int y = 1; y < ws-1; y += 1) {
try {
g.setColor(new Color(0, Data1[x][y], 0));
g.fillRect(x*resolution + 00, y*resolution + 00, resolution, resolution);
} catch (IllegalArgumentException ex) {
System.out.println("Error value: " + Data1[x][y] + ", [x,y] = [" + x + "," + y +"]");
}
}
}
}
}
// End function
// Sets up main Java window
public static void main(String[] args) {
int ws = 500; //window_size = ws
JFrame f = new JFrame("Cortex Ripples");
f.add(new JavaApplication1());
f.setSize((ws+16), (ws+39));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
// End function
}
// End Program