This is essentially a simulated brain that factors the time it takes for the electric current to travel from neuron to neuron. This allows the brain to become far more advanced.
import java.util.Scanner; import java.util.Random; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Timer; import java.util.TimerTask; import java.awt.event.*; import java.awt.*; import javax.swing.BorderFactory; import javax.swing.border.Border; import javax.swing.border.TitledBorder; import javax.swing.border.EtchedBorder; import javax.swing.*; import java.awt.event.MouseEvent; public class ReccurentNeuralNet extends JPanel{ Timer timer; int speed = 2; String symbols = "abcdefghijklmnopqrstuvwxyz;:',!?.()\"123456789 "; int screenCordX = 0; int screenCordY = 0; public JScrollPane scroller; public JScrollPane scroller2; public JTextArea display; public JTextField inputLine; public static Circle[] foodArray; public static Circle base; int xTransform = 0; int yTransform = 0; public static int rNum = 1; public static int cNum = 1000; public static int inNum = 46; public static int outNum = 46; public Brain first; public CompBrain comp; public static int keepBrainNum = 5; public static int foodNum = 20; public static int brainNum = 20; public static double mutationPercent = .10; static Brain[] brains; static Neuron[] inputs; int count = 0; int maxCount = 10000; int genNum = 1; public void paintComponent(Graphics g){ super.paintComponent(g); } public Brain[] shiftArray(int num, Brain[] brainArray){ Brain[] tempBrain = new Brain[brainArray.length]; tempBrain = (Brain[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ if(i == num){ brainArray[i] = null; } else{ brainArray[i] = tempBrain[i - 1]; } } return brainArray; } public static void main(String[] args){ brains = new Brain[brainNum]; ReccurentNeuralNet content = new ReccurentNeuralNet(); JFrame window = new JFrame(); window.setContentPane(content); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLocation(120,70); window.setSize(640,480); window.setVisible(true); } class Task extends TimerTask { public void run(){ /* count++; String[] inputArray = new String[inNum]; for(int i = 0; i < brainNum; i++){ brains[i].calculateWeights(inputArray); brains[i].calculateMovement(); } repaint(); */ } } public class mouseListener implements MouseMotionListener, MouseListener { boolean mouseHeld = false; int x1, x2, xDistance; int y1, y2, yDistance; public void mouseClicked(MouseEvent e){} public void mouseDragged(MouseEvent e){ if(mouseHeld == true){ x2 = e.getX(); y2 = e.getY(); xDistance = x1 - x2; yDistance = y1 - y2; xTransform += -xDistance; yTransform += -yDistance; x1 = x2; y1 = y2; } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseMoved(MouseEvent e){} public void mousePressed(MouseEvent e){ mouseHeld = true; x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e){ mouseHeld = false; } public void mouseWheel(MouseEvent e){} } public class textListener implements ActionListener{ public void actionPerformed(ActionEvent e){ display.setText(display.getText() + inputLine.getText() + "\n"); String[] inputs = new String[inputLine.getText().length()]; for(int i = 0; i < inputLine.getText().length(); i++){ inputs[i] = "" + inputLine.getText().charAt(i); } inputLine.setText(""); comp.calculateWeights(inputs); //first.setInputs(inputs); System.out.println("Generating a reply..."); String reply = comp.getBrain().calculateWeights(inputs); display.setText(display.getText() + reply + "\n"); } } public ReccurentNeuralNet(){ //timer = new Timer(); //timer.schedule(new Task(), 0, speed); //int rows, int columnNumber, boolean first, String[][] weight, int bRadius, int outputNum, int inputNum String[][] emptyWeights = new String[rNum * cNum][rNum * cNum + outNum]; first = new Brain(rNum, cNum, false, emptyWeights, 10, outNum, inNum); comp = new CompBrain(rNum, cNum, rNum, cNum, outNum, inNum, true, emptyWeights, inNum, first); Border textBorder = BorderFactory.createEtchedBorder(); display = new JTextArea(25, 55); display.setBorder(textBorder); display.setEditable(false); scroller = new JScrollPane( display ); this.add(scroller); inputLine = new JTextField("", 55); inputLine.setBorder(textBorder); inputLine.addActionListener(new textListener()); scroller2 = new JScrollPane( inputLine ); //scroller2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); this.add(scroller2); mouseListener listener = new mouseListener(); addMouseListener(listener); addMouseMotionListener(listener); } public Brain breedBrains(Brain brain1, Brain brain2){ Brain newBrain = new Brain(rNum, cNum, false, new String[0][0], 0, outNum, inNum); for(int i = 0; i < inNum; i++){ for(int u = 0; u < rNum * cNum; u++){ if(brain1.getInputWeights(i, u).equals("null") || brain2.getInputWeights(i, u).equals("null")){ if(brain1.getInputWeights(i, u).equals("null")){ } else{ double randomNum = Math.random(); if(randomNum < .5){ newBrain.setInputWeights(i, u, brain1.getInputWeights(i, u)); } } if(brain2.getInputWeights(i, u).equals("null")){ } else{ double randomNum = Math.random(); if(randomNum < .5){ newBrain.setInputWeights(i, u, brain2.getInputWeights(i, u)); } } } else{ double random = Math.random(); if(random < .5){ newBrain.setInputWeights(i, u, brain1.getInputWeights(i, u)); } else{ newBrain.setInputWeights(i, u, brain2.getInputWeights(i, u)); } } } } for(int i = 0; i < rNum * cNum; i++){ for(int u = 0; u < rNum * cNum + outNum; u++){ if(brain1.getWeights(i, u).equals("null") || brain2.getWeights(i, u).equals("null")){ if(brain1.getWeights(i, u).equals("null")){ } else{ double randomNum = Math.random(); if(randomNum < .5){ newBrain.setWeights(i, u, brain1.getWeights(i, u)); } } if(brain2.getWeights(i, u).equals("null")){ } else{ double randomNum = Math.random(); if(randomNum < .5){ newBrain.setWeights(i, u, brain2.getWeights(i, u)); } } } else{ double random = Math.random(); if(random < .5){ newBrain.setWeights(i, u, brain1.getWeights(i, u)); } else{ newBrain.setWeights(i, u, brain2.getWeights(i, u)); } } } } return newBrain; } public void createNextGen(){ Brain[] topBrains = new Brain[brainNum]; for(int i = 0; i < brainNum; i++){ for(int u = 0; u < brainNum; u++){ if(i == 0){ topBrains[i] = brains[i]; break; } if(topBrains[Math.abs(u - brainNum) - 1] != null){ if(brains[i].getScore() < topBrains[Math.abs(u - brainNum) - 1].getScore()){ int newPlace = Math.abs(u - brainNum); System.out.println("Array shifted"); topBrains = (Brain[]) shiftArray(newPlace, topBrains).clone(); topBrains[Math.abs(u - brainNum)] = brains[i]; break; } else{ if(u == brainNum - 1){ topBrains = (Brain[]) shiftArray(0, topBrains).clone(); topBrains[0] = brains[i]; } } } } } double totalScore = 0; for(int i = 0; i < brainNum; i++){ totalScore += brains[i].getScore(); } for(int i = 0; i < brainNum; i++){ brains[i].setPercent((brains[i].getScore() / totalScore) * 100); } int lowestNum = topBrains[brainNum - 1].getScore(); for(int i = 0; i < brainNum; i++){ topBrains[i].setAddedScore(Math.abs(lowestNum)); } int comScore = 0; for(int i = 0; i < brainNum; i++){ comScore += brains[i].getPercent(); brains[i].setPercent(brains[i].getPercent() + comScore); } boolean done = false; boolean done1 = false; Brain chosenBrain1 = null; Brain chosenBrain2 = null; Brain[] newBrainList = new Brain[brainNum]; for(int i = 0; i < keepBrainNum; i++){ newBrainList[i] = topBrains[i]; System.out.println((i + 1) + ": " + topBrains[i].getScore()); } for(int z = keepBrainNum; z < brainNum; z++){ done1 = false; done = false; while(done1 == false){ double random = Math.random() * 100; for(int i = 0; i < brainNum; i++){ if(i == 0){ if(brains[i].getPercent() > random){ chosenBrain1 = brains[i]; System.out.println("Brain 1: " + i); System.out.println("Score: " + chosenBrain1.getScore()); done1 = true; break; } } else if(brains[i].getPercent() >= random && brains[i - 1].getPercent() < random){ chosenBrain1 = brains[i]; System.out.println("Brain 1: " + i); System.out.println("Score: " + chosenBrain1.getScore()); done1 = true; break; } } } while(done == false){ double random1 = Math.random() * 100; for(int i = 0; i < brainNum; i++){ if(i == 0){ if(brains[i].getPercent() > random1){ if(chosenBrain1.equals(brains[i])){ } else{ chosenBrain2 = brains[i]; System.out.println("Brain 2: " + i); System.out.println("Score: " + chosenBrain2.getScore()); done = true; break; } } } else if(brains[i].getPercent() >= random1 && brains[i - 1].getPercent() < random1){ if(chosenBrain1.equals(brains[i])){ } else{ chosenBrain2 = brains[i]; System.out.println("Brain 2: " + i); System.out.println("Score: " + chosenBrain2.getScore()); done = true; break; } } } } newBrainList[z] = breedBrains(chosenBrain1, chosenBrain2); } for(int i = 0; i < brainNum; i++){ brains[i] = newBrainList[i]; } } } class Brain{ public int radius; double percent = 0; String symbols = "abcdefghijklmnopqrstuvwxyz;:',!?.()\"123456789 "; private int maxConnectionNum = 7; private int maxDelay = 20; private int divFunction = 8; private int divNeuron = 8; private int divPower = 8; private int divRotation = 2; Neuron[] activatedNeurons; int activatedCount = 0; private String[][] weights; private String[] connections1; private String[] connections2; private String[][] inputWeights; private String[] inputs1; private String[] inputs2; private Neuron[] neurons; private String[] neuronFunction; private String[] neuronPower; private Neuron[] inputs; private String[] inputString; double dx = Math.random() * 638 + 2; double dy = Math.random() * 478 + 2; int score = 0; int divideNum = 1; int speedReduce = 1; int foodCount; int rowNum; int colunmNum; double moveOut, rotationLeftOut, rotationRightOut; double[] outPuts; int outNum; int connectionNum; int inputConnectionNum; int x, y; double rotation = 0; public Brain(int rows, int columnNumber, boolean first, String[][] weight, int bRadius, int outputNum, int inputNum){ inputs = new Neuron[inputNum]; weights = new String[columnNumber * rows][columnNumber * rows + outputNum + 2]; inputWeights = new String[inputNum][columnNumber * rows + outputNum + 2]; outNum = outputNum; radius = bRadius; System.arraycopy(weight, 0, weights, 0, weight.length); rowNum = rows; colunmNum = columnNumber; neurons = new Neuron[rowNum * colunmNum + outputNum + 2]; neuronFunction = new String[neurons.length + ReccurentNeuralNet.outNum]; neuronPower = new String[neuronFunction.length]; outPuts = new double[outNum]; activatedNeurons = new Neuron[colunmNum * rowNum]; activatedCount = 0; if(first == true){ generateWeights(); } else{ for(int i = 0; i < rowNum * colunmNum; i++){ neurons[i] = new Neuron(i, maxConnectionNum); neurons[i].setDelay(Math.round((float)(5 + Math.random() * maxDelay))); } for(int i = 0; i < outNum + 2; i++){ neurons[i + rowNum * colunmNum] = new Neuron(i, maxConnectionNum); } System.out.println(neurons.length); for(int i = 0; i < inputs.length; i++){ inputs[i] = new Neuron(i, maxConnectionNum); } } } private void setInputs(String[] inputStringArray){ inputString = (String[]) inputStringArray.clone(); } private void generateWeights(){ connectionNum = 1000 + Math.round((float)(Math.random() * (maxConnectionNum * colunmNum * rowNum) / 1.5)); inputConnectionNum = 5 + Math.round((float)(Math.random() * (inputs.length * (maxConnectionNum - 5)))); int usedConnections = 0; for(int i = 0; i < rowNum * colunmNum; i++){ neurons[i] = new Neuron(i, maxConnectionNum); neurons[i].setDelay(Math.round((float)(5 + Math.random() * maxDelay))); } for(int i = 0; i < outNum + 2; i++){ neurons[i + rowNum * colunmNum] = new Neuron(i, 0); } System.out.println(neurons.length); for(int i = 0; i < inputs.length; i++){ inputs[i] = new Neuron(i, maxConnectionNum); } for(int i = 0; i < inputConnectionNum; i++){ boolean goOn = false; while(goOn == false){ int inputNeuron = Math.round((float)(Math.random() * (inputs.length - 1))); int neuron = Math.round((float)(Math.random() * (colunmNum * rowNum - 1))); double random = (double)(Math.random() * 16); double negativeRan = Math.random(); if(negativeRan < 0.5){ random = random * -1; } if(inputs[inputNeuron].getConnectionCount() < maxConnectionNum){ inputWeights[inputNeuron][neuron] = toBinary(random); inputs[inputNeuron].setConnections(neuron); goOn = true; } } } for(int i = 0; i < rowNum; i++){ for(int u = 0; u < connectionNum; u++){ boolean goOn = false; while(goOn == false){ int neuron1 = Math.round((float)(Math.random() * (rowNum * colunmNum - 1))); int neuron2 = Math.round((float)(Math.random() * (rowNum * colunmNum + this.outNum - 1 + 2))); double random = (double)(Math.random() * 16); double negativeRan = Math.random(); if(negativeRan < 0.5){ random = random * -1; } if(weights[neuron1][neuron2] == null && neurons[neuron1].getConnectionCount() < maxConnectionNum){ weights[neuron1][neuron2] = toBinary(random); neurons[neuron1].setConnections(neuron2); goOn = true; } } } } for(int i = 0; i < rowNum * colunmNum; i++){ neurons[i].shrinkArray(); } for(int i = 0; i < neuronFunction.length; i++){ double random = (double)(Math.random() * 16); double negativeRan = Math.random(); if(negativeRan < 0.5){ random = random * -1; } neuronFunction[i] = toBinary(random); } for(int i = 0; i < neuronFunction.length; i++){ double random = Math.random() * 16; neuronPower[i] = toBinary(random); } } public static String toBinary(double num){ double dNum = Math.abs(num); String sNum = "" + dNum; String bNum = ""; char ch = sNum.charAt(1); int nu = Character.getNumericValue(ch); if(nu == -1){ for(int i = 0; i < sNum.length(); i++){ char c = sNum.charAt(i); int iNum = Character.getNumericValue(c); for(int u = 0; u < 4; u++){ if( iNum == -1){ String binaryDec = "."; bNum = bNum + binaryDec; break; } else{ int binaryNum = iNum % 2; if(binaryNum == 1){ iNum = (iNum - 1) / 2; } else{ iNum = iNum / 2; } bNum = bNum + binaryNum; } } } } else{ int iNum = 0; for(int i = 0; i < sNum.length() - 1; i++){ if(i == 0){ char c1 = sNum.charAt(0); char c2 = sNum.charAt(1); String twoNum = "" + c1 + c2; iNum = Integer.parseInt(twoNum); } char c = sNum.charAt(i + 1); if(i != 0){ iNum = Character.getNumericValue(c); } for(int u = 0; u < 4; u++){ if( iNum == -1){ String binaryDec = "."; bNum = bNum + binaryDec; break; } else{ int binaryNum = iNum % 2; if(binaryNum == 1){ iNum = (iNum - 1) / 2; } else{ iNum = iNum / 2; } bNum = bNum + binaryNum; } } } } if(num < 0){ bNum = "-" + bNum; } else{ bNum = "+" + bNum; } return bNum; } public static double toDecimal(String bNum){ int convertedNum = 0; double finalNum = 0; String numString = ""; for(int u = 1; u < (bNum.length()) / 4; u++){ char c; convertedNum = 0; for(int i = 0; i < 4; i++){ if(u != 0){ c = bNum.charAt(1 + i + u * 4); } else{ c = bNum.charAt(i + u * 4); } int num = Character.getNumericValue(c); if(num == 1){ convertedNum = convertedNum + Math.round((float)Math.pow(2, Math.abs(i))); } } if(u == 1){ numString = numString + convertedNum + "."; } else{ numString = numString + convertedNum; } } //System.out.println(numString); finalNum = Double.parseDouble(numString); char nChar = bNum.charAt(0); if(nChar == '-'){ finalNum = finalNum * -1; } return finalNum; } public double getPercent(){ return percent; } public void setPercent(double newPercent){ this.percent = newPercent; } public int getScore(){ return score; } public String getWeights(int first, int second){ return weights[first][second]; } public void setWeights(int first, int second, String num){ weights[first][second] = num; } public void setInputWeights(int first, int second, String num){ inputWeights[first][second] = num; } public String getNeuronFunction(int num){ return neuronFunction[num]; } public void setNeuronFunction(int num, String s){ neuronFunction[num] = s; } public String getInputWeights(int first, int second){ if(inputWeights[first][second] == null){ return "null"; } else{ return inputWeights[first][second]; } } public void setAddedScore(int newScore){ this.score += newScore; } public void setFoodCount(int newFoodCount){ this.foodCount = newFoodCount; } public String getPower(int num){ return neuronPower[num]; } public void setPower(int num, String set){ neuronPower[num] = set; } public void setNeuronConnections(int neuronNum, int connectedNum, double value1, double value2){ boolean connected = false; for(int i = 0; i < neurons[neuronNum].getConnectionCount(); i++){ if(neurons[neuronNum].getConnections(i) == connectedNum){ connected = true; } } if(connected == false){ neurons[neuronNum].setConnections(connectedNum); System.out.println("Connection set!"); } weights[neuronNum][connectedNum] = toBinary((value1 + value2) / 2); } public void setInputConnections(int neuronNum, int connectedNum, double value1, double value2){ boolean connected = false; for(int i = 0; i < inputs[neuronNum].getConnectionCount(); i++){ if(inputs[neuronNum].getConnections(i) == connectedNum){ connected = true; } } if(connected == false){ inputs[neuronNum].setConnections(connectedNum); } inputWeights[neuronNum][connectedNum] = toBinary((value1 + value2) / 2); } public String calculateWeights(String[] inputArray){ String[] inputValues = new String[inputArray.length]; for(int i = 0; i < inputArray.length; i++){ inputValues[i] = inputArray[i]; } for(int i = 0; i < outNum; i++){ outPuts[i] = 0; } boolean stop = false; int time = 0; String answer = ""; while(stop == false){ Neuron[] newActivatedNeurons = new Neuron[colunmNum * rowNum]; if(time < inputValues.length){ int inputNumber = 0; for(int i = 0; i < symbols.length(); i++){ String test = "" + symbols.charAt(i); if(test.equalsIgnoreCase(inputValues[time])){ inputNumber = i; } } for(int i = 0; i < inputs[inputNumber].getConnectionCount(); i++){ double newValue = inputs[inputNumber].getValue() * toDecimal(inputWeights[inputNumber][inputs[inputNumber].getConnections(i)]); neurons[inputs[inputNumber].getConnections(i)].addValue(newValue); if(neurons[inputs[inputNumber].getConnections(i)].isActivated() == false){ activatedNeurons[activatedCount] = neurons[inputs[inputNumber].getConnections(i)]; activatedNeurons[activatedCount].setActivated(true); neurons[inputs[inputNumber].getConnections(i)].setActivated(true); System.out.println("Neuron: " + activatedNeurons[activatedCount].getNum() + " " + time); activatedCount++; } } } for(int i = 0; i < activatedCount; i++){ int num = i % 2; activatedNeurons = neuronActivated(activatedNeurons[i], activatedNeurons, i, num); //System.out.println(activatedNeurons[i].getNum() + " " + i); } Neuron[] activatedOutputs = new Neuron[outNum]; for(int i = 0; i < outNum + 2; i++){ if(neurons[i + colunmNum * rowNum].isActivated() == true){ if(i == outNum + 1){ stop = true; } else{ answer = answer + symbols.charAt(i); System.out.println(answer); if(answer.length() > 1000){ stop = true; } } } } time++; } return answer; } public Neuron[] neuronActivated(Neuron neuron, Neuron[] neuronArray, int activatedNum, int type){ int neuronPos = activatedNum; Neuron[] newNeuronArray = (Neuron[]) neuronArray.clone(); if(neuron.getRemainingDelay() == 0 && neuron.isActivated() == true){ for(int i = 0; i < neuron.getConnectionCount(); i++){ double newValue = neuron.getValue() * toDecimal(weights[neuron.getNum()][neuron.getConnections(i)]); neurons[neuron.getConnections(i)].addValue(newValue); if(neurons[neuron.getConnections(i)].isActivated() == false && neurons[neuron.getConnections(i)].getNum() < colunmNum * rowNum){ neurons[neuron.getConnections(i)].setActivated(true); newNeuronArray[activatedCount] = neurons[neuron.getConnections(i)]; System.out.println(neurons[neuron.getConnections(i)].getNum()); activatedCount++; } if(neurons[neuron.getConnections(i)].getNum() > colunmNum * rowNum){ neurons[neuron.getConnections(i)].setActivated(true); //System.out.println("Output!!"); } } newNeuronArray[neuronPos].setActivated(false); newNeuronArray = shiftArray(neuronPos, newNeuronArray); activatedCount--; } else if(neuron.isActivated() == true){ neuron.delayTick(); newNeuronArray[activatedNum] = neuron; neurons[neuron.getNum()] = neuron; } return newNeuronArray; } public int[] shiftArray(int num, int[] brainArray){ int[] tempBrain = new int[brainArray.length]; tempBrain = (int[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ brainArray[i] = tempBrain[i + 1]; } return brainArray; } public Neuron[] shiftArray(int num, Neuron[] brainArray){ Neuron[] tempBrain = new Neuron[brainArray.length]; tempBrain = (Neuron[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ brainArray[i] = tempBrain[i + 1]; } return brainArray; } public int[] shiftArrayBack(int num, int[] brainArray){ int[] tempBrain = new int[brainArray.length]; tempBrain = (int[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ if(i == num){ brainArray[i] = 0; } else{ brainArray[i] = tempBrain[i - 1]; } } return brainArray; } public void calculateScore(String response, String answer){ int rScore = 0; for(int i = 0; i < response.length(); i++){ if(i < answer.length()){ if(answer.charAt(i) == response.charAt(i)){ rScore++; } else{ rScore--; } } else{ rScore--; } } if(answer.length() > response.length()){ rScore = rScore - (answer.length() - response.length()); } this.score = rScore; } } class CompBrain{ public int radius; double percent = 0; String symbols = "abcdefghijklmnopqrstuvwxyz;:',!?.()\"123456789 "; private int maxConnectionNum = 7; private int maxDelay = 20; private int divFunction = 8; private int divNeuron = 8; private int divPower = 8; private int divRotation = 2; int neuronChange1Count = 0; int neuronChange2Count = 0; Neuron[] activatedNeurons; int activatedCount = 0; int timeNum; boolean goOn; private String[][] weights; private String[] connections1; private String[] connections2; private String[][] inputWeights; private Neuron[] neurons; private String[] neuronFunction; private String[] neuronPower; private Neuron[] inputs; private Neuron[] neuronChange1; private Neuron[] neuronChange2; private Brain createdBrain; private String[] inputString; double dx = Math.random() * 638 + 2; double dy = Math.random() * 478 + 2; int score = 0; int divideNum = 1; int speedReduce = 1; int foodCount; int rowNum; int colunmNum; boolean outputActivated; double moveOut, rotationLeftOut, rotationRightOut; double[] outPuts; int outCNum; int outRNum; int outONum; int outINum; int connectionNum; int inputConnectionNum; int connectionsMade = 0; int x, y; double rotation = 0; public CompBrain(int rows, int columnNumber, int outRowNum, int outColNum, int outOutNum, int outInputNum, boolean first, String[][] weight, int inputNum, Brain brain){ createdBrain = brain; outCNum = outColNum; outRNum = outRowNum; outONum = outOutNum; outINum = outInputNum; rowNum = rows; colunmNum = columnNumber; neurons = new Neuron[rowNum * colunmNum + outCNum * outRNum + outINum + outONum]; weights = new String[rowNum * colunmNum + outCNum * outRNum + outINum + outONum][rowNum * colunmNum + outCNum * outRNum + outINum + outONum]; inputWeights = new String[inputNum][columnNumber * rows]; inputs = new Neuron[inputNum]; neuronChange1 = new Neuron[(rowNum * colunmNum + outCNum * outRNum + outINum + outONum)]; neuronChange2 = new Neuron[(rowNum * colunmNum + outCNum * outRNum + outINum + outONum)]; activatedNeurons = new Neuron[colunmNum * rowNum]; activatedCount = 0; if(first == true){ generateWeights(); } } private void setInputs(String[] inputStringArray){ inputString = (String[]) inputStringArray.clone(); } private void generateWeights(){ connectionNum = 5000;//1000 + Math.round((float)(Math.random() * (maxConnectionNum * colunmNum * rowNum) / 1.5)); inputConnectionNum = 60;//5 + Math.round((float)(Math.random() * (inputs.length * (maxConnectionNum - 5)))); int usedConnections = 0; for(int i = 0; i < rowNum * colunmNum + outCNum * outRNum + outINum + outONum; i++){ neurons[i] = new Neuron(i, maxConnectionNum); neurons[i].setDelay(Math.round((float)(5 + Math.random() * maxDelay))); } for(int i = 0; i < inputs.length; i++){ inputs[i] = new Neuron(i, maxConnectionNum); } for(int i = 0; i < inputConnectionNum; i++){ boolean goOn = false; while(goOn == false){ int inputNeuron = Math.round((float)(Math.random() * (inputs.length - 1))); int neuron = Math.round((float)(Math.random() * (colunmNum * rowNum - 1))); double random = (double)(Math.random() * 16); double negativeRan = Math.random(); if(negativeRan < 0.5){ random = random * -1; } if(inputs[inputNeuron].getConnectionCount() < maxConnectionNum){ inputWeights[inputNeuron][neuron] = toBinary(random); inputs[inputNeuron].setConnections(neuron); goOn = true; } } } for(int i = 0; i < rowNum; i++){ for(int u = 0; u < connectionNum; u++){ boolean goOn = false; while(goOn == false){ int neuron1 = Math.round((float)(Math.random() * (rowNum * colunmNum - 1))); int neuron2 = Math.round((float)(Math.random() * (rowNum * colunmNum + outONum + outINum + outCNum * outRNum - 1))); double random = (double)(Math.random() * 16); double negativeRan = Math.random(); if(negativeRan < 0.5){ random = random * -1; } if(weights[neuron1][neuron2] == null && neurons[neuron1].getConnectionCount() < maxConnectionNum){ weights[neuron1][neuron2] = toBinary(random); neurons[neuron1].setConnections(neuron2); goOn = true; } } } } for(int i = 0; i < rowNum * colunmNum; i++){ neurons[i].shrinkArray(); } } public static String toBinary(double num){ double dNum = Math.abs(num); String sNum = "" + dNum; String bNum = ""; char ch = sNum.charAt(1); int nu = Character.getNumericValue(ch); if(nu == -1){ for(int i = 0; i < sNum.length(); i++){ char c = sNum.charAt(i); int iNum = Character.getNumericValue(c); for(int u = 0; u < 4; u++){ if( iNum == -1){ String binaryDec = "."; bNum = bNum + binaryDec; break; } else{ int binaryNum = iNum % 2; if(binaryNum == 1){ iNum = (iNum - 1) / 2; } else{ iNum = iNum / 2; } bNum = bNum + binaryNum; } } } } else{ int iNum = 0; for(int i = 0; i < sNum.length() - 1; i++){ if(i == 0){ char c1 = sNum.charAt(0); char c2 = sNum.charAt(1); String twoNum = "" + c1 + c2; iNum = Integer.parseInt(twoNum); } char c = sNum.charAt(i + 1); if(i != 0){ iNum = Character.getNumericValue(c); } for(int u = 0; u < 4; u++){ if( iNum == -1){ String binaryDec = "."; bNum = bNum + binaryDec; break; } else{ int binaryNum = iNum % 2; if(binaryNum == 1){ iNum = (iNum - 1) / 2; } else{ iNum = iNum / 2; } bNum = bNum + binaryNum; } } } } if(num < 0){ bNum = "-" + bNum; } else{ bNum = "+" + bNum; } return bNum; } public static double toDecimal(String bNum){ int convertedNum = 0; double finalNum = 0; String numString = ""; for(int u = 1; u < (bNum.length()) / 4; u++){ char c; convertedNum = 0; for(int i = 0; i < 4; i++){ if(u != 0){ c = bNum.charAt(1 + i + u * 4); } else{ c = bNum.charAt(i + u * 4); } int num = Character.getNumericValue(c); if(num == 1){ convertedNum = convertedNum + Math.round((float)Math.pow(2, Math.abs(i))); } } if(u == 1){ numString = numString + convertedNum + "."; } else{ numString = numString + convertedNum; } } //System.out.println(numString); finalNum = Double.parseDouble(numString); char nChar = bNum.charAt(0); if(nChar == '-'){ finalNum = finalNum * -1; } return finalNum; } public double getPercent(){ return percent; } public void setPercent(double newPercent){ this.percent = newPercent; } public int getScore(){ return score; } public String getWeights(int first, int second){ return weights[first][second]; } public void setWeights(int first, int second, String num){ weights[first][second] = num; } public void setInputWeights(int first, int second, String num){ inputWeights[first][second] = num; } public String getNeuronFunction(int num){ return neuronFunction[num]; } public void setNeuronFunction(int num, String s){ neuronFunction[num] = s; } public String getInputWeights(int first, int second){ if(inputWeights[first][second] == null){ return "null"; } else{ return inputWeights[first][second]; } } public Brain getBrain(){ return createdBrain; } public void setAddedScore(int newScore){ this.score += newScore; } public void setFoodCount(int newFoodCount){ this.foodCount = newFoodCount; } public String getPower(int num){ return neuronPower[num]; } public void setPower(int num, String set){ neuronPower[num] = set; } public void calculateWeights(String[] inputArray){ timeNum = 0; String[] inputValues = new String[inputArray.length]; for(int i = 0; i < inputArray.length; i++){ inputValues[i] = inputArray[i]; } boolean stop = false; int time = 0; String answer = ""; while(stop == false){ Neuron[] newActivatedNeurons = new Neuron[colunmNum * rowNum]; if(time < inputValues.length){ int inputNumber = 0; for(int i = 0; i < symbols.length(); i++){ String test = "" + symbols.charAt(i); if(test.equalsIgnoreCase(inputValues[time])){ inputNumber = i; } } for(int i = 0; i < inputs[inputNumber].getConnectionCount(); i++){ double newValue = inputs[inputNumber].getValue() * toDecimal(inputWeights[inputNumber][inputs[inputNumber].getConnections(i)]); neurons[inputs[inputNumber].getConnections(i)].addValue(newValue); if(neurons[inputs[inputNumber].getConnections(i)].isActivated() == false){ activatedNeurons[activatedCount] = neurons[inputs[inputNumber].getConnections(i)]; activatedNeurons[activatedCount].setActivated(true); neurons[inputs[inputNumber].getConnections(i)].setActivated(true); //System.out.println("Neuron: " + activatedNeurons[activatedCount].getNum() + " " + time); activatedCount++; } } } for(int i = 0; i < activatedCount; i++){ activatedNeurons = neuronActivated(activatedNeurons[i], activatedNeurons, i, i, time); //System.out.println(activatedNeurons[i].getNum() + " " + i); } //Neuron[] activatedOutputs = new Neuron[outNum]; time++; //System.out.println(time); if(time == 1000){ stop = true; } } } public Neuron[] neuronActivated(Neuron neuron, Neuron[] neuronArray, int activatedNum, int time, int totalTime){ int neuronPos = activatedNum; if(time == 0){ outputActivated = false; goOn = false; } Neuron[] newNeuronArray = (Neuron[]) neuronArray.clone(); if(neuron.getRemainingDelay() == 0 && neuron.isActivated() == true){ for(int i = 0; i < neuron.getConnectionCount() - 1; i++){ double newValue = neuron.getValue() * toDecimal(weights[neuron.getNum()][neuron.getConnections(i)]); neurons[neuron.getConnections(i)].addValue(newValue); if(neurons[neuron.getConnections(i)].isActivated() == false && neurons[neuron.getConnections(i)].getNum() < colunmNum * rowNum){ neurons[neuron.getConnections(i)].setActivated(true); newNeuronArray[activatedCount] = neurons[neuron.getConnections(i)]; //System.out.println(neurons[neuron.getConnections(i)].getNum()); activatedCount++; //System.out.println("Activated: " + activatedCount); } if(neurons[neuron.getConnections(i)].getNum() > colunmNum * rowNum && neurons[neuron.getConnections(i)].isActivated() == false){ //System.out.println("Output Activated " + timeNum + " " + totalTime + " " + neuronChange1Count + " " + neuronChange2Count); if(neuronChange1[neuronChange1Count] == null && timeNum == 0 && neurons[neuron.getConnections(i)].isActivated() == false){ neuronChange1[neuronChange1Count] = neurons[neuron.getConnections(i)]; neurons[neuron.getConnections(i)].setActivated(true); neuronChange1Count++; System.out.println("Output1! " + connectionsMade); //System.out.println(neuron.getConnections(i)); } else if(neuronChange1[neuronChange2Count] != null && timeNum == 1 && neurons[neuron.getConnections(i)].isActivated() == false){ neuronChange2[neuronChange2Count] = neurons[neuron.getConnections(i)]; neurons[neuron.getConnections(i)].setActivated(true); neuronChange2Count++; connectionsMade++; System.out.println(neuron.getConnections(i)); System.out.println("Output2! " + connectionsMade); } else if(timeNum == 1 && neurons[neuron.getConnections(i)].isActivated() == false){ neuronChange1[neuronChange2Count] = neurons[neuron.getConnections(i)]; neurons[neuron.getConnections(i)].setActivated(true); neuronChange2Count++; } if(neuronChange1Count == neuronChange2Count){ goOn = true; System.out.println("They are the same!"); } outputActivated = true; } } newNeuronArray[neuronPos].setActivated(false); newNeuronArray = shiftArray(neuronPos, newNeuronArray); activatedCount--; } else if(neuron.isActivated() == true){ neuron.delayTick(); newNeuronArray[activatedNum] = neuron; neurons[neuron.getNum()] = neuron; } if(goOn == true){ Neuron[] topNeurons = new Neuron[neuronChange2Count]; for(int i = 0; i < neuronChange2Count; i++){ for(int u = 0; u < neuronChange2Count; u++){ if(i == 0){ topNeurons[i] = neuronChange1[i]; break; } if(topNeurons[Math.abs(u - neuronChange2Count) - 1] != null){ if(neuronChange1[i].getValue() < topNeurons[Math.abs(u - neuronChange2Count) - 1].getValue()){ int newPlace = Math.abs(u - neuronChange2Count); topNeurons = (Neuron[]) shiftArray(newPlace, topNeurons).clone(); topNeurons[Math.abs(u - neuronChange2Count)] = neuronChange1[i]; break; } else{ if(u == neuronChange2Count - 1){ topNeurons = (Neuron[]) shiftArray(0, topNeurons).clone(); topNeurons[0] = neuronChange1[i]; } } } } } Neuron[] topNeurons2 = new Neuron[neuronChange2Count]; for(int i = 0; i < neuronChange2Count; i++){ for(int u = 0; u < neuronChange2Count; u++){ if(i == 0){ topNeurons2[i] = neuronChange2[i]; break; } if(topNeurons[Math.abs(u - neuronChange2Count) - 1] != null){ if(neuronChange1[i].getValue() < topNeurons2[Math.abs(u - neuronChange2Count) - 1].getValue()){ int newPlace = Math.abs(u - neuronChange2Count); topNeurons2 = (Neuron[]) shiftArray(newPlace, topNeurons2).clone(); topNeurons2[Math.abs(u - neuronChange2Count)] = neuronChange2[i]; break; } else{ if(u == neuronChange2Count - 1){ topNeurons2 = (Neuron[]) shiftArray(0, topNeurons).clone(); topNeurons2[0] = neuronChange2[i]; } } } } } int tot = rowNum * colunmNum; for(int i = 0; i < neuronChange1Count; i++){ if(neuronChange1[i].getNum() - tot < outINum && neuronChange2[i].getNum() - tot > outINum){ createdBrain.setInputConnections(neuronChange1[i].getNum() - tot, neuronChange2[i].getNum() - tot - outINum, neuronChange1[i].getValue(), neuronChange2[i].getValue()); } else if(neuronChange2[i].getNum() - tot < outINum && neuronChange2[i].getNum() - tot > outINum){ createdBrain.setInputConnections(neuronChange2[i].getNum() - tot, neuronChange1[i].getNum() - tot - outINum, neuronChange2[i].getValue(), neuronChange1[i].getValue()); } else if(neuronChange1[i].getNum() - tot > outRNum * outCNum && neuronChange2[i].getNum() - tot < outRNum * outCNum){ if(neuronChange2[i].getNum() - tot - outINum < 0){ createdBrain.setNeuronConnections(neuronChange2[i].getNum() - tot, neuronChange1[i].getNum() - tot - outINum, neuronChange2[i].getValue(), neuronChange1[i].getValue()); } else{ createdBrain.setNeuronConnections(neuronChange2[i].getNum() - tot - outINum, neuronChange1[i].getNum() - tot - outINum, neuronChange2[i].getValue(), neuronChange1[i].getValue()); } } else if(neuronChange2[i].getNum() - tot > outRNum * outCNum && neuronChange1[i].getNum() - tot < outRNum * outCNum){ if(neuronChange1[i].getNum() - tot - outINum < 0){ createdBrain.setNeuronConnections(neuronChange1[i].getNum() - tot, neuronChange2[i].getNum() - tot - outINum, neuronChange1[i].getValue(), neuronChange2[i].getValue()); } else{ createdBrain.setNeuronConnections(neuronChange1[i].getNum() - tot - outINum, neuronChange2[i].getNum() - tot - outINum, neuronChange1[i].getValue(), neuronChange2[i].getValue()); } } else if(neuronChange1[i].getNum() - tot > outINum && neuronChange1[i].getNum() - tot < outCNum * outRNum && neuronChange2[i].getNum() - tot > outINum && neuronChange2[i].getNum() - tot < outCNum * outRNum){ createdBrain.setNeuronConnections(neuronChange1[i].getNum() - tot - outINum, neuronChange2[i].getNum() - tot - outINum, neuronChange1[i].getValue(), neuronChange2[i].getValue()); } neurons[neuronChange1[i].getNum()].setActivated(false); neurons[neuronChange2[i].getNum()].setActivated(false); } Neuron[] tempNeuron = new Neuron[(rowNum * colunmNum + outCNum * outRNum + outINum + outONum)]; for(int i = 0; i < neuronChange2Count - neuronChange1Count; i++){ tempNeuron[i] = neuronChange1[neuronChange1Count + i]; } System.out.println("Went on!"); neuronChange1 = (Neuron[]) tempNeuron.clone(); neuronChange2 = new Neuron[(rowNum * colunmNum + outCNum * outRNum + outINum + outONum)]; neuronChange1Count = neuronChange2Count - neuronChange1Count; neuronChange2Count = 0; timeNum = 0; goOn = false; outputActivated = false; } else if(timeNum == 0 && outputActivated == true && time == activatedCount - 1 && goOn == false){ timeNum = 1; } return newNeuronArray; } public int[] shiftArray(int num, int[] brainArray){ int[] tempBrain = new int[brainArray.length]; tempBrain = (int[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ brainArray[i] = tempBrain[i + 1]; } return brainArray; } public Neuron[] shiftArray(int num, Neuron[] brainArray){ Neuron[] tempBrain = new Neuron[brainArray.length]; tempBrain = (Neuron[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ brainArray[i] = tempBrain[i + 1]; } return brainArray; } public int[] shiftArrayBack(int num, int[] brainArray){ int[] tempBrain = new int[brainArray.length]; tempBrain = (int[]) brainArray.clone(); for(int i = num; i < brainArray.length - 1; i++){ if(i == num){ brainArray[i] = 0; } else{ brainArray[i] = tempBrain[i - 1]; } } return brainArray; } public int score(String response, String answer){ return 0; } } class Shapes{ int x; int y; String type; public Shapes(){ } public String getType(){ return type; } public int getX(){ return x; } public int getY(){ return y; } } class Circle extends Shapes{ int radius; public Circle(int radius, int x, int y){ this.x = x; this.y = y; this.radius = radius; this.type = "Circle"; } public int getRadius(){ return this.radius; } } class Square extends Shapes{ int height; int width; public Square(int height, int width, int x, int y){ this.x = x; this.y = y; this.height = height; this.width = width; this.type = "Square"; } public String getType(){ return type; } public int getHeight(){ return this.height; } public int getWidth(){ return this.width; } } class Neuron{ int delayAmount = 0; int delayRemaining = 0; int num; int[] connections; double value; int length; int finalConnections = 0; boolean isActivated = false; int subject; public Neuron(int number, int connectionCount){ num = number; value = 0; connections = new int[connectionCount]; } public int getNum(){ return num; } public int getConnections(int i){ return connections[i]; } public double getValue(){ return value; } public int getLength(){ return length; } public int getConnectionCount(){ return finalConnections; } public int getDelay(){ return delayAmount; } public int getRemainingDelay(){ return delayRemaining; } public boolean isActivated(){ return isActivated; } public int getSubject(){ return subject; } public void addValue(double newValue){ value += newValue; } public void setConnections(int neuronNum){ connections[finalConnections] = neuronNum; if(finalConnections < connections.length - 1){ finalConnections++; } } public void setValue(double newValue){ value = newValue; } public void setLength(int newLength){ length = newLength; } public void setDelay(int newDelay){ delayAmount = newDelay; } public void setActivated(boolean newState){ isActivated = newState; if(newState == true){ delayRemaining = delayAmount; } } public void setSubject(int newSubject){ subject = newSubject; } public void shrinkArray(){ int[] tempConnections = new int[finalConnections]; for(int i = 0; i < finalConnections - 1; i++){ tempConnections[i] = connections[i]; } connections = null; connections = new int[finalConnections]; System.arraycopy(tempConnections, 0, connections, 0, tempConnections.length); this.setLength(connections.length); } public boolean checkConnectio(int connectedTo){ for(int i = 0; i < finalConnections; i++){ if(connections[i] == connectedTo){ return true; } } return false; } public void delayTick(){ if(delayRemaining != 0){ delayRemaining = delayRemaining - 1; } } } class Memory{ String mem; int num; public Memory(String memory, int number){ mem = memory; num = number; } public String getMemory(){ return mem; } public void setMemory(String newMemory){ mem = newMemory; } }
To learn more HTML/CSS, check out these tutorials!