-
Notifications
You must be signed in to change notification settings - Fork 0
/
travel.py
134 lines (92 loc) · 3.13 KB
/
travel.py
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
import pyhop
def car_price(dist):
return 10 + 1.5 * dist
def car_time(dist):
return 1.2 * dist
def taxi_price(dist):
return 10 + 2.5 * dist
def taxi_time(dist):
return 15 + 1.2 * dist
def pt_price(dist):
if dist < 20:
return 2
else:
return 25
def pt_time(dist):
return 10 + 0.7 * dist
def walk_time(dist):
return 3 * dist
def ride_car(state, x, y):
if state.loc['me'] == x and state.loc['car'] == x and \
state.cash >= car_price(state.dist[x][y]) and \
state.time >= car_time(state.dist[x][y]):
state.loc['me'] = y
state.loc['car'] = y
state.cash -= car_price(state.dist[x][y])
state.time -= car_time(state.dist[x][y])
return state
return False
def ride_public_transport(state, x, y):
if state.loc['me'] == x and \
state.cash >= pt_price(state.dist[x][y]) and \
state.time >= pt_time(state.dist[x][y]):
state.loc['me'] = y
state.cash -= pt_price(state.dist[x][y])
state.time -= pt_time(state.dist[x][y])
return state
return False
def walk(state, x, y):
if state.loc['me'] == x and state.dist[x][y] < 20 and \
state.time >= walk_time(state.dist[x][y]):
state.loc['me'] = y
state.time -= walk_time(state.dist[x][y])
return state
return False
def ride_taxi(state, x, y):
if state.loc['me'] == x and state.loc['taxi'] == x and state.dist[x][y] < 50 and \
state.cash >= taxi_price(state.dist[x][y]) and \
state.time >= taxi_time(state.dist[x][y]):
state.loc['me'] = y
state.loc['taxi'] = y
state.cash -= taxi_price(state.dist[x][y])
state.time -= taxi_time(state.dist[x][y])
return state
return False
def call_taxi(state, x):
if state.loc['taxi'] != x and \
state.time >= 5:
state.time -= 5
state.loc['taxi'] = x
return state
return False
pyhop.declare_operators(
ride_car,
ride_public_transport,
walk,
ride_taxi,
call_taxi
)
def travel_car(state, x, y):
return [('ride_car', x, y)]
def travel_taxi_pt(state, x, y):
return [('call_taxi', x), ('ride_taxi', x, 'train_station'), ('ride_public_transport', 'train_station', y)]
def travel_car_pt(state, x, y):
return [('ride_car', x, 'train_station'), ('ride_public_transport', 'train_station', y)]
def travel_pt_pt(state, x, y):
return [('ride_public_transport', x, 'train_station'), ('ride_public_transport', 'train_station', y)]
def travel_walk_pt(state, x, y):
return [('walk', x, 'train_station'), ('ride_public_transport', 'train_station', y)]
pyhop.declare_methods('travel', travel_car, travel_taxi_pt, travel_car_pt, travel_pt_pt, travel_walk_pt)
state = pyhop.State('state')
state.loc = {
'me': 'home',
'car': 'home',
'taxi': 'anywhere',
}
state.dist = {
'home': {'train_station': 10, 'target': 100},
'train_station': {'target': 100},
}
state.cash = 100
state.time = 97
result = pyhop.pyhop(state, [('travel', 'home', 'target')], verbose=3)