-
Notifications
You must be signed in to change notification settings - Fork 0
/
environments.js
140 lines (120 loc) · 3.54 KB
/
environments.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
133
134
135
136
137
138
139
140
import { Graphics, Application } from "pixi.js";
let HEIGHT = 450;
let WIDTH = 600;
const GRIDH = 6;
const GRIDW = 8;
let SQH = (HEIGHT-1)/GRIDH; // these both should
let SQW = (WIDTH-2)/GRIDW; // be about the same
const LEFT = 1;
const RIGHT = 2;
const UP = 3;
const DOWN = 4;
export class DefaultEnvironment {
constructor(pixiapp) {
this.pixiapp = pixiapp;
this.initGraphics();
this.reset();
}
reset(){
this.agent_X = randomInt(0, GRIDW - 1);
this.agent_Y = randomInt(0, GRIDH - 1);
this.target_X = GRIDW - 1;
this.target_Y = GRIDH - 1;
this.iterations = 0;
this.terminated = false;
this.display();
}
getObservation() {
return [this.agent_X, this.agent_Y];
}
getActions() {
const ans = []
if (this.agent_X > 0){
ans.push(LEFT)
}
if (this.agent_X < GRIDW - 1){
ans.push(RIGHT)
}
if (this.agent_Y > 0) {
ans.push(UP)
}
if (this.agent_Y < GRIDH - 1) {
ans.push(DOWN)
}
shuffle(ans);
return ans;
}
performAction(action) {
this.iterations+=1;
if (!this.getActions().includes(action)){
setRunState(0);
throw Error(`Action ${action} not allowed!`);
}
switch (action) {
case LEFT:
this.agent_X -= 1;
break;
case RIGHT:
this.agent_X += 1;
break;
case UP:
this.agent_Y -= 1;
break;
case DOWN:
this.agent_Y += 1;
break;
}
if (this.agent_X == this.target_X && this.agent_Y == this.target_Y){
this.terminated = true;
return +20;
} else {
return -1;
}
}
initGraphics() {
HEIGHT = this.pixiapp.renderer.view.height;
WIDTH = this.pixiapp.renderer.view.width;
SQH = (HEIGHT - 1) / GRIDH;
SQW = (WIDTH - 2) / GRIDW;
// make grid background
const background = new Graphics();
background.beginFill(0xffffff);
background.drawRect(0, 0, WIDTH, HEIGHT);
background.endFill();
background.lineStyle(1, 0x000000, 0.3);
for (let i = 1; i <= WIDTH; i += SQH) {
background.moveTo(i, 0);
background.lineTo(i, HEIGHT);
}
for (let i = 1; i <= HEIGHT; i += SQW) {
background.moveTo(0, i);
background.lineTo(WIDTH, i);
}
this.pixiapp.stage.addChild(background);
// make an agent
this.agent = new Graphics();
this.agent.lineStyle(1, 0x000000, 1);
this.agent.beginFill(0x22FFFF, 0.5);
this.agent.drawCircle(SQH/2, SQW/2, Math.min(SQH, SQW)/2.2);
this.pixiapp.stage.addChild(this.agent);
// make an target
this.target = new Graphics();
this.target.lineStyle(1, 0x000000, 1);
this.target.beginFill(0xFFFF00, 0.5);
this.target.drawStar(SQH/2, SQW/2, 5, Math.min(SQH, SQW)/2.2);
this.pixiapp.stage.addChild(this.target);
}
display(){
this.agent.x = SQH * this.agent_X;
this.agent.y = SQW * this.agent_Y;
this.target.x = SQH * this.target_X;
this.target.y = SQW * this.target_Y;
}
}
// helper
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function shuffle(array) {
array.sort(() => Math.random() - 0.5);
}