forked from fabricerosay/AGZ0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselfplay.jl
630 lines (553 loc) · 16.8 KB
/
selfplay.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
@inline function pitNetwork(
p1,
p2,
n,
temptresh,
affichage = 50;
usepuct = false,
vanille = (false, 100),
)
v = 0
nul = 0
d = 0
reward = 0
f = 0
testmode!(p1, true)
testmode!(p2, true)
net2 = conv2h_predictor(p2)
if usepuct
puct2 = TreePolicy("puct", true, 0, 0.3, 0, 1, 0, p2,4)
#puct1=TreePolicy("puct",true,0,0.3,0,1,0,p1)
end
if vanille[1]
net1 = x->vanilla(x,vanille[2])
elseif usepuct
net1 = TreePolicy("puct",true,0,0.3,0,1,0,p1,4)
else
net1 = conv2h_predictor(p2)
end
for i = 1:n
current = GameEnv((-1)^i)
if i % affichage == 0
println("game $i", (v, nul, d))
end
k = 0
#lp = legalPlays(current)
while !winner(current)[1]
if current.player == 1
if vanille[1]
q = net1(current)[1]
elseif usepuct
q = net1(current,100)[1]
else
q=net1(current)[1]
end
if k < temptresh
if vanille[1] || usepuct
moveIndex = getAction(sample(1:maxActions, Weights(q)))
else
moveIndex = moveIndex = findIndex(
current,
sample(1:getActionNumber(current), Weights(q)),
)
end
else
if vanille[1] || usepuct
moveIndex = getAction(argmax(q))
else
#
moveIndex = findIndex(current, argmax(q))
end
end
k += 1
else
if !usepuct
q = net2(current)[1]
else
q = puct2(current, 100)[1]
end
if k < temptresh
if !usepuct
moveIndex = findIndex(
current,
sample(1:getActionNumber(current), Weights(q)),
)
else
moveIndex = getAction(sample(1:maxActions, Weights(q)))
end
else
if !usepuct
moveIndex = findIndex(current, argmax(q))
else
moveIndex = getAction(argmax(q))
end
end
k += 1
end
current = playIndex(current, moveIndex)
end
finished = winner(current)[2]
if finished == -1
v += 1
elseif finished == 0
nul += 1
else
d += 1
end
end
testmode!(p1, :auto)
testmode!(p2, :auto)
return v, nul, d
end
#############play games till having n samples of data, workers is the number of games in parallel (on the gpu) #####
############# could also multithread but sometimes it gets instable and doesn't add that much speed ########
function batchedSelfplay(
rollout,
n,
workers,
net,
temptresh,
λ,
cpuct,
value = true,
root = nothing,
)
r = []
resglobal = [0, 0, 0]
rtemp = [[] for k = 1:workers]
rtempbis = [[] for k = 1:workers]
tempstate = zeros(sizeInput..., workers)######## need a hack to automate this
i = 1
current =[GameEnv() for k in 1:workers]# [playIndex(GameEnv(),getAction(sample(1:NN,Weights(root[1])))) for k = 1:workers]
#current=vcat([GameEnv() for k in 1:div(workers,2)],[playIndex(GameEnv(),rand(1:maxActions)) for k in 1:div(workers,2)])
k = zeros(workers)
step = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#### moins de bruit dirichlet ######
puct = TreePolicy("puct", true, 0.25, 1, 0, cpuct, 0, net,4)
puctfast = TreePolicy("puct", true, 0, 1, 0, cpuct, 0, net,16)
cpt = 0
logsample = true
res = [(false, 0) for k = 1:workers]
tour = 1
testmode!(net, true)
while true
#logsample = rand() < 0.25
# for j in 1:workers
# tempstate[:,j].=decoder(current[j])[:,:,:,1]
# end
# qplay=softmax(net(tempstate|>gpu)[1])|>cpu
##### oscillatorycap selfplay à la katago sofar best way was following ExIt ie follow policy not puct
tour += 1
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
if logsample
qv = puct(current, rollout)
else
qv=puctfast(current,200)
end
for j = 1:workers
if k[j] < temptresh
# lp=[getNumber(el) for el in legalPlays(current[j])]
# moveIndex=sample(lp,Weights(qplay[:,j][lp]))
moveIndex = getAction(sample(1:maxActions, Weights(qv[j][1])))
else
# lp=[getNumber(el) for el in legalPlays(current[j])]
# moveIndex=lp[argmax(qplay[:,j][lp])]
moveIndex = getAction(argmax(qv[j][1]))
end
k[j] += 1
if logsample
push!(
rtemp[j],
(
decoder(current[j]),
(qv[j][1], qv[j][2], current[j].player),
),
)
end
####### Randomize starting positions good ? ##########
if k[j]<=4
moveIndex=findIndex(current[j],rand(1:getActionNumber(current[j])))
end
current[j] = playIndex(current[j], moveIndex)
res[j] = winner(current[j])
end
j = 1
while j <= length(current)
if res[j][1]
resglobal[res[j][2]+2] += 1
for pos in rtemp[j]
x, (y, z, w) = pos
push!(rtempbis[j], (x, (y, 0.5*z+0.5*w*res[j][2])))
end
cpt += 1
if cpt / n * 100 > step[i]
println("avancement ", cpt / n * 100, "%")
i += 1
end
if n - cpt > workers - 1
rtemp[j] = []
res[j] = (false, 0)
k[j] = 0
#coup=getAction(sample(1:81,Weights(root[1])))
current[j] = GameEnv()# playIndex(GameEnv(),coup)
j+=1
else
deleteat!(rtemp, j)
deleteat!(res, j)
deleteat!(k, j)
deleteat!(current, j)
end
if cpt >= n
r = reduce(vcat, rtempbis)
# for k in 1:div(n,81)
# push!(
# r,
# (decoder(GameEnv()), (root[1]/sum(root[1]), root[2]))
# )
# end
println("résultat global", resglobal)
testmode!(net, :auto)
return r
end
else
j += 1
end
end
workers = length(current)
end
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
return r
end
function trainingPipeline(
net,
r = nothing,
value = true;
nbworkers = 100,
game = "UTTT",
bufferSize = 500000,
samplesNumber = 1000,
rollout = 1000,
cpuct = 2,
iteration = 100,
chkfrequency = 1,
batchsize = 512,
lr = 0.001,
epoch = 1,
temptresh = 12,
)
if r == nothing
r = []
train = false
else
train=true
end
trainingnet = deepcopy(net)
vanillarollout = 100
root = (zeros(NN),0)
if Sys.free_memory() / 2^20 < 300
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
t = 4
for i = 1:iteration
# nr,nv=vanilla(GameEnv(),100000)
# root=(root[1].+nr,(((i-1)*root[2]+nv)/i))
println("iteration: $i")
# println("état racine")
# show(stdout, "text/plain",reshape(root[1]/sum(root[1]),(N,N)))
# println("valeur départ", root[2])
# show(stdout, "text/plain",reshape(root.actionsN,(9,9)))
λ = 0#σ(2*(i/iteration-0.7))
test_position = batchedSelfplay(
rollout,
samplesNumber,
nbworkers,
net,
temptresh,
λ,
cpuct,
value,
root,
)
r = vcat(r, test_position)
if length(r) > bufferSize
r = r[length(r)-bufferSize:end]
end
# if i%2==0
# #rollout=min(rollout+50,800)
# t=min(t+1,20)
# end
if i % 1 == 0
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
traininPipe(
batchsize,
trainingnet,
r,
epoch = epoch,
lr = lr,
value = value,
βloss=1
)
end
if i % chkfrequency == 0
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
p = deepcopy(test_position)
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
tag = (i-1) % 100+1
@save pwd() * "/Games/" * game * "/Data/data$tag.json" p
p = []
end
if i % 1== 0
duel = pitNetwork(net, trainingnet, 40, temptresh,usepuct=true)
duel2 = pitNetwork(
net,
trainingnet,
40,
temptresh,
vanille = (true, vanillarollout),
usepuct=false
)
print("résultat contre mcts $vanillarollout", duel2)
if duel2[1] > duel2[3]
vanillarollout += 100
end
print("résultat du duel: ", duel)
if duel[1] > duel[3]
net = deepcopy(trainingnet)
end
end
reseau = trainingnet |> cpu
if i % chkfrequency == 0
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
#tag = div(i,4)
@save pwd() * "/Games/" * game * "/Data/reseau$i.json" reseau
end
if i >= 1
bufferSize = min(1000000, Int(round(bufferSize * 1.1)))
end
end
end
function recoverData(start, finish, game, plus = "")
r = []
for i = start:finish
@load pwd() * "/Games/" * game * "/Data/data" * plus * "$i.json" p
r = vcat(r, p)
end
return r
end
function bootstrapSelfplay(rollout, n, temptresh)
r = []
rtemp = []
i = 1
step = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
puct = TreePolicy("classic", true, 0.25, 0.3, 0, 1, 0, nothing)
cpt = 0
current = GameEnv()
res = winner(current)
k = 0
logsample=true
for j = 1:n
while !res[1]
if rand()<2
logsample=true
qv = puct(current, rollout)
else
logsample=false
qv = puct(current, 100)
end
if k < temptresh
moveIndex = getAction(sample(1:maxActions, Weights(qv[1])))
else
moveIndex = getAction(argmax(qv[1]))
end
k += 1
if logsample
push!(rtemp, (decoder(current), (qv[1], qv[2])))
end
current = playIndex(current, moveIndex)
res = winner(current)
end
for pos in rtemp
x, (y, w) = pos
push!(r, (x, (y, w)))
end
current = GameEnv()
res = winner(current)
k = 0
rtemp = []
end
return r
end
function bootstrapSelfplayParallel(rollout, n, temptresh, workers)
r = [[] for i = 1:workers]
@threads for i = 1:workers
r[i] = vcat(r[i], bootstrapSelfplay(rollout, n, temptresh))
end
for i = 1:workers
tag = i
p = r[i]
@save pwd() * "/Games/UTTT/Data/datavanilla$tag.json" p
end
end
function trainingPipelinePBT(
net,
r = nothing,
value = true;
nbworkers = 100,
game = "UTTT",
bufferSize = 500000,
samplesNumber = 1000,
rollout = 1000,
cpuct = 2,
iteration = 100,
chkfrequency = 1,
batchsize = 512,
lr = 0.001,
epoch = 1,
temptresh = 4,
npopulation=8
)
multiplie=[0.8,1.2]
trainingnets = [deepcopy(net) for i in 1:npopulation]
lr=[rand(multiplie)*0.01 for k in 1:npopulation ]
βloss=[rand(multiplie) for k in 1:npopulation ]
r=[]
v=collect(1:npopulation)
vanillarollout=100
λ=0
for i = 1:iteration
println("iteration: $i")
# test_position =batchedSelfplay(
# rollout,
# samplesNumber,
# nbworkers,
# trainingnets[v[end]],
# temptresh,
# λ,
# cpuct,
# value,
# nothing,
# )
indice=i
test_position=recoverData(indice,indice,"Reversi")
r = vcat(r, test_position)
if length(r) > bufferSize
r = r[length(r)-bufferSize:end]
end
# if i%2==0
# #rollout=min(rollout+50,800)
# t=min(t+1,20)
# end
if (i % 1 == 0 || train)
for k in 1:npopulation
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
traininPipe(
batchsize,
trainingnets[k],
r,
epoch = epoch,
lr = lr[k],
value = value,
βloss = βloss[k]
)
end
end
if i % chkfrequency == 0
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
p = deepcopy(test_position)
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
tag = i % 20
@save pwd() * "/Games/" * game * "/Data/data$i.json" p
p = []
end
if i % 1 == 0
scores=zeros(8)
for j in 1:7
for k in j+1:8
duel = pitNetwork(trainingnets[k], trainingnets[j], 40,temptresh,usepuct=false)
scores[j]+=duel[1]-duel[3]
scores[k]+=duel[3]-duel[1]
end
end
# if i % 1 == 0
# duel2 = pitNetwork(
# net,
# trainingnet,
# 50,
# temptresh,
# vanille = (true, vanillarollout),
# usepuct=true
# )
# print("résultat contre mcts $vanillarollout", duel2)
# end
println("résultat du duel: ", scores)
v=sortperm(scores)
duel=pitNetwork(trainingnets[end], trainingnets[v[end]], 40, temptresh,vanille=(true,vanillarollout),usepuct=false)
println("contre mcts $vanillarollout",duel)
if duel[1]>duel[3]
vanillarollout += 100
end
for k in 1:4
trainingnets[v[k]]=deepcopy(trainingnets[v[end]])
lr[v[k]]=rand(multiplie)*lr[v[end]]
βloss[v[k]]=rand(multiplie)*lr[v[end]]
end
println("meilleurs params so far, lr=",lr[v[end]]," βloss ",βloss[v[end]])
end
#
# if duel[1] > duel[3]
# net = deepcopy(trainingnet)
# end
#
reseau = trainingnets[v[end]] |> cpu
if i % chkfrequency == 0
if Sys.free_memory() / 2^20 < 700
println("memory reclaim")
GC.gc()
CuArrays.reclaim()
end
@save pwd() * "/Games/" * game * "/Data/reseau$i.json" reseau
end
if i >= 1
bufferSize = min(500000, Int(round(bufferSize * 1.1)))
end
end
#return trainingnets[v[end]]
end