-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions and vars.js
105 lines (94 loc) · 2.16 KB
/
functions and vars.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
const POPSIZE = 100,
WINDOWWIDTH = 400,
WINDOWHEIGHT = 300,
TIMER = 1500,
MUTATIONRATE = 0.2,
nt = neataptic,
neat = new nt.Neat(4, 2, null, {
mutation: nt.methods.mutation.ALL,
popsize: POPSIZE,
mutationRate: MUTATIONRATE,
elitism: 0
}),
genTag = document.getElementById('gens'),
aliveTag = document.getElementById('alive'),
lastScoreTag = document.getElementById('lastScore'),
timerTag = document.getElementById('timer');
var Engine = Matter.Engine,
Render = Matter.Render,
World = Matter.World,
Runner = Matter.Runner,
Bodies = Matter.Bodies,
agents = [],
agentsAlive = POPSIZE,
gens = 0,
timer = 0,
avarageScores = [],
scoresThisRound = [],
engine = Engine.create(),
world = engine.world,
render = Render.create({
element: document.getElementById('main'),
engine: engine,
options: {
width: 800,
height: 400,
showAngleIndicator: true,
wireframes: false
// showCollisions: true
}
});
Render.run(render);
var runner = Runner.create();
Runner.run(runner, engine);
var mouse = Matter.Mouse.create(render.canvas),
mouseConstraint = Matter.MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});
Matter.World.add(world, mouseConstraint);
// keep the mouse in sync with rendering
render.mouse = mouse;
function initPOP() {
agents = [];
for (var i = 0; i < POPSIZE; i++) {
agents.push(new Agent(i));
agents[i].addToWorld();
}
}
function drawPlots() {
var data = [{
x: avarageScores.length,
y: avarageScores,
type: 'bar'}
];
var layout = {
title: "Average Score",
xaxis: { title: "Generation" },
yaxis: { title: "Score" }
}
Plotly.newPlot('chart', data, layout);
var data = {
x: scoresThisRound,
type: 'histogram',
};
var layout = {
title: "Score of Agents from last generation",
xaxis: { title: "Score" },
yaxis: { title: "Amount of Agents" }
}
Plotly.newPlot('chart2', [data], layout);
}
function reset() {
agentsAlive = POPSIZE;
timer = 0;
gens++;
scoresThisRound = [];
lastScoreTag.innerHTML = "Average of last generation: " + Math.round(avarageScores[avarageScores.length - 1]);
genTag.innerHTML = "Generation: " + gens;
}