-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
179 lines (156 loc) · 5.5 KB
/
main.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
import argparse
import csv
import os
import random
import sys
import geojson
import numpy as np
import shapely
from shapely import geometry
from shapely.affinity import scale, translate
from shapely.ops import transform
from trajallocpy import Agent, CoverageProblem, Experiment, Task, Utility
def saveResults(experiment_title, results, directory="experiments/"):
results_header = [
"dataset_name",
"totalRouteLength",
"sumOfTaskLengths",
"totalRouteCosts",
"maxRouteCost",
"iterations",
"computeTime",
"num_tasks",
"number_of_agents",
]
isExist = os.path.exists(directory)
# Create a new directory if it does not exist
if not isExist:
os.makedirs(directory)
with open(directory + experiment_title + ".csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(results_header)
writer.writerows(results)
def main(
dataset_name,
experiment_title,
n_agents,
capacity,
show_plots=True,
debug=False,
):
results = []
run_experiment(
"custom",
n_agents,
capacity,
show_plots,
debug,
results,
"environment.geojson",
)
# files = Utility.getAllCoverageFiles(dataset_name)
# for file_name in files:
# run_experiment(experiment_title, n_agents, capacity, show_plots, debug, results, file_name)
def run_experiment(experiment_title, n_agents, capacity, show_plots, debug, results, file_name, export=True):
with open(file_name) as json_file:
geojson_file = geojson.load(json_file)
try:
geojson_file["crs"]
except KeyError:
print("Warning! No CRS is given and can cause odd behaviours!")
features = geojson_file["features"]
geometries = {
"obstacles": shapely.MultiPolygon(),
"tasks": shapely.MultiLineString(),
"boundary": shapely.Polygon(),
}
for feature in features:
if feature["geometry"]:
geometries[feature["id"]] = geometry.shape(feature["geometry"])
number_of_tasks = len(list(geometries["tasks"].geoms))
# Normalize the geoms
min_x, min_y, _, _ = geometries["boundary"].bounds
for key in geometries:
geometries[key] = translate(geometries[key], -min_x, -min_y)
print(file_name, " Tasks: ", number_of_tasks)
# Initialize coverage problem and the agents
geometries["boundary"] = geometries["boundary"].buffer(1)
# Scale each polygon in the MultiPolygon
scaled_polygons = []
for polygon in geometries["obstacles"].geoms:
scaled_polygon = polygon.buffer(-1) # scale(polygon, xfact=0.95, yfact=0.95, origin="centroid")
scaled_polygons.append(scaled_polygon)
# Create a new MultiPolygon with scaled polygons
scaled_multi_polygon = shapely.geometry.MultiPolygon(scaled_polygons)
task_list = []
for id, task in enumerate(geometries["tasks"].geoms):
task_list.append(Task.TrajectoryTask(id, task))
cp = CoverageProblem.CoverageProblem(
restricted_areas=scaled_multi_polygon,
search_area=geometries["boundary"],
tasks=task_list,
)
initial = cp.generate_random_point_in_problem().coords.xy
agent_list = [
Agent.config(id, (initial[0][0] + random.uniform(-10, 10), initial[1][0] + random.uniform(-10, 10)), capacity, max_velocity=10)
for id in range(n_agents)
]
exp = Experiment.Runner(coverage_problem=cp, enable_plotting=show_plots, agents=agent_list)
exp.solve(profiling_enabled=False, debug=debug)
# Save the results in a csv file
(
totalRouteLength,
sumOfTaskLengths,
totalRouteCosts,
iterations,
computeTime,
route_list,
maxRouteCost,
) = exp.evaluateSolution()
results.append(
[
file_name,
totalRouteLength,
sumOfTaskLengths,
totalRouteCosts,
maxRouteCost,
iterations,
computeTime,
cp.getNumberOfTasks(),
len(agent_list),
]
)
# Save the results to the csv
saveResults(experiment_title, results)
if __name__ == "__main__":
seed = 123
np.random.seed(seed)
random.seed(seed)
parser = argparse.ArgumentParser(description="Calculates a conflict free task allocation")
parser.add_argument("--dataset", type=str, help="The name of the dataset")
parser.add_argument("--experiment_name", type=str, help="The name of the experiment")
parser.add_argument("--n_robots", type=int, help="The number of robots to include")
parser.add_argument("--capacity", type=int, help="The capacity of the robots given in minutes")
parser.add_argument("--point_estimation", default=False, type=bool, help="Bool for wether to use point estimation")
parser.add_argument("--show_plots", default=False, type=bool, help="whether to show plots")
args = parser.parse_args()
if len(sys.argv) > 1:
main(
dataset_name=args.dataset,
experiment_title=args.experiment_name,
n_agents=args.n_robots,
capacity=args.capacity,
show_plots=args.show_plots,
)
else:
ds = "AC300"
n_agents = 3
capacity = 5000
main(
dataset_name=ds,
experiment_title=ds + "_" + str(n_agents) + "agents_" + str(capacity) + "capacity",
n_agents=n_agents,
capacity=capacity,
show_plots=True,
debug=False,
)