Skip to content

Commit 813c9c7

Browse files
committed
Fixup small things
- Fix coloring in flocking demo - Remove echo of PATH on python setup - Add learning parameters to traffic RL demo
1 parent a2d671f commit 813c9c7

3 files changed

Lines changed: 89 additions & 35 deletions

File tree

demos/Flocking Clusters.nlogo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ to go
3535
let clusters py:runresult "cl.dbscan(np.array(coords), eps)"
3636
(foreach (sort turtles) (item 1 clusters) [ [t c] ->
3737
ask t [
38-
set color item ((c + 1) mod 14) base-colors
38+
set color item ((c mod 13) + 1) base-colors
3939
]
4040
])
4141
tick

demos/Traffic Basic - Reinforcement.nlogo

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ globals [
55
speed-limit
66
speed-min
77
inputs
8+
loss
89
]
910

1011
turtles-own [
@@ -38,16 +39,17 @@ to setup
3839
py:set "state_dims" length inputs
3940
py:set "hl_size" 36
4041
py:set "num_actions" 3
41-
py:set "memory_size" 10000
42-
py:set "batch_size" 128
42+
py:set "memory_size" memory-size
43+
py:set "batch_size" batch-size
44+
py:set "lr" learning-rate
4345

4446
(py:run
4547
"model = Sequential()"
4648
"model.add(Dense(hl_size, input_shape=(state_dims,), activation='relu'))"
4749
"model.add(Dense(hl_size, activation='relu'))"
4850
"model.add(Dense(hl_size, activation='relu'))"
4951
"model.add(Dense(num_actions))"
50-
"optimizer = optimizers.adam()"
52+
"optimizer = optimizers.adam(lr=lr)"
5153
"model.compile(optimizer, 'mse')"
5254
"model.summary()"
5355
"memory = []")
@@ -107,10 +109,13 @@ to go
107109
if speed < speed-min [ set speed speed-min ]
108110
if speed > speed-limit [ set speed speed-limit ]
109111
fd speed
110-
set reward (log (speed + 0.0000000000005) 2)
112+
set reward (log (speed + 1e-8) 2)
111113
;set reward speed
112114
]
113-
if train? [ train ]
115+
if train? [
116+
remember
117+
train
118+
]
114119
tick
115120
end
116121

@@ -131,14 +136,18 @@ to select-actions
131136
])
132137
end
133138

134-
to train
139+
to remember
135140
ask turtles [ set next-state map runresult inputs ]
136141
let data [ (list state action reward next-state) ] of turtles
137142
py:set "new_exp" data
138143
(py:run
139144
"memory.extend(new_exp)"
140145
"if len(memory) > memory_size:"
141-
" memory = memory[-memory_size:]"
146+
" memory = memory[-memory_size:]")
147+
end
148+
149+
to train
150+
(py:run
142151
"sample_ix = np.random.randint(len(memory), size = batch_size)"
143152
"inputs = np.array([memory[i][0] for i in sample_ix])"
144153
"actions = np.array([memory[i][1] for i in sample_ix])"
@@ -179,10 +188,10 @@ end
179188
; See Info tab for full copyright and license.
180189
@#$#@#$#@
181190
GRAPHICS-WINDOW
182-
10
183-
405
184-
898
185-
569
191+
15
192+
425
193+
903
194+
589
186195
-1
187196
-1
188197
17.255
@@ -207,9 +216,9 @@ ticks
207216

208217
BUTTON
209218
110
210-
55
219+
160
211220
182
212-
96
221+
201
213222
NIL
214223
setup
215224
NIL
@@ -224,9 +233,9 @@ NIL
224233

225234
BUTTON
226235
193
227-
56
236+
161
228237
264
229-
96
238+
201
230239
NIL
231240
go
232241
T
@@ -256,9 +265,9 @@ HORIZONTAL
256265

257266
SLIDER
258267
15
259-
135
268+
240
260269
265
261-
168
270+
273
262271
deceleration
263272
deceleration
264273
0
@@ -271,9 +280,9 @@ HORIZONTAL
271280

272281
SLIDER
273282
15
274-
100
283+
205
275284
265
276-
133
285+
238
277286
acceleration
278287
acceleration
279288
0
@@ -295,7 +304,7 @@ speed
295304
0.0
296305
300.0
297306
0.0
298-
1.1
307+
1.0
299308
true
300309
false
301310
"" ""
@@ -306,9 +315,9 @@ PENS
306315

307316
BUTTON
308317
15
309-
55
318+
160
310319
97
311-
95
320+
200
312321
NIL
313322
setup-tf
314323
NIL
@@ -323,9 +332,9 @@ NIL
323332

324333
SLIDER
325334
15
326-
240
335+
345
327336
265
328-
273
337+
378
329338
discount
330339
discount
331340
0
@@ -338,14 +347,14 @@ HORIZONTAL
338347

339348
SLIDER
340349
15
341-
205
350+
310
342351
265
343-
238
352+
343
344353
exploration-rate
345354
exploration-rate
346355
0
347356
1
348-
0.05
357+
0.01
349358
0.01
350359
1
351360
NIL
@@ -362,7 +371,7 @@ NIL
362371
0.0
363372
300.0
364373
0.0
365-
10.0
374+
20.0
366375
true
367376
false
368377
"" ""
@@ -373,9 +382,9 @@ PENS
373382

374383
SLIDER
375384
15
376-
170
385+
275
377386
265
378-
203
387+
308
379388
stop-penalty
380389
stop-penalty
381390
0
@@ -388,15 +397,60 @@ HORIZONTAL
388397

389398
SWITCH
390399
15
391-
275
400+
380
392401
117
393-
308
402+
413
394403
train?
395404
train?
396405
0
397406
1
398407
-1000
399408

409+
SLIDER
410+
15
411+
50
412+
265
413+
83
414+
memory-size
415+
memory-size
416+
0
417+
100000
418+
10000.0
419+
1000
420+
1
421+
NIL
422+
HORIZONTAL
423+
424+
SLIDER
425+
15
426+
85
427+
265
428+
118
429+
batch-size
430+
batch-size
431+
0
432+
1024
433+
128.0
434+
32
435+
1
436+
NIL
437+
HORIZONTAL
438+
439+
SLIDER
440+
15
441+
120
442+
265
443+
153
444+
learning-rate
445+
learning-rate
446+
0
447+
0.01
448+
0.001
449+
0.0001
450+
1
451+
NIL
452+
HORIZONTAL
453+
400454
@#$#@#$#@
401455
## WHAT IS IT?
402456

src/PythonExtension.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ object PythonSubprocess {
4646
// When running language tests, prefix is blank and, in general, processes can't run in non-existent directories.
4747
// So we default to the home directory.
4848
val workingDirectory = if (prefix.exists) prefix else new File(System.getProperty("user.home"))
49-
val pb = new ProcessBuilder(cmd(pythonCmd, pyScript, port).asJava) .directory(workingDirectory)
49+
val pb = new ProcessBuilder(cmd(pythonCmd, pyScript, port).asJava).directory(workingDirectory)
5050
val proc = try {
5151
pb.start()
5252
} catch {
@@ -77,7 +77,7 @@ object PythonSubprocess {
7777
// neuteredPATH. If so, we want to execute with the users actual PATH. We use `path_helper` to get that. It's not
7878
// perfect; it will miss PATHs defined in certain files, but hopefully it's good enough.
7979
List("/bin/bash", "-c",
80-
s"eval $$(/usr/libexec/path_helper -s) ; echo $$PATH ; '$pythonCmd' '$pythonScript' $port")
80+
s"eval $$(/usr/libexec/path_helper -s) ; '$pythonCmd' '$pythonScript' $port")
8181
else
8282
List(pythonCmd, pythonScript, port.toString)
8383
cmd

0 commit comments

Comments
 (0)