人工智能知识表示实验1

问题描述:一个房间里,天花板上挂有一串香蕉,有一只猴子可在房间里任意活动(到处走动,推移箱子,攀登箱子等)。设房间里还有一只可被猴子移动的箱子,且猴子登上箱子时才能摘到香蕉,问猴子在某一状态下(设猴子位置为A,箱子位置为B,香蕉位置在C),如何行动可摘取到香蕉。

初始状态:猴子处在A处,箱子处在B处,香蕉悬挂在C处

目标状态:猴子和箱子同处C处,且猴子站在箱子B上摘到香蕉

代码如下(python):

class State:
    def __init__(self, monkey=-1, box=0, banana=1, monbox=-1):
        self.monkey = monkey  # -1:Monkey at A  0: Monkey at B  1:Monkey at C
        self.box = box  # -1:box at A  0:box at B  1:box at C
        self.banana = banana  # Banana at C,Banana=1
        self.monbox = monbox  # -1: monkey not on the box  1: monkey on the box

# 复制前一个状态情况给当前状态
def copyState(source):
    state = State()
    state.monkey = source.monkey
    state.box = source.box
    state.banana = source.banana
    state.monbox = source.monbox
    return state


# function monkeygoto,it makes the monkey goto the other place
def monkeygoto(b, i):
    state0 = State()
    state0=copyState(States[i])
    state0.monkey=b
    States.insert(i+1,state0)
    if (b == -1):
        routesave.insert(i, "Monkey go to A")
    if (b == 1):
        routesave.insert(i, "Monkey go to C")
    if (b == 0):
        routesave.insert(i, "Monkey go to B")


# function movebox,the monkey move the box to the other place
def movebox(a, i):
    state0 = State()
    state0=copyState(States[i])
    state0.monkey=a
    state0.box=a
    States.insert(i+1,state0)
    if (a == -1):
        routesave.insert(i, "Monkey move box to A")
    if (a == 1):
        routesave.insert(i, "Monkey move box to C")
    if (a == 0):
        routesave.insert(i, "Monkey move box to B")


# function climbonto,the monkey climb onto the box
def climbonto(i):
    state0 = State()
    state0=copyState(States[i])
    state0.monbox=1
    States.insert(i+1,state0)
    routesave.insert(i, "Monkey climb onto the box")


# function climbdown,monkey climb down from the box
def climbdown(i):
    state0 = State()
    state0=copyState(States[i])
    state0.monbox=-1
    States.insert(i+1,state0)
    routesave.insert(i, "Monkey climb down from the box")


# function reach,if the monkey,box,and banana are at the same place,the monkey reach banana
def reach(i):
    routesave.insert(i, "Monkey reach the banana")

# output the solution to the problem
def showSolution(i):
    print("Result to problem:")
    for c in range(i):
        print("Step %d : %s \n" % (c + 1, routesave[c]))

def nextStep(i):
    if (i >= 150):
        print("%s  \n", "steplength reached 150,have problem ")
    else:
        if States[i].banana == States[i].box:
            if States[i].monbox==1:
                reach(i)
                i += 1
                showSolution(i)
                return 1
            else:
                if States[i].monkey == States[i].box:
                    climbonto(i)
                    i += 1
                    return nextStep(i)
                else:
                    monkeygoto(States[i].box, i)
                    i += 1
                    return nextStep(i)
        else:
            if States[i].monbox==1:
                climbdown(i)
                i += 1
                return nextStep(i)
            else:
                if States[i].monkey == States[i].box:
                    movebox(States[i].banana, i)
                    i += 1
                    return nextStep(i)
                else:
                    monkeygoto(States[i].box, i)
                    i += 1
                    return nextStep(i)

if __name__ == "__main__":
    s = input("please input state: monkey, box, banana, ifMonkeyIsOnBox: \n")
    states = s.split(" ")
    state = State(int(states[0]), int(states[1]), int(states[2]), int(states[3]))
    States = [None] * 150
    routesave = [None] * 150
    States.insert(0, state)
    nextStep(0)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值