-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibers.rb
More file actions
593 lines (436 loc) · 9.94 KB
/
Copy pathFibers.rb
File metadata and controls
593 lines (436 loc) · 9.94 KB
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
#!/usr/local/bin/ruby
require 'fiber'
class Fiber
def []= arg, value
@hash = {} if @hash.nil?
@hash[arg] = value
end
def [] arg
@hash = {} if @hash.nil?
@hash[arg]
end
def inc_yields
@yields = 0 if @yields.nil?
@yields += 1
end
def yields
@yields = 0 if @yields.nil?
@yields
end
class << self
alias :prev_yield :yield
def init
@root_fiber = Fiber.current
$logger.info "Root fiber: #{ @root_fiber }"
end
def yield
if Fiber.current != @root_fiber
Fiber.current.inc_yields
prev_yield
end
end
def current?
Fiber.current == @root_fiber
end
end
end
#
# Following definitions used in Region
#
class WorkerFiber
attr_accessor :status
def now
Timer.now
end
def resume
start = now
@fiber.resume
diff = now - start
if @max_resume < diff
@max_resume = diff
end
end
def initialize name, region, list
@name = name
@region = region
@list = list
@turn = 0
@count = 0
@status = :init
@max_resume = 0.0
@fiber = Fiber.new do
run_fiber
end
@fiber[ :name ] = name
end
def stats
[ @name, @status.to_s, @count, @fiber.yields, (@max_resume*1000).to_i, list.length ]
end
def run_fiber
begin
longest_diff = nil
longest_count = nil
doing = true
while doing
$logger.info "waiting"
while list.length == 0
@status = :waiting
Fiber.yield
end
count = 0
start = now
@status = :running
init_loop
while list.length > 0
# Handle next item
action list.pop
count += 1
@count += 1
Fiber.yield
end
doing = !done_loop
$logger.info {
diff = ( (now - start)*1000 ).to_i
if longest_diff.nil? or diff > longest_diff
longest_diff = diff
longest_count = count
end
str = " Longest: #{ longest_count } in #{ longest_diff } msec"
"added #{ count } results in #{ diff} msec. #{ str }"
}
end
@status = :done
$logger.info "closing down."
end rescue $logger.info( "Boom! #{ $! }\n #{ $!.backtrace }" )
end
def list
@list
end
def init_loop
end
def done_loop
false
end
end
module WorkerQueue
# This is the way to include class methods from a mixin module.
# Amazingly, I can't find this call in the ruby docs.
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
# Note that we are using class instance variables,
# not class static variables!
def init
@list = [] if @list.nil?
@q_cache= {} if @q_cache.nil?
end
#
# return true if item added, false otherwise
#
def add_list item
init
if @q_cache[ item]
$logger.info { "#{ name } #{item} already queued" }
false
else
@q_cache[ item] = true
@list << item
true
end
end
def list
init
@list
end
end
end
class Fiber1 < WorkerFiber
include WorkerQueue
def initialize
super("fiber1", $region, Fiber1.list)
end
def init_loop
@new_count, @known_count, @replaced_count = 0, 0, 0
end
def action path
return if path.length == 0
$logger.info { "saving path: #{ path }" }
# Cache the result
new_tmp, known_tmp, replaced_tmp = @region.set_path path
@new_count += new_tmp
@known_count += known_tmp
@replaced_count += replaced_tmp
new_tmp, known_tmp, replaced_tmp = @region.set_path path.reverse
@new_count += new_tmp
@known_count += known_tmp
@replaced_count += replaced_tmp
end
def done_loop
$logger.info {
"new: #{ @new_count}, known: #{ @known_count }, replaced: #{ @replaced_count }"
}
false
end
end
#
# Fiber to preprocess and filter searches for the two search threads,
# to offload the computation from the main program.
#
# Gains are absolutely minimal, but what the hey. Every msec counts.
#
class SelectSearch < WorkerFiber
include WorkerQueue
def initialize
super("select", $region, SelectSearch.list )
end
def action item
from, to_list, do_shortest, max_length =item
sq_ants = Region.ants_to_squares to_list
$logger.info {
"handling search #{ from }-#{ sq_ants }, " +
"#{ do_shortest }, #{max_length}"
}
# Don't bother with empty input
return if from.nil? or from.region.nil?
return if sq_ants.nil? or sq_ants.length == 0
# Convert input to regions
from_r = from.region
to_list_r = Region.squares_to_regions sq_ants
to_list_r.compact!
# Don't bother with targetting from-region
to_list_r.delete from_r
if to_list_r.length == 0
$logger.info "to_list_r empty or same as from, not searching."
return
end
to_list_r.sort! # To make comparing list easier downstream
item = [ from_r, to_list_r, do_shortest, max_length]
$logger.info { "item #{ item }" }
if not max_length.nil? and max_length == -1
$logger.info { "Doing big search" }
BigSearch.add_list item
else
Fiber2.add_list item
end
end
end
class BigSearch< WorkerFiber
include WorkerQueue
def initialize
super("BigSearch", $region, BigSearch.list )
end
def action item
from_r, to_list_r, do_shortest, max_length = item
$logger.info { "searching regions #{ from_r }-#{ to_list_r }" }
# Results will be cached within this call
@region.find_paths from_r, to_list_r, do_shortest, max_length
end
end
class Fiber2 < WorkerFiber
include WorkerQueue
def initialize
super("fiber2", $region, Fiber2.list)
end
def action item
from_r, to_list_r, do_shortest, max_length = item
$logger.info { "searching regions #{ from_r }-#{ to_list_r }" }
# Results will be cached within this call
tmp = @region.find_paths from_r, to_list_r, do_shortest, max_length
if tmp.nil?
$logger.info "No results; retrying search as big search"
BigSearch.add_list [ from_r, to_list_r, do_shortest, -1 ]
end
end
end
class RegionsFiber < WorkerFiber
include WorkerQueue
def initialize
super("regions", $region, RegionsFiber.list)
end
def action source
$logger.info { "finding regions for #{ source }" }
@region.find_regions source
Fiber.yield
$patterns.fill_map source
end
end
class PatternsFiber < WorkerFiber
include WorkerQueue
def initialize
# NB: Patterns instance passed to instance var named @region!
# This is a potential source of confusion.
super("patterns", $patterns, PatternsFiber.list)
$patterns.init_tasks
end
# Confusion resolver.
def patterns
@region
end
def action square
# Only handle last square added with known region, ignore
# rest of queue
unless square.done_region
while square = @list.pop
break if square.done_region
end
end
@list.clear
return if square.nil?
$logger.info { "Got square #{ square}" }
patterns.show_field square
patterns.match_tests square
end
def done_loop
if patterns.all_confirmed
$logger.info "No more open tests; yay, we're done!"
true
else
false
end
end
end
class WalkFiber < WorkerFiber
# WorkerQueue intentionally not mixed in.
@@list = []
def self.add_list item
@@list << item
end
def initialize
super("walk", $region, @@list)
end
def action item
from, to = item
$pointcache.set_walk from, to, to, true, true
end
end
class BorderPatrolFiber < WorkerFiber
# WorkerQueue not mixed in.
# This is intentional; there can be multiple 'go'
# instructions in the queue, these should not be filtered
# for uniqueness.
@@list = []
@@obj = BorderPatrol.new
@@found_hills = false
def self.add_list item
@@list << item
end
def initialize
super("borderpatrol", $region, @@list)
end
def action source
$logger.info { "doing action #{ source }" }
@@obj.clear_liaison source
@@obj.action if @@list.empty?
end
def self.init_hill_regions
return if @@found_hills
$ai.hills.each_friend do |sq|
@@found_hills = true
@@obj.add_hill_region sq
end
if @@found_hills
$logger.info "got the hills"
@@list << "go"
return
end
end
def self.request_target sq, skip_regions
@@obj.next_liaison sq, skip_regions
end
def self.clear_target sq
BorderPatrolFiber.add_list sq
end
def self.known_region sq
@@obj.known_region sq.region
end
def self.get_center region
@@obj.get_center region
end
def self.num_exits region
@@obj.num_exits region
end
end
class Fibers
def initialize
@list = []
self
end
def init_fibers
@list += [
SelectSearch.new,
Fiber2.new,
BigSearch.new,
BorderPatrolFiber.new,
Fiber1.new,
RegionsFiber.new,
PatternsFiber.new,
WalkFiber.new
]
self
end
def resume
list = @list.clone
while list.length > 0
list.clone.each { |f|
# Remove done fibers immediately
if f.status == :done
list.delete f
next
end
begin
# Counters give some breathing space to other
# fibers; SelectSearch and Fiber1 have a tendency to
# completely take over fiber run time.
if f.kind_of? SelectSearch
count = 0
begin
f.resume
if count == 0 and f.status == :running
$logger.info "giving priority 1 to SelectSearch"
end
count += 1
$ai.turn.check_time_limit
end while f.status == :running and count < 200
elsif f.kind_of? Fiber1
count = 0
begin
f.resume
if count == 0 and f.status == :running
$logger.info "giving priority 2 to Fiber1"
end
count += 1
$ai.turn.check_time_limit
end while f.status == :running and count < 100
else
f.resume
$ai.turn.check_time_limit
end
rescue FiberError
$logger.info(true) { "Thread probably died...." }
# Help it out of its misery
f.status = :done
list.delete f
next
end
if f.status == :waiting
list.delete f
next
end
}
#$logger.info { "End loop list length: #{ list.length }" }
end
#$logger.info { "End" }
end
Format1 = "%12s %8s %7s %7s %5s %7s\n"
Format = "%12s %8s %7d %7d %5d %7d\n"
def status
str = "Fibers:\n" +
Format1 % [ "Name ", "status ", " count", "yields", " max", "queue" ] +
Format1 % [ "============", "========", " =====", "======", "=====", "=====" ]
@list.each { |f|
str << Format % f.stats
}
str
end
end