-
Notifications
You must be signed in to change notification settings - Fork 10
/
VRPTW_SA_6operators_0903.py
1246 lines (1083 loc) · 55.8 KB
/
VRPTW_SA_6operators_0903.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# _*_ coding: utf-8 _*_
"""
@Author : brucefeng10
@Contact : [email protected]
"""
import csv
import numpy as np
import time
import copy
import random
import matplotlib.pyplot as plt
def read_data(file_num):
# We found that delivery customer has to be served before 12:00, and pickup customers has to be served after 13:00
cust_need = {0: [1, 0, 0, 480, 1440]} # {cust_id: [cust_type, weight, volume, first_receive, last_receive], []}
time_mat = np.zeros([cust_num + charge_num, cust_num + charge_num])
dist_mat = np.zeros([cust_num + charge_num, cust_num + charge_num])
with open(r'data\inputnode_%s.csv' % file_num, 'rU') as fc:
reader = csv.reader(fc)
next(reader)
next(reader)
for v in reader:
# if v[1] == '1':
# cust_need[0] = [1, 0, 480, 1440]
if v[1] == '4':
cust_need[int(v[0]) - file_code * 10000] = [4, 0, 0, 480, 1440]
else:
first_t = v[6].split(':')
last_t = v[7].split(':')
cust_need[int(v[0]) - file_code*10000] = [int(v[1]), float(v[4]), float(v[5]), int(first_t[0])*60 + int(first_t[1]), int(last_t[0])*60 + int(last_t[1])]
# print cust_need
# print cust_time
with open(r'data\inputdistancetime_%s.txt' % file_num, 'r') as fd:
next(fd)
for row in fd:
to_list = row.strip('\n').split(',')
if int(to_list[1]) > 0:
from_id = int(to_list[1]) - file_code*10000
else:
from_id = int(to_list[1])
if int(to_list[2]) > 0:
to_id = int(to_list[2]) - file_code*10000
else:
to_id = int(to_list[2])
dist_mat[from_id, to_id] = int(to_list[3])
time_mat[from_id, to_id] = int(to_list[4])
return cust_need, time_mat, dist_mat
def time_nn(last_cust, curr_cust, remain_list, used_resource, rout_len, vehicle_type):
"""Given a vehicle and its current visiting customer, return the next visiting customer.
Here we use the Time-oriented Nearest-Neighborhood Heuristic proposed by Solomon(1987).
The closeness between customer i and j: C_ij = alp1*t_ij + alp2*h_ij + alp3*v_ij,
t_ij: travel time between i and j (distance);
h_ij = b_j - (b_i + s_i), b is start service time, and s is service time (idle time);
v_ij = l_j - (b_i + s_i + t_ij), l is the latest admissible service time, t_ij is the travel time (urgency)
The low value of C_ij, the better.
"""
if vehicle_type == 1:
veh_cap = iveco_cap
else:
veh_cap = truck_cap
visit_cust = [-1, 100000, 600000, 1000] # [cust_id, next_start, distance, closeness]
if rout_len > 6:
pass
elif cust_need[curr_cust][0] <= 2 or (cust_need[curr_cust][0] == 4 and cust_need[last_cust][0] <= 2): # current customer is a delivery
for cust in remain_list:
near_charg_id = cust_charge[cust]
# print 'checking customer: ', cust
if cust_need[cust][0] == 2: # next customer is a delivery
if used_resource[2] + dist_mat[curr_cust, cust] + dist_mat[cust, near_charg_id] > veh_cap[3]:
# print 'd out of battery 1'
continue # run out of battery before getting to the nearest charge station of the visiting customer
elif dist_mat[curr_cust, cust] > veh_cap[3] - used_resource[2]:
# print 'd out of battery 2'
continue # run out of battery before getting to the visiting customer
elif used_resource[0] + cust_need[cust][1] > veh_cap[1]:
# print 'd weight overload'
continue # weight overload
elif used_resource[1] + cust_need[cust][2] > veh_cap[2]:
# print 'd volume overload'
continue # volume overload
elif used_resource[3] + time_mat[curr_cust, cust] > cust_need[cust][4]:
# print 'd last receive time'
continue # can not arrive before last receive time
else:
wait_time = cust_need[cust][3] - (used_resource[3] + time_mat[curr_cust, cust])
if wait_time < 0:
next_start = used_resource[3] + time_mat[curr_cust, cust]
h_ij = time_mat[curr_cust, cust]
else:
next_start = cust_need[cust][3]
h_ij = next_start - used_resource[3]
v_ij = cust_need[cust][4] - (used_resource[3] + time_mat[curr_cust, cust])
close_ij = alp * time_mat[curr_cust, cust] + bet * h_ij +gam * v_ij # closeness between i and j
if close_ij < visit_cust[3]:
visit_cust[0] = cust
visit_cust[1] = next_start
visit_cust[2] = dist_mat[curr_cust, cust]
visit_cust[3] = close_ij
else:
continue
else: # next customer is a pickup
if used_resource[2] + dist_mat[curr_cust, cust] + dist_mat[cust, near_charg_id] > veh_cap[3]:
# print 'p out of battery 1'
continue # run out of battery before getting to the nearest charge station of the visiting customer
elif dist_mat[curr_cust, cust] > veh_cap[3] - used_resource[2]:
# print 'p out of battery 2'
continue # run out of battery before getting to the visiting customer
elif used_resource[3] + time_mat[curr_cust, cust] > cust_need[cust][4]:
# print 'p last receive time'
continue # can not arrive before last receive time
else:
wait_time = cust_need[cust][3] - (used_resource[3] + time_mat[curr_cust, cust])
if wait_time < 0:
next_start = used_resource[3] + time_mat[curr_cust, cust]
h_ij = time_mat[curr_cust, cust]
else:
next_start = cust_need[cust][3]
h_ij = next_start - used_resource[3]
v_ij = cust_need[cust][4] - (used_resource[3] + time_mat[curr_cust, cust])
close_ij = alp * time_mat[curr_cust, cust] + bet * h_ij +gam * v_ij # closeness between i and j
if close_ij < visit_cust[3]:
visit_cust[0] = cust
visit_cust[1] = next_start
visit_cust[2] = dist_mat[curr_cust, cust]
visit_cust[3] = close_ij
else:
continue
else: # current customer is a pickup
for cust in remain_list:
near_charg_id = cust_charge[cust]
if cust_need[cust][0] == 2:
continue # not delivery customer any more
elif used_resource[2] + dist_mat[curr_cust, cust] + dist_mat[cust, near_charg_id] > veh_cap[3]:
continue # run out of battery before getting to the nearest charge station of the visiting customer
elif dist_mat[curr_cust, cust] > veh_cap[3] - used_resource[2]:
continue # run out of battery before getting to the visiting customer
elif used_resource[0] + cust_need[cust][1] > veh_cap[1]:
continue # weight overload
elif used_resource[1] + cust_need[cust][2] > veh_cap[2]:
continue # volume overload
elif used_resource[3] + time_mat[curr_cust, cust] > cust_need[cust][4]:
continue # can not arrive before last receive time
else:
wait_time = cust_need[cust][3] - (used_resource[3] + time_mat[curr_cust, cust])
if wait_time < 0:
next_start = used_resource[3] + time_mat[curr_cust, cust]
h_ij = time_mat[curr_cust, cust]
else:
next_start = cust_need[cust][3]
h_ij = next_start - used_resource[3]
v_ij = cust_need[cust][4] - (used_resource[3] + time_mat[curr_cust, cust])
close_ij = alp * time_mat[curr_cust, cust] + bet * h_ij + gam * v_ij # closeness between i and j
if close_ij < visit_cust[3]:
visit_cust[0] = cust
visit_cust[1] = next_start
visit_cust[2] = dist_mat[curr_cust, cust]
visit_cust[3] = close_ij
else:
continue
# if visit_cust[0] == -1: # no customer to visit
# if dist_mat[curr_cust, 0] <= truck_cap[3] - used_resource[2]: # can get back to depot
# visit_cust[0] = 0
# visit_cust[1] = used_resource[-1] + time_mat[curr_cust, 0]
# else:
# visit_cust[0] = cust_charge[curr_cust] # get to the nearest charge station
# visit_cust[1] = used_resource[-1] + time_mat[curr_cust, visit_cust[0]]
if visit_cust[0] == -1: # no customer to visit
if 2 <= cust_need[curr_cust][0] <= 3: # for customers, first choose to get charged if no customers to visit
visit_cust[0] = cust_charge[curr_cust] # get to the nearest charge station
visit_cust[1] = used_resource[-1] + time_mat[curr_cust, visit_cust[0]]
else: # for charge stations and depot, go back to depot if no customers to visit
visit_cust[0] = 0
visit_cust[1] = used_resource[-1] + time_mat[curr_cust, 0]
return visit_cust
def check_violation(route, vehicle_type):
"""To check if a route is feasible using large vehicle(truck), and return check result and route cost."""
if len(route) == 2: # [0, 0] route
return True, 0
elif len(route) == 3 and cust_need[route[1]][0] == 4: # [0, charge, 0] route
return True, 0
else:
# 0the leaving time, 1accumulated distance, 2accumulated weight_song, 3accumulated volume_song,
# 4accumulated weight_shou, 5accumulated volume_shou, when arriving at a customer
accu_res = [480, 0, 0, 0, 0, 0]
if vehicle_type == 1:
veh_cap = iveco_cap
elif vehicle_type == 2:
veh_cap = truck_cap
else:
print 'Input wrong vehicle type!', vehicle_type
fixed_cost = veh_cap[5]
trans_cost = 0
wait_cost = 0
charge_cost = 0
if time_mat[0, route[1]] + 480 < cust_need[route[1]][3]:
accu_res[0] = cust_need[route[1]][3] - time_mat[0, route[1]]
for i in range(len(route) - 1):
last_cust = route[i]
curr_cust = route[i+1]
# checking leaving time
arr_time = accu_res[0] + time_mat[last_cust, curr_cust]
if arr_time < cust_need[curr_cust][3]:
accu_res[0] = cust_need[curr_cust][3] + oper_t
wait_time = cust_need[curr_cust][3] - arr_time
wait_cost += (wait_time / 60. * wait_cost0)
elif arr_time <= cust_need[curr_cust][4]:
accu_res[0] = arr_time + oper_t
else:
# print 'Infeasible route!(Service Time Error.)'
return False, 1000000
# checking vehicle max distance
trans_cost += (dist_mat[last_cust, curr_cust] * veh_cap[4])
if 2 <= cust_need[last_cust][0] <= 3:
accu_res[1] += dist_mat[last_cust, curr_cust]
else:
accu_res[1] = dist_mat[last_cust, curr_cust]
if accu_res[1] > veh_cap[3]:
# print 'Infeasible route!(Max Distance Error.)'
return False, 1000000
# checking vehicle max weight and volume
if cust_need[curr_cust][0] == 1:
accu_res[2:] = [0, 0, 0, 0]
elif cust_need[curr_cust][0] == 2:
accu_res[2] += cust_need[curr_cust][1]
accu_res[3] += cust_need[curr_cust][2]
elif cust_need[curr_cust][0] == 3:
accu_res[4] += cust_need[curr_cust][1]
accu_res[5] += cust_need[curr_cust][2]
else:
pass
if accu_res[2] > veh_cap[1] or accu_res[3] > veh_cap[2] or accu_res[4] > veh_cap[1] or accu_res[5] > veh_cap[2]:
# print 'Infeasible route!(Max Weight/Volume Error.)'
return False, 1000000
if cust_need[last_cust][0] == 4:
charge_cost += charg_cost0
# print trans_cost, wait_cost, charge_cost, fixed_cost
return True, trans_cost + wait_cost + charge_cost + fixed_cost
def lns_initial(small_veh):
"""
This is a Large Neighbour Search algorithm for VRPTW,
we choose the next visiting customer based on the least waiting time.
"""
sol = [] # [[0;2;5;0;4;6;0],[],...]
visited_p = []
to_vist = [i+1 for i in range(cust_num-1)] # [1,5,8,...]
itr = 0
while len(to_vist) > 0:
itr += 1
if itr < small_veh:
vehicle_type0 = 1
else:
vehicle_type0 = 2
used_res = [0, 0, 0, 480] # used weight, volume and distance of the vehicle, leave time
veh_rout = [0]
# print '\nA new vehicle will be used.'
while True:
curr_cust = veh_rout[-1]
if len(veh_rout) == 1:
last_cust = 0
else:
last_cust = veh_rout[-2]
# print used_res
next_one = time_nn(last_cust, curr_cust, to_vist, used_res, len(veh_rout), vehicle_type0)
next_cust, next_start = next_one[0], next_one[1]
if next_cust == 0: # next visiting customer is depot
if curr_cust == 0:
# print 'The current vehicle ends.'
break
else:
used_res[:3] = [0, 0, 0]
used_res[3] += (time_mat[curr_cust, next_cust] + depot_t)
# print 'Get back to the depot, and ready for a new round.'
elif cust_need[next_cust][0] == 2: # next visiting customer is delivery customer
used_res[0] += cust_need[next_cust][1]
used_res[1] += cust_need[next_cust][2]
used_res[2] += dist_mat[curr_cust, next_cust]
used_res[3] = (next_start + oper_t)
elif cust_need[next_cust][0] == 3: # next visiting customer is pickup customer
if curr_cust == 0: # current customer is depot
used_res[0] = cust_need[next_cust][1]
used_res[1] = cust_need[next_cust][2]
used_res[2] = dist_mat[curr_cust, next_cust]
used_res[3] = (next_start + oper_t)
elif cust_need[curr_cust][0] <= 2: # current customer is delivery customer
used_res[0] = cust_need[next_cust][1]
used_res[1] = cust_need[next_cust][2]
used_res[2] += dist_mat[curr_cust, next_cust]
used_res[3] = (next_start + oper_t)
else: # current customer is pickup customer or charge station
used_res[0] += cust_need[next_cust][1]
used_res[1] += cust_need[next_cust][2]
used_res[2] += dist_mat[curr_cust, next_cust]
used_res[3] = (next_start + oper_t)
else: # next visiting customer is a charge station
used_res[2] = 0
used_res[3] += (time_mat[curr_cust, next_cust] + charg_t)
veh_rout.append(next_cust)
# print 'Vehicle used resource: ', used_res
if cust_need[next_cust][0] == 2 or cust_need[next_cust][0] == 3:
# print 'visited customer', next_cust
to_vist.remove(next_cust)
if cust_need[next_cust][0] == 3:
visited_p.append(next_cust)
if cust_need[veh_rout[-2]][0] == 4: # the last visit was a charge station, to judge if this charge station is necessary
veh_rout_test = copy.deepcopy(veh_rout)
veh_rout_test.pop(-2)
if check_violation(veh_rout_test, 1)[0]:
if check_violation(veh_rout_test, 1)[1] < check_violation(veh_rout, 1)[1]:
sol.append(veh_rout_test)
continue
elif check_violation(veh_rout_test, 2)[0]:
if check_violation(veh_rout_test, 2)[1] < check_violation(veh_rout, 1)[1]:
sol.append(veh_rout_test)
continue
sol.append(veh_rout)
# print 'Last point 0 earliest leave time: ', int(used_res[-1]) / 60, ':', int(used_res[-1]) % 60
# print 'Route %s is: ' % itr, veh_rout
# print '*'*10, 'Iteration:', itr, '*'*10
return sol
def get_cost(solution, veh_type, if_write, run_t=289.3):
"""Given the solution saved in list, calculate the total cost of the solution.
Write the solution to local in the required format."""
result = [['trans_code', 'vehicle_type', 'dist_seq', 'distribute_lea_tm', 'distribute_arr_tm', 'distance', 'trans_cost', 'charge_cost', 'wait_cost', 'fixed_use_cost', 'total_cost', 'charge_cnt']]
total_cost = 0
veh_code = 0
for k, veh in enumerate(solution):
if veh_type[k] == 1:
trans0 = iveco_cap[4]
fix0 = iveco_cap[5]
else:
trans0 = truck_cap[4]
fix0 = truck_cap[5]
# get the output format
route = [0] * len(result[0])
veh_code += 1
route[0] = 'DP' + str(veh_code).zfill(4) # vehicle name
route[1] = veh_type[k] # vehicle type
route_ele = []
for ele in veh:
if ele == 0:
route_ele.append(str(ele))
else:
route_ele.append(str(ele + file_code * 10000))
route[2] = ';'.join(route_ele) # route
total_cost += fix0
total_cost += dist_mat[0, veh[1]] * trans0
if time_mat[0, veh[1]] + start_t <= cust_need[veh[1]][3]:
t = cust_need[veh[1]][3] + oper_t
time_out = int(cust_need[veh[1]][3] - time_mat[0, veh[1]])
route[3] = str(time_out / 60) + ':' + str(time_out % 60).zfill(2) # vehicle out time
else:
t = time_mat[0, veh[1]] + start_t + oper_t
route[3] = str(start_t / 60) + ':' + str(start_t % 60).zfill(2) # vehicle out time
total_wait_cost = 0
for i in range(2, len(veh)-1): # can not wait at the first 2 points
total_cost += (dist_mat[veh[i-1], veh[i]] * trans0)
if cust_need[veh[i]][0] == 4:
total_cost += charg_cost0
wait_t = cust_need[veh[i]][3] - (t + time_mat[veh[i-1], veh[i]])
if wait_t > 0:
# print veh[i-1], veh[i], wait_t
total_cost += (wait_t/60. * wait_cost0)
total_wait_cost += (wait_t/60. * wait_cost0)
t = cust_need[veh[i]][3] + oper_t
else:
if veh[i] == 0:
t += (time_mat[veh[i-1], veh[i]] + depot_t)
else:
t += (time_mat[veh[i - 1], veh[i]] + oper_t)
if veh[i] == 0: # get back to the depot and will depart again, wait cost is 1hour
total_cost += wait_cost0
total_wait_cost += wait_cost0
# print veh[i], str(int(t) / 60) + ':' + str(int(t) % 60).zfill(2)
in_time = int(t + time_mat[veh[-2], 0])
route[4] = str(in_time / 60) + ':' + str(in_time % 60).zfill(2) # vehicle back time
total_dist = 0
total_charg_cost = 0
total_charg_cnt = 0
for j in range(len(veh) - 1):
total_dist += dist_mat[veh[j], veh[j + 1]]
if veh[j] >= cust_num:
total_charg_cost += charg_cost0
total_charg_cnt += 1
route[5] = int(total_dist) # total distance
route[6] = round(route[5] * trans0, 2) # transfer cost
route[7] = total_charg_cost # total charge cost
route[8] = total_wait_cost
route[9] = fix0 # vehicle fixed cost
route[10] = route[6] + route[7] + route[8] + route[9] # total cost
route[11] = total_charg_cnt # charge count
result.append(route)
# print route
total_cost += dist_mat[veh[-2], 0] * trans0
# print 'Last leave time: ', int(t) / 60, ':', int(t) % 60
# print 'total distances: ', route[5]
if if_write:
with open(r'C:\Bee\ProjectFile\JD_GOC\results\Result_%s_%s.csv' % (file_code, run_t), 'wb') as fw:
writer = csv.writer(fw)
for v in result:
writer.writerow(v)
return total_cost
def vehicle_type_adjust(solution):
"""Given a solution only using large truck, check if if we can replace with a small vehicle to save cost."""
type_list = []
for veh in solution:
typ = 2
wei_shou = [0] # pickup weight
wei_song = [0] # delivery weight
vol_shou = [0]
vol_song = [0]
distance = [] # distance at each point
for i in range(len(veh) - 1):
if 2 <= cust_need[veh[i]][0] <= 3:
distance0 = distance[-1] + dist_mat[veh[i], veh[i+1]]
distance.append(distance0)
else:
distance0 = dist_mat[veh[i], veh[i+1]]
distance.append(distance0)
wei_song0, wei_shou0, vol_song0, vol_shou0 = 0, 0, 0, 0
if cust_need[veh[i+1]][0] == 2:
wei_song0 = wei_song[-1] + cust_need[veh[i+1]][1]
vol_song0 = vol_song[-1] + cust_need[veh[i+1]][2]
elif cust_need[veh[i+1]][0] == 3:
wei_shou0 = wei_shou[-1] + cust_need[veh[i + 1]][1]
vol_shou0 = vol_shou[-1] + cust_need[veh[i + 1]][2]
elif veh[i+1] == 0: # go back to the depot initialize vehicle resources
wei_song0, wei_shou0, vol_song0, vol_shou0 = 0, 0, 0, 0
else:
continue
wei_song.append(wei_song0)
wei_shou.append(wei_shou0)
vol_song.append(vol_song0)
vol_shou.append(vol_shou0)
if max(wei_song) > 2.5 or max(wei_shou) > 2.5 or max(vol_song) > 16 or max(vol_shou) > 16 or max(distance) > 120000:
print 'Shit!!!'
print 'Error route: ', veh
print 'wei_song wei_shou vol_song vol_shou distance: ', max(wei_song), max(wei_shou), max(vol_song), max(vol_shou), max(distance)
if max(wei_song) <= iveco_cap[1] and max(wei_shou) <= iveco_cap[1] and max(vol_song) <= iveco_cap[2] and max(vol_shou) <= iveco_cap[2] and max(distance) <= iveco_cap[3]:
typ = 1
type_list.append(typ)
return type_list
def route_type(route):
"""Given a route, return the vehicle type of the route. Samll vehicle first, large vehicle second."""
typ = 2
wei_shou = [0] # pickup weight
wei_song = [0] # delivery weight
vol_shou = [0]
vol_song = [0]
distance = [] # distance at each point
for i in range(len(route) - 1):
if 2 <= cust_need[route[i]][0] <= 3:
distance0 = distance[-1] + dist_mat[route[i], route[i + 1]]
distance.append(distance0)
else:
distance0 = dist_mat[route[i], route[i + 1]]
distance.append(distance0)
wei_song0, wei_shou0, vol_song0, vol_shou0 = 0, 0, 0, 0
if cust_need[route[i + 1]][0] == 2:
wei_song0 = wei_song[-1] + cust_need[route[i + 1]][1]
vol_song0 = vol_song[-1] + cust_need[route[i + 1]][2]
elif cust_need[route[i + 1]][0] == 3:
wei_shou0 = wei_shou[-1] + cust_need[route[i + 1]][1]
vol_shou0 = vol_shou[-1] + cust_need[route[i + 1]][2]
elif route[i + 1] == 0: # go back to the depot initialize vehicle resources
wei_song0, wei_shou0, vol_song0, vol_shou0 = 0, 0, 0, 0
else:
continue
wei_song.append(wei_song0)
wei_shou.append(wei_shou0)
vol_song.append(vol_song0)
vol_shou.append(vol_shou0)
if max(wei_song) > 2.5 or max(wei_shou) > 2.5 or max(vol_song) > 16 or max(vol_shou) > 16 or max(distance) > 120000:
print 'Shit!!!'
print 'Error route: ', route
print 'wei_song wei_shou vol_song vol_shou distance: ', max(wei_song), max(wei_shou), max(vol_song), max(
vol_shou), max(distance)
if max(wei_song) <= iveco_cap[1] and max(wei_shou) <= iveco_cap[1] and max(vol_song) <= iveco_cap[2] and max(
vol_shou) <= iveco_cap[2] and max(distance) <= iveco_cap[3]:
typ = 1
return typ
def cust_loc(sol, cust):
"""Get the route location and customer location of a customer."""
cust_ind = [] # [route_loc, cust_loc]
for i, rt in enumerate(sol):
if cust in rt:
cust_ind.append(i)
cust_ind.append(rt.index(cust))
return cust_ind
print 'Costomer not in the solution: ', cust
def shift_1_cust(sol_in1, cust, c_loc, curr_temp, sol_type1, sa_lns):
"""Try to move 1 customer to anywhere it can be put, and see if the move can cut the total cost."""
route_ing = copy.deepcopy(sol_in1[c_loc[0]])
route_new = route_ing
move_to_route = c_loc[0]
new_type = 2
origin_cost1 = check_violation(route_ing, sol_type1[c_loc[0]])[1]
route_ing.remove(cust) # move c in the current route
adjust_cost1 = min(check_violation(route_ing, 1)[1], check_violation(route_ing, 2)[1])
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
for j, rou in enumerate(sol_in1):
origin_cost2 = check_violation(rou, sol_type1[j])[1]
if j == c_loc[0]: # moving in the same route
for k in range(1, len(route_ing)):
if k == c_loc[1]:
continue # do not put it at the original position
rou_test = route_ing[:k] + [cust] + route_ing[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
else: # moving to a different route
for k in range(1, len(rou)):
rou_test = rou[:k] + [cust] + rou[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
if best_cut_cost > 1e-5:
print 'shift1 good', best_cut_cost
sol_in1[move_to_route] = route_new
sol_type1[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in1[c_loc[0]] = route_ing
sol_type1[c_loc[0]] = route_type(route_ing)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost/curr_temp) > prb:
print 'shift1', best_cut_cost
sol_in1[move_to_route] = route_new
sol_type1[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in1[c_loc[0]] = route_ing
sol_type1[c_loc[0]] = route_type(route_ing)
# return sol_in1
def shift_2_cust(sol_in2, cust, c_loc, curr_temp, sol_type2, sa_lns):
"""Try to move 2 consecutive customers to anywhere they can be put, see if they move can cut the total cost."""
route_ing = copy.deepcopy(sol_in2[c_loc[0]])
route_new = route_ing
move_to_route = c_loc[0]
new_type = 2
cust_folw = route_ing[c_loc[1]+1]
origin_cost1 = check_violation(route_ing, sol_type2[c_loc[0]])[1]
route_ing.remove(cust) # remove c in the current route
del route_ing[c_loc[1]] # remove customer following c
adjust_cost1 = min(check_violation(route_ing, 1)[1], check_violation(route_ing, 2)[1])
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
for j, rou in enumerate(sol_in2):
origin_cost2 = check_violation(rou, sol_type2[j])[1]
if j == c_loc[0]: # moving in the same route
for k in range(1, len(route_ing)):
if k == c_loc[1]:
continue
rou_test = route_ing[:k] + [cust, cust_folw] + route_ing[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
else: # moving to a different route
for k in range(1, len(rou)):
rou_test = rou[:k] + [cust, cust_folw] + rou[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
if best_cut_cost > 1e-5:
print 'shift2 good', best_cut_cost
sol_in2[move_to_route] = route_new
sol_type2[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in2[c_loc[0]] = route_ing
sol_type2[c_loc[0]] = route_type(route_ing)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost / curr_temp) > prb:
print 'shift2', best_cut_cost
sol_in2[move_to_route] = route_new
sol_type2[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in2[c_loc[0]] = route_ing
sol_type2[c_loc[0]] = route_type(route_ing)
# return sol_in2
def shift_3_cust(sol_in6, cust, c_loc, curr_temp, sol_type6, sa_lns):
"""Try to move 3 consecutive customers to anywhere they can be put, see if they move can cut the total cost."""
route_ing = copy.deepcopy(sol_in6[c_loc[0]])
route_new = route_ing
move_to_route = c_loc[0]
new_type = 2
cust_folw1 = route_ing[c_loc[1] + 1]
cust_folw2 = route_ing[c_loc[1] + 2]
origin_cost1 = check_violation(route_ing, sol_type6[c_loc[0]])[1]
route_ing.remove(cust) # remove c in the current route
del route_ing[c_loc[1]] # remove customer following c
del route_ing[c_loc[1]] # remove customer following following c
adjust_cost1 = min(check_violation(route_ing, 1)[1], check_violation(route_ing, 2)[1])
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
for j, rou in enumerate(sol_in6):
origin_cost2 = check_violation(rou, sol_type6[j])[1]
if j == c_loc[0]: # moving in the same route
for k in range(1, len(route_ing)):
if k == c_loc[1]:
continue
rou_test = route_ing[:k] + [cust, cust_folw1, cust_folw2] + route_ing[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
else: # moving to a different route
for k in range(1, len(rou)):
rou_test = rou[:k] + [cust, cust_folw1, cust_folw2] + rou[k:]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 1
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new = rou_test
move_to_route = j
new_type = 2
if best_cut_cost > 1e-5:
print 'shift3 good', best_cut_cost
sol_in6[move_to_route] = route_new
sol_type6[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in6[c_loc[0]] = route_ing
sol_type6[c_loc[0]] = route_type(route_ing)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost / curr_temp) > prb:
print 'shift3', best_cut_cost
sol_in6[move_to_route] = route_new
sol_type6[move_to_route] = new_type
if move_to_route != c_loc[0]: # moving to a different route
sol_in6[c_loc[0]] = route_ing
sol_type6[c_loc[0]] = route_type(route_ing)
def exchange_1_cust(sol_in3, cust, c_loc, curr_temp, sol_type3, sa_lns):
"""Exchange the position of two customers(same route or not) if feasible, and see if it can cut the total cost."""
route_ing = copy.deepcopy(sol_in3[c_loc[0]])
route_new_1 = route_ing
route_new_2 = route_ing
exch_to_route = c_loc[0]
origin_cost1 = check_violation(route_ing, sol_type3[c_loc[0]])[1]
# route_ing.remove(cust) # move c in the current route
# adjust_cost1 = check_violation(route_ing)[1]
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
for j, rou in enumerate(sol_in3):
origin_cost2 = check_violation(rou, sol_type3[j])[1]
if j == c_loc[0]: # exchange in the same route
for k in range(1, len(rou)-1):
if k == c_loc[1]:
continue
rou_test = copy.deepcopy(sol_in3[c_loc[0]])
rou_test[k], rou_test[c_loc[1]] = rou_test[c_loc[1]], rou_test[k]
if check_violation(rou_test, 1)[0]:
adjust_cost2 = check_violation(rou_test, 1)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test
route_new_2 = rou_test
exch_to_route = j
elif check_violation(rou_test, 2)[0]:
adjust_cost2 = check_violation(rou_test, 2)[1]
cost_cut_test = origin_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test
route_new_2 = rou_test
exch_to_route = j
else: # exchange to a different route
for k in range(1, len(rou)-1):
rou_test_1 = copy.deepcopy(sol_in3[c_loc[0]])
rou_test_2 = copy.deepcopy(rou)
rou_test_1[c_loc[1]] = rou[k]
rou_test_2[k] = cust
if check_violation(rou_test_1, 1)[0] and check_violation(rou_test_2, 1)[0]:
adjust_cost1 = check_violation(rou_test_1, 1)[1]
adjust_cost2 = check_violation(rou_test_2, 1)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test_1
route_new_2 = rou_test_2
exch_to_route = j
elif check_violation(rou_test_1, 2)[0] and check_violation(rou_test_2, 2)[0]:
adjust_cost1 = check_violation(rou_test_1, 2)[1]
adjust_cost2 = check_violation(rou_test_2, 2)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test_1
route_new_2 = rou_test_2
exch_to_route = j
if best_cut_cost > 1e-5:
print 'exchange1 good', best_cut_cost
sol_in3[c_loc[0]] = route_new_1
sol_in3[exch_to_route] = route_new_2
sol_type3[c_loc[0]] = route_type(route_new_1)
sol_type3[exch_to_route] = route_type(route_new_2)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost / curr_temp) > prb:
print 'exchange1', best_cut_cost
sol_in3[c_loc[0]] = route_new_1
sol_in3[exch_to_route] = route_new_2
sol_type3[c_loc[0]] = route_type(route_new_1)
sol_type3[exch_to_route] = route_type(route_new_2)
# return sol_in3
def exchange_2_cust(sol_in4, cust, c_loc, curr_temp, sol_type4, sa_lns):
"""Exchange 2 consecutive customers' position with another 2 customers' position, and see if it can cut cost."""
route_ing = copy.deepcopy(sol_in4[c_loc[0]])
route_new_1 = route_ing
route_new_2 = route_ing
cust_folw = route_ing[c_loc[1] + 1]
exch_to_route = c_loc[0]
origin_cost1 = check_violation(route_ing, sol_type4[c_loc[0]])[1]
# route_ing.remove(cust) # move c in the current route
# adjust_cost1 = check_violation(route_ing)[1]
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
for j, rou in enumerate(sol_in4):
origin_cost2 = check_violation(rou, sol_type4[j])[1]
if j != c_loc[0] and len(rou) >= 4: # exchange to a different route
for k in range(1, len(rou) - 2):
rou_test_1 = copy.deepcopy(sol_in4[c_loc[0]])
rou_test_2 = copy.deepcopy(rou)
rou_test_1[c_loc[1]], rou_test_1[c_loc[1] + 1] = rou[k], rou[k + 1]
rou_test_2[k], rou_test_2[k + 1] = cust, cust_folw
if check_violation(rou_test_1, 1)[0] and check_violation(rou_test_2, 1)[0]:
adjust_cost1 = check_violation(rou_test_1, 1)[1]
adjust_cost2 = check_violation(rou_test_2, 1)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test_1
route_new_2 = rou_test_2
exch_to_route = j
if check_violation(rou_test_1, 2)[0] and check_violation(rou_test_2, 2)[0]:
adjust_cost1 = check_violation(rou_test_1, 2)[1]
adjust_cost2 = check_violation(rou_test_2, 2)[1]
cost_cut_test = origin_cost1 + origin_cost2 - adjust_cost1 - adjust_cost2
if cost_cut_test > best_cut_cost:
best_cut_cost = cost_cut_test
route_new_1 = rou_test_1
route_new_2 = rou_test_2
exch_to_route = j
if best_cut_cost > 1e-5:
print 'exchange2 good', best_cut_cost
sol_in4[c_loc[0]] = route_new_1
sol_in4[exch_to_route] = route_new_2
sol_type4[c_loc[0]] = route_type(route_new_1)
sol_type4[exch_to_route] = route_type(route_new_2)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost / curr_temp) > prb:
print 'exchange2', best_cut_cost
sol_in4[c_loc[0]] = route_new_1
sol_in4[exch_to_route] = route_new_2
sol_type4[c_loc[0]] = route_type(route_new_1)
sol_type4[exch_to_route] = route_type(route_new_2)
# return sol_in4
def two_exchange_sol(sol_in5, curr_temp, sol_type5, sa_lns):
"""Two-Exchange operator: For two customers i and j on the same route where i is visited before j,
remove arcs (i,i+),(j,j+); add arcs (i,j),(i+,j+); and reverse the orientation of the arcs between i+ and j.
Given a solution, check all possible neighborhood.
"""
solu = copy.deepcopy(sol_in5)
best_cut_cost0 = -1000
best_cut_cost = best_cut_cost0 # best cost cut of moving this customer
adjust_rou_ind = 0
route_new = sol_in5[adjust_rou_ind]
for i, rou in enumerate(solu):
if len(rou) >= 6:
origin_cost = check_violation(rou, sol_type5[i])[1]
for k in range(1, len(rou)-4):
for l in range(k+3, len(rou)-1):
route_test = copy.deepcopy(rou)
route_test[k], route_test[l] = route_test[l], route_test[k]
route_test[k+1: l] = route_test[l-1:k:-1] # middle reverse
if check_violation(route_test, 1)[0]:
adjust_cost = check_violation(route_test, 1)[1]
if origin_cost - adjust_cost > best_cut_cost:
best_cut_cost = origin_cost - adjust_cost
adjust_rou_ind = i
route_new = route_test
elif check_violation(route_test, 2)[0]:
adjust_cost = check_violation(route_test, 2)[1]
if origin_cost - adjust_cost > best_cut_cost:
best_cut_cost = origin_cost - adjust_cost
adjust_rou_ind = i
route_new = route_test
if best_cut_cost > 1e-5:
print '2exchange good', best_cut_cost
sol_in5[adjust_rou_ind] = route_new
sol_type5[adjust_rou_ind] = route_type(route_new)
elif sa_lns and best_cut_cost < -1e-5:
prb = random.uniform(0, 1)
if np.exp(best_cut_cost / curr_temp) > prb:
print '2exchange', best_cut_cost
sol_in5[adjust_rou_ind] = route_new
sol_type5[adjust_rou_ind] = route_type(route_new)
# return sol_in5
def two_opt(sol_in7, cust, c_loc, curr_temp, sol_type7, sa_lns):
"""2-opt*: given customer i in route a and customer j in route b, exchange the following sequences of i and j.
for example, initial route a: ...-i-i1-i2-..., initial route b: ...-j-j1-j2-...
New route a: ...-i-j1-j2-..., new route b: ...-j-i1-i2-..."""