I wrote the following on a whim. I realize it does not do what you need (it creates a random path, then calculates the distance of that path); but maybe it will give you some ideas.
Code
from random import choice
#TEST PURPOSES, hardcoded user input
starting_node = '3'
#data about system
exits=[['0'],['5'],['1','4'],['2','6'],['1','5'],['2'],['2','3']]
distance = dict()
distance[1,5]=-1
distance[2,1]=1
distance[2,4]=2
distance[3,2]=2
distance[3,6]=-8
distance[4,1]=-4
distance[4,5]=3
distance[5,2]=7
distance[6,2]=5
distance[6,3]=10
#create random path of length 10
def Make_Move(path,current_hop,current_location):
next_step = choice(exits[int(current_location)])
path+=next_step
current_location=next_step
if current_hop == 10:
return path
else:
current_hop+=1
return Make_Move(path,current_hop,current_location)
#calculate distance of given path
def Calculate_Distance(path):
sum_of_distance = 0
for x in range(0,9):
initial_location = int(path[x])
final_location = int(path[x+1])
sum_of_distance+=distance[initial_location,final_location]
return sum_of_distance
path = Make_Move(starting_node,2,starting_node)
distance = Calculate_Distance(path)
print 'physical path = ' + path
print 'distance = ' + str(distance)
This post was edited by Azrad on Apr 4 2013 09:16pm