注意:不是严格按照一阶谓词逻辑写法,仅供参考
房内有一个猴子,一个箱子,天花板挂了一串香蕉,其位置如图所示。猴子为了拿到香蕉,它必须把箱子搬到香蕉下面,然后再爬到箱子上。请定义必要的谓词,列出问题的初始化状态(即下图所示状态),目标状态(猴子拿到了香蕉,站在箱子上,箱子位于位置b)
1、定义描述环境状态的谓词
AT(x,y) x在y处 x={monkey,box,banana} y={a,b,c}
HOLD(s) 猴子拿着s s={banana,empty}
ON(z) z在箱子上 z={monkey,empty}
2、使用谓词、连接词、量词来表示环境状态
初始状态S:
AT(monkey,a)∧AT(banana,b)∧AT(box,c)∧HOLD(empty)∧ON(empty)
目标状态T:
AT(monkey,b)∧AT(banana.b)∧AT(box,b)∧HOLD(banana)∧ON(monkey)
3、从初始状态到目标状态的转化,猴子需要完成一系列的操作,定义操作谓词表示其动作
GO(m,n) 猴子从m处移动到n处 m,n={a,b,c}
MOVE(m,n) 猴子把箱子从m处移到n处 m,n={a,b,c}
CLIME(m) 猴子在m处爬上箱子 m={a,b,c}
PICK(m) 猴子在m处摘到香蕉 m={a,b,c}
这四个操作也可以用条件和动作来表示。条件直接用谓词公式表示,是为完成相应操作所必须具备的条件:当条件中的事实均成立时,则可执行该操作。动作通过前后状态的变化表示,即通过删除或增加谓词公式来描述动作前后的状态。
GO(m,n) 猴子从m处移动到n处 m,n={a,b,c}
条件:AT(monkey,m)
动作_删除:AT(monkey,m)
动作_增加:AT(monkey,n)
MOVE(m,n) 猴子把箱子从m处移到n处 m,n={a,b,c}
条件:AT(monkey,m)∧AT(box,m)
动作_删除:AT(monkey,m)∧AT(box,m)
动作_增加:AT(monkey,n)∧AT(box,n)
CLIME(m) 猴子在m处爬上箱子 m={a,b,c}
条件:AT(monkey,m)∧AT(box,m)∧AT(banana,m)∧ON(empty)
动作_删除:ON(empty)
动作_增加:ON(monkey)
PICK(m) 猴子在m处摘到香蕉 m={a,b,c}
条件:AT(monkey,m)∧AT(box,m)∧AT(banana,m)∧ON(monkey)∧HOLD(empty)
动作_删除:HOLD(empty)
动作_增加:HOLD(banana)
4、按照行动计划,一步步使用操作进行状态变化,直至达到目标状态
AT(monkey,a)∧AT(banana,b)∧AT(box,c)∧HOLD(empty)∧ON(empty)
GO(a,c)
AT(monkey,c)∧AT(banana,b)∧AT(box,c)∧HOLD(empty)∧ON(empty)
MOVE(c,b)
AT(monkey,b)∧AT(banana,b)∧AT(box,b)∧HOLD(empty)∧ON(empty)
CLIME(b)
AT(monkey,b)∧AT(banana,b)∧AT(box,b)∧HOLD(empty)∧ON(monkey)
PICK(b)
AT(monkey,b)∧AT(banana.b)∧AT(box,b)∧HOLD(banana)∧ON(monkey)
5、最终行动操作序列为:
GO(a,c) MOVE(c,b) CLIME(b) PICK(b)
#include
#include
#include
using namespace std;
int step = 1;
struct state
{
string AT_monkey;
string AT_banana;
string AT_box;
string HOLD;
string ON;
};
void print(struct state s)
{
cout << "AT(monkey," << s.AT_monkey << "), ";
cout << "AT(banana," << s.AT_banana << "), ";
cout << "AT(box," << s.AT_box << "), ";
cout << "HOLD(" << s.HOLD << "), ";
cout << "ON(" << s.ON << ")" << endl;
}
bool go(struct state& s, string pos1, string pos2)
{
if (s.AT_monkey == pos1)
{
s.AT_monkey = pos2;
cout << " ==>step" << step++ << ": " << "go(" << pos1 << "," << pos2 << ")" << endl;
print(s);
return true;
}
else
return false;
return true;
}
bool move(struct state& s, string pos1, string pos2)
{
if (s.AT_monkey == pos1 && s.AT_box == pos1)
{
s.AT_monkey = pos2;
s.AT_box = pos2;
cout << " ==>step" << step++ << ": " << "move(" << pos1 << "," << pos2 << ")" << endl;
print(s);
return true;
}
else
return false;
return true;
}
bool clime(struct state& s, string pos)
{
if (s.AT_monkey == pos && s.AT_box == pos && s.AT_banana == pos && s.ON == "empty")
{
s.ON = "monkey";
cout << " ==>step" << step++ << ": " << "clime(" << pos << ")" << endl;
print(s);
return true;
}
else
return false;
return true;
}
bool pick(struct state& s, string pos)
{
if (s.AT_monkey == pos && s.AT_box == pos && s.AT_banana == pos && s.HOLD == "empty" && s.ON == "monkey")
{
s.HOLD = "banana";
cout << " ==>step" << step++ << ": " << "pick(" << pos << ")" << endl;
print(s);
return true;
}
else
return false;
return true;
}
int main()
{
string monkey, banana, box;
struct state s;
cout << "Please input the position of monkey,banana,box using a,b,c" << endl;
cin >> monkey >> banana >> box;
cout << endl << "The position of monkey,banana,box is showed as follow:" << endl;
cout << "monkey: " << monkey << endl;
cout << "banana: " << banana << endl;
cout << "box: " << box << endl;
s.AT_monkey = monkey;
s.AT_banana = banana;
s.AT_box = box;
s.HOLD = "empty";
s.ON = "empty";
cout << endl << "The operation steps is showed as follow:" << endl;
print(s);
go(s, s.AT_monkey, s.AT_box);
move(s, s.AT_monkey, s.AT_banana);
clime(s, s.AT_banana);
pick(s, s.AT_banana);
cout << endl << endl;
system("pause");
}