猴子摘香蕉问题

科技资讯 投稿 6500 0 评论

猴子摘香蕉问题

简介

猴子位置为A香蕉位置在B箱子位置为C),如何行动可摘取到香蕉?

猴子摘香蕉问题PEAS

性能 环境 执行器 感受器
猴子站在箱子上摘得香蕉 香蕉、箱子位置 (四肢)Move,Climb,Push,Grasp (眼睛) Site,Hold,On,Hang

基本的定义

定义状态谓词

    \(SITE(x , y)\): \(x\) 在 \(y\) 处
  • \(HANG)(w,y)\): \(w\) 悬挂在 \(y\)处
  • \(ON(z)\): \(z\) 站在箱子上
  • \(HOLDS(z)\): \(z\) 手里拿着香蕉

变元的个体域

  • \(y\)的个体域是\(\{a,b,c\}\) (地点为 a,b,c)

  • \(w\)的个体域是\(\{Banana\}\)

初始状态

\[S_0 = SITE(Monkey, a) \wedge HANG(Banana, b) \wedge SITE(Box, c) \wedge \neg ON(Monkey) \wedge \neg HOLDS(Monkey) \]

A处,香蕉悬挂在B处,箱子放在C处

目标状态

\[S_g = SITE(Monkey,b) \wedge \neg HANG(Banana,b) \wedge SITE(Box,b) \wedge ON(Monkey) \wedge HOLDS(Monkey) \]

B处。

定义四个操作

\[Goto(u,v): 猴子从u走到v处。\\ Pushbox(v,w): 猴子推着箱子从v走到w处。\\ Climbbox: 猴子爬上箱子\\ Grasp: 猴子摘到香蕉 \]

各操作的条件和动作

    \(Goto(u,v)\)
    • \(条件:\neg ON(Monkey), SITE(Monkey,u)\)
    • 动作:
      • $删除表:SITE(Monkey,u) $
      • \(添加表:SITE(Monkey,v)\)
  • \(Pushbox(v,w)\)
      \(条件:\neg ON(Monkey), SITE(Monkey,v), SITE(Box,v)\)
    • 动作:
      • \(删除表:SITE(Monkey,v),SITE(Box,v)\)
      • \(添加表:SITE(Monkey, w), SITE(Box, w)\)
  • \(Climbbox\)
      \(条件:\neg ON(Monkey), SITE(Monkey,w),SITE(Box, w)\)
    • 动作:
      • \(删除表:\neg ON(Monkey)\)
      • \(添加表:ON(Monkey)\)
  • \(Grasp\)
      \(条件:ON(Monkey), SITE(Box, b), HANG(Banana,b)\)
    • 动作:
      • $删除表:\neg HOLDS(Monkey),HANG(Banana,b) $
      • \(添加表:HOLDS(Monkey), \neg HANG(Banana,b)\)

求解

      M: 猴子的位置
    • B:香蕉的位置
    • Box:箱子的位置
    • \(On = 0\):猴子在地板上
    • \(On = 1\):猴子在箱子上
    • \(H = 0\):猴子没有抓到香蕉
    • \(H=1\):猴子抓到了香蕉
  1. 初始状态:(a, b, c, 0, 0)

  2. (b, b, b, 1, 1)

    • \(r_1: IF (x,y,z,0,0) \; THEN (w,y,z,0,0)\) 猴子到处乱走
    • $r_2: IF(z,y,z,0,0) ; THEN(z',y,z',0,0) $ 猴子推箱子走
    • \(r_3: IF(x,y,x,0,0) \; THEN(x,y,x,1,0)\) 猴子爬上箱子
    • \(r_4: IF(x,y,x,1,0) \; THEN(x,y,x,0,0)\) 猴子爬下箱子
    • \(r_5: IF(x,x,x,1,0) \; THEN(x,x,x,1,1)\) 猴子摘香蕉
    • 其中,\(x,y,z,w\)为变量。
  3. 具体过程:

      \(r_1: IF(a,b,c,0,0) \; THEN(c,b,c,0,0)\) 猴子走到箱子处
    • \(r_2: IF(c,b,c,0,0) \; THEN(b,b,b,0,0)\) 猴子将箱子推到香蕉处
    • \(r_3: IF(b,b,b,0,0) \; THEN(b,b,b,1,0)\) 猴子爬上箱子
    • \(r_5: IF(b,b,b,1,0) \; THEN(b,b,b,1,1)\) 猴子摘到香蕉

    在已知事实下,\(r_1 \rightarrow r_2 \rightarrow r_3 \rightarrow r_5\),即可得到香蕉。

代码

# 猴子走向箱子
def Goto(Monkey, Box):
    global i  # 步数
    i += 1
    print("step " + str(i) + ": " + "Monkey从" + Monkey + "走向" + Box)


# 猴子推箱子到香蕉处
def Pushbox(Box, Banana):
    global i
    i += 1
    print("step " + str(i) + ": " + "Monkey将Box从" + Box + "推向" + Banana)


# 猴子爬上箱子
def Climbbox():
    global i
    i += 1
    print("step " + str(i) + ": " + "Monkey爬上Box")


# 猴子摘取香蕉
def Grasp():
    global i
    i += 1
    print("step " + str(i) + ": " + "Monkey摘到Banana")


# 猴子爬下箱子
def Dropbox():
    global i
    i += 1
    print("step " + str(i) + ": " + "Monkey爬下box")

# 检查输入
def Check(Monkey, Banana, Box, On, H):
    if Monkey != Box and On == "1":
        print("不合法的输入!")
        return False
    if H == "1":
        print("Monkey已摘到banana")
        return False
    return True
if __name__ == "__main__":
    i = 0
    print("请输入Monkey位置,Banana的位置,Box的位置,猴子是否在箱子上(1:在,0:不在)上以及猴子是否摘取香蕉(1:是,0:否):")
    Monkey, Banana, Box, On, H = input().split(",")
    Flag = Check(Monkey, Banana, Box, On, H)
    while Flag:
        # r1规则:箱子和猴子不在一起才能走向箱子
        if Monkey != Box:
            Goto(Monkey, Box)
            Monkey = Box
            continue

        # r2规则:猴子跟箱子在一起,且不跟香蕉在一起,以及猴子不在箱子上,才能推箱子到香蕉处
        if Box != Banana and Monkey == Box and On == "0":
            Pushbox(Box, Banana)
            Monkey = Box = Banana
            continue

        # r3规则:猴子不在箱子上,并且猴子跟箱子在一起才能爬
        if On == "0" and Monkey == Box:
            Climbbox()
            On = "1"
            continue

        # r5规则:猴子在箱子上,并且处在香蕉位置,且猴子没有摘取香蕉,才能摘取香蕉
        if On == "1" and Box == Banana and H == "0":
            Grasp()
            H = "1"
            continue

        # r4规则:猴子在箱子上,但不在香蕉的位置,爬下箱子
        if On == "1" and Box != Banana:
            Dropbox()
            On = "0"
            continue

        # 猴子取到香蕉,则结束循环
        if H == "1":
            break

编程笔记 » 猴子摘香蕉问题

赞同 (34) or 分享 (0)
游客 发表我的评论   换个身份
取消评论

表情
(0)个小伙伴在吐槽