Class: DeepBeige
- Inherits:
-
Object
- Object
- DeepBeige
- Defined in:
- lib/deepbeige.rb,
lib/deepbeige/main.rb
Defined Under Namespace
Classes: Main
Instance Method Summary collapse
- #get_move(position, moves) ⇒ Object
- #id ⇒ Object
-
#initialize ⇒ DeepBeige
constructor
A new instance of DeepBeige.
- #learn(game) ⇒ Object
- #start_game(game_name) ⇒ Object
- #train(generations, game) ⇒ Object
Constructor Details
Instance Method Details
#get_move(position, moves) ⇒ Object
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 |
# File 'lib/deepbeige.rb', line 26 def get_move position, moves game = game_from_name @game_name game.quiet = true game.reload_position moves best_move = "" best_score = -2 game.legal_moves.each do |move| game2 = game_from_name @game_name game2.quiet = true game2.reload_position moves game2.play_move game.next_player, move input = game2.current_position.values if game.next_player == 1 input = [] game2.current_position.values.each do |value| input << -value end end @neural_net.input = input @neural_net.evaluate score = @neural_net.output_value #p "move #{move} evaluated as #{score}" if score > best_score best_score = score best_move = move end end best_move end |
#id ⇒ Object
10 11 12 |
# File 'lib/deepbeige.rb', line 10 def id @neural_net.id end |
#learn(game) ⇒ Object
59 60 61 |
# File 'lib/deepbeige.rb', line 59 def learn game generate_population 30, game.name end |
#start_game(game_name) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/deepbeige.rb', line 14 def start_game game_name @game_name = game_name case game_name when "NoughtsAndCrosses" @neural_net.generate :inputs => 9, :outputs => 1, :tiers => 3 @neural_net.load_from_file (ENV["HOME"] + "/." + $application_name + "/NoughtsAndCrosses/best.txt") when "PickANumber" @neural_net.generate :inputs => 3, :outputs => 1, :tiers => 2 @neural_net.load_from_file (ENV["HOME"] + "/." + $application_name + "/PickANumber/best.txt") end end |
#train(generations, game) ⇒ Object
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 |
# File 'lib/deepbeige.rb', line 63 def train generations, game @game_name = game.name @population = load_population game.name #reset scores scores = {} @population.each do |neuralnet| scores[neuralnet.id] = 0 end generation_number = 1 generations.times do puts "Evolving Generation #{generation_number}" player_number = 0 @population.each do |neuralnet| player = DeepBeige.new player.neural_net = neuralnet player.game_name = game.name 5.times do play_as_player game.name, player, 1, scores end 5.times do play_as_player game.name, player, 2, scores end player_number += 1 end leaderboard = scores.sort_by {|id, count| count} position = 1 5.times do leader = leaderboard[leaderboard.count - position] puts "#{position}. pts: #{leader[1]}, #{leader[0]}" position +=1 end @population.each do |neural_net| if neural_net.id == leaderboard.last[0] neural_net.save_to_file (ENV["HOME"] + "/." + $application_name + "/#{@game_name}/best.txt") end end i = 0 while i < 15 do @population.delete_if {|neural_net| neural_net.id == leaderboard[i][0]} i +=1 end new_nets = [] @population.each do |neural_net| new_net = neural_net.clone new_net.mutate new_nets << new_net end @population.concat new_nets scores = {} @population.each do |neural_net| scores[neural_net.id] = 0 end generation_number +=1 end save_population game.name end |