我正在为游戏开发AI,我想使用MinMax算法和Alpha-Beta修剪.
我对它是如何工作有一个粗略的想法,但我仍然无法从头开始编写代码,所以我花了最近两天在网上寻找某种伪代码.
我的问题是,我在网上发现的每个伪代码似乎都是基于找到最佳移动的价值,而我需要返回最佳移动而不是数字.
我当前的代码基于这个伪代码(源代码)
minimax(level, player, alpha, beta){ // player may be "computer" or "opponent"
if (gameover || level == 0)
return score
children = all valid moves for this "player"
if (player is computer, i.e., max's turn){
// Find max and store in alpha
for each child {
score = minimax(level - 1, opponent, alpha, beta)
if (score > alpha) alpha = score
if (alpha >= beta) break; // beta cut-off
}
return …Run Code Online (Sandbox Code Playgroud) java algorithm artificial-intelligence minmax alpha-beta-pruning
你如何处理游戏,如果满足条件,同一个玩家移动?
我试过这样的事情,但我不认为这是对的:
function negamax(node, depth, ?, ?, color)
if node is a terminal node or depth = 0
return color * the heuristic value of node
else
foreach child of node
if (condition is met) // the same player moves
val := negamax(child, depth-1, ?, ?, color)
else
val := -negamax(child, depth-1, -?, -?, -color)
if val??
return val
if val??
?:=val
return ?
Run Code Online (Sandbox Code Playgroud) 我目前正在使用min-max和Alpha-beta修剪为othello开发一个简单的AI.
我的问题与董事会状态的评估功能有关.
我目前正在考虑通过计算来评估它
1)光盘数
2)没有合法行动
3)特定职位的重要性
因此,假设根节点是初始游戏状态.第一个动作是AI的动作,而第二个动作是对手的动作.
0
/ \ AI's Action
1 1
/ \ \ Opponent's action
2 2 2
Run Code Online (Sandbox Code Playgroud)
在节点级别1,我是否会评估我的AI芯片的光盘数量以及它在完成操作后的时间点可以进行的合法移动数量?
在节点级别2,我是否评估对手的筹码的盘数以及在对手完成动作之后它可以进行的合法移动的数量?意思是AI移动 - >对手移动==>此时我评估对手的盘数和对手可以合法的数量.
只是想检查我是否在正确的道路上,因为计算刚刚完成一个动作的玩家的合法移动数量感觉很奇怪.
谢谢和问候,Nat
虽然我理解MiniMax树和alpha-beta修剪概念,但我不明白为什么在许多(例如维基百科)有关alpha-beta修剪的资源中存在像α> =β这样的条件.具体来说,平等是令人困惑的.据我所知,alpha beta返回minmax将返回的移动,但大部分时间更快.但这个例子与它相矛盾:
.
/ | \
1 3* 2
/ | / \ | \ \
1 1 5 3 4 3 2
Run Code Online (Sandbox Code Playgroud)
以上是原始的最小 - 最大树.正如我们所看到的,它将选择一个得分为3的移动.现在让我们做alpha-beta:
.
/ | \
1 3* 3*
/ | / \ | \
1 1 5 3 4 3
Run Code Online (Sandbox Code Playgroud)
它切断了最右边的移动,因为3> = 3.但是算法可以在两个移动之间进行选择,因为它们具有相同的分数,但正如我们在min-max中看到的那样,正确的选择稍微差一些.如果算法仅指定α>β,则不会发生这种情况,因此它也需要搜索2.
维基百科的伪代码(以及许多其他资源)中的错字是什么?或者我在这里误解了一些非常大的东西.
如何从变量中获取最小,最大滑块值.
当我尝试滑动时,滑块设置为最大值并停止工作.
如果我输入最小值和最大值而不是它可以正常工作.
这是一个显示问题的小提琴:
<INPUT TYPE="text" CLASS="slidervalue">
<DIV CLASS="slider"></DIV>
<INPUT CLASS="getmin" TYPE="text" VALUE="1">
<INPUT CLASS="getmax" TYPE="text" VALUE="10">
var getmin = $('.getmin').val();
var getmax = $('.getmax').val();
$(".slider").slider({
range: "max",
min: getmin,
max: getmax,
value: 2,
slide: function( event, ui ) {
$( ".slidervalue" ).val( ui.value );
}
});
Run Code Online (Sandbox Code Playgroud)
我必须从a中找到最小值/最大值(min x,min y,max x,max y)
vector<cv::Point>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
vector<cv::Point> contour;
Run Code Online (Sandbox Code Playgroud)
...
Min = Point(640, 480) ;
Max = Point(0,0) ;
for (int j=0; j<(int)contour.size(); j++)
{
if (contour[j].x < Min.x) Min.x = contour[j].x ;
if (contour[j].y < Min.y) Min.y = contour[j].y ;
if (contour[j].x > Max.x) Max.x = contour[j].x ;
if (contour[j].y > Max.y) Max.y = contour[j].y ;
}
Run Code Online (Sandbox Code Playgroud)
这很好用.我使用mimmax STL开发了一个版本:
auto XminXmax = minmax_element(contour.begin(), contour.end(), [](Point p1,Point p2) {return p1.x < p2.x; });
auto YminYmax = minmax_element(contour.begin(), contour.end(), [](Point p1,Point …Run Code Online (Sandbox Code Playgroud) 我正在尝试为四连胜(或连接四或连接四)游戏实现 MinMax 算法。
我想我明白了,它应该构建一棵可能的板树,达到一定的深度,评估它们并返回它们的分数,然后我们只取这些分数的最大值。
因此,aiChooseCol()通过调用检查每个可能列的分数MinMax()并返回具有最大分数的列。
现在我不确定这是正确的打电话方式吗MinMax()?
检查一下是否正确temp = Math.Max(temp, 1000);?
我还没有制作启发式函数,但这至少应该识别一个获胜列并选择它,但目前它只是选择左侧的第一个免费列......我不知道我做错了什么。
private int AiChooseCol()
{
int best = -1000;
int col=0;
for (int i = 0; i < m_Board.Cols; i++)
{
if (m_Board.CheckIfColHasRoom(i))
{
m_Board.FillSignInBoardAccordingToCol(i, m_Sign);
int t = MinMax(5, m_Board, board.GetOtherPlayerSign(m_Sign));
if (t > best)
{
best = t;
col = i;
}
m_Board.RemoveTopCoinFromCol(i);
}
}
return col;
}
private int MinMax(int Depth, board Board, char PlayerSign)
{
int temp=0;
if …Run Code Online (Sandbox Code Playgroud) 我一直#include <minmax.h>在我的脚本中使用min()并按max()预期使用。我向某人展示了这个,他们以前从未见过它,说这对他们不起作用,并问我为什么不包括<algorithm>和调用std::min()or std::max()。
所以我的问题基本上是,为什么我不是?我在一本关于 C++ 的书中发现了这一点:“C++ 设计模式和衍生品定价”。谷歌搜索"minmax.h",我在最上面的结果中找到了对那本书的引用,所以更让我觉得这是不正常的。
谁能告诉我这是什么?
这两个 CSS 函数都将值限制在上限和下限之间。
我知道的唯一区别是 minmax() 只能在 CSS 网格中使用。
我使用此R代码将数据标准化为最小值和最大值:
normalize <- function(x) {
return ((x - min(x)) / (max(x) - min(x)))
}
mydata <- as.data.frame(lapply(mydata , normalize))
Run Code Online (Sandbox Code Playgroud)
如何对数据进行非规范化?