Tuesday, February 20, 2018

Best First Search AI

Tags

Code :

alist={
    'A':['B','C','D'],
    'B':['H'],
    'C':['G','F'],
    'D':['E','P'],
    'E':[],
    'F':[],
    'G':[],
    'H':['J','I'],
    'I':['M','L','K'],
    'J':[],
    'K':[],
    'L':[],
    'M':[],
    'P':[]

}

hfun={
    'A':0,
    'B':5,
    'C':6,
    'D':3,
    'E':8,
    'F':12,
    'G':14,
    'H':7,
    'I':5,
    'J':6,
    'K':1,
    'L':10,
    'M':2,
    'P':9}

def fsort(v):
    for j in range(0,len(v)):
        for i in range(0,len(v)-j-1):
            if(hfun[v[i]]>hfun[v[i+1]]):
                temp=v[i+1];
                v[i+1]=v[i];
                v[i]=temp;


oqueue=[];
cqueue=[];
goal='I'
oqueue.append('A');
success=Falsewhile len(oqueue)!=0:
    node= oqueue.pop(0)
    if node is goal:
        success=True        break    for i in alist[node]:
        oqueue.append(i)

    cqueue.append(node)

    fsort(oqueue)
    print(cqueue)

if(success):
    print("Found")
else:
    print("Not Found")

Output:
Please try at your IDE