-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFunction_scripts.py
More file actions
306 lines (258 loc) · 11.2 KB
/
Function_scripts.py
File metadata and controls
306 lines (258 loc) · 11.2 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from __future__ import print_function
from ortools.constraint_solver import routing_enums_pb2
from ortools.constraint_solver import pywrapcp
import time
import random
from haversine import haversine, Unit
import pandas as pd
import numpy as np
import Depo_Program as VRP
def ongoing(listnode,ID):
route=listnode[ID]
# randint = random.randint
temp_id=random.randint(0,len(route)-2)
for i in range(temp_id):
del route[0]
''' print(i," : ",route)
print(route)
print('current postion',route[0])'''
return route[0]
def new_package(new_loc,cpov):
data=pd.read_csv("data_3.csv")
lat_data=data.filter(['lat'])
long_data=data.filter(['lng'])
lat_data=lat_data.values
long_data=long_data.values
geocode2=[lat_data[new_loc],long_data[new_loc]]
dis=[]
for i in range(len(cpov)):
pos=cpov[i]
geocode1=[lat_data[pos],long_data[pos]]
temp=haversine(geocode1,geocode2,unit=Unit.METERS)
dis.append(temp)
new_assignment_to=dis.index(min(dis))
return new_assignment_to
def new_route(current_pos,current_path,new_node):
# using Or tool by google to find the shorthest parth that can be taken by the user
##Documents\PythonPrograming\Package_Problem\Practice\Routing
data=pd.read_csv("data_3.csv")
new_path=current_path
OG_path=current_path
new_path.append(new_node)
data=pd.read_csv("data_3.csv")
lat_data=data.filter(['lat'])
long_data=data.filter(['lng'])
lat_data=lat_data.values
long_data=long_data.values
def create_data_model():
data1 = {}
data1['distance_matrix']=np.zeros((len(new_path),len(new_path)))
for i in range(len(new_path)):
for j in range(len(new_path)):
geocode1=[lat_data[new_path[i]],long_data[new_path[i]]]
geocode2=[lat_data[new_path[j]],long_data[new_path[j]]]
data1['distance_matrix'][i][j]=1000*haversine(geocode1,geocode2)
data1['num_vehicles'] =1
data1['starts'] = [new_path.index(current_pos)]
data1['ends'] = [new_path.index(OG_path[-2])]
return data1
def print_solution(data, manager, routing, solution):
"""Prints solution on console."""
a=[]
max_route_distance = 0
for vehicle_id in range(data['num_vehicles']):
index = routing.Start(vehicle_id)
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
route_distance = 0
while not routing.IsEnd(index):
plan_output += ' {} -> '.format(OG_path[manager.IndexToNode(index)])
a.append(OG_path[manager.IndexToNode(index)])
previous_index = index
index = solution.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id)
plan_output += '{}\n'.format(OG_path[manager.IndexToNode(index)])
a.append(OG_path[manager.IndexToNode(index)])
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
print(plan_output)
max_route_distance = max(route_distance, max_route_distance)
print('Maximum of the route distances: {}m'.format(max_route_distance))
return a
"""Entry point of the program."""
# Instantiate the data problem.
data = create_data_model()
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(
len(data['distance_matrix']), data['num_vehicles'], data['starts'],
data['ends'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Create and register a transit callback.
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Distance constraint.
dimension_name = 'Distance'
routing.AddDimension(
transit_callback_index,
0, # no slack
2000, # vehicle maximum travel distance
True, # start cumul to zero
dimension_name)
distance_dimension = routing.GetDimensionOrDie(dimension_name)
distance_dimension.SetGlobalSpanCostCoefficient(100)
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
# Print solution on console.
if solution:
plan_output=print_solution(data, manager, routing, solution)
return plan_output
def Call_Depo_Program():
target,nov= VRP.main()
return target,nov
def Add_New_package(AtDepo,target,nov):
new_depot=AtDepo
current_pov=[]
for i in range(nov):
current_pov.append(ongoing(target,i))
assign_to_agent=new_package(new_depot,current_pov)
print("assign_to_agent : ",assign_to_agent)
print("assigned agent route",current_pov[assign_to_agent])
print("")
loc_pos=target[assign_to_agent].index(current_pov[assign_to_agent])
route_current=[]
for i in range(loc_pos,len(target[assign_to_agent])):
route_current.append(target[assign_to_agent][i])
plan_output=new_route(current_pov[assign_to_agent],route_current,new_depot)
return assign_to_agent,plan_output
def Call_OneDepoOneVehicle():
data=pd.read_csv("data_2.csv")
def func(string):
val = string.split(',')
lat = val[0][1:]
lng = val[1][0:-1]
return lat, lng
data['lat'] = data.apply(lambda x: func(x['location'])[0], axis=1)
data['lng'] = data.apply(lambda x: func(x['location'])[1], axis=1)
data.to_csv("data_3.csv")
lat_data=data.filter(['lat'])
long_data=data.filter(['lng'])
lat_data=lat_data.values
long_data=long_data.values
def create_data_model():
data = {}
data['distance_matrix']=np.zeros((len(lat_data),len(long_data)))
for i in range(len(lat_data)):
for j in range(len(lat_data)):
geocode1=[lat_data[i],long_data[i]]
geocode2=[lat_data[j],long_data[j]]
data['distance_matrix'][i][j]=1000*haversine(geocode1,geocode2)
data['num_vehicles'] =1
data['depot'] = 10
data['demands'] = [10, 1, 1, 2, 4, 2, 4, 8, 8, 2, 0, 1, 2, 4, 4, 8, 8, 2, 5, 6, 7, 3, 1, 5 ,7]
data['vehicle_capacities'] = [25]
return data
def print_solution(data, manager, routing, assignment):
total_distance = 0
total_load = 0
a=[]
dropped_nodes = 'Dropped nodes:'
for node in range(routing.Size()):
if routing.IsStart(node) or routing.IsEnd(node):
continue
if assignment.Value(routing.NextVar(node)) == node:
dropped_nodes += ' {}'.format(manager.IndexToNode(node))
print(dropped_nodes)
for vehicle_id in range(data['num_vehicles']):
a.append([])
index = routing.Start(vehicle_id)
plan_output = 'Route for vehicle {}:\n'.format(vehicle_id)
route_distance = 0
route_load = 0
while not routing.IsEnd(index):
node_index = manager.IndexToNode(index)
route_load += data['demands'][node_index]
plan_output += ' {0} Load({1}) -> '.format(node_index, route_load)
a[vehicle_id].append(node_index)
previous_index = index
index = assignment.Value(routing.NextVar(index))
route_distance += routing.GetArcCostForVehicle(
previous_index, index, vehicle_id)
plan_output += ' {0} Load({1})\n'.format(
manager.IndexToNode(index), route_load)
a[vehicle_id].append(manager.IndexToNode(index))
plan_output += 'Distance of the route: {}m\n'.format(route_distance)
plan_output += 'Load of the route: {}\n'.format(route_load)
print(plan_output)
total_distance += route_distance
total_load += route_load
print('Total distance of all routes: {}m'.format(total_distance))
print('Total load of all routes: {}'.format(total_load))
return a
"""Solve the CVRP problem."""
# Instantiate the data problem.
data = create_data_model()
print(len(data['distance_matrix']), data['num_vehicles'], data['depot'], 5*"=")
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(
len(data['distance_matrix']), data['num_vehicles'], data['depot'])
# Create Routing Model.
routing = pywrapcp.RoutingModel(manager)
# Create and register a transit callback.
def distance_callback(from_index, to_index):
"""Returns the distance between the two nodes."""
# Convert from routing variable Index to distance matrix NodeIndex.
from_node = manager.IndexToNode(from_index)
to_node = manager.IndexToNode(to_index)
return data['distance_matrix'][from_node][to_node]
transit_callback_index = routing.RegisterTransitCallback(distance_callback)
# Define cost of each arc.
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
# Add Distance constraint.
dimension_name = 'Distance'
routing.AddDimension(
transit_callback_index,
0, # no slack
3000, # vehicle maximum travel distance
True, # start cumul to zero
dimension_name)
distance_dimension = routing.GetDimensionOrDie(dimension_name)
distance_dimension.SetGlobalSpanCostCoefficient(100)
def demand_callback(from_index):
"""Returns the demand of the node."""
# Convert from routing variable Index to demands NodeIndex.
from_node = manager.IndexToNode(from_index)
return data['demands'][from_node]
demand_callback_index = routing.RegisterUnaryTransitCallback(
demand_callback)
routing.AddDimensionWithVehicleCapacity(
demand_callback_index,
0, # null capacity slack
data['vehicle_capacities'], # vehicle maximum capacities
True, # start cumul to zero
'Capacity')
index = manager.NodeToIndex(7)
routing.VehicleVar(index).SetValues([-1, 2,3,4])
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# Solve the problem.
start_time=time.time()
solution = routing.SolveWithParameters(search_parameters)
end_time=time.time()
print(end_time-start_time)
# Print solution on console.
if solution:
result=print_solution(data, manager, routing, solution)
return result,data['num_vehicles']