for rout in range(1,6):
print 'From: '+str(int(s_dict[rout]['Origin']))+','+' to: '+str(int((s_dict[rout]['Destination'])))+','+' Stops: '+(s_dict[rout]['Stops'])+','+' Cost: '+(s_dict[rout]['Cost'])+','+' Time: '+(s_dict[rout]['Time'])
print 'All routes:'
for n in range(len(all_path[rout-1])):
all_routs=''
for s in range(len(all_path[rout-1][n])):
all_routs+= str(all_path[rout-1][n][s])
stops=str(len(all_routs)-2)
cost=0
for trips in range(len(sec)):
if sec[trips][X]==(all_path[rout-1][n][0]) or sec[trips][X]==(all_path[rout-1][n][1]):
cost+=sec[trips][3]
print '->'.join(all_routs)+', Stops: '+stops+', Cost: '+str(cost)X索引不是代码的一部分,因为它是导致问题的原因,我找不到适当的方法来索引它
代码的目的是接收s_dict的“请求”,并将其与main_dict的trip.信息相匹配。在s_dict[0]中,客户希望从Origin '2‘到Destination '5',Cost是0,这意味着价格不重要,Time、Stops是99,这也意味着不管有多少用户。现在,我应该找到从“2”到“5”的所有可用路径,并返回每个成本/消耗时间的金额。
s_dict={ {“起源”:“002”、“目的地”:“005”、“成本”:“0000.00”、“停止”:“99”、“时间”:“00.00”}, {“起源”:“002”,“目的地”:“005”,“成本”:“0000.00”,“停止”:“11”,“时间”:'00.00'}, {“起源”:“002”、“目的地”:“005”、“成本”:“1450.11”、“停止”:“99”、“时间”:“00.00”}, {‘起源’:'004',‘目的地’:'005',‘成本’:'1550.11',‘停止’:'99',‘时间’:'22.22'}, {“原产地”:“001”、“目的地”:“005”、“成本”:“0000.00”、“停止”:'99‘、’时间‘:’11.00‘} main_dict= {1:{“原产地”:“001”,“目的地”:“002”,“成本”:“0100.00”,“时间”:“04.00”}, {“原产地”:“002”,“目的地”:“003”,“成本”:“0500.00”,“时间”:“01.50”}, {“起源”:“002”,“目的地”:“004”,“成本”:“0700.00”,“时间”:“10.00”}, {“原产地”:“002”,“目的地”:“005”,“成本”:“1500.00”,“时间”:“05.75”}, {“原产地”:“003”,“目的地”:“004”,“成本”:“0200.00”,“时间”:“11.40”}, {“原产地”:“004”,“目的地”:“005”,“成本”:“0750.00”,“时间”:“10.50”}, {‘起源’:'004',‘目的地’:'006',‘成本’:'0550.00',‘时间’:'06.75'}}
我从main_dict中取出信息,因为这样做对我来说更容易,所以我制作了sec。
sec=[ 1,2,4.0,100.0, 2,3,1.5,500, 2,4,10.0,700, 2,5,5.75,1500.0, 3,4,11.4,200.0, 4,5,10.5,750.0, 4、6、6.75、550.0] all_path=[ [2,3,4,5,2,4,5,2,5], [4,5],[1,2,3,4,5,1,2,4,5,1,2,2,5]
all_path来自于s_dict,在第三种情况下,它需要2个特定值--它是2-> 5,这意味着我想从原点2到目的地5,我应该显示每个可用的路径,也就是说2->3->4->5 ->4->5,2->5 ->5,2->5,现在我想得到每一次旅行的成本。换句话说,如果我们取2->4->5,那么在s_dict中它将以2作为原点,3作为目标,在cost变量中输出成本值,然后以3作为原点和4作为目标,并将成本与值相加到cost变量。我的问题是在索引if sec[trips][0]==(all_path[tin-1][n][X]) or sec[trips][1]==(all_path[tin-1][n][X]):中,问题主要是如何索引X "X不是代码的一部分“我已经尝试了很多方法来解决它,但是它不能工作,最好是一直改变目的地,保持相同的原点,所以成本2->3 + 2->4 +2>5而不是2->3 + 3->4 +4>5。
发布于 2017-01-01 20:32:16
经过长时间的评论,我认为这是无法回答你的具体问题。我假设您在到达costs=99时已经解决了all_path问题。在这种情况下,我认为您应该将代码重构为如下所示,以摆脱您所处的索引地狱。这绝不是完美的精炼,但希望更容易理解。
import random
import itertools
###### Generate some fake data for our dict ######
# First get all of our location pairings as tuples
loc_pairs = list(itertools.product(range(1, 6), range(1, 6)))
# Build cost dictionary. Tuple key is (from-location, to-location)
cost_dict = {}
for loc_pair in loc_pairs:
cost_dict[loc_pair] = {'cost': random.randint(0, 50),
'time': random.randint(0, 50)}
##### Now your data for paths ######
all_path=[[[2, 3, 4, 5], [2, 4, 5], [2, 5]], [[4, 5]], [[1, 2, 3, 4, 5],
[1, 2, 4, 5], [1, 2, 5]]]
### Build the printout
for start_location in all_path:
for route in start_location:
locations_visited = ' -> '.join(str(item) for item in route)
costs = 0
times = 0
try:
for x in range(len(route)-1):
costs += cost_dict[(route[x], route[x+1])]['cost']
times += cost_dict[(route[x], route[x+1])]['time']
print("The route: {}".format(locations_visited))
print("costs: {}".format(costs))
print("took: {}".format(times))
except:
pass假设您的main_dict在其数据结构中是准确的,您可以使用以下方法构建一个真正的成本字典:
real_costs = {1: {'Origin': '001', 'Destination': '002', 'Cost': '0100.00', 'Time': '04.00'},
2: {'Origin': '002', 'Destination': '003', 'Cost': '0500.00', 'Time': '01.50'},
3: {'Origin': '002', 'Destination': '004', 'Cost': '0700.00', 'Time': '10.00'},
4: {'Origin': '002', 'Destination': '005', 'Cost': '1500.00', 'Time': '05.75'},
5: {'Origin': '003', 'Destination': '004', 'Cost': '0200.00', 'Time': '11.40'},
6: {'Origin': '004', 'Destination': '005', 'Cost': '0750.00', 'Time': '10.50'},
7: {'Origin': '004', 'Destination': '006', 'Cost': '0550.00', 'Time': '06.75'}}
real_cost_dict = {}
for key, value in real_costs.items():
pairing = (value.get('Origin'), value.get('Destination'))
real_cost_dict[pairing] = {'cost': value.get('Cost'),
'time': value.get('Time')}发布于 2017-01-01 19:56:20
如果我对代码的理解是正确的话,这里基本上想要的是某种函数。
基本上:
def path_finding_algorithm('[INPUT]'):
//loads of processing here
//
// '[PROCESS]'
//
return '[OUTPUT]'您只向我们提供了['OUTPUT']和['PROCESS']的缩短版本。但是我们必须知道['INPUT']是什么,['PROCESS']想要做什么。
否则,没有足够的信息来回答你的问题。
一个很好的起点是陈述一种情况:
我正在设计一个路径查找算法,类似于导航应用程序。我使用一个网格映射来实现这一点,代价是顶点之间的距离。这是我的成本函数: //CODE// 有什么方法来优化它吗?
https://stackoverflow.com/questions/41417837
复制相似问题