-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemoRTS.py
More file actions
193 lines (162 loc) · 9.19 KB
/
Copy pathdemoRTS.py
File metadata and controls
193 lines (162 loc) · 9.19 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
from bin.distance_matrix.distance_matrix import DistanceMatrix
from bin.distance_matrix.uniform_distance_matrix_generator import UniformDistanceMatrixGenerator
from bin.problem_instantiator import ProblemInstance
from bin.rts_solver import RTSSolver
from bin.veicles.drone import Drone
from bin.veicles.energy_function import EnergyFunction
from bin.veicles.truck import Truck
from bin.util.drawer import draw_solution
from random import random, seed
from statistics import mean
import csv
SUPPRESS_OUTPUT = True
NUMBER_OF_CLIENT_NODES = [8, 12]
NUMBER_OF_TRAVEL_NODES = [8, 12]
SPACE_DIMENSION = 14000
SEEDS = [1, 2, 3, 4, 5]
NUMBERS_OF_AVAILABLE_DRONES = [1, 2, 3, 4, 5, 6, 7, 8]
class QuadricopterEnergyFunction(EnergyFunction):
def apply(self, flight_distance, carried_weight, is_hovering: bool):
if carried_weight <= 0.375:
return (31.000 + ((33.110 - 31.000) * (carried_weight - 0.000))/0.375) * flight_distance
if carried_weight <= 0.750:
return (33.110 + ((35.364 - 33.110) * (carried_weight - 0.375))/0.375) * flight_distance
if carried_weight <= 1.125:
return (35.364 + ((37.712 - 35.364) * (carried_weight - 0.750))/0.375) * flight_distance
if carried_weight <= 1.500:
return (37.712 + ((40.342 - 37.712) * (carried_weight - 1.125))/0.375) * flight_distance
if carried_weight <= 1.875:
return (40.342 + ((43.088 - 40.342) * (carried_weight - 1.500))/0.375) * flight_distance
if carried_weight <= 2.250:
return (43.088 + ((46.021 - 43.088) * (carried_weight - 1.875))/0.375) * flight_distance
if carried_weight <= 2.625:
return (46.021 + ((49.154 - 46.021) * (carried_weight - 2.250))/0.375) * flight_distance
if carried_weight <= 3.000:
return (49.154 + ((52.500 - 49.154) * (carried_weight - 2.625))) * flight_distance
def apply_if_hovering(self, hovering_time: float):
return 31.000 * hovering_time
class OctocopterEnergyFunction(EnergyFunction):
def apply(self, flight_distance, carried_weight, is_hovering: bool):
if carried_weight <= 2.5:
return (200.00 + ((217.41 - 200.00) * (carried_weight - 0.00))/2.5) * flight_distance
if carried_weight <= 5.0:
return (217.41 + ((236.34 - 217.41) * (carried_weight - 2.5))/2.5) * flight_distance
if carried_weight <= 7.5:
return (236.34 + ((256.92 - 236.34) * (carried_weight - 5.0))/2.5) * flight_distance
if carried_weight <= 10.0:
return (256.92 + ((279.28 - 256.92) * (carried_weight - 7.5))/2.5) * flight_distance
if carried_weight <= 12.5:
return (279.28 + ((303.60 - 279.28) * (carried_weight - 10.0))/2.5) * flight_distance
if carried_weight <= 15.0:
return (303.60 + ((330.03 - 303.60) * (carried_weight - 12.5))/2.5) * flight_distance
if carried_weight <= 17.5:
return (330.03 + ((358.77 - 330.03) * (carried_weight - 15.0))/2.5) * flight_distance
if carried_weight <= 20.0:
return (358.77 + ((390.00 - 358.77) * (carried_weight - 17.5))) * flight_distance
def apply_if_hovering(self, hovering_time: float):
return 200.000 * hovering_time
TRUCK_SPEED = 10 # m/s
TRUCK = Truck(TRUCK_SPEED)
QUADRICOPTER_ENERGY_FUNCTION = QuadricopterEnergyFunction()
QUADRICOPTER_MAX_WEIGHT = 3.000 # kg
QUADRICOPTER_MAX_ENERGY_AVAILABLE = [540000 * 1, 900000 * 1] # Joule*kg
QUADRICOPTER_SPEED = [10, 15] # m/s
OCTOCOPTER_ENERGY_FUNCTION = OctocopterEnergyFunction()
OCTOCOPTER_MAX_WEIGHT = 20.000 # kg
OCTOCOPTER_MAX_ENERGY_AVAILABLE = [540000 * 10, 900000 * 10] # Joule*kg
OCTOCOPTER_SPEED = [10, 15] # m/s
drones = []
for speed in QUADRICOPTER_SPEED:
for max_energy in QUADRICOPTER_MAX_ENERGY_AVAILABLE:
drones.append(Drone(speed, QUADRICOPTER_MAX_WEIGHT, max_energy, QUADRICOPTER_ENERGY_FUNCTION))
for speed in OCTOCOPTER_SPEED:
for max_energy in OCTOCOPTER_MAX_ENERGY_AVAILABLE:
drones.append(Drone(speed, OCTOCOPTER_MAX_WEIGHT, max_energy, OCTOCOPTER_ENERGY_FUNCTION))
def generate_random_weight_sequence(length, random_seed):
sequence = []
seed(random_seed)
for i in range(length):
sequence.append(random() * 2.3)
return sequence
WEIGHTS_SEED = 100
TRUCK_DISTANCE_FACTOR = 1.6
def compute_truck_distance_matrix(distance_matrix):
matrix = []
for row in distance_matrix:
r = []
for el in row:
r.append(el * TRUCK_DISTANCE_FACTOR)
matrix.append(r)
return matrix
def generate_instances(num_of_drones, drone_type):
problem_instances = []
for n in NUMBER_OF_CLIENT_NODES:
for m in NUMBER_OF_TRAVEL_NODES:
for s in SEEDS:
generator = UniformDistanceMatrixGenerator(s)
distance_matrix = generator.generate(m, n, SPACE_DIMENSION)
problem_instances.append(ProblemInstance(generator.get_clients_coordinates(),
generator.get_travels_coordinates(),
generate_random_weight_sequence(n, WEIGHTS_SEED),
distance_matrix,
DistanceMatrix(compute_truck_distance_matrix(distance_matrix.
get_truck_distance_matrix(n))),
num_of_drones,
drone_type,
TRUCK))
return problem_instances
with open('computational_results.csv', mode='w') as results:
results_writer = csv.writer(results, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
results_writer.writerow(["Drone Speed (m/s)", "Energy Density (J/kg)", "Rotors", "k", "Objective Function (s)",
"TSP obj (s)", "Time (s)", "feasible instances"])
counter = 0
total_number_of_instances_to_be_computed = \
len(drones) * len(NUMBERS_OF_AVAILABLE_DRONES) * len(NUMBER_OF_CLIENT_NODES) * len(NUMBER_OF_TRAVEL_NODES) * \
len(SEEDS)
for drone in drones:
for k in NUMBERS_OF_AVAILABLE_DRONES:
total_time_list = []
computational_time_list = []
number_of_instances = 0
number_of_infeasible_instances = 0
tsp_obj_values = []
for problem_instance in generate_instances(k, drone):
rts_solver = RTSSolver(problem_instance)
solution = rts_solver.solve()
total_time_list.append(solution.total_time)
computational_time_list.append(solution.computational_time)
tsp_obj_values.append(solution.tsp_obj_value * TRUCK_DISTANCE_FACTOR * TRUCK_SPEED)
number_of_instances += 1
if solution.is_infeasible:
number_of_infeasible_instances += 1
counter += 1
print(f"PROGRESS: {(100 * counter) / total_number_of_instances_to_be_computed}%\n"
f"Computed: {counter}/{total_number_of_instances_to_be_computed} instances\n\n")
if not SUPPRESS_OUTPUT:
print("\n\nPROBLEM INSTANCE" + "\nnumber of clients: " + str(len(problem_instance.client_nodes)) +
"\nnumber of travel nodes: " + str(len(problem_instance.travel_nodes)) +
"\ndrone: " + str(problem_instance.drone) + "\ntruck: " + str(problem_instance.truck) +
"\nnumber of drones: " + str(problem_instance.number_of_available_drones))
print("---visit order---")
for node in rts_solver.visit_order:
print(node)
print("-----------------")
print("is feasible = " + str(not solution.is_infeasible) +
"\nwarehouse: " + str(problem_instance.get_warehouse()))
print("-----ACTIVE EDGES-----")
if not solution.is_infeasible:
for edge in solution.active_edges:
print(edge)
print("----------------------")
print("Total time: " + str(round(solution.total_time, 2)) + "\nComputational time: " + str(
round(solution.computational_time, 3)))
draw_solution(problem_instance, solution, SPACE_DIMENSION)
results_writer.writerow([drone.speed,
drone.max_energy_available,
4 if drone.max_weight == 3 else 8,
k,
round(mean(total_time_list), 2),
round(mean(tsp_obj_values), 2),
round(mean(computational_time_list), 3),
number_of_instances - number_of_infeasible_instances])
print("\n\nTERMINATED")