-
Notifications
You must be signed in to change notification settings - Fork 0
/
hcvrp.pl
200 lines (172 loc) · 6.27 KB
/
hcvrp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
:- lib(ic).
:- lib(ic_global).
:- lib(branch_and_bound).
:- set_flag(print_depth, 10000).
vehicles([35, 40, 55, 15, 45, 25, 85, 55]).
clients([c(15, 77, 97), c(23, -28, 64), c(14, 77, -39), c(13, 32, 33), c(18, 32, 8), c(18, -42, 92), c(19, -8, -3),
c(10, 7, 14), c(18, 82, -17), c(20, -48, -13), c(15, 53, 82), c(19, 39, -27), c(17, -48, -13), c(12, 53, 82),
c(11, 39, -27), c(15, -48, -13), c(25, 53, 82), c(14, -39, 7), c(22, 17, 8), c(23, -38, -7)]).
hcvrp(NCl, NVe, Timeout, Solution, Cost, Time) :-
Timeout =:= 0, !,
Full is NCl * NVe,
length(Result, Full), %% flattened list
Result #:: 0..NCl,
clients(AllClients),
vehicles(AllVehicles),
getN(NCl, AllClients, Clients),
getN(NVe, AllVehicles, Vehicles),
getValuesLists(Clients, Quantities, Xs, Ys),
distances(Xs, Ys, NCl, Distances), %% construct distance matrix
constraints(Result, Quantities, Vehicles, NVe, NCl, Distances),
costDef(Result, NVe, NCl, Distances, Costs),
flatten(Costs, FlatCosts),
Cost #= sum(FlatCosts),
bb_min(search(Result, 0, first_fail, indomain_split, complete, []), Cost, _),
unflatten(Result, NCl, Sol), %% convert to list of lists
deleteAllZeros(Sol, Solution).
hcvrp(NCl, NVe, Timeout, Solution, Cost, Time) :-
Timeout =\= 0, !,
Full is NCl * NVe,
length(Result, Full), %% flattened list
Result #:: 0..NCl,
clients(AllClients),
vehicles(AllVehicles),
getN(NCl, AllClients, Clients),
getN(NVe, AllVehicles, Vehicles),
getValuesLists(Clients, Quantities, Xs, Ys),
distances(Xs, Ys, NCl, Distances), %% construct distance matrix
constraints(Result, Quantities, Vehicles, NVe, NCl),
costDef(Result, NVe, NCl, Distances, Costs),
flatten(Costs, FlatCosts),
Cost #= sum(FlatCosts),
bb_min(search(Result, 0, first_fail, indomain_split, complete, []), Cost, bb_options{timeout:Timeout}),
unflatten(Result, NCl, Sol), %% convert to list of lists
deleteAllZeros(Sol, Solution).
constraints(Result, Quantities, Vehicles, NVe, NCl) :-
capacities(Result, 1, NVe, NCl, Quantities, Vehicles),
count_occurrences(Result, NCl, 0).
count_occurrences(_, NCl, NCl).
count_occurrences(Result, NCl, Client) :-
Cl is Client + 1,
occurrences(Cl, Result, 1),
count_occurrences(Result, NCl, Cl).
%% For each vehicle make sure that orders <= capacity
capacities([], _, _, _, _, _).
capacities(Result, Vehicle, NVe, NCl, Quantities, Vehicles) :-
getRow(Result, Remain, Row, NCl, 1),
element(Vehicle, Vehicles, Capacity),
checkRow(Row, Quantities, Res),
sum(Res) #=< Capacity,
Next is Vehicle + 1,
capacities(Remain, Next, NVe, NCl, Quantities, Vehicles).
costDef([], _, _, _, []).
costDef(Result, NVe, NCl, Distances, [Cs|Costs]) :-
getRow(Result, Remain, Row, NCl, 1),
consecutive(Row, NCl, Indices), %% Returns list of indices for distance matrix
costPerRow(Indices, Distances, Cs),
costDef(Remain, NVe, NCl, Distances, Costs), !.
%% Returns list of distances per vehicle
costPerRow([], _, []).
costPerRow([I|Indices], Distances, [C|Costs]) :-
element(I, Distances, C),
costPerRow(Indices, Distances, Costs).
%% Returns orders of row's clients
checkRow([], _, []).
checkRow([C|Clients], Quantities, [Q|Result]) :-
C #> 0, !,
element(C, Quantities, Q),
checkRow(Clients, Quantities, Result).
checkRow([C|Clients], Quantities, [0|Result]) :-
C #= 0,
checkRow(Clients, Quantities, Result).
%% Returns I-th row of flattened list
getRow([L|List], List, [L|[]], NCl, NCl).
getRow([L|List], Remain, [L|Row], NCl, Index) :-
I is Index + 1,
getRow(List, Remain, Row, NCl, I), !.
%% Returns first N elements from list
getN(N, Full, Partial) :-
length(Partial, N),
append(Partial, _, Full).
getValues(c(D, X, Y), D, X, Y).
getValuesLists([], [], [], []).
getValuesLists([H|Clients], [Q|Quantities], [X|Xs], [Y|Ys]) :-
getValues(H, Q, X, Y),
getValuesLists(Clients, Quantities, Xs, Ys).
%% Euclidean distance of two points
distance(Xs, Ys, Index1, Index2, Distance) :-
Index1 =\= 0, Index2 =\= 0, !,
element(Index1, Xs, X1), element(Index2, Xs, X2),
element(Index1, Ys, Y1), element(Index2, Ys, Y2),
Z is (X2-X1)*(X2-X1) + (Y2-Y1)*(Y2-Y1),
sqrt(Z, D),
Dis is round(D * 1000),
Distance is integer(Dis).
distance(Xs, Ys, Index1, Index2, Distance) :-
Index1 =\= 0, Index2 =:= 0, !,
element(Index1, Xs, X1),
element(Index1, Ys, Y1),
Z is X1*X1 + Y1*Y1,
sqrt(Z, D),
Dis is round(D * 1000),
Distance is integer(Dis).
distance(Xs, Ys, Index1, Index2, Distance) :-
Index1 =:= 0, Index2 =\= 0, !,
element(Index2, Xs, X2),
element(Index2, Ys, Y2),
Z is X2*X2 + Y2*Y2,
sqrt(Z, D),
Dis is round(D * 1000),
Distance is integer(Dis).
distance(_, _, _, _, 0).
%% Construct (NCl+1)*(NCl+1) matrix
calcDistances(_, _, NCl, Client1, _, []) :-
Client1 > NCl, !.
calcDistances(Xs, Ys, NCl, Client1, Client2, Distances) :-
Client2 > NCl, !,
NextClient1 is Client1 + 1,
NextClient2 is 0,
calcDistances(Xs, Ys, NCl, NextClient1, NextClient2, Distances).
calcDistances(Xs, Ys, NCl, Client1, Client2, [D|Distances]) :-
distance(Xs, Ys, Client1, Client2, D), !,
NextClient2 is Client2 + 1,
calcDistances(Xs, Ys, NCl, Client1, NextClient2, Distances).
%% Calculate distance between all points
distances(Xs, Ys, NCl, Distances) :-
calcDistances(Xs, Ys, NCl, 0, 0, Distances).
%% Input: list of clients to be served,
%% Output: indices of distance matrix
consecutive(Clients, NCl, Result) :-
pairs(Clients, NCl, 0, Result).
pairs([], NCl, Previous, [Index|Result]) :-
Previous =\= 0,
Index is (Previous*NCl+1) + 1,
pairs([], NCl, 0, Result).
pairs([], _, Previous, []) :-
Previous =:= 0.
pairs([C], NCl, Previous, [Index|Result]) :-
Index is Previous*(NCl+1) + C + 1,
pairs([], NCl, C, Result).
pairs([C|[Next|Clients]], NCl, Previous, [Index|Result]) :-
Index is Previous*(NCl+1) + C + 1,
pairs([Next|Clients], NCl, C, Result), !.
%% Printing Purposes
%% Convert list to list of lists, N is the length of each sublist
unflatten([], _, []).
unflatten(L, N, UL) :-
unflt(L, 1, N, UL), !.
unflt([H|T], N, N, [[H]|UL]):-
unflatten(T, N, UL).
unflt([H|T], N1 , N, [[H|TMP]|UL]):-
N2 is N1 + 1,
unflt(T, N2 , N, [TMP| UL]).
%% Delete zeros from list of results
deleteAllZeros([], []).
deleteAllZeros([H|T], [L|Result]) :-
deleteZeros(H, L),
deleteAllZeros(T, Result).
deleteZeros([],[]).
deleteZeros([0|T], L) :- !,
deleteZeros(T, L).
deleteZeros([H|T], [H|T2]) :-
deleteZeros(T, T2).