人工智能实验代码整理

人工智能实验代码整理

状态空间表示——猴子摘香蕉

# -*- coding: utf-8 -*-
# State类,记录当前状态

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(state_i):
    state_iplus1 = State()
    state_iplus1.monkey = state_i.monkey
    state_iplus1.box = state_i.box
    state_iplus1.banana = state_i.banana
    state_iplus1.monbox = state_i.monbox
    return state_iplus1
# function monkeygoto,it makes the monkey goto the other place
def monkeygoto(b,i):
    a=b
    if (a==-1):
        States[i+1]=copyState(States[i])
        States[i+1].monkey=-1
        routesave.insert(i,"Monkey go to A")
    elif(a==0):
        States[i+1]=copyState(States[i])
        States[i+1].monkey=0
        routesave.insert(i,"Monkey go to B")
    elif(a==1):
        States[i+1]=copyState(States[i])
        States[i+1].monkey=1
        routesave.insert(i,"Monkey go to C")
    else:
        print("parameter is wrong")

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



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


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


# 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+1):
        print("Step %d : %s \n"%(c+1,routesave[c]))



# perform next step
def nextStep(i):
    #print("nextstep"+str(i))
    #print States[i].box
    j=0
    if(i>=150):
        print("%s  \n", "steplength reached 150,have problem ")
    if(States[i].monbox == 1 and States[i].monkey == States[0].banana and States[i].banana == States[0].banana and States[i].box == States[0].banana):
        print("sss")
        showSolution(i)
        return      

    for c in range(i):
        if(States[c].monkey==States[i].monkey and States[c].box==States[i].box and States[c].banana==States[i].banana and States[c].monbox==States[i].monbox ):
            showSolution(i)
            return 

      
    j=i+1
    if (States[i].box == States[i].monkey and States[i].box == States[i].banana):
        #print("1")
        if(States[i].monbox==-1):
            climbonto(i)
            reach(i + 1)
            nextStep(j)
        else:
            reach(i + 1)
            nextStep(j)
    if (States[i].box == States[i].monkey and States[i].box != States[i].banana):
        #print("2")
        if (States[i].monbox == -1):
            movebox(States[i].banana, i)
            nextStep(j)
        else:
            climbdown(i)
            nextStep(j)
    if (States[i].box != States[i].monkey and States[i].box == States[i].banana):
        #print("3")
        monkeygoto(States[i].box, i)
        nextStep(j)
    if (States[i].box !=
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值