forked from vinhnguyen15/mutual_aid_tsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
65 lines (58 loc) · 2.24 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
from flask import Flask, request, jsonify
import json
import traceback
from models import solve_tsp
from datetime import datetime
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# @app.route('/log_test')
# def log_test():
# app.logger.info('Testing logger info')
# app.logger.error('Testing logger error')
# return 'Test message written to logger'
@app.route('/ping', methods=['GET'])
def ping():
headers = {'Content-type': 'application/json; charset=utf-8'}
response = {
'message': 'ok',
'time': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
}
return jsonify(response), 200, headers
@app.route('/shortest-route', methods=['GET'])
def get_shortest_route():
# parse input data
s_request = request.get_data()
data = json.loads(s_request)
fields = ['addresses', 'pickups', 'pickup_dropoff_constraints']
if not all(key in data for key in fields):
return 'Data JSON must have the following fields: \n {}'.format(', '.join(fields)), 400
try:
# solve the tsp problem
opt_obj, opt_sol, opt_loc, opt_addr = solve_tsp(data)
response = {'geolocations': opt_loc, 'optimal_order': opt_sol, 'optimal_objective': opt_obj}
return jsonify(response), 200
except Exception as e:
tb = traceback.format_exc()
return 'Error solving the TSP model: \n {}\n {}'.format(e, tb), 500
@app.route('/shortest-route-given-geocodes', methods=['POST'])
def get_shortest_route_given_geocodes():
# parse input data
s_request = request.get_data()
data = json.loads(s_request)
fields = ['addresses', 'pickup_dropoff_constraints']
if not all(key in data for key in fields):
return 'Data JSON must have the following fields: \n {}'.format(', '.join(fields)), 400
try:
# solve the tsp problem
opt_obj, opt_sol, opt_loc, opt_addr = solve_tsp(data, given_geocodes=True)
response ={
'optimal_order': opt_sol,
'optimal_objective': opt_obj,
'optimal_addresses': opt_addr}
return jsonify(response), 200
except Exception as e:
tb = traceback.format_exc()
return 'Error solving the TSP model: \n {}\n {}'.format(e, tb), 500
if __name__ == '__main__':
app.run(debug=True)