/* * aka. addNeuron () */ public int addNode(int historicalID) { Gene randLink; do { randLink = links.get(rand.nextInt(links.size())); } while (randLink.isDisabled()); double[] from = getNeuronPos(randLink.getFrom()); double[] to = getNeuronPos(randLink.getTo()); int minx = sm.getCornerOffset() + sm.getWidth() * (sm.getSmallTileSize() + 2) + 2; if (from[0] < minx) { from[0] = minx; } randLink.setDisabled(true); int neuralID = neurons.size() + numInputs; neurons.add(new Neuron((from[0] + to[0]) / 2, (int) (from[1] + to[1]) / 2)); // (int from_, int to_, double weight_, int histID_, boolean negative_, boolean disabled_ addLink(randLink.getFrom(), neuralID, 1.0, historicalID, false); addLink(neuralID, randLink.getTo(), randLink.getWeight(), historicalID + 1, false); bubbleSortNeurons(); return historicalID + 2; }
/* * This method wipes all neuron data on links and reconstructs it. */ public void hardCorrectNeuron() { for (Neuron n : neurons) { n.clearLink(); } for (Gene l : links) { getNeuron(l.getTo()).addLink(l); } }
@Override public String toString() { String temp = "Num inputs: " + numInputs + " : Num neurons: " + neurons.size() + "\n Links \n From : To : HistID"; for (Gene l : links) { temp += "\n" + l.getFrom() + ":" + l.getTo() + ":" + l.getHistID(); } return temp; }
/* * This method ensures that no links are pointing backwards. */ public void correctLinks() { double pos1; double pos2; for (Gene l : links) { pos1 = getNeuronPos(l.getFrom())[0]; // System.out.println("link"); // System.out.println(l.getTo()); // System.out.println(neurons.size()); pos2 = getNeuronPos(l.getTo())[0]; // System.out.println("link2"); if (pos1 > pos2) { // System.out.println("swap"); l.swapFromTo(); } } // System.out.println("fully done"); }
public void addLink(Gene l) { links.add(l); getNeuron(l.getTo()).addLink(l); }
public void draw(Graphics2D g) { Rectangle rect; double colorLevel; double[] from; double[] to; int red; int green; int blue; int alpha; for (Gene l : links) { if (l.isDisabled()) { continue; } if (l.getWeight() < 0) { red = 255; blue = 0; green = 0; g.setColor(Color.RED); } else { red = 0; blue = 0; green = 255; g.setColor(Color.GREEN); } if (getNeuronActivation(l.getFrom()) == null) { alpha = 100; red *= 0.4; blue *= 0.5; green *= 0.5; } else { alpha = 255; } g.setColor(new Color(red, green, blue, alpha)); from = getNeuronPos(l.getFrom()); to = getNeuronPos(l.getTo()); g.drawLine((int) from[0], (int) from[1], (int) to[0], (int) to[1]); } for (Neuron n : neurons) { rect = new Rectangle( (int) (n.getx() - neuronSize / 2), n.gety() - neuronSize / 2, neuronSize, neuronSize); g.setColor(new Color(0, 0, 0)); g.draw(rect); if (n.getActivation() == null) { continue; } rect.setSize((int) rect.getWidth() - 1, (int) rect.getHeight() - 1); rect.setLocation((int) rect.getX() + 1, (int) rect.getY() + 1); colorLevel = ((n.getActivation() + 1.0) / 2.0) * (255.0); g.setColor(new Color((int) colorLevel, (int) colorLevel, (int) colorLevel)); g.fill(rect); } }