Here's a complete implementation of a Python Flask server that takes 10 coordinates from
the user (via a POST request), runs a route optimization algorithm (using a simple brute-
force TSP method for this small size), and returns the optimized route and total distance.
✅ Features:
• Accepts 10 coordinate pairs via JSON.
• Calculates optimized path (Traveling Salesman Problem).
• Returns the optimal order and total distance.
• Uses Euclidean distance.
🔧 app.py — Flask Server with TSP Optimization
from flask import Flask, request, jsonify
from itertools import permutations
import math
app = Flask(__name__)
def euclidean_distance(coord1, coord2):
return math.sqrt((coord1[0] - coord2[0])**2 +
(coord1[1] - coord2[1])**2)
def total_route_distance(route):
distance = 0
for i in range(len(route)):
distance += euclidean_distance(route[i], route[(i
+ 1) % len(route)])
return distance
def find_best_route(coords):
best_order = []
min_distance = float('inf')
for perm in permutations(coords):
current_distance = total_route_distance(perm)
if current_distance < min_distance:
min_distance = current_distance
best_order = perm
return list(best_order), min_distance
@app.route('/optimize-route', methods=['POST'])
def optimize_route():
data = request.get_json()
coords = data.get("coordinates", [])
if not isinstance(coords, list) or len(coords) != 10:
return jsonify({"error": "Please provide exactly
10 coordinates as a list of [x, y] pairs."}), 400
try:
optimized_route, total_distance =
find_best_route(coords)
return jsonify({
"optimized_route": optimized_route,
"total_distance": round(total_distance, 2)
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)
🧪 Sample curl request to test:
curl -X POST http://127.0.0.1:5000/optimize-route \
-H "Content-Type: application/json" \
-d '{"coordinates": [[0,0],[1,2],[3,1],[4,0],[2,2],
[5,5],[1,1],[6,2],[7,1],[3,3]]}'
📝 Notes:
• This uses brute-force, which is ne for ≤10 points, but for more, consider using
heuristic algorithms (e.g. Ant Colony, Genetic Algorithm, or Google OR-Tools).
• You can add visualization or map-based UI later if needed.
Would you like to see this extended to use a Genetic Algorithm, integrate Google Maps API,
or add a frontend UI to submit the coordinates?
fi