-
Notifications
You must be signed in to change notification settings - Fork 7
/
Greedy2.py
96 lines (79 loc) · 2.76 KB
/
Greedy2.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
from H_Hy_Men import VRPLibReader
class Customer:
def __init__(self, num, demand):
self.num = num
self.demand = demand
self.is_visited = False
class UAV:
def __init__(self, cap):
self.cap = cap
self.load = 0
self.routes = []
self.current_location = 0
def check_if_fit(self, demand):
return self.load + demand <= self.cap
def transport(self, cus):
self.routes.append(cus)
self.load += cus.demand
self.current_location = cus.num
cost = 0
distances = VRPLibReader.distmat
dimension = VRPLibReader.n
capacity = VRPLibReader.capacity
customers = []
UAVs = []
for i in range(dimension):
customers.append(Customer(i, VRPLibReader.things[i]))
for i in range(dimension):
UAVs.append(UAV(capacity))
def unassigned_customer_exists(cus_list):
for i in cus_list:
if not i.is_visited:
return True
return False
def greedy_solver():
uav_index = 0
global cost
while unassigned_customer_exists(customers):
cus_index = 0
candidate = None
min_cost = float('inf')
if len(UAVs[uav_index].routes) == 0:
UAVs[uav_index].transport(customers[0])
if not customers[cus_index].is_visited:
customers[cus_index].is_visited = True
for i in range(dimension):
if not customers[i].is_visited:
if UAVs[uav_index].check_if_fit(customers[i].demand):
candidate_cost = distances[UAVs[uav_index].current_location][i]
if min_cost > candidate_cost:
min_cost = candidate_cost
cus_index = i
candidate = customers[i]
if candidate is None:
if uav_index + 1 < len(UAVs):
if UAVs[uav_index].current_location != 0:
final_cost = distances[UAVs[uav_index].current_location][0]
UAVs[uav_index].transport(customers[0])
cost += final_cost
uav_index += 1
else:
exit("The problem cannot be resolved under these constrains")
else:
UAVs[uav_index].transport(candidate)
customers[cus_index].is_visited = True
cost += min_cost
final_cost = distances[UAVs[uav_index].current_location][0]
UAVs[uav_index].transport(customers[0])
cost += final_cost
greedy_solver()
# for i in range(dimension):
# if UAVs[i].routes:
# print("UAV", i, ":")
# for j in range(len(UAVs[i].routes)):
# if j == len(UAVs[i].routes) - 1:
# print(UAVs[i].routes[j].num)
# else:
# print(UAVs[i].routes[j].num, "->")
# print()
print("best cost=", cost)