Skip to content

Optimization Example

Using VRPTW with Distance Objective

Test file for VRPTW (Vehicle Routing Problem with Time Windows) with Distance Objective.

This test file creates valid payloads according to the validate_payload function requirements:

  • All jobs must have the same start location
  • Jobs must have unique end locations
  • Job start locations should not intersect with end locations
  • All vehicles must have the same start location
  • Vehicles must have the same start location as jobs
  • Vehicles must return to start location (start == end for vehicles)
  • All vehicles must have the same profile
  • All vehicles must have the same capacity
  • All vehicles must have the same time window
  • Jobs must have time windows for both start and end locations
import requests


# Configuration 
API_BASE_URL = "https://api.flio.ai"
OPTIMIZE_ENDPOINT = "/solver/optimize"
API_KEY = "YOUR-API-KEY"

payload = {
  "vehicles": [
    {
      "id": "vehicle_1",
      "start": [41.0082, 28.9784],
      "end": [41.0082, 28.9784],
      "constraints": {
        "profile": "car",
        "capacity": 30,
        "time_window": ["08:00:00", "18:00:00"],
        "max_distance": 50000,
        "max_travel_time": 28800,
        "max_tasks": 3,
        "per_km": 1000,
        "per_hour": 3600,
        "fixed": 0,      
        }
    },
    {
      "id": "vehicle_2",
      "start": [41.0082, 28.9784],
      "end": [41.0082, 28.9784],
      "constraints": {
        "profile": "car",
        "capacity": 30,
        "time_window": ["08:00:00", "18:00:00"],
        "max_distance": 50000,
        "max_travel_time": 28800,
        "max_tasks": 3,
        "per_km": 1000,
        "per_hour": 3600,
        "fixed": 0,
      }
    }
  ],
  "jobs": [
    {
      "id": "job_1",
      "amount": 10,
      "start": {
        "id": "depot",
        "coordinate": [41.0082, 28.9784],
        "setup": 180,
        "service": 300,
        "time_window": ["08:00:00", "18:00:00"]
      },
      "end": {
        "id": "customer_1",
        "coordinate": [41.0122, 28.9820],
        "setup": 60,
        "service": 240,
        "time_window": ["09:00:00", "11:00:00"]
      },
      "priority": 50,
      "skills": []
    },
    {
      "id": "job_2",
      "amount": 10,
      "start": {
        "id": "depot",
        "coordinate": [41.0082, 28.9784],
        "setup": 180,
        "service": 300,
        "time_window": ["08:00:00", "18:00:00"]
      },
      "end": {
        "id": "customer_2",
        "coordinate": [41.0050, 28.9750],
        "setup": 75,
        "service": 220,
        "time_window": ["09:00:00", "11:00:00"]
      },
      "priority": 50,
      "skills": []
    },
    {
      "id": "job_3",
      "amount": 10,
      "start": {
        "id": "depot",
        "coordinate": [41.0082, 28.9784],
        "setup": 180,
        "service": 300,
        "time_window": ["08:00:00", "18:00:00"]
      },
      "end": {
        "id": "customer_3",
        "coordinate": [41.0150, 28.9850],
        "setup": 90,
        "service": 300,
        "time_window": ["14:00:00", "16:00:00"]
      },
      "priority": 50,
      "skills": []
    },
    {
      "id": "job_4",
      "amount": 10,
      "start": {
        "id": "depot",
        "coordinate": [41.0082, 28.9784],
        "setup": 180,
        "service": 300,
        "time_window": ["08:00:00", "18:00:00"]
      },
      "end": {
        "id": "customer_4",
        "coordinate": [41.0030, 28.9720],
        "setup": 120,
        "service": 360,
        "time_window": ["14:00:00", "16:00:00"]
      },
      "priority": 50,
      "skills": []
    },
    {
      "id": "job_5",
      "amount": 10,
      "start": {
        "id": "depot",
        "coordinate": [41.0082, 28.9784],
        "setup": 180,
        "service": 300,
        "time_window": ["08:00:00", "18:00:00"]
      },
      "end": {
        "id": "customer_5",
        "coordinate": [41.0180, 28.9880],
        "setup": 100,
        "service": 250,
        "time_window": ["12:00:00", "14:00:00"]
      },
      "priority": 50,
      "skills": []
    }
  ],
  "options": {
    "problem": "VRPTW",
    "objective": "distance",
    "solver": "vroom",
    "map": "cartesian",
    "timeout": 30000
  }
} 


url = f"{API_BASE_URL}{OPTIMIZE_ENDPOINT}?api_key={API_KEY}"
print(f"\nSending request to: {url}")
response = requests.post(url, json=payload)
print(f"Response Status: {response.status_code}")
print(f"Response Headers: {dict(response.headers)}")

if response.status_code == 200:
    response_data = response.json()
    print(f"Response: {response_data}")

    # Print summary
    if "summary" in response_data:
        summary = response_data["summary"]
        print(f"\n=== SUMMARY ===")
        print(f"Total Distance: {summary.get('distance', 'N/A')} meters")
        print(f"Total Duration: {summary.get('duration', 'N/A')} seconds")
        print(f"Total Cost: {summary.get('cost', 'N/A')}")
        print(f"Unassigned Jobs: {summary.get('unassigned', 'N/A')}")
else:
    print(f"Error: {response.status_code}")
    print(f"Response: {response.text}")