double leftBorder = -2;
double rightBorder = (this.network.getNeuronCount() - 1) * this.distanceBetweenNeurons.x + 2;
for (int i = 0; i < this.network.getNeuronCount(); i++) {
Polygon2D polTarget = this.getAgentShapeInEnvironment(i);
Neuron currentNeuron = this.network.getNeuron(i);
double dicke = polTarget.getBoundingBox().getWidth() / 30;
double inputOutputArrowLength = dicke * 6;
// Draw input / output.
if (currentNeuron.isInput()) {
Color color = Color.orange;
Polygon2D points = new Polygon2D();
ArrayList<Double> thicks = new ArrayList<Double>(2);
thicks.add(dicke);
thicks.add(dicke);
Vector2D startPoint = new Vector2D(leftBorder, polTarget.getBoundingBox().middle().y);
points.add(startPoint);
points.add(new Vector2D(leftBorder + inputOutputArrowLength, polTarget.getBoundingBox().middle().y));
Polygon2D arrow = pfeilMaster.segmentPfeilPol2D(points,
thicks, ArrowMaster.KUGEL_ENDE,
ArrowMaster.EINFACHE_SPITZE_1, new Vector2D(1, 1),
new Vector2D(1, 1));
arrow = this.getPolygonInVisualization(arrow);
g.setColor(color);
g.fillPolygon(arrow.toPol());
g.setColor(Color.black);
g.drawPolygon(arrow.toPol());
g.drawString(StaticMethods.round(currentNeuron.getInput(), 2)
+ "", (float) startPoint.x, (float) startPoint.y - 5);
Vector2D textPos = new Vector2D(startPoint.x, startPoint.y - 0.2);
textPos = this.getPointInVisualization(textPos);
g.setColor(Color.blue);
g.setFont(new Font("", Font.PLAIN, 10));
g.drawString(
"[" + Math.round(currentNeuron.getInput() * 10000.0) / 10000.0 + "]",
(int) textPos.x,
(int) textPos.y);
}
if (currentNeuron.isOutput()) {
Color color = Color.orange;
Polygon2D points = new Polygon2D();
ArrayList<Double> thicks = new ArrayList<Double>(2);
thicks.add(dicke);
thicks.add(dicke);
Vector2D startPoint = new Vector2D(rightBorder - inputOutputArrowLength, polTarget.getBoundingBox().middle().y);
points.add(startPoint);
points.add(new Vector2D(rightBorder, polTarget.getBoundingBox().middle().y));
Polygon2D arrow = pfeilMaster.segmentPfeilPol2D(points,
thicks, ArrowMaster.KUGEL_ENDE,
ArrowMaster.EINFACHE_SPITZE_1, new Vector2D(1, 1),
new Vector2D(1, 1));
arrow = this.getPolygonInVisualization(arrow);
g.setColor(color);
g.fillPolygon(arrow.toPol());
g.setColor(Color.black);
g.drawPolygon(arrow.toPol());
g.drawString(StaticMethods.round(currentNeuron.getOutput(), 2)
+ "", (float) startPoint.x, (float) startPoint.y - 8);
color = Color.orange;
Vector2D textPos = new Vector2D(startPoint.x, startPoint.y - 0.2);
textPos = this.getPointInVisualization(textPos);
g.setColor(Color.blue);
g.setFont(new Font("", Font.PLAIN, 10));
g.drawString(
"[" + Math.round(currentNeuron.getOutput() * 10000.0) / 10000.0 + "]",
(int) textPos.x,
(int) textPos.y);
}
double maxWeight = Double.NEGATIVE_INFINITY;
double minWeight = Double.POSITIVE_INFINITY;
for (NeuralLink link : currentNeuron.getIncomingLinks()) {
if (link.getWeight() > maxWeight) {
maxWeight = link.getWeight();
}
if (link.getWeight() < minWeight) {
minWeight = link.getWeight();
}
}
// Paint links.
for (NeuralLink link : this.network.getNeuron(i).getIncomingLinks()) {
Polygon2D polSource = this.getAgentShapeInEnvironment(link.getSourceNeuronID());
double xSource = polSource.getBoundingBox().middle().x;
double ySource = 0;
double xTarget = 0;
double yTarget = polTarget.getBoundingBox().middle().y;
Vector2D weightPosSelf = null;
if (i > link.getSourceNeuronID()) {
ySource = polSource.getBoundingBox().lowerRightCorner().y;
xTarget = polTarget.getBoundingBox().upperLeftCorner().x - pfeilSpitze.getBoundingBox().getWidth();
} else if (i < link.getSourceNeuronID()) {
ySource = polSource.getBoundingBox().upperLeftCorner().y;
xTarget = polTarget.getBoundingBox().lowerRightCorner().x + pfeilSpitze.getBoundingBox().getWidth();
} else { // ==
weightPosSelf = new Vector2D(polSource.centerPoint().x - 1, polSource.centerPoint().y - 1.075);
}
Polygon2D punkte = new Polygon2D();
punkte.add(new Vector2D(xSource, ySource));
punkte.add(new Vector2D(xSource, yTarget));
punkte.add(new Vector2D(xTarget, yTarget));
LinkedList<Double> dicken = new LinkedList<Double>();
double weightFactor;
if (Math.abs(maxWeight) > Math.abs(minWeight)) {
minWeight = -maxWeight;
} else {
maxWeight = -minWeight;
}
if (link.getWeight() >= 0) {
weightFactor = link.getWeight() / maxWeight;
} else {
weightFactor = link.getWeight() / minWeight;
}
if (weightFactor > 0 && weightFactor < 0.26) {
weightFactor = 0.26;
}
if (weightFactor <= 0 && weightFactor > -0.26) {
weightFactor = -0.26;
}
if (Double.isNaN(weightFactor)) {
weightFactor = 0.1;
}
double actualDicke = dicke * weightFactor;
dicken.add(actualDicke);
dicken.add(actualDicke);
dicken.add(actualDicke);
// Arrow.
Polygon2D pol = pfeilMaster.segmentPfeilPol2D(
punkte,
dicken,
ArrowMaster.EINFACHER_ABSCHLUSS,
ArrowMaster.EINFACHE_SPITZE_1,
new Vector2D(1, 1),
new Vector2D(dicke / actualDicke, dicke / actualDicke));
pol = this.getPolygonInVisualization(pol);
if (link.getWeight() > 0) {
g.setColor(Color.black);
} else {
g.setColor(Color.red);
}
if (weightPosSelf == null) {
g.fillPolygon(pol.toPol());
g.drawPolygon(pol.toPol());
}
// Weight text.
Font font;
if (Math.abs(weightFactor) > 0.5) {
font = new Font("", Font.BOLD, 12);
} else {
font = new Font("", Font.PLAIN, 10);
}
Vector2D weightPos = new Vector2D(
polSource.getBoundingBox().middle().x + 0.15,
polTarget.getBoundingBox().middle().y - 0.15);
if (weightPosSelf != null) {
weightPos = weightPosSelf;
}
weightPos = this.getPointInVisualization(weightPos);
g.setFont(font);
g.drawString("" + StaticMethods.round(link.getWeight(), 2),
(int) weightPos.x, (int) weightPos.y);
}
Vector2D textPos = new Vector2D(i * this.distanceBetweenNeurons.x - 1, i * this.distanceBetweenNeurons.y + 1.25);
textPos = this.getPointInVisualization(textPos);
g.setColor(Color.blue);
g.setFont(new Font("", Font.PLAIN, 10));
g.drawString(
"[" + Math.round(currentNeuron.getNetOutput() * 10000.0) / 10000.0 + "]",
(int) textPos.x,
(int) textPos.y);
}
return img;