-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
145 lines (126 loc) · 4.01 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
from flask import Flask, jsonify, request, render_template
from auction.test_auction import run_auction
from flightcosts.cost_estimator import estimate_costs
from flightcosts.estimation_parameters import EstimationParameters
from flightpath.obstacle import Obstacle
from flightpath.path_planner import create_flight_path, create_flight_paths
from shared.geo.coordinate import Coordinate
from tsp.optimizer import Optimizer
from tsp.parameters import LocalSearchMetaheuristic, FirstSolutionStrategy
from flask_cors import CORS
app = Flask(__name__, template_folder='templates', static_folder='static')
CORS(app)
@app.route("/flight_path", methods=['GET'])
def get_flight_path():
"""
Input:
{
"start": {
"latitude": 1.23,
"longitude": 1.23,
},
"end": {
"latitude": 1.23,
"longitude": 1.23,
},
"obstacles": [
{
"coordinates": {
"latitude": 1.23,
"longitude": 1.23,
}
},
{
"coordinates": {
"latitude": 1.23,
"longitude": 1.23,
}
},
]
}
"""
# Parse input
json_data = request.get_json()
start = Coordinate.from_json(json_data['start'])
end = Coordinate.from_json(json_data['end'])
obstacles = [Obstacle.from_json(obstacle) for obstacle in json_data['obstacles']]
# Create flight path
flight_path = create_flight_path(start, end, obstacles)
return jsonify(flight_path.to_json())
@app.route("/flight_costs", methods=['GET'])
def get_flight_costs():
"""
Input:
{
"coordinates": [
{
"latitude": 1.23,
"longitude": 1.23,
},
{
"latitude": 1.23,
"longitude": 1.23,
}
],
"obstacles": [
{
"coordinates": {
"latitude": 1.23,
"longitude": 1.23,
}
},
{
"coordinates": {
"latitude": 1.23,
"longitude": 1.23,
}
},
]
}
"""
# Parse input
json_data = request.get_json()
obstacles = [Obstacle.from_json(obstacle) for obstacle in json_data['obstacles']]
coordinates = [Coordinate.from_json(coordinate) for coordinate in json_data['coordinates']]
estimation_parameters = EstimationParameters.from_json(json_data['estimation_parameters'])
# Create flight paths and estimate their costs
flight_paths = create_flight_paths(coordinates, obstacles)
costs = estimate_costs(flight_paths, estimation_parameters)
return jsonify(costs)
@app.route("/solve_tsp", methods=['GET'])
def solve_tsp():
# The first point is the depot
"""
Input:
{
"costs": [
[1, 2],
[3, 4]
],
"parameters" : {
"local_search_metaheuristic": "GUIDED_LOCAL_SEARCH",
"first_solution_strategy": "AUTOMATIC",
"solution_limit": 100,
}
}
"""
# Parse input
json_data = request.get_json()
local_search_metaheuristic = LocalSearchMetaheuristic[json_data['parameters']['local_search_metaheuristic']]
first_solution_strategy = FirstSolutionStrategy[json_data['parameters']['first_solution_strategy']]
# Create optimizer
optimizer = Optimizer(json_data['costs'], local_search_metaheuristic.value, first_solution_strategy.value,
json_data['parameters']['solution_limit'], False)
# Solve TSP
optimal_order = optimizer.optimize()
return jsonify(optimal_order)
@app.route("/", methods=['GET'])
def get_index():
return render_template("index.html")
@app.route("/get_simulation_state", methods=['POST'])
def get_simulation_state():
data = request.get_json()
print(data)
result=run_auction(data)
print(result)
return jsonify(result)