-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.cpp
221 lines (200 loc) · 5.4 KB
/
maze.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Author: Josh Bryan <[email protected]>
* This file is copyright 2010 by Josh Bryan.
*
* This file is part of Reinforcement Learning Demo.
*
* Foobar is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>
#include <vector>
#include <cmath>
#include "maze.h"
#include "discrete.h"
Maze::Maze(int x, int y) :
grid(boost::extents[x][y]),
location(),
stochastic_actions_(false)
{
std::vector<double> probs(x*y,1.0);
//ensure start position is neither goal
//nor hazard
probs[0] = 0;
//create goals
for (int i = 0; i < 2; i++)
{
DiscreteVariate goal(probs);
std::size_t g = goal();
grid.origin()[g] = 5.0f;
probs[g] = 0;
}
//create hazards
for (int i = 0; i < 3*sqrt(x*y); i++)
{
DiscreteVariate traps(probs);
std::size_t t = traps();
grid.origin()[t] = -1.0f;
probs[t] = 0;
}
location[0] = 0;
location[1] = 0;
}
/*
* Draw the maze as a colored grid.
*/
void Maze::draw_maze() const
{
grid_t::size_type x,y;
GLfloat x_scale = 1.0 / grid.shape()[0];
GLfloat y_scale = 1.0 / grid.shape()[1];
//draw the grid
glLineWidth(3.0);
for (x = 0; x < grid.shape()[0]; x++)
{
for (y = 0; y < grid.shape()[1]; y++)
{
GLfloat red, blue, green;
if (x == 0 && y == 0)
{
//color home differently
red = 0; green = 0; blue = 1;
}
else
{
red = (grid[x][y] > 0) ? 1.0 - fabs(grid[x][y]) : 1.0f;
green = (grid[x][y] < 0) ? 1.0 - fabs(grid[x][y]) : 1.0f;
blue = 1.0 - fabs(grid[x][y]);
}
glColor3f(red, green, blue);
glBegin( GL_LINE_LOOP );
glVertex3f( x * x_scale, y * y_scale, 0.0);
glVertex3f( (x+0.9) * x_scale, y * y_scale, 0.0);
glVertex3f( (x+0.9) * x_scale, (y+0.9) * y_scale, 0.0);
glVertex3f( x * x_scale, (y+0.9) * y_scale, 0.0);
glEnd();
}
}
//draw current location
glColor3f( 0.0, 0.0, 1.0);
x = location[0];
y = location[1];
glBegin( GL_POLYGON ); //base
glVertex3f( (x + 0.2) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.2) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.7) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.7) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glEnd();
glBegin( GL_POLYGON ); //side
glVertex3f( (x + 0.45) * x_scale, (y + 0.45) * y_scale, x_scale * 0.65);
glVertex3f( (x + 0.2) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.2) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glEnd();
glBegin( GL_POLYGON ); //side
glVertex3f( (x + 0.45) * x_scale, (y + 0.45) * y_scale, x_scale * 0.65);
glVertex3f( (x + 0.2) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.7) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glEnd();
glBegin( GL_POLYGON ); //side
glVertex3f( (x + 0.45) * x_scale, (y + 0.45) * y_scale, x_scale * 0.65);
glVertex3f( (x + 0.7) * x_scale, (y + 0.7) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.7) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glEnd();
glBegin( GL_POLYGON ); //side
glVertex3f( (x + 0.45) * x_scale, (y + 0.45) * y_scale, x_scale * 0.65);
glVertex3f( (x + 0.2) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glVertex3f( (x + 0.7) * x_scale, (y + 0.2) * y_scale, x_scale * 0.2);
glEnd();
}
/*
* update the position of the agent based on the action it performs
*/
float Maze::perform_action(action_t action)
{
//if the environment is stochastic
if (stochastic_actions_)
{
//randomly select "real" action
std::vector<double> probs;
for (Maze::action_t a = Maze::MIN_ACT; a < Maze::NUM_ACT;
a = Maze::action_t(a+1))
{
//choose requested action with probability .7 and another
//action with probability .1
probs.push_back(a == action ? 7 : 1);
}
DiscreteVariate rand_action(probs);
action = action_t(rand_action());
}
//get the updated location
location = transition(location, action);
float value = grid(location);
//if this was a target, jump to initial position
if (value > 0)
{
location[0] = 0;
location[1] = 0;
}
//return reward in state - 1 cost for action
return value - 0.005;
}
/*
* Give a new location by computing the effect of the action on the
* old location
*/
Maze::location_t Maze::transition(location_t loc, action_t action)
{
switch (action)
{
case UP:
if (loc[1] < grid.shape()[1]-1)
loc[1]++;
break;
case DOWN:
if (loc[1] > 0)
loc[1]--;
break;
case RIGHT:
if (loc[0] < grid.shape()[0]-1)
loc[0]++;
break;
case LEFT:
if (loc[0] > 0)
loc[0]--;
break;
}
return loc;
}
/*
* get the width of the maze
*/
unsigned int Maze::get_width() const
{
return grid.shape()[0];
}
/*
* get the height of the maze
*/
unsigned int Maze::get_height() const
{
return grid.shape()[1];
}
/*
* set whether to update state stochastically or no
*/
void Maze::set_stochastic_actions( bool stochastic )
{
stochastic_actions_ = stochastic;
}