-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsist2class.pl
More file actions
122 lines (108 loc) · 4 KB
/
consist2class.pl
File metadata and controls
122 lines (108 loc) · 4 KB
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
:- module(consist, [
consist/2
]).
/** <module> Tool for arranging train consists
*
* This version handles keeping the chair car passengers from
* walking through the sleepign cars.
*/
:- use_module(library(clpfd)).
%! consist(+Cars:list, -OrderedCars:list) is nondet
%
% @arg Cars a bag of cars as a list
% @arg OrderedCars a valid ordering of the cars as a list
%
% for simplicity the engine's assumed.
%
consist(Cars, OrderedCars) :-
% for sanity, make sure Cars is a ground list
% of cars
is_list(Cars),
ground(Cars),
maplist(car_type, Cars),
% we'll need the train length for many things
length(Cars, Len),
% create a list of the train positions
% 1 based
length(Order, Len),
Order ins 1..Len,
all_different(Order),
% add the constraints
constrain_positions(Cars, Order),
reduce_sleeper_traffic(Cars, Order),
% minimize distance to diner
% we constrain Dist to be distance to diner
diner_dist(Cars, Order, Dist),
% label the cars
labeling([min(Dist), ffc, enum], [Dist| Order]), % ffc dramatically faster
pairs_keys_values(Pairs, Order, Cars),
keysort(Pairs, OrderedPairs),
pairs_keys_values(OrderedPairs, _, OrderedCars).
reduce_sleeper_traffic(Cars, Order) :-
% find the limits of chair cars
( memberchk(chair, Cars)
-> rst(Cars, Order)
; true
).
rst(Cars, Order) :-
nth1(I, Cars, chair),
nth1(I, Order, A),
find_chair_diner_limits(Cars, Order, A, A, Min, Max),
enforce_no_sleepers(Cars, Order, Min, Max).
find_chair_diner_limits([], _Order, Min, Max, Min, Max).
find_chair_diner_limits([chair | Cars], [O | Order],
CurMin, CurMax, Min, Max) :-
NewCurMin #= min(CurMin, O),
NewCurMax #= max(CurMax, O),
find_chair_diner_limits(Cars, Order, NewCurMin, NewCurMax, Min, Max).
find_chair_diner_limits([diner | Cars], [O | Order],
CurMin, CurMax, Min, Max) :-
NewCurMin #= min(CurMin, O),
NewCurMax #= max(CurMax, O),
find_chair_diner_limits(Cars, Order, NewCurMin, NewCurMax, Min, Max).
find_chair_diner_limits([X | Cars], [_ | Order],
CurMin, CurMax, Min, Max) :-
\+ memberchk(X, [diner, chair]),
find_chair_diner_limits(Cars, Order, CurMin, CurMax, Min, Max).
enforce_no_sleepers(Cars, Order, Min, Max) :-
maplist(no_zzz(Min, Max), Cars, Order).
no_zzz(Min, Max, sleeper, Order) :-
Order #< Min #\/ Order #> Max.
no_zzz(_, _, X, _) :-
X \= sleeper.
constrain_positions(Cars, Order) :-
length(Cars, Len),
% min and max car # passengers can reach
[MinWalk, MaxWalk] ins 1..Len,
MinWalk #=< MaxWalk,
maplist(a_car(Len, MinWalk, MaxWalk), Cars, Order).
% the observation must be at the end, and we must
% be able to walk to it
a_car(Len, _MinWalk, Len, observation, Len).
% the baggage and RPO cars must be outside the walk area
a_car(_Len, MinWalk, MaxWalk, rpo, Order) :-
Order #> MaxWalk #\/ Order #< MinWalk.
a_car(_Len, MinWalk, MaxWalk, baggage, Order) :-
Order #> MaxWalk #\/ Order #< MinWalk.
% other cars must be walkable
a_car(_Len, MinWalk, MaxWalk, Car, Order) :-
member(Car, [sleeper, chair, diner, lounge, dome]),
Order #>= MinWalk,
Order #=< MaxWalk.
car_type(X) :-
member(X, [rpo, baggage, sleeper, chair, diner, lounge, dome, observation]).
diner_dist(Cars, _, 0) :-
\+ memberchk(diner, Cars). % no diner!
diner_dist(Cars, Order, Dist) :-
nth1(DinerIndex, Cars, diner), % diner location in Cars
nth1(DinerIndex, Order, DinerLoc), % unbound diner location
diner_dist(Cars, Order, DinerLoc, 0, Dist).
diner_dist([], _, _, Dist, Dist).
diner_dist([Car | T], [Order|OT], DinerLoc, SoFar, Dist) :-
member(Car, [sleeper, chair]),
NewSoFar #= SoFar + abs(DinerLoc - Order),
diner_dist(T, OT, DinerLoc, NewSoFar, Dist).
% these cars don't count
diner_dist([Car | T], [_|OT], DinerLoc, SoFar, Dist) :-
member(Car, [rpo, baggage, diner, lounge, dome, observation]),
diner_dist(T, OT, DinerLoc, SoFar, Dist).