计算机博弈 Negamax 负极值算法

在计算机博弈大赛中Negamax算法是Max-Min算法的一种变形,在代码实现上,它可以更加的简洁

原理
敌对方一定不会选择使我能获胜的节点,也就是一方要最大值,一方要最小值,只不过最小值用负值来表示了。递归的value计算公式如下:
value= -NegaMax( p, d-1)

注意其中的负号,d是深度,p是此刻状态。负极大值算法的核心在于:父节点的值是各子节点的值的负数的极大值。

图解请添加图片描述

伪代码:

public int negamax(Board board, int depth, int alpha, int beta, int sign){ 
if(depth == null || board.getPossibleMovesNumber(colour) == 0){ 
    return calculateBoardFunction(board); 
} 
else{ 
    List<Move> possibleMoves = board.getPossibleMoves(); 
    foreach(Move move in possibleMoves){ 
    Board newBoard = board.clone(); 
    newBoard.makeMove(move); 
    alpha = Math.max(alpha, -negamax(newBoard, depth-1, -beta, -alpha, -sign); 
    if(alpha >= beta){ 
    break; 
    } 
    } 
return alpha; 
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
极大极小值算法(Minimax Algorithm)是一种博弈算法,常用于人工智能领域中的决策树问题。该算法通过递归地搜索博弈树,找到最优的下一步决策。在搜索过程中,假设对手会采取最优策略,因此算法会选择对自己最有利的决策,同时也会考虑到对手的最优策略,以避免自己处于劣势。 C语言实现极大极小值算法的步骤如下: 1. 定义博弈树的节点结构体,包括当前状态、当前玩家、当前深度等信息。 2. 定义估值函数,用于评估当前状态的价值。 3. 定义maxSearch和minSearch函数,分别代表当前玩家为MAX和MIN的情况下的搜索过程。在搜索过程中,需要递归地搜索子节点,并计算出每个子节点的估值。 4. 在maxSearch和minSearch函数中,根据当前玩家的不同,选择最大或最小的估值,并返回该估值。 5. 在主函数中,调用maxSearch函数,得到最优的下一步决策。 下面是一个简单的C语言实现极大极小值算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_DEPTH 5 // 定义博弈树的节点结构体 typedef struct node { int state; int player; int depth; struct node *children[3]; } Node; // 估值函数 int evaluate(int state) { if (state == 1) { return 1; } else if (state == -1) { return -1; } else { return 0; } } // maxSearch函数 int maxSearch(Node *node) { if (node->depth == MAX_DEPTH) { return evaluate(node->state); } int maxVal = -100; for (int i = 0; i < 3; i++) { if (node->children[i] == NULL) { node->children[i] = (Node *)malloc(sizeof(Node)); node->children[i]->state = -node->player; node->children[i]->player = -node->player; node->children[i]->depth = node->depth + 1; } int val = minSearch(node->children[i]); if (val > maxVal) { maxVal = val; } } return maxVal; } // minSearch函数 int minSearch(Node *node) { if (node->depth == MAX_DEPTH) { return evaluate(node->state); } int minVal = 100; for (int i = 0; i < 3; i++) { if (node->children[i] == NULL) { node->children[i] = (Node *)malloc(sizeof(Node)); node->children[i]->state = -node->player; node->children[i]->player = -node->player; node->children[i]->depth = node->depth + 1; } int val = maxSearch(node->children[i]); if (val < minVal) { minVal = val; } } return minVal; } int main() { Node *root = (Node *)malloc(sizeof(Node)); root->state = 0; root->player = 1; root->depth = 0; for (int i = 0; i < 3; i++) { root->children[i] = NULL; } int bestVal = -100; int bestMove = -1; for (int i = 0; i < 3; i++) { root->children[i] = (Node *)malloc(sizeof(Node)); root->children[i]->state = -root->player; root->children[i]->player = -root->player; root->children[i]->depth = root->depth + 1; int val = minSearch(root->children[i]); if (val > bestVal) { bestVal = val; bestMove = i; } } printf("Best move: %d\n", bestMove); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_刘文凯_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值