File size: 3,888 Bytes
e067c2d
47bba68
e067c2d
 
 
 
 
 
 
47bba68
 
 
 
 
e067c2d
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
47bba68
e067c2d
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
47bba68
e067c2d
 
47bba68
e067c2d
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
47bba68
e067c2d
 
 
 
 
 
 
47bba68
e067c2d
 
 
 
 
47bba68
e067c2d
 
 
 
 
47bba68
e067c2d
 
 
 
 
 
47bba68
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Pydantic models for API requests and responses."""

from pydantic import BaseModel, Field
from typing import Optional, List, Tuple
from enum import Enum


class Algorithm(str, Enum):
    """Available search algorithms."""

    BF = "BF"  # Breadth-first search
    DF = "DF"  # Depth-first search
    ID = "ID"  # Iterative deepening
    UC = "UC"  # Uniform cost search
    GR1 = "GR1"  # Greedy with Manhattan heuristic
    GR2 = "GR2"  # Greedy with Euclidean heuristic
    AS1 = "AS1"  # A* with Manhattan heuristic
    AS2 = "AS2"  # A* with Tunnel-aware heuristic


class Position(BaseModel):
    """A position on the grid."""

    x: int
    y: int

    def to_tuple(self) -> Tuple[int, int]:
        return (self.x, self.y)


class SegmentData(BaseModel):
    """Segment data for API."""

    src: Position
    dst: Position
    traffic: int = Field(ge=0, le=4)


class StoreData(BaseModel):
    """Store data for API."""

    id: int
    position: Position


class DestinationData(BaseModel):
    """Destination data for API."""

    id: int
    position: Position


class TunnelData(BaseModel):
    """Tunnel data for API."""

    entrance1: Position
    entrance2: Position
    cost: Optional[int] = None


# Request Models


class GridConfig(BaseModel):
    """Configuration for grid generation."""

    width: Optional[int] = Field(None, ge=5, le=50)
    height: Optional[int] = Field(None, ge=5, le=50)
    num_stores: Optional[int] = Field(None, ge=1, le=3)
    num_destinations: Optional[int] = Field(None, ge=1, le=10)
    num_tunnels: Optional[int] = Field(None, ge=0, le=10)
    obstacle_density: float = Field(0.1, ge=0.0, le=0.5)


class SearchRequest(BaseModel):
    """Request for running a search/plan."""

    initial_state: str
    traffic: str
    strategy: Algorithm
    visualize: bool = False


class PathRequest(BaseModel):
    """Request for finding a single path."""

    grid_width: int
    grid_height: int
    start: Position
    goal: Position
    segments: List[SegmentData]
    tunnels: List[TunnelData] = []
    strategy: Algorithm


class CompareRequest(BaseModel):
    """Request for comparing all algorithms."""

    initial_state: str
    traffic: str


# Response Models


class PathData(BaseModel):
    """Path result data."""

    plan: str
    cost: float
    nodes_expanded: int
    path: List[Position]


class GridData(BaseModel):
    """Complete grid state data."""

    width: int
    height: int
    stores: List[StoreData]
    destinations: List[DestinationData]
    tunnels: List[TunnelData]
    segments: List[SegmentData]


class GenerateResponse(BaseModel):
    """Response from grid generation."""

    initial_state: str
    traffic: str
    parsed: GridData


class SearchResponse(BaseModel):
    """Response from search/plan execution."""

    plan: str
    cost: float
    nodes_expanded: int
    runtime_ms: float
    memory_kb: float
    cpu_percent: float
    path: List[Position]
    steps: Optional[List[dict]] = None


class PlanResponse(BaseModel):
    """Response from delivery planning."""

    output: str
    assignments: List[dict]
    total_cost: float
    total_nodes_expanded: int
    runtime_ms: float
    memory_kb: float
    cpu_percent: float


class ComparisonResult(BaseModel):
    """Result of comparing a single algorithm."""

    algorithm: str
    name: str
    plan: str
    cost: float
    nodes_expanded: int
    runtime_ms: float
    memory_kb: float
    cpu_percent: float
    is_optimal: bool = False


class CompareResponse(BaseModel):
    """Response from algorithm comparison."""

    comparisons: List[ComparisonResult]
    optimal_cost: float


class AlgorithmInfo(BaseModel):
    """Information about an algorithm."""

    code: str
    name: str
    description: str


class AlgorithmsResponse(BaseModel):
    """List of available algorithms."""

    algorithms: List[AlgorithmInfo]