• OK, it's on.
  • Please note that many, many Email Addresses used for spam, are not accepted at registration. Select a respectable Free email.
  • Done now. Domine miserere nobis.

Improved Java Program

Black Rose

An unbreakable bond
Local time
Today 5:21 AM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
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
 

dr froyd

__________________________________________________
Local time
Today 12:21 PM
Joined
Jan 26, 2015
Messages
1,485
---
yo AK.. i actually tried to look at this code a while back

i think there's bugs in it. I don't remember exactly but i think at some places you used random draws of integers and used them as array indices that clearly mismatched the dimensions of the arrays

but that's an issue which is consequence of a bigger issue; when you have an endless shitshow of nested loops and they all effectively fill data into matrices, you should modularize things more, i.e. packaging things into functions. For example, there's many places where you do stuff like this:

Code:
for (int x = 0; x < ws; x++) {
    for (int y = 0; y < ws; y++) {
        // matrix operations
    }
}

that looks like a matrix operation that is done repeatedly throughout the code and should be a function. This would make it easier to understand what is going on
 

Black Rose

An unbreakable bond
Local time
Today 5:21 AM
Joined
Apr 4, 2010
Messages
11,431
---
Location
with mama
yo AK.. i actually tried to look at this code a while back

i think there's bugs in it. I don't remember exactly but i think at some places you used random draws of integers and used them as array indices that clearly mismatched the dimensions of the arrays

yes, I did, so I fixed it with the operator "accumulate".

but that's an issue which is consequence of a bigger issue; when you have an endless shitshow of nested loops and they all effectively fill data into matrices, you should modularize things more, i.e. packaging things into functions. For example, there's many places where you do stuff like this:

Code:
for (int x = 0; x < ws; x++) {
    for (int y = 0; y < ws; y++) {
        // matrix operations
    }
}

that looks like a matrix operation that is done repeatedly throughout the code and should be a function. This would make it easier to understand what is going on

In computer programming bugs are when data overwrites data.

I never learned about matrices in Java I only learned about how nesting worked so in my code there are exactly zero errors of data overwriting data.

It happens to be that it took me 13 years to do this one program because I am not a programmer. I understand that GPU do such things as tensors but I will never be able to do that on my PC because it is 10 years old. All the Tensor math programs work on CUDA or such and such languages which my PC cannot do. So parallelism is not going to work well.

I plan on buying a new PC soon and I was just wondering what people would think of my old program code. I used NetBeans IDE 8.1

This is my plan for my new computer:

Metatron

Grace Hopper chipset;
1 petaflops
1 terabyte on-chip RAM

SSD hard drive.

Wall mounted: 4K monitor, 250Hz,
touch screen and wireless mouse/keyboard.

Virtual Reality set

Brainwaves band

Built-in Broadband satellite internet modem.

All wireless.

Verizon $30 a month.

Zero moving parts except for the fan.

Windows 12 operating system.

Home security cameras.

face recognition

cellphone (google pixel)

-

earphones

video chat

google chrome

safty mode

transfer files

connect to smartphone

acount settings

battery backup
 

scorpiomover

The little professor
Local time
Today 12:21 PM
Joined
May 3, 2011
Messages
3,383
---
@Black Rose: Much better. :like:

@dr froyd was able to follow it well enough to give you some feedback on it.
 
Top Bottom