forked from ayberkydn/haxRL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnvironment.js
110 lines (92 loc) · 4.64 KB
/
Environment.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
class Environment {
constructor(render = true, sound = true, resetDelay = true, randomStart = true) {
this.render = render;
this.sound = sound;
this.resetDelay = resetDelay;
this.randomStart = randomStart;
this.agents = [];
this.episodeEnd = false;
this.waitingForReset = false;
//this.episodeEndChecker = () => (this.scene.checkGoals() && !this.episodeEnd);
this.episode = 1;
this.episodeEndChecker = () => (this.scene.checkGoals()) //|| this.step == 1000);
this.step = 0;
this.scene = new Scene();
this.scene.addObject(new Box(5, cWidth - 5, 5, cHeight - 5, 0));
this.scene.addObject(new Disc(cWidth / 2, cHeight / 2, middleFieldRadius, 10, 1, 1).makeGhost().makeHollow().setOuterColor(Color.border));
this.scene.addObject(new VerticalBorder(cWidth / 2, cHeight / 2, cHeight - 2 * topbottomMargin, null).makeGhost());
this.scene.addObject(new HorizontalBorder(cWidth / 2, topbottomMargin, cWidth - 2 * leftrightMargin, borderRestitution).extendTo(Way.up).setCollisionMask([Ball]));
this.scene.addObject(new HorizontalBorder(cWidth / 2, cHeight - topbottomMargin, cWidth - 2 * leftrightMargin, borderRestitution).extendTo(Way.down).setCollisionMask([Ball]));
this.scene.addObject(new VerticalBorder(leftrightMargin, (cHeight / 2 - goalLength / 2 + topbottomMargin) / 2, cHeight / 2 - topbottomMargin - goalLength / 2, borderRestitution).extendTo(Way.left).setCollisionMask([Ball]));
this.scene.addObject(new VerticalBorder(leftrightMargin, cHeight - (cHeight / 2 - goalLength / 2 + topbottomMargin) / 2, cHeight / 2 - topbottomMargin - goalLength / 2, borderRestitution).extendTo(Way.left).setCollisionMask([Ball]));
this.scene.addObject(new VerticalBorder(cWidth - leftrightMargin, (cHeight / 2 - goalLength / 2 + topbottomMargin) / 2, cHeight / 2 - topbottomMargin - goalLength / 2, borderRestitution).extendTo(Way.right).setCollisionMask([Ball]));
this.scene.addObject(new VerticalBorder(cWidth - leftrightMargin, cHeight - (cHeight / 2 - goalLength / 2 + topbottomMargin) / 2, cHeight / 2 - topbottomMargin - goalLength / 2, borderRestitution).extendTo(Way.right).setCollisionMask([Ball]));
this.scene.addObject(new Goal(leftrightMargin, cHeight / 2, Way.left, goalLength));
this.scene.addObject(new Goal(cWidth - leftrightMargin, cHeight / 2, Way.right, goalLength));
this.scene.addObject(new Ball(cWidth / 2, cHeight / 2, ballRadius, ballMass, ballRestitution, ballDamping));
}
addAgent(agent, side) {
agent.env = this;
agent.setSide(side);
this.agents.push(agent);
this.scene.addObject(agent.player);
agent.ball = this.scene.metaObjects.balls[0];
if (this.agents.length == 2) {
this.agents[0].opponent = this.agents[1];
this.agents[1].opponent = this.agents[0];
}
}
linkAgentsExperience() {
if (this.agents[0] instanceof NNQLearnerAgent && this.agents[1] instanceof NNQLearnerAgent) {
this.agents[0].experienceReplay = this.agents[1].experienceReplay;
console.log("Experience linking successful.");
} else {
throw "Agents missing or incompatible for exp linking";
}
}
resetScene() {
this.scene.reset();
this.waitingForReset = false;
this.episodeEnd = false;
if (this.randomStart) {
this.scene.metaObjects.balls[0].applyImpulse(new Vector(Math.random() - 0.5, Math.random() - 0.5).mult(20));
}
this.step = 0;
}
update() {
for (let agent of this.agents) {
agent.act();
}
this.scene.update();
if (this.episodeEndChecker()) {
this.episodeEnd = true;
console.clear();
}
for (let agent of this.agents) {
agent.learn();
}
if (this.episodeEnd == true) {
if (this.resetDelay) {
if (this.waitingForReset == false) {
window.setTimeout(this.resetScene.bind(this), 2500);
if (this.sound == true) {
new Audio("goalsound.mp3").play();
}
this.waitingForReset = true
}
} else {
if (this.sound == true) {
new Audio("goalsound.mp3").play();
}
this.resetScene();
} //such spaghetti much code wow, fix this
}
this.draw();
this.step += 1;
}
draw() {
if (this.render == true) {
this.scene.draw();
}
}
}