我有一個有效的車輛路徑問題解決方案,它是在 Python 中使用 Google 的 OR-Tools 實現的。我有一個包含 16 個位置的時間矩陣、每個位置的時間窗口以及丟棄每個位置的懲罰。所有值都以秒為單位。我有意只用一輛車解決這個問題(實質上是解決旅行推銷員問題)。只要需要,我允許車輛在任何地點等待。我已將某些位置的掉落懲罰設置得非常高,因為我不想讓它們掉落。時間矩陣中表示的每個位置都會有一個時間窗口,它表示自一天開始以來的時間(28800 相當于上午 8:00,64800 相當于下午 6:00,等等)我設置上限最大值為 64800,因為我希望車輛在下午 6:00 之前完成。我已將矩陣中的第一個位置指定為起始位置?,F在,我希望矩陣中的第二個位置是結束位置。下面是我的源代碼 - 它運行成功,但確實使用矩陣中的第二個位置作為它創建的解決方案的結束位置。輸出{'Dropped':?[4,?5],?'Schedule':?[[0,?28800,?28800],?[9,?28901,?29249],?[13,?31173,?31521],?[15,?33414,?33762],?[8,?36292,?36640],?[14,?39535,?39883],?[2,?43200,?43200],?[6,?45676,?46195],?[7,?47868,?48387],?[3,?50400,?50400],?[11,?54641,?57541],?[10,?56997,?59897],?[12,?59663,?62563],?[1,?64800,?64800],?[0,?64800,?64800]]}我的理解是需要對 RoutingIndexManager 進行主要更改。就像 Google OR-Tools 文檔似乎表明的那樣,我嘗試了以下更改:manager?=?pywrapcp.RoutingIndexManager(len(Matrix),1,0)到manager?=?pywrapcp.RoutingIndexManager(len(Matrix),1,[0],[1])但這會導致錯誤:WARNING:?Logging?before?InitGoogleLogging()?is?written?to?STDERR
F0820?15:13:16.748222?62401984?routing.cc:1433]?Check?failed:?kUnassigned?!=?indices[i]?(-1?vs.?-1)?
***?Check?failure?stack?trace:?***我在使用 OR 工具時是否存在任何明顯的錯誤?我很樂意回答任何問題。任何幫助將不勝感激!謝謝你!
1 回答

海綿寶寶撒
TA貢獻1809條經驗 獲得超8個贊
首先,正確更改管理器構造函數代碼。
????#?Create?the?routing?index?manager.[-]?manager?=?pywrapcp.RoutingIndexManager(len(Matrix),?1,?0) [+]?manager?=?pywrapcp.RoutingIndexManager(len(Matrix),?1,?[0],?[1])
其次,您不能在析取中指定開始或結束節點:
????#?Allow?to?drop?nodes.[-]?for?node?in?range(1,?len(Matrix)): [+]?for?node?in?range(2,?len(Matrix)): ??????routing.AddDisjunction([manager.NodeToIndex(node)],?Penalties[node])
第三,您不能在開始或結束節點上使用NodeToIndex(node_index)
,因為它并不總是雙射。
????#?Add?time?window?constraints?for?each?location?except?start?and?end?location. ????for?location_idx,?time_window?in?enumerate(Windows): [-]???if?location_idx?==?0: [+]???if?location_idx?==?0?or?location_idx?==?1:????????continue ??????index?=?manager.NodeToIndex(location_idx) ??????time_dimension.CumulVar(index).SetRange(time_window[0],?time_window[1])
最后,一定要在結束位置設置一個時間窗口:
????index?=?routing.Start(0) ????time_dimension.CumulVar(index).SetRange(Windows[0][0],Windows[0][1]) [+]?index?=?routing.End(0) [+]?time_dimension.CumulVar(index).SetRange(Windows[1][0],Windows[1][1])
添加回答
舉報
0/150
提交
取消