File size: 10,263 Bytes
e40294e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from fastapi import FastAPI, HTTPException
from fastapi.staticfiles import StaticFiles
from uuid import uuid4
from typing import Dict, List, Any
from dataclasses import dataclass, asdict
from threading import Lock, Event
import re
import copy

from pydantic import BaseModel, Field as PydanticField
from random import Random

from .domain import OrderPickingSolution, OrderPickingSolutionModel
from .converters import solution_to_model, model_to_solution
from .demo_data import (
    generate_demo_data, build_products, build_trolleys, build_orders,
    build_trolley_steps, validate_bucket_capacity, START_LOCATION, BUCKET_CAPACITY
)
from .solver import solver_manager, solution_manager
from .warehouse import calculate_distance_to_travel


app = FastAPI(docs_url='/q/swagger-ui')

# =============================================================================
# Thread-safe solution caching
# =============================================================================
# The solver runs in a Java thread (via JPype) and calls Python callbacks.
# We snapshot solution data IMMEDIATELY in the callback (while solver paused)
# and store the immutable snapshot. API handlers read from this cache.

# Thread-safe cache: stores SNAPSHOTS as dicts (immutable after creation)
cached_solutions: Dict[str, Dict[str, Any]] = {}
cached_distances: Dict[str, Dict[str, int]] = {}
cache_lock = Lock()

# Events to signal when first solution is ready
first_solution_events: Dict[str, Event] = {}

# Domain objects for score analysis
data_sets: Dict[str, OrderPickingSolution] = {}


@dataclass
class MatchAnalysisDTO:
    name: str
    score: str
    justification: str


@dataclass
class ConstraintAnalysisDTO:
    name: str
    weight: str
    score: str
    matches: List[MatchAnalysisDTO]


class DemoConfigModel(BaseModel):
    """Configuration for generating custom demo data."""
    orders_count: int = PydanticField(default=40, ge=5, le=100, alias="ordersCount")
    trolleys_count: int = PydanticField(default=8, ge=2, le=15, alias="trolleysCount")
    bucket_count: int = PydanticField(default=6, ge=2, le=10, alias="bucketCount")

    model_config = {"populate_by_name": True}


@app.get("/demo-data")
async def get_demo_data_list() -> List[str]:
    """Get available demo data sets."""
    return ["DEFAULT"]


@app.get("/demo-data/{demo_name}", response_model=OrderPickingSolutionModel)
async def get_demo_data_by_name(demo_name: str) -> OrderPickingSolutionModel:
    """Get a specific demo data set."""
    if demo_name != "DEFAULT":
        raise HTTPException(status_code=404, detail=f"Demo data '{demo_name}' not found")
    domain_solution = generate_demo_data()
    return solution_to_model(domain_solution)


@app.post("/demo-data/generate", response_model=OrderPickingSolutionModel)
async def generate_custom_demo(config: DemoConfigModel) -> OrderPickingSolutionModel:
    """Generate demo data with custom configuration."""
    random = Random(37)  # Fixed seed for reproducibility

    validate_bucket_capacity(BUCKET_CAPACITY)

    products = build_products(random)
    trolleys = build_trolleys(
        config.trolleys_count,
        config.bucket_count,
        BUCKET_CAPACITY,
        START_LOCATION
    )
    orders = build_orders(config.orders_count, products, random)
    trolley_steps = build_trolley_steps(orders)

    # Pre-assign steps evenly across trolleys so we have paths to visualize immediately
    if trolleys:
        for i, step in enumerate(trolley_steps):
            trolley = trolleys[i % len(trolleys)]
            trolley.steps.append(step)
            step.trolley = trolley

    domain_solution = OrderPickingSolution(
        trolleys=trolleys,
        trolley_steps=trolley_steps
    )
    return solution_to_model(domain_solution)


def update_solution(job_id: str, solution: OrderPickingSolution):
    """
    Update solution cache. Called by solver callback from Java thread.

    CRITICAL: We snapshot ALL data IMMEDIATELY in the callback while the solver
    is paused. This prevents race conditions where the Java solver modifies
    domain objects while we're reading them.
    """
    # Snapshot step assignments for each trolley FIRST (before any iteration)
    trolley_snapshots = []
    for t in solution.trolleys:
        # Copy the steps list immediately - this is the critical snapshot
        step_ids = [s.id for s in t.steps]
        trolley_snapshots.append((t.id, len(step_ids), step_ids))

    # Log for debugging
    step_counts = [f"T{tid}:{count}" for tid, count, _ in trolley_snapshots]
    print(f"[CALLBACK] job={job_id} score={solution.score} steps=[{' '.join(step_counts)}]")

    # Now convert to API model (uses the same solution state we just logged)
    api_model = solution_to_model(solution)
    solution_dict = api_model.model_dump(by_alias=True)

    # Calculate distances
    distances = {}
    for trolley in solution.trolleys:
        distances[trolley.id] = calculate_distance_to_travel(trolley)

    # Update cache atomically
    with cache_lock:
        cached_solutions[job_id] = solution_dict
        cached_distances[job_id] = distances

    # Signal that first solution is ready
    if job_id in first_solution_events:
        first_solution_events[job_id].set()

    # Keep domain object reference for score analysis
    data_sets[job_id] = solution


@app.post("/schedules")
async def solve(solution_model: OrderPickingSolutionModel) -> str:
    """Submit a problem to solve. Returns job ID."""
    job_id = str(uuid4())
    domain_solution = model_to_solution(solution_model)

    data_sets[job_id] = domain_solution

    # Initialize cache with empty state - will be updated by callbacks
    with cache_lock:
        cached_solutions[job_id] = solution_to_model(domain_solution).model_dump(by_alias=True)
        cached_distances[job_id] = {}

    # Start solver - callbacks update cache when construction completes and on improvements
    (solver_manager.solve_builder()
        .with_problem_id(job_id)
        .with_problem(domain_solution)
        .with_first_initialized_solution_consumer(lambda solution: update_solution(job_id, solution))
        .with_best_solution_consumer(lambda solution: update_solution(job_id, solution))
        .run())

    return job_id


@app.get("/schedules/{problem_id}")
async def get_solution(problem_id: str) -> Dict[str, Any]:
    """Get the current solution for a given job ID."""
    solver_status = solver_manager.get_solver_status(problem_id)

    # Read from thread-safe cache (populated by solver callbacks)
    with cache_lock:
        cached = cached_solutions.get(problem_id)

    if not cached:
        raise HTTPException(status_code=404, detail="Solution not found")

    # Return cached solution with current status
    result = dict(cached)
    result["solverStatus"] = solver_status.name if solver_status else None

    return result


@app.get("/schedules/{problem_id}/status")
async def get_status(problem_id: str) -> dict:
    """Get the solution status, score, and distances for a given job ID."""
    # Read from thread-safe cache
    with cache_lock:
        cached = cached_solutions.get(problem_id)
        distances = cached_distances.get(problem_id, {})

    if not cached:
        raise HTTPException(status_code=404, detail="Solution not found")

    solver_status = solver_manager.get_solver_status(problem_id)

    # Parse score from cached solution
    score_str = cached.get("score", "")
    hard_score = 0
    soft_score = 0
    if score_str:
        # Parse score like "0hard/-12345soft"
        match = re.match(r"(-?\d+)hard/(-?\d+)soft", str(score_str))
        if match:
            hard_score = int(match.group(1))
            soft_score = int(match.group(2))

    return {
        "score": {
            "hardScore": hard_score,
            "softScore": soft_score,
        },
        "solverStatus": solver_status.name if solver_status else None,
        "distances": distances,
    }


@app.delete("/schedules/{problem_id}")
async def stop_solving(problem_id: str) -> Dict[str, Any]:
    """Terminate solving for a given job ID. Returns the best solution so far."""
    solver_manager.terminate_early(problem_id)

    # Read from thread-safe cache
    with cache_lock:
        cached = cached_solutions.get(problem_id)
        if not cached:
            raise HTTPException(status_code=404, detail="Solution not found")
        result = dict(cached)

    solver_status = solver_manager.get_solver_status(problem_id)
    result["solverStatus"] = solver_status.name if solver_status else None

    return result


@app.get("/schedules/{problem_id}/score-analysis")
async def analyze_score(problem_id: str) -> dict:
    """Get score analysis for current solution."""
    import asyncio
    from concurrent.futures import ThreadPoolExecutor

    solution = data_sets.get(problem_id)
    if not solution:
        raise HTTPException(status_code=404, detail="Solution not found")

    # Run blocking JPype call in thread pool to not block async event loop
    loop = asyncio.get_event_loop()
    with ThreadPoolExecutor() as pool:
        analysis = await loop.run_in_executor(pool, solution_manager.analyze, solution)
    constraints = []
    for constraint in getattr(analysis, 'constraint_analyses', []) or []:
        matches = [
            MatchAnalysisDTO(
                name=str(getattr(getattr(match, 'constraint_ref', None), 'constraint_name', "")),
                score=str(getattr(match, 'score', "0hard/0soft")),
                justification=str(getattr(match, 'justification', ""))
            )
            for match in getattr(constraint, 'matches', []) or []
        ]
        constraints.append(ConstraintAnalysisDTO(
            name=str(getattr(constraint, 'constraint_name', "")),
            weight=str(getattr(constraint, 'weight', "0hard/0soft")),
            score=str(getattr(constraint, 'score', "0hard/0soft")),
            matches=matches
        ))
    return {"constraints": [asdict(constraint) for constraint in constraints]}


@app.get("/schedules")
async def list_solutions() -> List[str]:
    """List the job IDs of all submitted solutions."""
    return list(data_sets.keys())


app.mount("/", StaticFiles(directory="static", html=True), name="static")