-
Notifications
You must be signed in to change notification settings - Fork 8
/
mountaincar.jl
231 lines (187 loc) · 6.38 KB
/
mountaincar.jl
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
222
223
224
225
226
227
228
229
230
231
using Cairo, Colors, Printf
mutable struct MountainCar
x::Float64 # position
v::Float64 # speed
end
function update(car::MountainCar, a::Symbol)
if a == :left
act = -1
elseif a == :right
act = 1
elseif a == :coast
act = 0
else
error("Invalid action: $a")
end
v2 = car.v + act * 0.001 + cos(3 * car.x) * (-0.0025)
v2 = clamp(v2, -0.07, 0.07)
x2 = clamp(car.x + v2, -1.2, 0.6)
return MountainCar(x2, v2)
end
function Cairo.set_source_rgba(ctx::CairoContext, color::Colorant)
r = convert(Float64, red(color))
g = convert(Float64, green(color))
b = convert(Float64, blue(color))
a = convert(Float64, alpha(color))
set_source_rgba(ctx, r, g, b, a)
ctx
end
get_terrain_height(x) = 1/3*sin(3 * x) + 1/3
reward(car::MountainCar, a::Symbol) = get_terrain_height(car.x)
function render_terrain!(ctx::CairoContext, color::Colorant=colorant"grey")
Cairo.save(ctx)
set_source_rgba(ctx, color)
new_sub_path(ctx)
move_to(ctx, -1.2, -0.01)
line_to(ctx, 0.6, -0.01)
for x in range(0.6, stop=-1.2, length=101)
line_to(ctx, x, get_terrain_height(x))
end
close_path(ctx)
Cairo.fill(ctx)
restore(ctx)
ctx
end
function render_mountain_car!(ctx::CairoContext, car::MountainCar, color::Colorant=colorant"black"; car_radius::Float64=0.05)
Cairo.save(ctx)
Cairo.translate(ctx, car.x, get_terrain_height(car.x) + car_radius)
Cairo.arc(ctx, 0.0, 0.0, car_radius, 0, 2pi)
set_source_rgba(ctx, color)
Cairo.fill(ctx)
restore(ctx)
ctx
end
function render_position_overlay!(ctx::CairoContext, car::MountainCar, color::Colorant=colorant"black"; car_radius::Float64=0.05)
Cairo.save(ctx)
# draw horizontal position line
set_source_rgba(ctx, color)
set_line_width(ctx, 2.0) # [pixels]
y = get_terrain_height(car.x) + car_radius
move_to(ctx, car.x, y)
# line_to(ctx, 0.0, y)
move_to(ctx, 0.0, y-0.04)
# line_to(ctx, 0.0, y+0.04)
# Cairo.stroke(ctx)
restore(ctx)
# deal with the inverted y-axis issue for text rendered
mat = get_matrix(ctx)
mat2 = [mat.xx mat.xy mat.x0;
mat.yx mat.yy mat.y0]
Cairo.save(ctx)
reset_transform(ctx)
select_font_face(ctx, "Sans", Cairo.FONT_SLANT_NORMAL, Cairo.FONT_WEIGHT_NORMAL)
set_font_size(ctx, 35)
set_source_rgba(ctx, color)
# position line label (handle center alignment)
text_x = car.x
text_y = y + 0.13
pos = mat2*[text_x text_y 1.0]'
text = @sprintf("x = %7.2f", car.x)
extents = Cairo.text_extents(ctx, text)
pos[1] -= (extents[3]/2 + extents[1])
pos[2] -= (extents[4]/2 + extents[2])
move_to(ctx, pos[1], pos[2])
show_text(ctx, text)
# speed label
text_x = car.x
text_y = y + 0.08
pos = mat2*[text_x text_y 1.0]'
text = @sprintf("v = %+7.2f", car.v)
extents = Cairo.text_extents(ctx, text)
pos[1] -= (extents[3]/2 + extents[1])
pos[2] -= (extents[4]/2 + extents[2])
move_to(ctx, pos[1], pos[2])
show_text(ctx, text)
restore(ctx)
ctx
end
function render_action_overlay!(ctx::CairoContext, car::MountainCar, action::Symbol, color::Colorant=colorant"maroon", car_radius::Float64=0.05)
# Draw an arrow pointing to the direction of the action
# Determine arrow direction based on action
if action == :left
angle = pi
elseif action == :right
angle = 0
else
return ctx
end
Cairo.save(ctx)
set_source_rgba(ctx, color)
Cairo.translate(ctx, car.x, get_terrain_height(car.x) + car_radius)
# Draw arrow
arrow_length = 0.12
arrow_width = 0.04
move_to(ctx, 0, 0)
set_line_width(ctx, 8.0)
line_to(ctx, arrow_length * cos(angle)*0.98, arrow_length * sin(angle)*0.98)
Cairo.stroke(ctx)
move_to(ctx, arrow_length * cos(angle), arrow_length * sin(angle))
line_to(ctx, (arrow_length - arrow_width) * cos(angle - pi/12), (arrow_length - arrow_width) * sin(angle - pi/12))
line_to(ctx, (arrow_length - arrow_width) * cos(angle + pi/12), (arrow_length - arrow_width) * sin(angle + pi/12))
close_path(ctx)
Cairo.fill(ctx)
restore(ctx)
ctx
end
function render_reward!(ctx::CairoContext, reward::Float64, color::Colorant=colorant"black")
# deal with the inverted y-axis issue for text rendered
mat = get_matrix(ctx)
mat2 = [mat.xx mat.xy mat.x0;
mat.yx mat.yy mat.y0]
Cairo.save(ctx)
reset_transform(ctx)
select_font_face(ctx, "Sans", Cairo.FONT_SLANT_NORMAL, Cairo.FONT_WEIGHT_NORMAL)
set_font_size(ctx, 45)
set_source_rgba(ctx, color)
# position line label (handle center alignment)
text_x = -0.3
text_y = 0.5
pos = mat2*[text_x text_y 1.0]'
text = @sprintf("R = %7.3f", reward)
extents = Cairo.text_extents(ctx, text)
pos[1] -= (extents[3]/2 + extents[1])
pos[2] -= (extents[4]/2 + extents[2])
move_to(ctx, pos[1], pos[2])
show_text(ctx, text)
restore(ctx)
ctx
end
function render_mountain_car(car::MountainCar, color::Colorant=colorant"blue";
canvas_width::Int = 1440,
canvas_height::Int = 800,
camera_pos::Tuple{Float64,Float64} = (-0.3,0.5),
camera_zoom::Float64 = 800.0,
render_pos_overlay::Bool = false,
reward::Float64=NaN,
action::Symbol=:none
)
s, ctx = get_canvas_and_context(canvas_width, canvas_height, camera_zoom=camera_zoom, camera_pos=camera_pos)
render_terrain!(ctx)
render_mountain_car!(ctx, car, color)
if render_pos_overlay
render_position_overlay!(ctx, car)
end
if !isnan(reward)
render_reward!(ctx, reward)
end
if action != :none
render_action_overlay!(ctx, car, action)
end
s
end
function get_canvas_and_context(canvas_width::Int, canvas_height::Int;
camera_pos::Tuple{Float64,Float64} = (0.0,0.0), # [m]
camera_zoom::Float64 = 0.5, # [pix/m]
)
s = CairoRGBSurface(canvas_width, canvas_height)
ctx = creategc(s)
# clear background
set_source_rgb(ctx, 1.0,1.0,1.0)
paint(ctx)
# reset the transform such that (0,0) is the middle of the image
reset_transform(ctx)
Cairo.translate(ctx, canvas_width/2, canvas_height/2) # translate to image center
Cairo.scale(ctx, camera_zoom, -camera_zoom ) # [pix -> m]
Cairo.translate(ctx, -camera_pos[1], -camera_pos[2]) # translate to camera location
(s, ctx)
end