-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1179 lines (1022 loc) · 43.8 KB
/
app.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
import streamlit as st
import pandas as pd
import altair as alt
import numpy as np
import sys
import os
from ortools.linear_solver import pywraplp
import random
import copy
import networkx as nx
import matplotlib.pyplot as plt
from urllib.error import URLError
def file_selector(folder_path='testset_1/'):
filenames = os.listdir(folder_path)
selected_filename = st.selectbox(
'1. Select Input file from current directory', filenames)
return os.path.join(folder_path, selected_filename)
def read_ints(s):
return [int(i) for i in s.split(' ')]
def read_input_IP(input_filepath):
with open(input_filepath, 'r') as f:
input_file = f.read().split('\n')
nl, mt, ns, nt = read_ints(input_file[0])
orgs, dests, wght, time, pps = [], [], [], [], []
for i in range(ns):
l = read_ints(input_file[1 + i])
orgs.append(l[0])
dests.append(l[1])
wght.append(l[2])
time.append(l[3])
pps.append(l[4])
startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = [], [], [], [], [], []
for i in range(nt):
l = read_ints(input_file[1+ns+i])
startTripLoc.append(l[0])
endTripLoc.append(l[1])
startTime.append(l[2])
endTime.append(l[3])
ppkg.append(l[4])
mw.append(l[5])
route = np.zeros((nt, nl, nl), int)
route = route.tolist()
for t in range(nt):
route[t][startTripLoc[t]][endTripLoc[t]] = 1
tim = np.zeros((nt, mt, mt), int)
tim = tim.tolist()
for t in range(nt):
tim[t][startTime[t]][endTime[t]] = 1
return nl, mt, ns, nt, orgs, dests, wght, time, pps, route, tim, ppkg, mw
def read_input_IP_Manual(nl, mt, ns, nt, shipmentsArray, tripsArray):
orgs, dests, wght, time, pps = [], [], [], [], []
for i in range(len(shipmentsArray)):
l = read_ints(shipmentsArray[i])
orgs.append(l[0])
dests.append(l[1])
wght.append(l[2])
time.append(l[3])
pps.append(l[4])
startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = [], [], [], [], [], []
for i in range(nt):
l = read_ints(tripsArray[i])
startTripLoc.append(l[0])
endTripLoc.append(l[1])
startTime.append(l[2])
endTime.append(l[3])
ppkg.append(l[4])
mw.append(l[5])
route = np.zeros((nt, nl, nl), int)
route = route.tolist()
for t in range(nt):
route[t][startTripLoc[t]][endTripLoc[t]] = 1
tim = np.zeros((nt, mt, mt), int)
tim = tim.tolist()
for t in range(nt):
tim[t][startTime[t]][endTime[t]] = 1
return nl, mt, ns, nt, orgs, dests, wght, time, pps, route, tim, ppkg, mw
def read_input_Greedy(input_filepath):
with open(input_filepath, 'r') as f:
input_file = f.read().split('\n')
nl, mt, ns, nt = read_ints(input_file[0])
orgs, dests, wght, time, pps = [], [], [], [], []
for i in range(ns):
l = read_ints(input_file[1 + i])
orgs.append(l[0])
dests.append(l[1])
wght.append(l[2])
time.append(l[3])
pps.append(l[4])
startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = [], [], [], [], [], []
for i in range(nt):
l = read_ints(input_file[1+ns+i])
startTripLoc.append(l[0])
endTripLoc.append(l[1])
startTime.append(l[2])
endTime.append(l[3])
ppkg.append(l[4])
mw.append(l[5])
route = np.zeros((nt, nl, nl), int)
route = route.tolist()
for t in range(nt):
route[t][startTripLoc[t]][endTripLoc[t]] = 1
tim = np.zeros((nt, mt, mt), int)
tim = tim.tolist()
for t in range(nt):
tim[t][startTime[t]][endTime[t]] = 1
return nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw
def read_input_Greedy_Manual(nl, mt, ns, nt, shipmentsArray, tripsArray):
orgs, dests, wght, time, pps = [], [], [], [], []
for i in range(ns):
l = read_ints(shipmentsArray[i])
orgs.append(l[0])
dests.append(l[1])
wght.append(l[2])
time.append(l[3])
pps.append(l[4])
startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = [], [], [], [], [], []
for i in range(nt):
l = read_ints(tripsArray[i])
startTripLoc.append(l[0])
endTripLoc.append(l[1])
startTime.append(l[2])
endTime.append(l[3])
ppkg.append(l[4])
mw.append(l[5])
route = np.zeros((nt, nl, nl), int)
route = route.tolist()
for t in range(nt):
route[t][startTripLoc[t]][endTripLoc[t]] = 1
tim = np.zeros((nt, mt, mt), int)
tim = tim.tolist()
for t in range(nt):
tim[t][startTime[t]][endTime[t]] = 1
return nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw
def read_input_Graph(input_filepath):
with open(input_filepath, 'r') as f:
input_file = f.read().split('\n')
nl, mt, ns, nt = read_ints(input_file[0])
orgs, dests, wght, time, pps = [], [], [], [], []
for i in range(ns):
l = read_ints(input_file[1 + i])
orgs.append(l[0])
dests.append(l[1])
wght.append(l[2])
time.append(l[3])
pps.append(l[4])
startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = [], [], [], [], [], []
for i in range(nt):
l = read_ints(input_file[1+ns+i])
startTripLoc.append(l[0])
endTripLoc.append(l[1])
startTime.append(l[2])
endTime.append(l[3])
ppkg.append(l[4])
mw.append(l[5])
route = np.zeros((nt, nl, nl), int)
route = route.tolist()
for t in range(nt):
route[t][startTripLoc[t]][endTripLoc[t]] = 1
tim = np.zeros((nt, mt, mt), int)
tim = tim.tolist()
for t in range(nt):
tim[t][startTime[t]][endTime[t]] = 1
return nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw, tim, route
def ip(ns, nt, nl, mt, ppkg, mw, route, tim, orgs, dests, wght, time, pps):
solver = pywraplp.Solver.CreateSolver('SCIP')
x_st = []
y_s = []
f_sil = []
objective = solver.Objective()
for s in range(ns):
y_s.append(solver.BoolVar('Shipment'+str(s)))
objective.SetCoefficient(y_s[s], 1)
temparr = []
for t in range(nt):
temparr.append(solver.BoolVar(f'Shipment_{s}_Trip_{t}'))
x_st.append(temparr)
temparr = []
for i in range(nl):
temparr2 = []
for l in range(mt):
temparr2.append(solver.BoolVar(
f'Shipment_{s}_loc_{i}_time_{l}'))
temparr.append(temparr2)
f_sil.append(temparr)
objective.SetMaximization()
# (2) Respect stated price by customer
for s in range(ns):
solver.Add(solver.Sum([x_st[s][t]*ppkg[t]*wght[s]
for t in range(nt)]) <= pps[s]*y_s[s])
# (3) Respect weight per trip
for t in range(nt):
solver.Add(solver.Sum([x_st[s][t]*wght[s]
for s in range(ns)]) <= mw[t])
# (4) Packages are at their origin initialy
for s in range(ns):
for i in range(nl):
solver.Add(f_sil[s][orgs[s]][0] == 1)
# (5) f_sil
for s in range(ns):
for i in range(nl):
for l in range(1, mt):
solver.Add(f_sil[s][i][l] == f_sil[s][i][l-1]-solver.Sum([x_st[s][t]*route[t][i][j]*tim[t][l][e] for j in range(nl) for e in range(mt)
for t in range(nt)])+solver.Sum([x_st[s][t]*route[t][j][i]*tim[t][b][l] for j in range(nl) for b in range(mt) for t in range(nt)]))
# (6) Leave only Once
for s in range(ns):
for i in range(nl):
solver.Add(solver.Sum([x_st[s][t]*route[t][i][j]
for t in range(nt) for j in range(nl)]) <= 1)
# (7) Max one place per time
for s in range(ns):
for l in range(mt):
solver.Add(solver.Sum([f_sil[s][i][l] for i in range(nl)]) <= 1)
# (8) Shipments cannot leave final dest
for s in range(ns):
solver.Add(solver.Sum([x_st[s][t]*route[t][dests[s]][j]
for j in range(nl) for t in range(nt)]) == 0)
# (9) Taken when delivered
for s in range(ns):
solver.Add(f_sil[s][dests[s]][time[s]] == y_s[s])
# (10) Shipments stop when no
# for s in range(ns):
# solver.Add(solver.Sum([x_st[s][t] for t in range(nt)])<=nt*y_s[s])
status = solver.Solve()
if status == solver.OPTIMAL:
returnedArray = []
for s in range(ns):
returnedArrayTemp = []
# print(y_s[s],y_s[s].solution_value())
for t in range(nt):
if x_st[s][t].solution_value() == 1:
returnedArrayTemp.append(t)
returnedArray.append(returnedArrayTemp)
# print(x_st[s][t],x_st[s][t].solution_value())
# for i in range(nl):
# for l in range(mt):
# if f_sil[s][i][l].solution_value()==1:
# print(f_sil[s][i][l],f_sil[s][i][l].solution_value())
return round(solver.Objective().Value()), returnedArray
def greedy(ns, nt, nl, mt, ppkg, mw, startTripLoc, endTripLoc, startTime, endTime, orgs, dests, wght, time, pps):
change = True
shipmentTimes = np.zeros(ns)
res = []
deliveredNum = 0
for i in range(ns):
res.append([])
# print(pps, mw, orgs, dests, shipmentTimes)
while(change):
change = False
for s in range(ns):
shipOrg = orgs[s]
shipDest = dests[s]
shipCurTime = shipmentTimes[s]
shipMaxTime = time[s]
shipPps = pps[s]
shipWght = wght[s]
changeShip = False
for t in range(nt):
tripOrg = startTripLoc[t]
tripDest = endTripLoc[t]
tripStartTim = startTime[t]
tripEndTim = endTime[t]
tripPpkg = ppkg[t]
tripWght = mw[t]
#print(shipMaxTime, tripEndTim)
if shipOrg == tripOrg and shipDest == tripDest and shipCurTime <= tripStartTim and shipMaxTime >= tripEndTim and shipPps >= tripPpkg*shipWght and tripWght >= shipWght:
# print("Here")
pps[s] -= tripPpkg*shipWght
mw[t] -= shipWght
orgs[s] = tripDest
shipmentTimes[s] = tripEndTim
change = True
changeShip = True
res[s].append(t)
deliveredNum += 1
break
if not changeShip:
minCost = 1e9
minIndex = -1
for t in range(nt):
tripOrg = startTripLoc[t]
tripDest = endTripLoc[t]
tripStartTim = startTime[t]
tripEndTim = endTime[t]
tripPpkg = ppkg[t]
tripWght = mw[t]
if shipOrg == tripOrg and shipCurTime <= tripStartTim and shipMaxTime >= tripEndTim and shipPps >= tripPpkg*shipWght and tripWght >= shipWght:
# print("Here2")
if minCost > tripPpkg*shipWght:
minCost = tripPpkg*shipWght
minIndex = t
if minIndex != -1:
pps[s] -= ppkg[minIndex]*shipWght
mw[minIndex] -= shipWght
orgs[s] = endTripLoc[minIndex]
shipmentTimes[s] = endTime[minIndex]
res[s].append(minIndex)
change = True
for s in range(ns):
if orgs[s] != dests[s]:
res[s] = []
# print(pps, mw, orgs, dests, shipmentTimes)
return deliveredNum, res
# Dynamic Programming Solution
def fixArray(array1, ns):
array2 = []
temp = []
for elem in array1:
if elem == -1:
array2.append(temp)
temp = []
else:
if elem != -2:
temp.append(elem)
for i in range(len(array2), ns):
array2.append([])
return array2
memo = {}
numberOfRuns = 0
def dp(ns, nt, ppkg, mw, startTripLoc, endTripLoc, startTime, endTime, orgs, dests, wght, time, pps, shipmentTimes):
global numberOfRuns
numberOfRuns += 1
mwString = ""
for digit in mw:
mwString += str(digit)+","
# base Case
if ns <= 0:
return 0, []
shipOrg = orgs[0]
# print(orgs)
shipDest = dests[0]
shipCurTime = shipmentTimes[0]
shipMaxTime = time[0]
shipPps = pps[0]
shipWght = wght[0]
# subproblem already solved
if (ns, mwString, orgs[0], shipmentTimes[0]) in memo:
return memo[(ns, mwString, orgs[0], shipmentTimes[0])]
# go look for trip
maxDelNum = 0
endOfShip = False
theTrip = -1
theArray = []
for t in range(nt):
tripOrg = startTripLoc[t]
tripDest = endTripLoc[t]
tripStartTim = startTime[t]
tripEndTim = endTime[t]
tripPpkg = ppkg[t]
tripWght = mw[t]
if shipOrg == tripOrg and shipCurTime <= tripStartTim and shipMaxTime >= tripEndTim and shipPps >= tripPpkg*shipWght and tripWght >= shipWght:
if shipDest == tripDest:
mwclone = mw[:]
mwclone[t] -= shipWght
deliveredNum, returnedArray = dp(ns-1, nt, ppkg, mwclone, startTripLoc, endTripLoc,
startTime, endTime, orgs[1:], dests[1:], wght[1:], time[1:], pps[1:], shipmentTimes[1:])
deliveredNum += 1
if deliveredNum > maxDelNum:
theArray = returnedArray
maxDelNum = deliveredNum
endOfShip = True
theTrip = t
else:
ppsclone = pps[:]
ppsclone[0] -= tripPpkg*shipWght
mwclone = mw[:]
mwclone[t] -= shipWght
orgsclone = orgs[:]
orgsclone[0] = tripDest
shipmentTimesclone = shipmentTimes[:]
shipmentTimesclone[0] = tripEndTim
deliveredNum, returnedArray = dp(ns, nt, ppkg, mwclone, startTripLoc, endTripLoc,
startTime, endTime, orgsclone, dests, wght, time, ppsclone, shipmentTimesclone)
if deliveredNum > maxDelNum:
theArray = returnedArray
maxDelNum = deliveredNum
endOfShip = False
theTrip = t
else:
deliveredNum, returnedArray = dp(ns-1, nt, ppkg, mw, startTripLoc, endTripLoc, startTime,
endTime, orgs[1:], dests[1:], wght[1:], time[1:], pps[1:], shipmentTimes[1:])
if deliveredNum > maxDelNum:
theArray = returnedArray
maxDelNum = deliveredNum
endOfShip = True
theTrip = -2
deliveredNum, returnedArray = dp(ns-1, nt, ppkg, mw, startTripLoc, endTripLoc, startTime,
endTime, orgs[1:], dests[1:], wght[1:], time[1:], pps[1:], shipmentTimes[1:])
if deliveredNum > maxDelNum:
theArray = returnedArray
maxDelNum = deliveredNum
endOfShip = True
theTrip = -2
# add to memo
if endOfShip:
memo[(ns, mwString, orgs[0], shipmentTimes[0])
] = maxDelNum, [theTrip, -1]+theArray
return maxDelNum, [theTrip, -1]+theArray
else:
memo[(ns, mwString, orgs[0], shipmentTimes[0])
] = maxDelNum, [theTrip]+theArray
return maxDelNum, [theTrip]+theArray
#==========================================================================================#
# Genetic is coming
def run(ns, nt, nl, mt, ppkg, mw, startTripLoc, endTripLoc, startTime, endTime, orgs, dests, wght, time, pps):
change = True
shipmentTimes = np.zeros(ns)
res = []
for i in range(ns):
res.append([])
# print(pps, mw, orgs, dests, shipmentTimes)
while(change):
change = False
for s in range(ns):
shipOrg = orgs[s]
shipDest = dests[s]
shipCurTime = shipmentTimes[s]
shipMaxTime = time[s]
shipPps = pps[s]
shipWght = wght[s]
changeShip = False
# if there is a trip that dirctly delevers the shipments from the
# current location directly take that trip
for t in range(nt):
tripOrg = startTripLoc[t]
tripDest = endTripLoc[t]
tripStartTim = startTime[t]
tripEndTim = endTime[t]
tripPpkg = ppkg[t]
tripWght = mw[t]
#print(shipMaxTime, tripEndTim)
if shipOrg == tripOrg and shipDest == tripDest and shipCurTime <= tripStartTim and shipMaxTime >= tripEndTim and shipPps >= tripPpkg*shipWght and tripWght >= shipWght:
# print("Here")
pps[s] -= tripPpkg*shipWght
mw[t] -= shipWght
orgs[s] = tripDest
shipmentTimes[s] = tripEndTim
change = True
changeShip = True
res[s].append(t)
break
if not changeShip and shipOrg != shipDest:
randomized = []
# check all possible trips that can be choosen for the current shipment
# and collect a random trip from the pool of trips
for t in range(nt):
tripOrg = startTripLoc[t]
tripDest = endTripLoc[t]
tripStartTim = startTime[t]
tripEndTim = endTime[t]
tripPpkg = ppkg[t]
tripWght = mw[t]
if shipOrg == tripOrg and shipCurTime <= tripStartTim and shipMaxTime >= tripEndTim and shipPps >= tripPpkg*shipWght and tripWght >= shipWght:
# print("Here2")
randomized.append(t)
randInd = random.randint(0, len(randomized))
if randInd != 0:
randInd -= 1
# print(randInd, randomized, s)
if len(randomized) != 0:
randT = randomized[randInd]
pps[s] -= ppkg[randT]*shipWght
mw[randT] -= shipWght
orgs[s] = endTripLoc[randT]
shipmentTimes[s] = endTime[randT]
res[s].append(randT)
change = True
# remove the packages that where not deleverd correctly
# side note the prices where not reset so they still contributed in the trip
# so other trips may not collect the right ship because it has full capacity.
# however this is greedy
for s in range(ns):
if orgs[s] != dests[s]:
res[s] = []
# print(pps, mw, orgs, dests, shipmentTimes)
return res
def array2d_to_string(array):
result = ""
for i in range(len(array)):
for j in range(len(array[i])):
result += str(array[i][j])+','
result += ";"
return result[:-1]
def dict_to_population(dic):
result = []
for key in dic:
shipments = key.split(';')
ship = []
for str_trip in shipments:
if str_trip != []:
trips = str_trip.split(',')
trips = [int(x) for x in trips if x != '']
ship.append(trips)
result.append(ship)
return result
counter = 0
def create_population(population):
global counter
current_population = 0
while(True):
if counter == 2000:
print("Could only find: ", current_population)
break
random_shipment_indices = random.sample(range(ns), ns)
counter += 1
orgs_random = np.zeros(ns)
dests_random = np.zeros(ns)
wght_random = np.zeros(ns)
time_random = np.zeros(ns)
pps_random = np.zeros(ns)
i = 0
for index in random_shipment_indices:
orgs_random[i] = orgs[index]
dests_random[i] = dests[index]
wght_random[i] = wght[index]
time_random[i] = time[index]
pps_random[i] = pps[index]
i += 1
random_trip_indices = random.sample(range(nt), nt)
startTripLoc_random = np.zeros(nt)
endTripLoc_random = np.zeros(nt)
startTime_random = np.zeros(nt)
endTime_random = np.zeros(nt)
ppkg_random = np.zeros(nt)
mw_random = np.zeros(nt)
i = 0
for index in random_trip_indices:
startTripLoc_random[i] = startTripLoc[index]
endTripLoc_random[i] = endTripLoc[index]
startTime_random[i] = startTime[index]
endTime_random[i] = endTime[index]
ppkg_random[i] = ppkg[index]
mw_random[i] = mw[index]
i += 1
res = run(ns, nt, nl, mt, ppkg_random, mw_random, startTripLoc_random, endTripLoc_random,
startTime_random, endTime_random, orgs_random, dests_random, wght_random, time_random, pps_random)
i = 0
res_org = copy.deepcopy(res)
for index in random_shipment_indices:
res_org[index] = res[i][:]
i += 1
dict_r = {}
i = 0
for index in random_trip_indices:
dict_r[i] = index
i += 1
for i in range(ns):
for j in range(len(res_org[i])):
res_org[i][j] = dict_r[res_org[i][j]]
key = array2d_to_string(res_org)
if key in dict_sol:
continue
else:
dict_sol[key] = 1
current_population += 1
if current_population == population:
break
def crossover(x, y):
randomgene = random.sample(range(ns), ns)
childx = []
childy = []
for i in range(ns):
if randomgene[i] > ns/2:
childx.append(x[i])
childy.append(y[i])
else:
childx.append(y[i])
childy.append(x[i])
return childx, childy
def validate(solution):
trip_taken = np.zeros(nt)
for i in range(ns):
for j in range(len(solution[i])):
trip_taken[solution[i][j]] += wght[i]
for i in range(len(trip_taken)):
if (trip_taken[i] > mw[i]):
return False
return True
def calculate_fitness(solution):
score = 0
for arr in solution:
if arr != []:
score += 1
return score
def GA():
global population
for i in range(gen):
# calculate fitness score
score_population = []
for result in population:
fitness = calculate_fitness(result)
score_population.append(fitness)
zipped_scores = zip(score_population, population)
sorted_scores_lists = sorted(zipped_scores)
population = [element for _, element in sorted_scores_lists]
fittest_pop = population[:]
for j in range(len(fittest_pop)):
for k in range(j+1, len(fittest_pop)):
parent_x = fittest_pop[j]
parent_y = fittest_pop[k]
childx, childy = crossover(parent_x, parent_y)
least_fittest = sorted_scores_lists[0][0]
fitnessx = -1
fitnessy = -1
if validate(childx):
mutation = random.randint(0, 200)
if mutation < 3:
shipment_mutated = random.randint(0, ns-1)
childx[shipment_mutated] = []
fitnessx = calculate_fitness(childx)
if validate(childy):
mutation = random.randint(0, 200)
if mutation < 3:
shipment_mutated = random.randint(0, ns-1)
childy[shipment_mutated] = []
fitnessy = calculate_fitness(childy)
if fitnessx != -1:
if fitnessy != -1:
if fitnessx > fitnessy:
if fitnessx > least_fittest:
population = population[1:]
population.append(childx)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append((fitnessx, childx))
least_fittest = sorted_scores_lists[0][0]
if fitnessy > least_fittest:
population = population[1:]
population.append(childy)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append(
(fitnessy, childy))
else:
if fitnessy > least_fittest:
population = population[1:]
population.append(childy)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append((fitnessy, childy))
least_fittest = sorted_scores_lists[0][0]
if fitnessx > least_fittest:
population = population[1:]
population.append(childx)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append(
(fitnessx, childx))
else:
if fitnessx > least_fittest:
population = population[1:]
population.append(childx)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append((fitnessx, childx))
else:
if fitnessy > least_fittest:
population = population[1:]
population.append(childy)
sorted_scores_lists = sorted_scores_lists[1:]
sorted_scores_lists.append((fitnessy, childy))
return population[-1]
# Creating the output map
def graph_fun(edges, weight, name):
G = nx.MultiGraph(edges)
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_color='green', node_size=500, alpha=1)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weight, font_color='r')
ax = plt.gca()
for e in G.edges:
ax.annotate("",
xy=pos[e[1]], xycoords='data',
xytext=pos[e[0]], textcoords='data',
arrowprops=dict(arrowstyle="->", color="0.5",
shrinkA=15, shrinkB=15,
patchA=None, patchB=None,
connectionstyle="arc3,rad=rrr".replace('rrr', str(0.3*e[2])
),
),
)
plt.axis('off')
# plt.show()
name = name + ": "
st.write(name)
st.pyplot(plt, clear_figure=True)
def create_edges(start, end, weight, t1, t2, p):
#print(start, end)
output = [(start[a], end[a]) for a in range(len(start))]
weight = {output[a]: str(weight[a]) + "," + str(t1[a]) + "," +
str(t2[a]) + "," + str(p[a]) for a in range(len(output))}
return output, weight
def collect_output(start, end, testcase, i):
file = open(f"testset_1_output/Algo{i}/test_{testcase}.out")
output_temp = []
for f in file:
res = []
for i in f.split(" "):
z = f.split(" ")
if not "\n" in i:
res.append(i)
output_temp.append(res)
output = []
# print(weight_temp)
for o in range(len(output_temp)):
# print(output_temp[o])
if len(output_temp[o]) != 0:
for i in output_temp[o]:
output.append((start[int(i)], end[int(i)]))
arr = [oo for out in output_temp for oo in out]
print(arr)
weight = {output[a]: arr[a] for a in range(len(output))}
print(weight, output)
return output, weight
def draw_figure(testcase, algo):
# inputs
nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw, tim, route = read_input_Graph(
f"testset_1/test_{testcase}.in")
input_trip_edges, weight_i_t = create_edges(
startTripLoc, endTripLoc, mw, startTime, endTime, ppkg)
input_ship_edges, weight_i_s = create_edges(
orgs, dests, wght, [0]*ns, time, pps)
output_path, weight_o = collect_output(
startTripLoc, endTripLoc, testcase, algo)
# graph
graph_fun(input_trip_edges, weight_i_t, "Input Trips")
graph_fun(input_ship_edges, weight_i_s, "Input Shipment Org and Dest")
graph_fun(output_path, weight_o, "Output Shipment Trips")
# Streamlit web app
st.write("""
# Ship it
Send your packages all over the world with the best prices!
""")
inputType = st.selectbox(
'1. Upload an input file or input required fields manually', ["Upload a file", "Input manually"])
inputAlgo = st.selectbox(
'2. Select an Algorithm', ["IP", "DP", "Greedy", "Genetic"])
if inputType == "Input manually":
nl = st.text_input('Number of locations', '6', key='0')
mt = st.text_input('Maximum time allowed', '10', key='1')
ns = st.text_input('Number of Shipments', '4', key='2')
nt = st.text_input('Number of locations', '6', key='3')
with st.form("second_form"):
if inputType == "Upload a file":
filename = file_selector()
st.write('You selected `%s`' % filename)
else:
shipmentsArray = [0] * int(ns)
tripsArray = [0] * int(nt)
for i in range(int(ns)):
shipmentsArray[i] = st.text_input(
'orgs,dests,wght,time,pps', '0 1 10 5 40', key=str(i+100000))
# shipmentNumber = 'Shipment ' + str(i) + ': '
# st.write(shipmentNumber, shipmentsArray[i])
for i in range(int(nt)):
tripsArray[i] = st.text_input(
'startTripLoc,endTripLoc,startTime,endTime,ppkg,mw', '3 5 5 6 3 30', key=str(i+200000))
# tripNumber = 'Trip ' + str(i) + ': '
# st.write(tripNumber, tripsArray[i])
if(inputAlgo == 'Genetic'):
max_population = st.text_input(
'Max Population: ', '12', key='563453332')
max_population = int(max_population)
# max iterations
gen = st.text_input('Number of generations: ',
'100', key='563453315332')
gen = int(gen)
st.write('You selected `%s`' % inputAlgo)
runned = st.form_submit_button("Run")
if runned:
if(inputAlgo == "IP"):
if (inputType == "Upload a file"):
nl, mt, ns, nt, orgs, dests, wght, time, pps, route, tim, ppkg, mw = read_input_IP(
filename)
st.write("Running input file", filename,
"using", inputAlgo, "Algorithm")
else:
nl, mt, ns, nt, orgs, dests, wght, time, pps, route, tim, ppkg, mw = read_input_IP_Manual(
int(nl), int(mt), int(ns), int(nt), shipmentsArray, tripsArray)
# for i in range(int(ns)):
# shipmentNumber = 'Shipment ' + str(i) + ': '
# st.write(shipmentNumber, shipmentsArray[i])
# for i in range(int(nt)):
# tripNumber = 'Trip ' + str(i) + ': '
# st.write(tripNumber, tripsArray[i])
indexMe = ['Shipment'] * ns
arrayDF = ['Shipment'] * ns
# st.write(shipmentsArray[0].split(','))
for i in range(ns):
indexMe[i] = 'Shipment ' + str(i)
arrayDF[i] = shipmentsArray[i].split(' ')
dfObj = pd.DataFrame(
arrayDF, columns=['orgs', 'dests', 'wght', 'time', 'pps'], index=indexMe)
st.write('Inputed shipments: ')
st.dataframe(dfObj)
indexTrips = ['Trips'] * nt
arrayDF = ['Trips'] * nt
for i in range(nt):
indexTrips[i] = 'Trip ' + str(i)
arrayDF[i] = tripsArray[i].split(' ')
dfObj = pd.DataFrame(
arrayDF, columns=['startTripLoc', 'endTripLoc', 'startTime', 'endTime', 'ppkg', 'mw'], index=indexTrips)
st.write('Inputed Trips: ')
st.dataframe(dfObj)
st.write('Inputed number of locations: ', nl)
st.write('Inputed maximum time allowed: ', mt)
st.write('Inputed number of Shipments: ', ns)
st.write('Inputed number of trips: ', nt)
shipmentDelivered, returnedArray = ip(ns, nt, nl, mt, ppkg, mw, route,
tim, orgs, dests, wght, time, pps)
# df = pd.DataFrame(returnedArray)
# st.dataframe(df)
# st.write("Output: ", returnedArray)
st.write("Output: ")
st.write("Number of shipments delivered: ", shipmentDelivered)
indexMe = ['Shipment'] * ns
isSent = ['Shipment'] * ns
arrayDF = ['Shipment'] * ns
for i in range(ns):
indexMe[i] = 'Shipment ' + str(i)
if(len(returnedArray[i]) != 0):
isSent[i] = True
else:
isSent[i] = False
arrayDF[i] = [isSent[i], returnedArray[i]]
dfObj = pd.DataFrame(arrayDF, columns=[
'isDelievered', 'Path'], index=indexMe)
st.dataframe(dfObj)
elif(inputAlgo == "DP"):
if (inputType == "Upload a file"):
nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = read_input_Greedy(
filename)
st.write("Running input file", filename,
"using", inputAlgo, "Algorithm")
else:
nl, mt, ns, nt, orgs, dests, wght, time, pps, startTripLoc, endTripLoc, startTime, endTime, ppkg, mw = read_input_Greedy_Manual(
int(nl), int(mt), int(ns), int(nt), shipmentsArray, tripsArray)
# for i in range(int(ns)):
# shipmentNumber = 'Shipment ' + str(i) + ': '
# st.write(shipmentNumber, shipmentsArray[i])
# for i in range(int(nt)):
# tripNumber = 'Trip ' + str(i) + ': '
# st.write(tripNumber, tripsArray[i])
indexMe = ['Shipment'] * ns
arrayDF = ['Shipment'] * ns
# st.write(shipmentsArray[0].split(','))
for i in range(ns):
indexMe[i] = 'Shipment ' + str(i)
arrayDF[i] = shipmentsArray[i].split(' ')
dfObj = pd.DataFrame(
arrayDF, columns=['orgs', 'dests', 'wght', 'time', 'pps'], index=indexMe)
st.write('Inputed shipments: ')
st.dataframe(dfObj)
indexTrips = ['Trips'] * nt
arrayDF = ['Trips'] * nt
for i in range(nt):
indexTrips[i] = 'Trip ' + str(i)
arrayDF[i] = tripsArray[i].split(' ')
dfObj = pd.DataFrame(
arrayDF, columns=['startTripLoc', 'endTripLoc', 'startTime', 'endTime', 'ppkg', 'mw'], index=indexTrips)
st.write('Inputed Trips: ')
st.dataframe(dfObj)
# st.write(nl, mt, ns, nt, orgs, dests, wght, time, pps,
# startTripLoc, endTripLoc, startTime, endTime, ppkg, mw)
st.write('Inputed number of locations: ', nl)
st.write('Inputed maximum time allowed: ', mt)
st.write('Inputed number of Shipments: ', ns)
st.write('Inputed number of trips: ', nt)
shipmentTimes = [0]*int(ns)
num, array = dp(ns, nt, ppkg, mw, startTripLoc, endTripLoc,
startTime, endTime, orgs, dests, wght, time, pps, shipmentTimes)
st.write("Output: ")