-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtravel_planner_refactoring.py
209 lines (164 loc) · 5.11 KB
/
travel_planner_refactoring.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
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
199
200
201
202
203
204
205
# we are receiving inputs from the mobile phone
# we are receiving the startCity
# we are receiving the startDate
# we are receiving the cities
# we are receiving the days
# we are receiving the routes ## ok
# make a function that take care of days
from traveler_planner import printRoutePossibilities
from combinations import printCombinations
from combinations_on_list import printCombinationsArray
from depth_first_traversal import find_all_paths2
from permutations_origin import permutationsFromOrigin
from permutations_origin import permutationsReturnOrigin
from collections import defaultdict
from random import uniform
from list_of_dates import printListOfDates
from graph import Graph
import timeit
from datetime import date
# USER INPUTS
routes = [('MADRID','GRECE'),('MADRID','LISBONNE'),('GRECE','LISBONNE'),('GRECE','MADRID'),('LISBONNE','GRECE'),('LISBONNE','MADRID')] # generated by virtual table in sql on the device
days = [0,1,2]
startDate = date(2017, 1, 2)
#startCity = "MADRID"
cities = ["MADRID","GRECE","LISBONNE"]
# OUTPUT
### TODO
def transformArray(routes):
"""
take in argument routes and return it in a better format
time : 0.004s
"""
array=[]
for couple in routes:
array.append(couple[0]+ "-" +couple[1])
return array
#print(transformArray(routes))
def transformArray2(routes):
"""
take in argument routes and return it in a better format
time : 0.004s
"""
array=[]
for couple in routes:
array.append(couple+ "-" +couple)
return array
#WHAT WE HAVE IN DATABASE (prices)
def createFakePriceDatabase(routes):
"""
take in argument the routes generated by the device with the ryanair database of airports routes and return a fake database of prices
time : 1.7s
"""
transformedArray = transformArray(routes)
priceDatabase = {key:[int(round(uniform(20, 180))) for _ in range(364)] for key in transformedArray}
return priceDatabase
# GENERATED WITH USER INPUTS
def createOptimizedGraph(routes):
"""
take the routes user inputs generated by sql and return an optimized graph
time : 0.007s
"""
graphClass = Graph(routes, directed=True)
return graphClass.getGraph()
def optimizedRoutePossibilities(routes,cities):
"""
take in argument routes and return all the paths via dfs
time : 0.16s
"""
graph = createOptimizedGraph(routes)
for couple in permutationsFromOrigin(cities):
if couple is not None:
#yield find_all_paths2(graph,couple[0],couple[1])[0]
print(find_all_paths2(graph,couple[0],couple[1])[0])
#optimizedRoutePossibilities(routes,cities)
def optimizedRoutePossibilities2(routes,cities):
"""
take in argument routes and return all the paths via dfs
time : 0.16s
"""
graph = createOptimizedGraph(routes)
for couple in permutationsFromOrigin(cities):
if couple is not None:
#yield find_all_paths2(graph,couple[0],couple[1])[0]
path = find_all_paths2(graph,couple[0],couple[1])[0]
if couple[0] in graph[path[-1]]:
yield path
optimizedRoutePossibilities2(routes,cities)
def optimizedMinRoutePossibility(routes,startDate,cities,days):
"""
take in argument routes and return all the paths via dfs
time : 0.16s
"""
calendar = printListOfDates()
indexStartDate = calendar.index(startDate)
priceDatabase = createFakePriceDatabase(routes)
sumPricesArray = []
for possibility in optimizedRoutePossibilities2(routes,cities):
i=0
prices=[]
print(possibility)
for couple in permutationsReturnOrigin(possibility):
print(couple)
#print(priceDatabase[couple[0]+"-"+couple[1]])
priceForDate = priceDatabase[couple[0]+"-"+couple[1]][indexStartDate + i]
#print(priceForDate)
prices += [priceForDate]
i+=1
print(prices)
sumPrices = sum(prices)
print(sumPrices)
sumPricesArray+=[sumPrices]
print(sumPricesArray)
print(min(sumPricesArray))
#optimizedMinRoutePossibility(routes,startDate,cities,days)
def finalResult(routes,startDate,cities,days):
"""
take in argument routes and return all the paths via dfs
time : 0.16s
"""
calendar = printListOfDates()
indexStartDate = calendar.index(startDate)
priceDatabase = createFakePriceDatabase(routes)
sumPricesArray = []
for possibility in optimizedRoutePossibilities2(routes,cities):
i=0
prices=[]
#print(possibility)
for couple in permutationsReturnOrigin(possibility):
#print(couple)
#print(priceDatabase[couple[0]+"-"+couple[1]])
priceForDate = priceDatabase[couple[0]+"-"+couple[1]][indexStartDate + i]
#print(priceForDate)
prices += [priceForDate]
i+=1
print(prices)
sumPrices = sum(prices)
#print(sumPrices)
sumPricesArray+=[sumPrices]
print(sumPricesArray)
print(min(sumPricesArray))
route = [
{"title":"blabla",
"price":3
}
]
details = [
{"title":"details de blabla",
"price":2
},
{"title":"details 2 de blabla",
"price":1
}
]
return route, details
print(finalResult(routes,startDate,cities,days))
## TIME FUCTIONS
# def wrapper(func, *args, **kwargs):
# def wrapped():
# return func(*args, **kwargs)
# return wrapped
# #wrapped = wrapper(transformArray, routes)
# wrapped = wrapper(optimizedRoutePossibilities, routes,cities)
# #wrapped = wrapper(createOptimizedGraph, routes)
# print(timeit.timeit(wrapped, number=1000))