You are on page 1of 4

Ariyan M Kabir

ENME 696 HW2


Problem 1a
Backward Value Iteration

#Q:nodelist
#NCT:NodeConnectionTable.
#NCT[i][k]:CosttoGofromnode'i'tonode'k'
#CG:CosttoGoTable
#CG[i][1]:CosttoGofromnode'i'togoalnode
#CC[i][2]:Optimalnextnodefornode'i'
Q=[0,1,2,3,4]
NCT=[
[1000,2,1000,1000,1000],
[1,1000,4,1000,1000],
[1000,1000,1000,3,7],
[1000,1000,1,1,1],
[1000,1000,1000,1000,1000]]
CG=[[0,1000,1000],[1,1000,1000],[2,1000,1000],[3,1000,1000],[4,0,4]]
N=len(Q)
k=N1
steps=10#limitonnumberofsteps
count=0
while(k>1):
if(count>steps):break
foriinQ:
if(NCT[i][k]<1000andi!=kandCG[i][1]>CG[k][1]+NCT[i][k]):
CG[i][1]=CG[k][1]+NCT[i][k]
CG[i][2]=k
k=k1
count=count+1
foriinrange(0,N):
if(CG[i][1]==0):
print"optimalcosttogofornode%c:%d\n"%(chr(CG[i][0]+97),CG[i][1])
else:
print"optimalcosttogofornode%c:%d,optimalnextnode:%c\n"%(chr(CG[i]
[0]+97),CG[i][1],chr(CG[i][2]+97))
print"donein%dstep:\n"%(min(steps,count))

Output:
optimal cost to go for node a: 10, optimal next node: b
optimal cost to go for node b: 8, optimal next node: c
optimal cost to go for node c: 4, optimal next node: d
optimal cost to go for node d: 1, optimal next node: e
optimal cost to go for node e: 0
done in 5 steps:

Ariyan M Kabir
ENME 696 HW2
Problem 1b
Forward Value Iteration

#Q:nodelist
#NCT:NodeConnectionTable.
#NCT[i][k]:CosttoGofromnode'i'tonode'k'
#CC:CosttoComeTable
#CC[i][1]:CosttoCometonode'i'frominitialnode
#CC[i][2]:Optimalpreviousnodefornode'i'
Q=[0,1,2,3,4]
NCT=[
[1000,2,1000,1000,1000],
[1,1000,4,1000,1000],
[1000,1000,1000,3,7],
[1000,1000,1,1,1],
[1000,1000,1000,1000,1000]
]
CC=[[0,0,0],[1,1000,1000],[2,1000,1000],[3,1000,1000],[4,1000,1000]]
N=len(Q)
foriinQ:
forkinQ:
if(NCT[i][k]<1000andi!=kandCC[k][1]>NCT[i][k]+CC[i][1]):
CC[k][1]=NCT[i][k]+CC[i][1]
CC[k][2]=i
foriinQ:
print"optimalCosttoCometonode%c:%d,optimalpreviousnode:%c\n"%(chr(CC[i]
[0]+97),CC[i][1],chr(CC[i][2]+97))

Output:
optimal Cost-to-Come to node a: 0, optimal previous node: a
optimal Cost-to-Come to node b: 2, optimal previous node: a
optimal Cost-to-Come to node c: 6, optimal previous node: b
optimal Cost-to-Come to node d: 9, optimal previous node: c
optimal Cost-to-Come to node e: 10, optimal previous node: d

Problem 2a
Backward Dijkstra's Algorithm
#Q:nodelist
#NCT:NodeConnectionTable.
#NCT[i][k]:CosttoGofromnode'i'tonode'k'
#CG:CosttoGoTable
#CG[i][1]:CosttoGofromnode'i'
#CG[i][2]:Optimalnextnodefornode'i'
Q=[0,1,2,3,4]
NCT=[
[1000,10,3,1000,1000],
[1000,1000,1,2,1000],
[1000,4,1000,8,2],
[1000,1000,1000,1000,7],
[1000,1000,1000,9,1000],
]
CG=[
[0,1000,1000],
[1,1000,1000],
[2,1000,1000],
[3,0,0],
[4,1000,1000],
]
N=len(Q)
#PopulatingCG,CosttoGoTable
while(Q):
s=CG[0][0]
forkinQ:
if(CG[k][1]<CG[s][1]ands!=k):
s=k
foriinrange(0,N):
if(NCT[i][s]<1000andi!=sandCG[i][1]>NCT[i][s]+CG[s][1]):
CG[i][1]=NCT[i][s]+CG[s][1]
CG[i][2]=s
print"donefors=%d,i=%d"%(s,i)
Q.remove(s)
foriinrange(0,N):
if(CG[i][1]==0):
print"\ncosttogofornode%c:%d\n"%(chr(CG[i][0]+97),CG[i][1])
else:
print"\ncosttogofornode%c:%d,optimalnextnode%c\n"%(chr(CG[i]
[0]+97),CG[i][1],chr(CG[i][2]+97))

Output:
cost to go for node a : 9, optimal next node c
cost to go for node b : 2, optimal next node d
cost to go for node c : 6, optimal next node b
cost to go for node d : 0
cost to go for node e : 9, optimal next node d

Ariyan M Kabir
ENME 696 HW2
Problem 2b
Comparison of Backward Value Iteration with Backward Dijkstra's Algorithm

Both the algorithm starts testing from goal node and goes backward calculating the Cost-to-Go
for each node.
In both the algorithms, each node is tested in terms of cost to come to this node from its all
possible previous nodes to find the minimum cost to go for those previous nodes.
In Backward Value Iteration,

You might also like