Class: Numeron::Analyzer
- Inherits:
-
Object
- Object
- Numeron::Analyzer
- Defined in:
- lib/numeron/analyzer.rb
Instance Attribute Summary collapse
-
#calculator ⇒ Object
Returns the value of attribute calculator.
Instance Method Summary collapse
- #all_list ⇒ Object
- #build_calculator ⇒ Object
-
#calc_average(attack, cases, can_zero = false) ⇒ Object
attackの結果、推定される結果に対する可能性の数の平均を計算する.
-
#initialize(calculator) ⇒ Analyzer
constructor
A new instance of Analyzer.
-
#run(mode = :average, options = {}) ⇒ Hash
次の1手の計算.
-
#run_average_mode(cases = nil) ⇒ Object
最悪のケースを0e1b, 1e0bと想定し、それぞれの結果が出た際に可能性の平均値が最も少なくなるケースを推定する.
- #run_possibilities ⇒ Object
-
#run_worstcase_mode(worst_case = {}) ⇒ Object
最悪のケースを0e1bと推定し、その結果として最も可能性の数が最も少なくなるケースを推定する.
Constructor Details
#initialize(calculator) ⇒ Analyzer
Returns a new instance of Analyzer.
6 7 8 |
# File 'lib/numeron/analyzer.rb', line 6 def initialize(calculator) @calculator = calculator end |
Instance Attribute Details
#calculator ⇒ Object
Returns the value of attribute calculator.
5 6 7 |
# File 'lib/numeron/analyzer.rb', line 5 def calculator @calculator end |
Instance Method Details
#all_list ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/numeron/analyzer.rb', line 125 def all_list [].tap do |f| (0..9).to_a.each { |i| (0..9).to_a.each { |j| next if i == j (0..9).to_a.each { |k| next if i == k || j == k f << i.to_s + j.to_s + k.to_s } } } end end |
#build_calculator ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/numeron/analyzer.rb', line 117 def build_calculator _calculator = Numeron::Calculator.new _calculator.possibilities = @calculator.possibilities.clone _calculator.mays = @calculator.mays.clone _calculator.histories = @calculator.histories.clone return _calculator end |
#calc_average(attack, cases, can_zero = false) ⇒ Object
attackの結果、推定される結果に対する可能性の数の平均を計算する
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/numeron/analyzer.rb', line 93 def calc_average(attack, cases, can_zero = false) result = [] cases.each do |pattern| _calculator = build_calculator _calculator.input(attack, pattern[:eat], pattern[:bite]) result << _calculator.possibilities.size end n = 0 sum = 0 result.each do |f| sum = sum + f n = n + 1 if f > 0 end if n > 0 return sum.to_f / n.to_f elsif can_zero return 0 else return 10000 end end |
#run(mode = :average, options = {}) ⇒ Hash
次の1手の計算
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/numeron/analyzer.rb', line 13 def run(mode = :average, = {}) case mode when :average run_average_mode([:cases]) when :worst run_worstcase_mode([:worst_case]) when :possibilities run_possibilities end end |
#run_average_mode(cases = nil) ⇒ Object
最悪のケースを0e1b, 1e0bと想定し、それぞれの結果が出た際に可能性の平均値が最も少なくなるケースを推定する
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/numeron/analyzer.rb', line 25 def run_average_mode(cases = nil) cases ||= [ {eat: 0, bite: 1}, {eat: 1, bite: 0} ] recommend = [] min_size = 10000 all_list.each do |f| average = calc_average(f, cases) if min_size == average recommend << f elsif min_size > average recommend = [f] min_size = average end end { recommend: recommend, size: min_size } end |
#run_possibilities ⇒ Object
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 |
# File 'lib/numeron/analyzer.rb', line 64 def run_possibilities possibilities = @calculator.possibilities.clone recommend = [] min_size = 10000 cases = [ {eat: 0, bite: 0}, {eat: 0, bite: 1}, {eat: 0, bite: 2}, {eat: 1, bite: 0}, {eat: 1, bite: 1}, {eat: 2, bite: 0} ] possibilities.each do |f| average = calc_average(f, cases, true) if min_size == average recommend << f elsif min_size > average recommend = [f] min_size = average end end { recommend: recommend, size: min_size } end |
#run_worstcase_mode(worst_case = {}) ⇒ Object
最悪のケースを0e1bと推定し、その結果として最も可能性の数が最も少なくなるケースを推定する
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/numeron/analyzer.rb', line 46 def run_worstcase_mode(worst_case = {}) worst_case = {eat: 0, bite: 1}.merge(worst_case) min_size = 10000 recommend = [] all_list.each do |f| _calculator = build_calculator _calculator.input(f, worst_case[:eat], worst_case[:bite]) if min_size == _calculator.possibilities.size recommend << f elsif min_size > _calculator.possibilities.size recommend = [f] min_size = _calculator.possibilities.size end end { recommend: recommend, size: min_size } end |