Class: Mjai::Manue::Player
- Inherits:
-
Player
- Object
- Player
- Mjai::Manue::Player
- Defined in:
- lib/mjai/manue/player.rb
Defined Under Namespace
Instance Method Summary collapse
- #can_be_hora?(pais) ⇒ Boolean
-
#get_hora_prob_with_monte_carlo(tehais, visible_set, num_visible) ⇒ Object
This is too slow but left here as most precise baseline.
- #get_scene(params) ⇒ Object
-
#initialize(params) ⇒ Player
constructor
A new instance of Player.
- #random_test ⇒ Object
- #respond_to_action(action) ⇒ Object
- #to_pai_set(pais) ⇒ Object
Constructor Details
#initialize(params) ⇒ Player
Returns a new instance of Player.
103 104 105 106 107 108 109 |
# File 'lib/mjai/manue/player.rb', line 103 def initialize(params) super() @score_type = params[:score_type] data_dir = File.dirname(__FILE__) + "/../../../share" @danger_tree = DangerEstimator::DecisionTree.new("#{data_dir}/danger.all.tree") @hora_prob_estimator = HoraProbabilityEstimator.new("#{data_dir}/hora_prob.marshal") end |
Instance Method Details
#can_be_hora?(pais) ⇒ Boolean
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/mjai/manue/player.rb', line 313 def can_be_hora?(pais) pai_set = to_pai_set(pais) kotsus = pai_set.select(){ |pai, c| c >= 3 } toitsus = pai_set.select(){ |pai, c| c >= 2 } num_cont = 1 # TODO 重複を考慮 num_shuntsus = 0 pais.map(){ |pai| pai.remove_red() }.sort().uniq().each_cons(2) do |prev_pai, pai| if pai.type != "t" && pai.type == prev_pai.type && pai.number == prev_pai.number + 1 num_cont += 1 if num_cont >= 3 num_shuntsus += 1 num_cont = 0 end else num_cont = 1 end end return kotsus.size + num_shuntsus >= 4 && toitsus.size >= 1 end |
#get_hora_prob_with_monte_carlo(tehais, visible_set, num_visible) ⇒ Object
This is too slow but left here as most precise baseline.
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 |
# File 'lib/mjai/manue/player.rb', line 285 def get_hora_prob_with_monte_carlo(tehais, visible_set, num_visible) invisibles = [] for pai in self.game.all_pais.uniq next if pai.red? (4 - visible_set[pai]).times() do invisibles.push(pai) end end num_tsumos = game.num_pipais / 4 hora_freq = 0 num_tries = 1000 num_tries.times() do tsumos = invisibles.sample(num_tsumos) pais = tehais + tsumos #p [:pais, pais.sort().join(" ")] can_be = can_be_hora?(pais) #p [:can_be, can_be] next if !can_be shanten = ShantenAnalysis.new(pais, -1, [:normal], 14, false) #pp [:shanten, tehais, tsumos, shanten.shanten] #if shanten.shanten == -1 # pp [:comb, shanten.combinations[0]] #end hora_freq += 1 if shanten.shanten == -1 end return hora_freq.to_f() / num_tries end |
#get_scene(params) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/mjai/manue/player.rb', line 262 def get_scene(params) visible = [] visible += self.game.dora_markers visible += self.tehais for player in self.game.players visible += player.ho + player.furos.map(){ |f| f.pais }.flatten() end visible_set = to_pai_set(visible) default_params = { :visible_set => visible_set, :context => self.context, :hora_prob_estimator => @hora_prob_estimator, :num_remain_turns => self.game.num_pipais / 4, :furos => self.furos, :score_type => @score_type, :player => self, } params = default_params.merge(params) # pp params.reject(){ |k, v| [:visible_set, :hora_prob_estimator, :context].include?(k) } return Scene.new(params) end |
#random_test ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/mjai/manue/player.rb', line 342 def random_test() all_pais = (["m", "p", "s"].map(){ |t| (1..9).map(){ |n| Pai.new(t, n) } }.flatten() + (1..7).map(){ |n| Pai.new("t", n) }) * 4 while true pais = all_pais.sample(13).sort() puts(pais.join(" ")) (nj, nm, jimp, mimp) = get_improvers(pais) p [nj, nm] for name, imp in [["jimp", jimp], ["mimp", mimp]] for pais in imp.to_a().sort() puts("%s: %s" % [name, pais.join(" ")]) end end gets() end end |
#respond_to_action(action) ⇒ Object
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 |
# File 'lib/mjai/manue/player.rb', line 111 def respond_to_action(action) if !action.actor case action.type when :start_kyoku @prereach_sutehais_map = {} end elsif action.actor == self case action.type when :tsumo, :chi, :pon, :reach current_shanten_analysis = ShantenAnalysis.new(self.tehais, nil, [:normal]) current_shanten = current_shanten_analysis.shanten if can_hora?(current_shanten_analysis) return create_action({ :type => :hora, :target => action.actor, :pai => action.pai, }) elsif can_reach?(current_shanten_analysis) return create_action({:type => :reach}) elsif self.reach? return create_action({:type => :dahai, :pai => action.pai, :tsumogiri => true}) end #p [:shanten, current_shanten] if current_shanten == 0 sutehai_cands = self.possible_dahais else safe_probs = {} for pai in self.possible_dahais safe_probs[pai] = 1.0 end has_reacher = false for player in self.game.players if player != self && player.reach? #p [:reacher, player, @prereach_sutehais_map[player]] has_reacher = true scene = DangerEstimator::Scene.new({ :game => self.game, :me => self, :dapai => nil, :reacher => player, :prereach_sutehais => @prereach_sutehais_map[player], }) for pai in safe_probs.keys if scene.anpai?(pai) safe_prob = 1.0 else safe_prob = 1.0 - @danger_tree.estimate_prob(scene, pai) end safe_probs[pai] *= safe_prob end end end if has_reacher for pai, safe_prob in safe_probs #log("%s: safe_prob=%.3f\n" % [pai, safe_prob]) end end max_safe_prob = safe_probs.values.max sutehai_cands = safe_probs.keys.select(){ |pai| safe_probs[pai] == max_safe_prob } end #p [:sutehai_cands, sutehai_cands] scene = get_scene({ :current_shanten_analysis => current_shanten_analysis, :sutehai_cands => sutehai_cands, }) #p [:dahai, scene.best_dahai] tsumogiri = [:tsumo, :reach].include?(action.type) && scene.best_dahai == self.tehais[-1] return create_action({ :type => :dahai, :pai => scene.best_dahai, :tsumogiri => tsumogiri, }) end else # action.actor != self case action.type when :dahai if self.can_hora? return create_action({ :type => :hora, :target => action.actor, :pai => action.pai, }) else furo_actions = self.possible_furo_actions if !furo_actions.empty? && !self.game.players.any?(){ |pl| pl != self && pl.reach_state != :none } current_shanten_analysis = ShantenAnalysis.new(self.tehais, nil, [:normal]) current_scene = get_scene({ :current_shanten_analysis => current_shanten_analysis, :sutehai_cands => [nil], }) current_expected_points = current_scene.evals[nil].expected_points for action in furo_actions next if action.type == :daiminkan # TODO Implement later remains = self.tehais.dup() for pai in action.consumed remains.delete_at(remains.index(pai)) end furo = Furo.new({ :type => action.type, :taken => action.pai, :consumed => action.consumed, :target => action.target, }) shanten_analysis_with_furo = ShantenAnalysis.new(remains, nil, [:normal]) scene_with_furo = get_scene({ :current_shanten_analysis => shanten_analysis_with_furo, :furos => self.furos + [furo], :sutehai_cands => remains.uniq(), }) best_eval = scene_with_furo.best_dahais. map(){ |pai| scene_with_furo.evals[pai] }. max_by(){ |e| e.expected_points } expected_points_with_furo = best_eval.expected_points puts("furo_cand: %s" % action) puts(" shanten: %d -> %d" % [ current_shanten_analysis.shanten, shanten_analysis_with_furo.shanten, ]) puts(" ept: %d -> %d" % [current_expected_points, expected_points_with_furo]) if expected_points_with_furo > current_expected_points #gets() # kari return action end end end end when :reach_accepted @prereach_sutehais_map[action.actor] = action.actor.sutehais.dup() end end return nil end |
#to_pai_set(pais) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/mjai/manue/player.rb', line 334 def to_pai_set(pais) pai_set = Hash.new(0) for pai in pais pai_set[pai.remove_red()] += 1 end return pai_set end |