-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootcamp.rb
605 lines (501 loc) · 26.2 KB
/
bootcamp.rb
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
require "./brains.rb"
require "./darwin.rb"
#This file contains a collection of classes which are used in the evolution of the neural networks.
#Nothing in here is needed to run a trained NN in rubywarrior.
#There are several classes which are used to run evolution on 'populations' in a number of ways.
#
#BootCamp - used to evolve a population of NN's to respond to a set of predefined inputs and outputs
#The predefined inputs/outputs are from AssaultCourse and DrillSergeant observes a NN's performance over the AssaultCourse
#
#CombatTraining - used to evolve NN's in a rubywarrior level
#The performance of the NN in the rubywarrior level is determined by Invigilator
#
#AgentTraining - used to evolve NN's over all levels in rubywarrior EPIC mode.
#requires epic unlocked.
#
#FieldTraining - used to evolve NN's over several/all rubywarrior levels.
#requires some setup and levels to be unlocked; A specific rubywarrior folder needs to be created for each level according to a naming convention see FieldTraining for more.
#
#Agent and Field use Threads to evaluate a NN's performance in all levels together, thier output gets a bit nutts at times ;)
#
#All four training grounds inherit from BasicTraining
#Provides some common methods for runnning evolution, saving/loading/autosaving populations to file and some getting/setter methods for the GA (population,muation_rate)
#
#Each of the four 'training grounds' set the args for a genetic alg and define the 'fitness function'. All follow the same basic format and only define an initialize method, ie;
#
# class BootCamp < BasicTraining
# def initialize n_layers
# #init vars
# @ga = MGA.new(:generations => 1000, :mutation_rate => 10, :gene_length => @gene_length, :fitness => Proc.new{|genome, gen|
# nn = Brains[n_layers-1].new(@nodes, genome)
# #code to test nn's performance and return its score
# })
# end
# end
#
#Training grounds take a single integer arg in thier initialize method which defines how many layers (n_layers) the NN should have; 1, 2 or 3.
#The :set_config_for method in BasicTraining defines how many nodes to used in each layer as sets @nodes.
#
#Brains (from brains.rb) simply indexes the three types of NN, 1, 2 and 3 layer, ie; Brians[0] returns a 1 layer NN Brain. see brains.rb for more.
#
#Usage
#
#bc = BootCamp.new(2)
#bc.train
#bc.save_pop "popname"
#bc.write_best #find best genome and save to genome file
#base class which all training grounds inherit.
class BasicTraining
require 'digest'
attr_accessor :ga, :nodes, :gene_length
#sets ups config vars used in training ground. n_layers defines number of layers for NN.
def set_config_for n_layers = 2
raise "Look, just no!" unless [1,2,3].include?(n_layers) #0 layers makes no sense, 4 is pointless as a 3 can do any math func (kinda).
@warrior_name = Dir.getwd.split("/").last.sub("-beginner", "")
@n_layers = n_layers
#define numbers of nodes. if changed, changes need to be reflected in player.rb
nodes_1_layer = {:in => 16, :out => 8}
nodes_2_layer = {:in => 16, :inner => 6, :out => 8}
nodes_3_layer = {:in => 16, :inner => 6, :inner2 => 6, :out => 8}
@nodes = [nodes_1_layer, nodes_2_layer, nodes_3_layer][@n_layers-1]
@gene_length = Brain.required_genome_length_for(nodes)
end
def write_best
genome = @ga.best
File.open("./genome",'w'){|f| f.write( genome.join(",") )}
end
def population
@ga.population
end
def population= new_population
@ga.population = new_population
end
def save_pop name = nil
PopBuilder.save_pop(@ga.population, name)
end
def load_pop f_name
pop = PopBuilder.load_pop(f_name)
@ga.population = pop
end
def build_pop_from pop_size = 30, genome_dir = "genomes by level"
builder = PopBuilder.new
builder.read_genomes genome_dir
pop = builder.make_pop(pop_size)
@ga.population = pop
@ga.popsize = pop.size
@ga
end
alias build_pop build_pop_from
def mutation_rate mutation_rate = nil
orig_m = @ga.mutation_rate
return orig_m if mutation_rate.nil?
@ga.mutation_rate = mutation_rate
puts "mutation rate changed; #{orig_m} => #{mutation_rate}"
end
def remark_on score
@target_score ||= 0
if score > @highest_score
@highest_score = score
print "\t\t<----BestSoFar"
elsif score == @highest_score && score >= @target_score
print "\t\t<----Combat Ready!!"
end
end
def reset_high_score
@highest_score = -10000
end
def graduate
training_grounds = [BootCamp, CombatTraining, AgentTraining, FieldTraining]
cur_pos = training_grounds.index(self.class)
return "No more training grounds, run write_best and go kick RW's ass!" if cur_pos >= training_grounds.size-1
new_training_groud = training_grounds[cur_pos.next].new(@n_layers)
new_training_groud.population = self.population.clone
new_training_groud
end
#calls evolve on the Genetic Algorithm in @ga.
#also provides functionality for auto saving the population during evolution. Really ugly method, really not important, just the start button.
def train use_new_name = false
if @auto_save_every_n_generations #can be set to an int to have the population written to file even n generations.
n = @auto_save_every_n_generations
i = @ga.generations/n
#workout what name to use to auto save population as. If use_name_name is true a new name will be used for that call to :train.
if use_new_name && !use_new_name.eql?(true) #not false, but not true; a string perhaps
@uniq_name = use_new_name
use_new_name = false
end
use_new_name = true unless @uniq_name
if use_new_name
d = Digest::MD5.new
d << @ga.instance_variables.map{|v| @ga.instance_variable_get(v) }.compact.join
@uniq_name = d.hexdigest
end
name = "current_pop_#{@nodes.size-1}layer-#{@nodes.values.join("-")}_#{self.class.to_s}_#{@uniq_name}"
i.times do
@ga.evolve(n)
print "\n\nran another #{n} generations. Saving population as #{name}...."
self.save_pop name
puts "done.\n\n"
end
else
@ga.evolve
end
end
alias run train
end
#BootCamp runs the evolution of a population of NNs over training examples from AssaultCourse.
class BootCamp < BasicTraining
attr_accessor :recruit
#n_layers - define which NN to use 1, 2 or 3 layered.
def initialize n_layers = 2
@auto_save_every_n_generations = 1000
set_config_for n_layers #=> sets @warrior_name, @n_layers, @nodes, @gene_length
@ga = MGA.new(:generations => 100000, :mutation_rate => 10, :gene_length => @gene_length, :popsize => 30, :fitness => Proc.new{|genome, gen|
print "#{gen} |"
@recruit = Brains[@n_layers-1].new(@nodes, genome) #initialize brain(neural net) with current genome.
d = DrillSergeant.new #initialize a DrillSergeant, a class used to score the brain response to a set of predefined inputs in AssaultCourse
d.recruit = @recruit #set the brain to be evaulated
#Basic walking drills - move in only available dir
d.test_recruit_on(AssaultCourse::BasicManuvers) #learn to walk
d.test_recruit_on(AssaultCourse::Retreat)
d.test_recruit_on(AssaultCourse::BasicAssault) #learn to attack in adjacent sqaures
unless d.score.include?(0)
d.test_recruit_on(AssaultCourse::Recovery ) #learn to recover when damaged
d.test_recruit_on(AssaultCourse::CloseQuaterCombat) #learn to attack enemy in closed spaces
d.test_recruit_on(AssaultCourse::AdvancedCombat) #learn to shoot and move toward distant targets
d.test_recruit_on(AssaultCourse::Rescue) #basic rescue - rescue captive in adjacent sqaures
end
message = "\t\t - Graduated BootCamp!" unless d.score.include?(0) #40
score = d.score.sum.to_i
#AssaultCourse.points.values.sum
print "\t\t- #{score}"
print message if message
puts " "
score
})
end
end
#CombatTraining runs the evolution of a population of NNs in the current level of rubywarrior.
class CombatTraining < BasicTraining
def initialize n_layers = 2
@auto_save_every_n_generations = 500
set_config_for n_layers #=> sets @warrior_name, @n_layers, @nodes, @gene_length
reset_high_score
@ga =MGA.new(:generations => 5000, :mutation_rate => 2, :gene_length => @gene_length, :fitness => Proc.new{|genome, gen|
print "#{gen}"
File.open("./genome", 'w'){|f| f.write( genome.join(",") )} #write the genome to file which Player will use
invigilator = Invigilator.new(@warrior_name) #invigilator class examins output from rubywarrior and assigns points for various actions. Invigilator#score_results == the fitness function
results = `rubywarrior -t 0 -s` #run runywarrior
#use invigilator to get the final score. Also returns the break down of points for displaying.
score, level_score, level_total, n_turns, turn_score, time_bonus, clear_bonus = invigilator.score_results(results)
print " | levelscore: #{level_score} | turnscore: #{turn_score.round(2)} | bonus(t:c): #{time_bonus}:#{clear_bonus} | turns: #{n_turns} | Total: #{level_total} | fitnes: #{score.round(2)}"
remark_on score
puts "."
score
})
end
end
#AgentTrainingruns the evolution of a population of NNs over all the levels of rubywarrior in epic mode.
#Only available once passed epic mode.
class AgentTraining < BasicTraining
def initialize n_layers = 2
@auto_save_every_n_generations = 100
@target_score = 842
set_config_for n_layers #=> sets @warrior_name, @n_layers, @nodes, @gene_length
reset_high_score
@ga =MGA.new(:generations => 5000, :mutation_rate => 2, :gene_length => @gene_length, :cache_fitness => true, :fitness => Proc.new{|genome, gen|
puts "#{gen}\n"
genome_file = "./genome"
File.open(genome_file,'w'){|f| f.write( genome.join(",") )}
score_sum = 0
threads = []
levels = [1,2,3,4,5,6,7,8,9]
levels.each do |lvl|
threads << Thread.new{
results = `rubywarrior -t 0 -s -l #{lvl}`
invigilator = Invigilator.new
score, level_score, level_total, n_turns, turn_score, time_bonus, clear_bonus = invigilator.score_results results
puts "Level#{lvl} | levelscore: #{level_score} | turnscore: #{turn_score.round(2)} | bonus(t:c): #{time_bonus}:#{clear_bonus} | turns: #{n_turns} | Total: #{level_total} | fitnes: #{score.round(2)}"
instance_variable_set("@ans#{lvl}", score)
}
end
threads.each{|t| t.join}
score_sum = levels.map{|lvl| instance_variable_get("@ans#{lvl}")}.compact.sum
puts "\n\t==Summed Score #{score_sum}"
remark_on score_sum
puts "."
score_sum
})
end
end
#FieldTraining runs the evolution of a population of NNs in each level of rubywarrior (non-epic)
#Requires some setup. Needs a rubywarrior dir setup for each level named levelxbot where x is the level number.
class FieldTraining < BasicTraining
attr_accessor :levels, :level_weight
def initialize n_layers = 2
@auto_save_every_n_generations = 100
@target_score = 200
set_config_for n_layers #=> sets @warrior_name, @n_layers, @nodes, @gene_length
reset_high_score
@initial_dir = Dir.getwd
@levels = [1,2,3,4,5,6,7,8,9]
#weight level scores to account for some levels having more potential points than others. Try to prevent breeding with preference for levels
#level_weight = [0.8, 1.0, 0.8, 0.6, 0.4, 0.9, 1.0, 1.0, 1.0] #stab in dark values
#ace_scores = [15, 26, 71, 90, 123, 105, 50, 46, 100]#level ace scores.
#level_weight = ace_scores.map{|i| (15/i.to_f).round(1)} #=> [1.0, 0.6, 0.2, 0.2, 0.1, 0.1, 0.3, 0.3, 0.2]
@level_weight = [1.0, 0.5, 0.2, 0.2, 0.1, 0.2, 0.4, 0.4, 0.3]
rootdir = "/home/sujimichi/coding/lab/rubywarrior"
@ga =MGA.new(:generations => 5000, :mutation_rate => 2, :gene_length => @gene_length, :cache_fitness => true, :fitness => Proc.new{|genome, gen|
puts "#{gen}\n"
Dir.chdir(rootdir)
puts "\n\n"
threads = []
@levels.each do |lvl|
Dir.chdir("#{rootdir}/level#{lvl}bot-beginner")
File.open("./genome", 'w'){|f| f.write( genome.join(",") )} #write the genome to file which Player will use
threads << Thread.new {
invigilator = Invigilator.new #invigilator class examins output from rubywarrior and assigns points for various actions. Invigilator#score_results == the fitness function
results = `rubywarrior -t 0 -s` #run runywarrior
#use invigilator to get the final score. Also returns the break down of points for displaying.
score, level_score, level_total, n_turns, turn_score, time_bonus, clear_bonus = invigilator.score_results(results)
score = score * @level_weight[lvl-1]
puts "level-#{lvl}|levelscore: #{level_score} | turnscore: #{turn_score.round(2)} | bonus(t:c): #{time_bonus}:#{clear_bonus} | turns: #{n_turns} | Total: #{level_total} | fitnes: #{score.round(2)}"
instance_variable_set("@ans#{lvl}", score) #set result in an @var ie @ans1. Done so threads don't try to write answer to a common var.
Dir.chdir(@initial_dir)
}
sleep(0.3) #This sleep is a horrible hack arround the problem of current directory not being thread safe.
#For each level it first changes into the levels directory and then runs rubywarrior in a new Thread. Then sleeps.
#After the sleep it moves the the next level and moves to its dir and again runs rubywarrior in a new Thread.
#Without the sleep all the threads would be started almost at the same time and would start in which ever directory was now the current directory.
#This could cause the first level's rubywarrior command to be called in the next levels dir.
end
threads.each{|t| t.join}
score_sum = levels.map{|lvl| instance_variable_get("@ans#{lvl}")}.compact.sum #collect up and sum the defined @vars with the results.
puts "| Summed Score #{score_sum}"
remark_on score_sum
puts "."
score_sum
})
end
end
#AssaultCourse defines a set of predefined inputs and thier expected output grouped into a number of constants.
class AssaultCourse
#input mapping;
# < /\ > \/ => left, forward, right, backward
# Hc, Hp, Ar => Health_current, Health_previous and Armed?
# r => representational bias
#
# [w<, w/\, w>, w\/, e<, e/\, e>, e\/, c<, c/\, c>, c\/, Ar, Hc, Hp, r]
# [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, 1]
def self.points
AssaultCourse.constants.map{|c| {c => AssaultCourse.const_get(c).size}}.inject{|i,j| i.merge(j)}
end
r = 1 #representational bias
BasicManuvers = {
#[0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :left],
[1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :forward],
#[1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :right],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :backward],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :forward, '0wf'], #Dont just sit there, do something.
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :forward, 'fwc']
}
Retreat = {
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.9, 1, r] => [:walk, :backward, 'RetR1'], #walls either side T infront, health low -> retreat
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.9, 1, r] => [:walk, :backward, 'RetR2'], #walls either side T infront, health low, being shot at -> retreat
}
#Basic Attack - attack enemy in adjacent squares when in open space
BasicAssault = {
#[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:attack, :left],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:attack, :forward],
#[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:attack, :right],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, r] => [:attack, :backward],
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:attack, :forward, 'af2'], #attack forward with walls either side
[1, 0, 1, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 1, 0.0, 0, r] => [:shoot, :forward, 'SF1']
}
#learn to recover after fight
Recovery = {
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.90, 0, r] => [:rest, :rest, 'R9'], #recover from 90% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.80, 0, r] => [:rest, :rest, 'R8'], #recover from 80% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.70, 0, r] => [:rest, :rest, 'R7'], #recover from 70% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.60, 0, r] => [:rest, :rest, 'R6'], #recover from 50% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.50, 0, r] => [:rest, :rest, 'R5'], #recover from 50% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.40, 0, r] => [:rest, :rest, 'R4'], #recover from 40% damage
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.65, 1, r] => [:walk, :forward, 'WFD0'], #limp on if only slightly hurt and under fire (specific example from level 4)
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.40, 1, r] => [:walk, :forward, 'WFD1'], #limp on if only slightly hurt and under fire
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.20, 1, r] => [:walk, :forward, 'WFD2'], #limp on if only slightly hurt and under fire
}
CloseQuaterCombat = {
[1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0.0, 0, r] => [:pivot, :backward, 'PV'], #watch your back maggot!
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, r] => [:attack, :forward, 'AF1'], #walls either side T infront
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.1, 1, r] => [:attack, :forward, 'AF2'], #walls either side T infront attacking
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.3, 1, r] => [:attack, :forward, 'AF3'], #walls either side T infront attacking
[1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0.5, 1, r] => [:attack, :forward, 'AF4'], #walls either side T infront attacking
}
AdvancedCombat = {
[1, 0, 1, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, r] => [:walk, :forward, 'mv0'], #walk toward distant target
[1, 0, 1, 0, 0, 0.3, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, r] => [:walk, :forward, 'mv1'], #walk toward distant target
[1, 0, 1, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, r] => [:walk, :forward, 'mv2'], #walk toward distant target
[1, 0, 1, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0, r] => [:attack, :forward, 'AF0'], #attack forward with vision but no gun
[1, 0, 1, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 1, 0.0, 0, r] => [:shoot, :forward, 'SF1'], #walls either side high_threat target in distance infront
[1, 0, 1, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 1, 0.2, 1, r] => [:shoot, :forward, 'SF2'], #walls either side high_threat target in distance infront
[1, 0, 1, 0, 0, 0.6, 0, 0, 0, 0, 0, 0, 1, 0.4, 1, r] => [:shoot, :forward, 'SF3'], #walls either side high_threat target in distance infront
#[1, 0, 1, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 1, 0.0, 0, r] => [:walk, :backward, 'AIM1'],
#[1, 0, 1, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 1, 0.2, 1, r] => [:walk, :backward, 'AIM2']
}
Rescue = {
#[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, r] => [:rescue, :left],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, r] => [:rescue, :forward],
#[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, r] => [:rescue, :right],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, r] => [:rescue, :backward],
}
end
#Invigilator is used to inspect the results from a run of rubywarrior and calculate a score.
#It is essentially the fitness function for the GAs.
#As each fitness evaluation calls a new instance the Invigilator can be changed part way throu an Agent or FieldTraining run.
#stop training run with ctrl+c, paste in new Invigilator and continue training run.
class Invigilator
def initialize name = Dir.getwd.split("/").last.sub("-beginner", "")
@warrior_name = Dir.getwd.split("/").last.sub("-beginner", "")
end
def score_results results
lines = results.split("\n")
turns = results.split("- turn")
begin
level_total = lines.select{|line| line.include?("Total Score:")}.first.split("=").last.to_i
time_bonus = lines.select{|line| line.include?("Time Bonus:")}.first.split(" ").last.to_i if results.include?("Time Bonus:")
clear_bonus = lines.select{|line| line.include?("Clear Bonus:")}.first.split(" ").last.to_i if results.include?("Clear Bonus:")
rescue
level_total = -200 #punishment for failing to complete level
end
time_bonus ||= 0
clear_bonus ||= 0
#level score based of points awarded during game.
level_score = lines.select{|line| line.include?("earns")}.map{|l| l.split[2].to_i}.inject{|i,j| i+j}
level_score ||= -100 #punishment for not earning anything
turn_score = []
turns.each do |turn|
turn_score << 15 if turn.match(/#{@warrior_name} receives (\d) health/) && !( turn.match(/#{@warrior_name} takes (\d) damage/) || turn.match(/already fit as a fiddle/) )
turn_score << -20 if turn.match(/already fit as a fiddle/) #equates to doing nothing.
%w[forward backward left right].each do |dir|
turn_score << 3 if turn.match(/#{@warrior_name} attacks #{dir} and hits/) && !(turn.match(/#{@warrior_name} attacks #{dir} and hits nothing/) || turn.match(/hits Captive/))
turn_score << -6 if turn.match(/#{@warrior_name} attacks #{dir} and hits/) && (turn.match(/#{@warrior_name} attacks #{dir} and hits nothing/) || turn.match(/hits Captive/))
turn_score << 4 if turn.match(/#{@warrior_name} shoots #{dir} and hits/) && !(turn.match(/#{@warrior_name} shoots #{dir} and hits nothing/) || turn.match(/hits Captive/))
turn_score << -8 if turn.match(/#{@warrior_name} shoots #{dir} and hits/) && (turn.match(/#{@warrior_name} shoots #{dir} and hits nothing/) || turn.match(/hits Captive/))
turn_score << 50 if turn.match(/#{@warrior_name} unbinds #{dir} and rescues Captive/)
end
#will already have points for forward attack, this is a bonus for successful *forward* attack
turn_score << 1 if turn.match(/#{@warrior_name} attacks forward and hits/) && !(turn.match(/#{@warrior_name} attacks forward and hits nothing/) || turn.match(/hits Captive/))
turn_score << -6 if turn.match(/Captive dies/)
turn_score << -4 if turn.match(/#{@warrior_name} does nothing/)
turn_score << -4 if turn.match(/#{@warrior_name} walks/) && turn.match(/#{@warrior_name} bumps/)
turn_score << 2 if turn.match(/#{@warrior_name} walks forward/) && !turn.match(/#{@warrior_name} bumps/)
end
turn_score = turn_score.sum
n_turns = turns.size-1
turn_score = (turn_score.to_f/n_turns)*4
bonus = clear_bonus*3 + time_bonus*3 #times three to increase onerous to earn bonuses.
score = level_score + level_total + bonus + (turn_score/n_turns.to_f)
return [score, level_score, level_total, n_turns, turn_score, time_bonus, clear_bonus]
end
end
#DrillSergeant provides a way of testing a NN's response to a given input. The method 'test_recruit_on' takes a hash which defines {input_array => response_array}
#response_array must include [action, impulse] but can also include an alternative 'code' and different point value ie[:walk, :forward, 'wfd', 2]
#Several examples can be passed in one test. 1.score will return the current score which is an array of accumulated points
#
# d = DrillSergeant.new
# d.recruit = recruit #recruit is a 'brain' from Brains
# d.test( {
# [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :forward],
# [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, r] => [:walk, :backward]
# )}
class DrillSergeant
attr_accessor :score, :recruit
def initialize
@score = []
end
def test_recruit_on args
args.each do |input,response|
code = response[2] || response.map{|s| s.to_s.each_char.map.first}.join #code is str which is output if the input == response
points = response[3] || 1 #number of points awarded for input == response, default 1
@score << (@recruit.act_on(input).eql?(response[0..1]) ? (print(code);points) : 0)
end
end
end
class PopBuilder
attr_accessor :genomes
def read_genomes genome_dir = "genomes by level"
d = Dir.getwd
Dir.chdir(genome_dir)
files = Dir.open(".").to_a.select{|f| f.include?("genome")}
@genomes = files.map do |file|
File.open(file, "r"){|f| f.readlines}.join.split(",").map{|s| s.to_f}
end
Dir.chdir(d)
end
def make_pop size = 80
read_genomes unless @genomes
n = size/@genomes.size
pop = []
n.times{ @genomes.each{|genome| pop << genome} }
pop
end
def get_best pop, ga, best_n = 10
pop.sort_by{|m| ga.fitness(m)}.reverse[0..best_n]
end
def combine_best_from pops, ga, end_size = nil
@genomes = pops.map{|pop| get_best(pop,ga) }.flatten
end_size = @genomes.size * 2 if end_size.nil?
make_pop(end_size)
end
def self.load_pop f_name
require 'json'
pop_file = "./#{f_name}"
pop = File.open(pop_file, "r"){|f| f.readlines}
JSON.parse pop.first
end
def self.save_pop population, f_name = nil
unless f_name
puts "enter a file name for population"
f_name = gets.chomp
end
return "no filename" if f_name.nil? || f_name.empty?
pop_file = "./#{f_name}"
File.open(pop_file,'w'){|f| f.write( population )}
end
end
class CrossBreeder
# introduce_trait(:from => <weak_genome_with_good_trait>, :into => <strong_genome_or_(small)pop_without_trait>)
def introduce_trait args
weak_g = args[:from]
args[:popsize] ||= 40
if args[:into].map{|i| i.is_a?(Array)}.all?
puts "given population"
strong_pop = args[:into]
else
puts "given genome"
strong_pop = Array.new(1){args[:into]}
end
pop = []
strong_pop.each{|m|
pop << m
9.times{ pop << weak_g}
}
pop = grow_pop_to pop, args[:popsize]
end
def grow_pop_to pop, new_size
npop = []
n = new_size/pop.size
n.times{ pop.each{|g| npop << g} }
npop
end
def breed pop, training_groud = FieldTraining.new
training_groud.population = pop
training_groud.ga.mutation_rate = 0.01 #almost no mutation
training_groud.ga.cross_over_rate = 0.1 #take more from the weaker genome when making new genome.
#training_groud.train
training_groud
end
end
c = CrossBreeder.new
pop = c.introduce_trait :from => g6, :into => g_strong
ft = c.breed pop
ft.train