Skip to content

Commit 4a5dafb

Browse files
committed
Added slowdowns with example file
1 parent 6283680 commit 4a5dafb

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

examples/slowdowns/set_slowdowns.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
# codebeat:disable[SIMILARITY, LOC, ABC]
3+
4+
import argparse
5+
from route4me import Route4Me
6+
7+
from route4me.constants import (
8+
ALGORITHM_TYPE,
9+
OPTIMIZE,
10+
DEVICE_TYPE,
11+
TRAVEL_MODE,
12+
DISTANCE_UNIT
13+
)
14+
15+
16+
def main(api_key):
17+
r4m = Route4Me(api_key)
18+
19+
optimization = r4m.optimization
20+
address = r4m.address
21+
optimization.algorithm_type(ALGORITHM_TYPE.TSP)
22+
optimization.share_route(0)
23+
optimization.store_route(0)
24+
optimization.route_time(0)
25+
optimization.rt(1)
26+
optimization.route_max_duration(86400)
27+
optimization.vehicle_max_distance_mi(10000)
28+
optimization.route_name('Single Driver Round Trip With Slowdowns - Large Slowdowns')
29+
optimization.optimize(OPTIMIZE.TIME)
30+
optimization.distance_unit(DISTANCE_UNIT.MI)
31+
optimization.device_type(DEVICE_TYPE.WEB)
32+
optimization.travel_mode(TRAVEL_MODE.DRIVING)
33+
34+
# Set Slowdowns
35+
service_time = 20
36+
travel_time = 30
37+
optimization.set_slowdowns(service_time, travel_time)
38+
39+
address.add_address(
40+
address='754 5th Ave New York, NY 10019',
41+
lat=40.7636197,
42+
lng=-73.9744388,
43+
alias='Bergdorf Goodman',
44+
is_depot=1,
45+
time=0
46+
)
47+
address.add_address(
48+
address='717 5th Ave New York, NY 10022',
49+
lat=40.7669692,
50+
lng=-73.9693864,
51+
alias='Giorgio Armani',
52+
time=0
53+
)
54+
address.add_address(
55+
address='888 Madison Ave New York, NY 10014',
56+
lat=40.7715154,
57+
lng=-73.9669241,
58+
alias='Ralph Lauren Women\'s and Home',
59+
time=0
60+
)
61+
address.add_address(
62+
address='1011 Madison Ave New York, NY 10075',
63+
lat=40.7772129,
64+
lng=-73.9669,
65+
alias='Yigal Azrou\u00ebl',
66+
time=0
67+
)
68+
address.add_address(
69+
address='440 Columbus Ave New York, NY 10024',
70+
lat=40.7808364,
71+
lng=-73.9732729,
72+
alias='Frank Stella Clothier',
73+
time=0
74+
)
75+
address.add_address(
76+
address='324 Columbus Ave #1 New York, NY 10023',
77+
lat=40.7803123,
78+
lng=-73.9793079,
79+
alias='Liana',
80+
time=0
81+
)
82+
address.add_address(
83+
address='110 W End Ave New York, NY 10023',
84+
lat=40.7753077,
85+
lng=-73.9861529,
86+
alias='Toga Bike Shop',
87+
time=0
88+
)
89+
address.add_address(
90+
address='555 W 57th St New York, NY 10019',
91+
lat=40.7718005,
92+
lng=-73.9897716,
93+
alias='BMW of Manhattan',
94+
time=0
95+
)
96+
address.add_address(
97+
address='57 W 57th St New York, NY 10019',
98+
lat=40.7558695,
99+
lng=-73.9862019,
100+
alias='Verizon Wireless',
101+
time=0
102+
)
103+
104+
response = r4m.run_optimization()
105+
106+
print("Optimized route:")
107+
for route in response["routes"]:
108+
print(route["addresses"])
109+
110+
111+
if __name__ == '__main__':
112+
parser = argparse.ArgumentParser(description='Single Driver Round Trip With Slowdowns - 20,30')
113+
parser.add_argument('--api_key', dest='api_key', help='Route4Me API KEY',
114+
type=str, required=True)
115+
args = parser.parse_args()
116+
main(args.api_key)
117+
118+
# codebeat:enable[SIMILARITY, LOC, ABC]

route4me/optimization.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from .api_endpoints import ADDRESS_HOST, API_HOST
44
from .base import Base
55
from .exceptions import ParamValueException, APIException
6+
from .slowdowns import Slowdowns
67

78

89
class Optimization(Base):
@@ -121,3 +122,13 @@ def delete_address_from_optimization(self, **kwargs):
121122
return e.to_dict()
122123
else:
123124
raise ParamValueException('params', 'Params are not complete')
125+
126+
def set_slowdowns(self, service_time, travel_time):
127+
"""
128+
Set slowdowns param
129+
:param service_time:
130+
:param travel_time:
131+
:return:
132+
"""
133+
slowdowns = Slowdowns(service_time, travel_time)
134+
self._copy_data({"slowdowns": slowdowns.to_dict()})

route4me/slowdowns.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Slowdowns():
4+
"""
5+
Slowdowns Management
6+
"""
7+
8+
def __init__(self, service_time, travel_time):
9+
self.validate(service_time, travel_time)
10+
self.service_time = service_time
11+
self.travel_time = travel_time
12+
13+
def validate(self, service_time, travel_time):
14+
if not isinstance(service_time, int) or service_time < 0 or service_time > 50:
15+
raise ValueError("Service time must be an integer between 0 and 50")
16+
17+
if not isinstance(travel_time, int) or travel_time < 0 or travel_time > 50:
18+
raise ValueError("Travel time must be an integer between 0 and 50")
19+
20+
def to_dict(self):
21+
return {
22+
'service_time': self.service_time,
23+
'travel_time': self.travel_time
24+
}

0 commit comments

Comments
 (0)