-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
132 lines (118 loc) · 3.35 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function getIntersectionPoint(A, B, C, D) {
// Line AB represented as a1x + b1y = c1
var a1 = B.y - A.y;
var b1 = A.x - B.x;
var c1 = a1 * (A.x) + b1 * (A.y);
// Line CD represented as a2x + b2y = c2
var a2 = D.y - C.y;
var b2 = C.x - D.x;
var c2 = a2 * (C.x) + b2 * (C.y);
var determinant = a1 * b2 - a2 * b1;
if (determinant == 0) {
// The lines are parallel
return Infinity;
}
else {
var x = (b2 * c1 - b1 * c2) / determinant;
var y = (a1 * c2 - a2 * c1) / determinant;
return createVector(x, y);
}
}
function drawLines(lines) {
for (var i in lines) {
line(lines[i][0].x, lines[i][0].y, lines[i][1].x, lines[i][1].y);
}
}
function drawPoints() {
for (var i in deathPoints) {
push();
fill('red');
stroke('red');
circle(deathPoints[i].x, deathPoints[i].y, 2);
pop();
}
}
function init() {
createLines();
deathPoints = [];
agents = [];
pointsLastGen = [];
if (checkboxOn && slider.value() < 100 - STEP) slider.value(slider.value() + STEP);
for (var i = 0; i < POPSIZE; i++) {
agents.push(new Agent(i));
}
}
function createLines() {
lines = [
[createVector(0, CANVAS_HEIGHT / 2 - 40), createVector(200, CANVAS_HEIGHT / 2 - 40)],
[createVector(0, CANVAS_HEIGHT / 2 + 40), createVector(200, CANVAS_HEIGHT / 2 + 40)],
];
for (var i = 0; i < 5; i++) {
if (randomGeneration) {
var offset = random((-CANVAS_HEIGHT / 2 + 40) / 100 * slider.value(), (CANVAS_HEIGHT / 2 - 40) / 100 * slider.value());
lines.push([lines[lines.length - 2][1], createVector((i + 2) * 200, CANVAS_HEIGHT / 2 - 40 + offset)]);
lines.push([lines[lines.length - 2][1], createVector((i + 2) * 200, CANVAS_HEIGHT / 2 + 40 + offset)]);
} else {
var offset = (CANVAS_HEIGHT / 2 - 40) / 100 * slider.value() * (Math.round(Math.random()) * 2 - 1);
lines.push([lines[lines.length - 2][1], createVector((i + 2) * 200, CANVAS_HEIGHT / 2 - 40 + offset)]);
lines.push([lines[lines.length - 2][1], createVector((i + 2) * 200, CANVAS_HEIGHT / 2 + 40 + offset)]);
}
}
}
function run() {
if (agentsAlive > 0) {
for (var i = 0; i < POPSIZE; i++) {
agents[i].collide(lines);
agents[i].move(lines);
agents[i].draw(lines);
}
} else {
lastAv = neat.getAverage();
genAvs.push(lastAv * 0.1);
neat.evolve().then(_ => { difficulties.push(-slider.value()); charts(); agentsAlive = POPSIZE; init(); time = 0; genCounter++; best = max(lastAv, best); });
}
}
function drawThings() {
line(best, 0, best, CANVAS_HEIGHT);
drawLines(lines);
drawPoints();
}
function charts() {
var data = [{
x: genAvs.length,
y: genAvs,
name: 'Punktzahl',
type: 'bar'
},
{
x: difficulties.length,
y: difficulties,
name: 'Schwierigkeit',
type: 'bar'
},
];
var layout = {
title: "Durschnittliche Punktzahl",
xaxis: { title: "Generation" },
yaxis: { title: "Punktzahl" },
barmode: 'relative'
}
Plotly.newPlot('gens', data, layout);
var data = {
x: pointsLastGen,
type: 'histogram',
};
var layout = {
title: "Punktzahl der Agenten der letzten Generation",
xaxis: { title: "Erreichte Punktzahl" },
yaxis: { title: "Anzahl der Agenten" }
}
Plotly.newPlot('histogram', [data], layout);
}
function checkBox() {
if (checkboxOn) {
slider.attribute('disabled', '');
} else {
slider.removeAttribute('disabled');
}
}