[英]ECLiPSe CLP - TSP with Time WIndow. How do i calculate the cost?
我正在研究 TSP 的變體,其中每個節點都有一個時間 Window,但我在計算成本 function 時遇到了一些問題。 我使用的是繼任者 model,所以我有一個列表,其中每個變量表示下一個目的地,Xi = j 是從節點 i 到節點 j 的鏈接。
我的代碼如下所示:
:-lib(ic).
:-lib(branch_and_bound).
:-lib(propia).
:-[nodes].
tsp(Next, Cost):-
Cost::1..10000,
Next::1..10000,
%Constraints
alldifferent(Next),
different_from_index(Next),
circuit(Next),
create_objective(Next, Cost),
minimize(labeling(Next), Cost).
其中different_from_index是 Next 變量的索引與其值之間的約束: Next[i] != i ,而create_objective是定義目標 function 的謂詞。 首先 create_objective 謂詞創建鏈接成本列表,因此使用簡單的 sumlist 謂詞很容易獲得成本。 但是我需要為每個節點定義一個時間 window 我想是這樣的:
time_window([], _, _, 0).
time_window([HCost | TCost], Next, Start, Cost):-
element(Start, Next, Destination),
time_window(TCost, Next, Destination, Cost1),
Cost #= Cost1 + HCost,
node(Destination, Min, Max) infers most,
Cost #>= Min, Cost #=< Max.
[H成本| TCost] 是前面提到的成本列表,但已排序和反轉(因此我將列表的 n 元素作為第一個鏈接,將 n-1 作為第二個鏈接,依此類推)。 此外,節點謂詞包含在開頭加載的 prolog 文件中。 不幸的是,這似乎不起作用:它既不返回 false 也不返回錯誤的解決方案。 經過一段時間的計算,我收到此消息:
[eclipse 2]: tsp(Next, Cost).
bb_min: search did not instantiate cost variable
Aborting execution ...
Abort
我理解錯誤,但我不知道如何解決它。 我使用更簡單的 model 和類似的 time_window 謂詞成功地做到了這一點,但在這種情況下似乎不合適。
誰能幫我嗎? 謝謝指教。
讓我們從以下基本 TSP 程序開始。 它將距離矩陣Dist
作為輸入,並維護一個后繼數組Next
和一個距離數組Legs
,其中包含從每個節點到其后繼節點的距離。 我們最小化Cost
,這是行駛的總距離。
:- lib(ic).
:- lib(branch_and_bound).
tsp(Dist, Next, Cost) :-
dim(Dist, [N,N]), % get distance matrix size
dim(Next, [N]), % successor variables
Next #:: 1..N,
circuit(Next), % they must form a circuit
dim(Legs, [N]), % Legs[I] is distance I->Next[I]
( for(I,1,N), param(Dist,Next,Legs) do
element(I, Next, J),
element(J, Dist[I], DistIJ),
element(I, Legs, DistIJ)
),
Cost #= sum(Legs),
% search and minimize
minimize(search(Legs,0,smallest,indomain_min,complete,[]), Cost).
要啟用時間 window 處理,請添加一個數組Time
給出每個節點的到達時間,然后可以根據要求對其進行約束。 節點I
的后繼節點的到達時間可以計算為I
的到達時間加上從I
到其后繼節點的旅行時間(為簡單起見,假設距離 = 時間,並且我們在時間 0 從節點 1 開始)。 這將導致
tsp(Dist, Next, Time, Cost) :-
dim(Dist, [N,N]), % get distance matrix size
dim(Next, [N]), % successor variables
Next #:: 1..N,
circuit(Next), % they must form a circuit
dim(Legs, [N]), % Legs[I] is distance I->Next[I]
dim(Time, [N]), % Time[I] is arrival time at I
( for(I,1,N), param(Dist,Next,Legs,Time) do
element(I, Next, J),
element(J, Dist[I], DistIJ),
element(I, Legs, DistIJ),
( I==1 -> TimeAtI = 0 ; element(I, Time, TimeAtI) ),
element(J, Time, TimeAtJ),
TimeAtJ #= TimeAtI + DistIJ
),
Cost #= sum(Legs), % total distance travelled
% search and minimize
minimize(search(Legs,0,smallest,indomain_min,complete,[]), Cost).
樣品運行:
?- data(b6, Dist), tsp(Dist, Next, Time, Cost).
Found a solution with cost 2495
Found a solution with cost 2441
Found a solution with cost 2336
Found no solution with cost 1525.0 .. 2335.0
Dist = []([](0, 153, 510, 706, 966, 581),
[](153, 0, 422, 664, 997, 598),
[](510, 422, 0, 289, 744, 390),
[](706, 664, 289, 0, 491, 265),
[](966, 997, 744, 491, 0, 400),
[](581, 598, 390, 265, 400, 0))
Next = [](2, 3, 4, 5, 6, 1)
Time = [](2336, 153, 575, 864, 1355, 1755)
Cost = 2336
Yes (0.00s cpu)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.