首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以使用Apache Spark RDD进行递归计算?

是否可以使用Apache Spark RDD进行递归计算?
EN

Stack Overflow用户
提问于 2015-06-17 21:24:39
回答 2查看 5.2K关注 0票数 5

我正在使用Scala和Apache Spark开发国际象棋引擎(我需要强调的是,我的理智不是这个问题的主题)。我的问题是Negamax算法本质上是递归的,当我尝试朴素的方法时:

代码语言:javascript
运行
复制
class NegaMaxSparc(@transient val sc: SparkContext) extends Serializable  {
  val movesOrdering = new Ordering[Tuple2[Move, Double]]() {
    override def compare(x: (Move, Double), y: (Move, Double)): Int =
      Ordering[Double].compare(x._2, y._2)
  }

  def negaMaxSparkHelper(game: Game, color: PieceColor, depth: Int, previousMovesPar: RDD[Move]): (Move, Double) = {
    val board = game.board

    if (depth == 0) {
      (null, NegaMax.evaluateDefault(game, color))
    } else {
      val moves = board.possibleMovesForColor(color)
      val movesPar = previousMovesPar.context.parallelize(moves)

      val moveMappingFunc = (m: Move) => { negaMaxSparkHelper(new Game(board.boardByMakingMove(m), color.oppositeColor, null), color.oppositeColor, depth - 1, movesPar) }
      val movesWithScorePar = movesPar.map(moveMappingFunc)
      val move = movesWithScorePar.min()(movesOrdering)

      (move._1, -move._2)
    }
  }

  def negaMaxSpark(game: Game, color: PieceColor, depth: Int): (Move, Double) = {
    if (depth == 0) {
      (null, NegaMax.evaluateDefault(game, color))
    } else {
      val movesPar = sc.parallelize(new Array[Move](0))

      negaMaxSparkHelper(game, color, depth, movesPar)
    }
  }
}

class NegaMaxSparkBot(val maxDepth: Int, sc: SparkContext) extends Bot {
  def nextMove(game: Game): Move = {
    val nms = new NegaMaxSparc(sc)
    nms.negaMaxSpark(game, game.colorToMove, maxDepth)._1
  }
}

我得到了:

代码语言:javascript
运行
复制
org.apache.spark.SparkException: RDD transformations and actions can only be invoked by the driver, not inside of other transformations; for example, rdd1.map(x => rdd2.values.count() * x) is invalid because the values transformation and count action cannot be performed inside of the rdd1.map transformation. For more information, see SPARK-5063.

问题是:这个算法可以使用Spark递归实现吗?如果不是,那么解决这个问题的合适的Spark-way是什么?

EN

Stack Overflow用户

发布于 2015-06-17 22:57:07

只有驱动程序可以在RDD上启动计算。原因是,尽管RDD“感觉”像是常规的数据集合,但在幕后它们仍然是分布式集合,因此在它们上启动操作需要协调所有远程从站上的任务执行,而这一点在大多数时候是对我们隐藏的。

因此,从从机递归,即直接从从机动态启动新的分布式任务是不可能的:只有驱动器可以处理这种协调。

这里有一个简化问题的可能的替代方案(如果我没弄错的话)。其思想是连续构建Moves的实例,每个实例表示来自初始状态的Move的完整序列。

Moves的每个实例都能够将自身转换为一组Moves,每个实例对应于相同的Move序列加上一个可能的下一个Move

在此基础上,驱动程序只需按我们想要的深度连续flatMap Moves,生成的RDDMoves将为我们并行执行所有操作。

这种方法的缺点是所有深度级别都保持同步,即在进入下一步之前,我们必须计算级别n (即级别nRDD[Moves] )的所有移动。

下面的代码没有经过测试,它可能有缺陷,甚至不能编译,但希望它提供了一个如何解决问题的想法。

代码语言:javascript
运行
复制
/* one modification to the board */
case class Move(from: String, to: String)

case class PieceColor(color: String)

/* state of the game */ 
case class Board {

    // TODO
    def possibleMovesForColor(color: PieceColor): Seq[Move] = 
        Move("here", "there") :: Move("there", "over there") :: Move("there", "here") :: Nil

    // TODO: compute a new instance of board here, based on current + this move
    def update(move: Move): Board = new Board
}


/** Solution, i.e. a sequence of moves*/ 
case class Moves(moves: Seq[Move], game: Board, color: PieceColor) {    
    lazy val score = NegaMax.evaluateDefault(game, color)

    /** @return all valid next Moves  */
    def nextPossibleMoves: Seq[Moves] = 
        board.possibleMovesForColor(color).map { 
            nextMove => 
              play.copy(moves = nextMove :: play.moves, 
                        game = play.game.update(nextMove)
        } 

}

/** Driver code: negaMax: looks for the best next move from a give game state */
def negaMax(sc: SparkContext, game: Board, color: PieceColor, maxDepth: Int):Moves = {

    val initialSolution = Moves(Seq[moves].empty, game, color)

    val allPlays: rdd[Moves] = 
        (1 to maxDepth).foldLeft (sc.parallelize(Seq(initialSolution))) {
        rdd => rdd.flatMap(_.nextPossibleMoves)
    }

    allPlays.reduce { case (m1, m2) => if (m1.score < m2.score) m1 else m2}

}
票数 3
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30892986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档