This is the Flask backend for the Location Optimization Tool, a geospatial decision support system. It provides RESTful APIs to solve:
- The P-Median problem for facility location optimization
- The Traveling Salesman Problem (TSP) using a Genetic Algorithm (GA)
The backend is designed to work with the React frontend deployed here and supports both synchronous and asynchronous operations.
- Flask for API framework
- PuLP for solving the P-Median Linear Program
- NumPy, Pandas for computation
- Gunicorn for production serving
- Flask-CORS for cross-origin communication
- dotenv for environment configuration
- Backend URL: https://operations-research-project.onrender.com
Solves the P-Median problem for selecting p facility locations given:
- A list of coordinates
- Fixed facility costs
- Value of
p
{
"coordinates": [[lat1, lon1], [lat2, lon2], ...],
"fixed_costs": [100, 200, 150, ...],
"p_val": 3
}{
"selected_facilities": [false, true, false, true],
"total_cost": 374.52,
"status": "Optimal"
}- Computes all pairwise distances using the Haversine formula
- Solves a Mixed-Integer Linear Program using PuLP
- Assumes all points can be both clients and candidate facilities
- Returns
[]andInfinityif LP is infeasible or not optimal
Starts an asynchronous Genetic Algorithm to solve a TSP over the provided coordinates.
{
"coordinates": [[lat1, lon1], [lat2, lon2], ...],
"generations": 1000,
"pop_size": 200,
"elite_size": 10,
"mutation_rate": 0.01
}{
"job_id": "abcd-1234"
}{
"status": "completed",
"result": {
"route_indices": [1, 4, 3, 2, 1],
"route": [[...], [...], ...],
"total_distance": 123.45,
"computation_time": 2.34,
"status": "success",
"log": [
{"generation": 1, "best_distance": 700.3},
...
]
}
}- Uses real-world distance via the Haversine formula
- Implements selection, crossover (OX/CX), and mutation (swap/inversion)
- Logs progress over generations
- No persistent storage: results vanish after app restarts
- Route indices assume original coordinate order
- Stochastic: may produce different solutions across runs
git clone https://github.com/your-username/location-optimizer-backend.git
cd location-optimizer-backend
pip install -r requirements.txtCreate a .env file:
FLASK_ENV=development
ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000python -m src.api.routesOr for production:
gunicorn src.api.routes:app├── src/
│ ├── api/
│ │ └── routes.py # Main Flask routes & job manager
│ └── core/
│ ├── lp_solver.py # P-Median optimizer using PuLP
│ └── ga_solver.py # TSP optimizer using Genetic Algorithm
├── requirements.txt
└── .env
flask>=2.0.0
flask-cors>=3.0.10
pulp>=2.0.0
numpy>=1.19.0
pandas>=1.0.0
gunicorn==21.2.0
python-dotenv