File size: 3,641 Bytes
e067c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
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
146
147
import axios from 'axios';
import type {
  GridState,
  GridConfig,
  Algorithm,
  SearchResult,
  PlanResult,
  ComparisonResult,
  AlgorithmInfo,
  Position,
  Segment,
  Tunnel,
} from '../types';

// Use environment variable or default to localhost for development
// In production (HF Spaces), use relative URLs since nginx proxies /api
const API_BASE = import.meta.env.VITE_API_URL || '';

const api = axios.create({
  baseURL: API_BASE,
  headers: {
    'Content-Type': 'application/json',
  },
});

// Health check
export async function checkHealth(): Promise<{ status: string }> {
  const response = await api.get('/api/health');
  return response.data;
}

// List algorithms
export async function getAlgorithms(): Promise<AlgorithmInfo[]> {
  const response = await api.get('/api/algorithms');
  return response.data.algorithms;
}

// Generate random grid
export async function generateGrid(config: GridConfig = {}): Promise<{
  initialState: string;
  traffic: string;
  parsed: GridState;
}> {
  const response = await api.post('/api/grid/generate', {
    width: config.width,
    height: config.height,
    num_stores: config.numStores,
    num_destinations: config.numDestinations,
    num_tunnels: config.numTunnels,
    obstacle_density: config.obstacleDensity ?? 0.1,
  });
  return {
    initialState: response.data.initial_state,
    traffic: response.data.traffic,
    parsed: response.data.parsed,
  };
}

// Find path
export async function findPath(
  gridWidth: number,
  gridHeight: number,
  start: Position,
  goal: Position,
  segments: Segment[],
  tunnels: Tunnel[],
  strategy: Algorithm
): Promise<SearchResult> {
  const response = await api.post('/api/search/path', {
    grid_width: gridWidth,
    grid_height: gridHeight,
    start,
    goal,
    segments: segments.map(s => ({
      src: s.src,
      dst: s.dst,
      traffic: s.traffic,
    })),
    tunnels: tunnels.map(t => ({
      entrance1: t.entrance1,
      entrance2: t.entrance2,
    })),
    strategy,
  });
  return {
    plan: response.data.plan,
    cost: response.data.cost,
    nodesExpanded: response.data.nodes_expanded,
    runtimeMs: response.data.runtime_ms,
    memoryKb: response.data.memory_kb,
    cpuPercent: response.data.cpu_percent,
    path: response.data.path,
    steps: response.data.steps,
  };
}

// Create plan
export async function createPlan(
  initialState: string,
  traffic: string,
  strategy: Algorithm,
  visualize: boolean = false
): Promise<PlanResult> {
  const response = await api.post('/api/search/plan', {
    initial_state: initialState,
    traffic,
    strategy,
    visualize,
  });
  return {
    output: response.data.output,
    assignments: response.data.assignments,
    totalCost: response.data.total_cost,
    totalNodesExpanded: response.data.total_nodes_expanded,
    runtimeMs: response.data.runtime_ms,
    memoryKb: response.data.memory_kb,
    cpuPercent: response.data.cpu_percent,
  };
}

// Compare algorithms
export async function compareAlgorithms(
  initialState: string,
  traffic: string
): Promise<{ comparisons: ComparisonResult[]; optimalCost: number }> {
  const response = await api.post('/api/search/compare', {
    initial_state: initialState,
    traffic,
  });
  return {
    comparisons: response.data.comparisons.map((c: any) => ({
      algorithm: c.algorithm,
      name: c.name,
      plan: c.plan,
      cost: c.cost,
      nodesExpanded: c.nodes_expanded,
      runtimeMs: c.runtime_ms,
      memoryKb: c.memory_kb,
      cpuPercent: c.cpu_percent,
      isOptimal: c.is_optimal,
    })),
    optimalCost: response.data.optimal_cost,
  };
}

export default api;