Alpha-Beta剪枝方法的问题,返回了Beta?也许我不明白它的工作原理

难道不应该返回DRAW(平局)吗?

def alphabeta(alpha, beta, player)
      best_score = -INFINITY
      if not self.has_available_moves?
        return DRAW
      elsif self.has_this_player_won?(player) == player
        return WIN
      elsif self.has_this_player_won?(1 - player) == 1 - player
        return LOSS
      else
        self.remaining_moves.each do |move|
          if alpha >= beta then return alpha end 

          self.make_move_with_index(move, player)
          move_score = -alphabeta(-beta, -alpha, 1 - player)
          self.undo_move(move)

          if move_score > alpha
            alpha = move_score
            next_move = move
          end
          best_score = alpha
        end
      end
      return best_score
    end

常量:

WIN = 1
LOSS = -1
DRAW = 0
INFINITY = 100

COMPUTER = 0
HUMAN = 1

测试用例:

   # computer is 0, human is 1
   # c h c
   # _ h h
   # _ c h -- computer's turn
   test "make sure alpha-beta pruning works (human went first) 1" do
     @board.state = [0,1,0,nil,1,1,nil,0,1]
     score = @board.alphabeta(100, -100, Board::COMPUTER)
     assert_equal(Board::DRAW, score)

   end

相关方法和其他有助于阅读以上代码的内容:

self.state = Array.new(9)

    WAYS_TO_WIN = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],[0, 4, 8], [2, 4, 6]]


 def make_move_with_index(index, player)
        self.state[index] = player
    end


    def undo_move(index)
        self.state[index] = nil
    end

def has_this_player_won?(player)
      WAYS_TO_WIN.each do |way_to_win|
          return true if self.state.values_at(*way_to_win).uniq.size == 1 and self.state[way_to_win[0]] == player
      end

      return false
    end

    def remaining_moves
        self.state.each_with_index.map{|e,i| (e.nil?) ? i : nil }.compact
    end

    def has_available_moves?
        return self.state.include? nil
    end

回答:

你忽略了我在之前问题中的评论。 has_this_player_won? 返回一个布尔值,它永远不可能等于一个整数 player。 此外,你开始时的逻辑是错误的:即使没有剩余的移动,游戏也可能有赢家。 最后,对递归函数的第一次调用应该使用 alpha=-inf, beta=+inf。 相关代码段:


if self.has_this_player_won?(1 - player)
    return LOSS
elsif not self.has_available_moves?
    return DRAW
else
    ...
score = @board.alphabeta(-INFINITY, INFINITY, Board::COMPUTER)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注