An empirical investigation into distilling Google OR-Tools CP-SAT modeling capabilities into Qwen2.5-Coder-3B-Instruct to generate and self-repair optimization scripts on consumer hardware (AMD Radeon RX 7900 XT, 20 GB VRAM). Comparing a 100% local teacher pipeline (Qwen2.5-Coder-14B) against a frontier cloud teacher (OpenAI) across In-Distribution (Gold 50) and Out-of-Distribution (OOD 50) benchmarks.
Evaluated out-of-the-box in Zero-Shot, Qwen2.5-Coder-3B scores 0% across our 50 reference problems: it hallucinates non-existent OR-Tools methods (such as cp_model.StatusName) or violates strict integer typing in CP-SAT. Building a reliable, specialized 3B solver model required combining three methodological pillars.
Every synthetic script generated by the teachers (OpenAI and Qwen-14B) is executed in an isolated Python sandbox against the ortools.sat.python.cp_model solver. Only scripts returning a verified OPTIMAL integer objective within 10 seconds are retained for distillation.
When an LLM writes a problem statement first and code second, it frequently hardcodes undocumented penalties or bounds in the script ("phantom constraints"). We invert the pipeline: the teacher inspects the verified CP-SAT code and writes an exhaustive specification capturing every constant, index bound, and objective term.
Each 330-sample training split combines 275 direct trajectories with 55 multi-turn debugging trajectories (faulty CP-SAT code + Python traceback + repaired script) trained across a full 5,120-token context window, enabling autonomous recovery at inference time (--retries 1).
We evaluate all models across two strictly balanced 50-problem test suites (25 OpenAI-authored + 25 Qwen-14B-authored instances per suite):
test_gold_50.jsonl — 4 Trained Logistics & Maritime Families):
Hub & Facility Allocation, Multi-Dimensional Bin Packing & Loading, Empty Container Repositioning, and Port & Warehouse Scheduling.
test_ood_50.jsonl — 4 Unseen Mathematical Archetypes):
Graph Coloring & Frequency Allocation, 1D Cutting Stock Roll Trimming, Precedence-Constrained Project Scheduling (RCPSP), and Budgeted Sensor Network Coverage.
Syntactic & Solver Feasibility: The generated Python script executes without runtime exceptions within a 10s timeout, invokes cp_model properly, reaches a proven OPTIMAL status (not INFEASIBLE or MODEL_INVALID), and prints a valid JSON payload.
Mathematical & Semantic Equivalence: Not only is the script valid and OPTIMAL, but its numerical objective value matches the ground-truth optimal integer (objective == ground_truth), proving every constraint and cost coefficient was modeled accurately.
Automated Self-Correction (--retries 1): Number of problems out of 50 that failed on Pass 1 (syntax error, CP-SAT API error, or non-optimal status) and were autonomously fixed by the model on Pass 2 after receiving the exact Python traceback.
| Model / Variant | Parameters | Gold 50 • Valid | Gold 50 • Exact | Gold 50 • Repaired | OOD 50 • Valid | OOD 50 • Exact | OOD 50 • Repaired |
|---|---|---|---|---|---|---|---|
| Base-3B (Zero-Shot) | 3.1B | 0.0% (0/50) | 0.0% (0/50) | 0 | 0.0% (0/50) | 0.0% (0/50) | 0 |
| Teacher Local Qwen-14B (Zero-Shot) | 14.7B | 46.0% (23/50) | 40.0% (20/50) | 10 | 48.0% (24/50) | 42.0% (21/50) | 12 |
| Student-Cloud-3B (LoRA OpenAI • 5120) | 3.1B | 64.0% (32/50) | 56.0% (28/50) | 6 | 14.0% (7/50) | 14.0% (7/50) | 1 |
| Student-Local-3B (LoRA Qwen-14B • 5120) | 3.1B | 86.0% (43/50) | 72.0% (36/50) | 7 | 6.0% (3/50) | 4.0% (2/50) | 0 |
| Teacher OpenAI (Frontier • Ceiling) | Frontier | 100.0% (50/50) | 88.0% (44/50) | 0 | 98.0% (49/50) | 86.0% (43/50) | 0 |
In Zero-Shot on Gold 50, Qwen2.5-Coder-14B suffers from frequent API hallucinations (46% valid code). Because our pipeline uses CP-SAT execution as an oracle filter, only verified optimal trajectories were distilled into the 3B model. The student absorbs the 14B's mathematical formulation while eliminating its syntactic instability.
While Student-Local-3B dominates on trained logistics families (86% valid / 72% exact), it drops to 6% valid / 4% exact on unseen OOD archetypes. Intra-family LoRA SFT acts as a rigid domain compiler, whereas Student-Cloud-3B—trained on OpenAI's broader structural diversity—retains 3.5x higher exact accuracy on OOD (14% vs. 4%) with 100% semantic precision (7/7).
On unseen OOD domains, the Frontier Teacher achieves virtually identical exact accuracy to Gold (86% vs. 88%), and the 14B Teacher reaches 42% exact accuracy (leveraging 12 self-repairs). Synthesizing novel mathematical constraint topologies from scratch remains fundamentally bound by pre-trained parameter scale (14B+) rather than 3B adapter fine-tuning.
Inspect how multi-turn self-correction repairs CP-SAT API violations at inference time, and how Code-to-Problem Back-Translation eliminates unstated phantom constraints.
# Pass 1 bug: Passing a linear sum directly inside OnlyEnforceIf
for w in range(num_warehouses):
model.Add(
sum(flow[w, c] for c in range(num_clients)) <= cap[w]
).OnlyEnforceIf(open_var[w])
# --- CP-SAT Runtime Traceback Fed Back to Student-3B ---
AttributeError: 'LinearExpr' object has no attribute 'Index'
in model.Add(...).OnlyEnforceIf(open_var[w])
(Note: OnlyEnforceIf requires a clean literal or properly
constructed linear constraint, or linearize via big-M:
sum(flow[w, c]) <= cap[w] * open_var[w])
# Pass 2 fix: Student-3B replaces the faulty indicator with a
# direct big-M capacity coupling constraint and valid StatusName call
for w in range(num_warehouses):
model.Add(
sum(flow[w, c] for c in range(num_clients))
<= cap[w] * open_var[w]
)
solver = cp_model.CpSolver()
solver.parameters.max_time_in_seconds = 10.0
status = solver.Solve(model)
print(json.dumps({
"status": solver.StatusName(status),
"objective": round(solver.ObjectiveValue()),
"variables": {"open": [solver.Value(v) for v in open_var]}
}))
All fine-tuning runs were executed locally on Arch Linux using an AMD Radeon RX 7900 XT (20 GB VRAM) in native float16 precision (r=16, lora_alpha=32, targeting all linear projection layers: 29.93M trainable parameters or 0.96% of total weights, 3 epochs, 5,120-token max sequence length).
Student-Cloud-3B requires 15.14 GB VRAM and 23m 52s (final loss 0.0789) compared to 11.94 GB VRAM and 17m 54s (final loss 0.0687) for Student-Local-3B. OpenAI generates longer, more structurally diverse scripts, whereas Qwen-14B produces compact code naturally aligned with the Qwen-3B student's token distribution.
To answer our core research question: Yes, but strictly within the boundaries of trained in-distribution problem families. While our study focused on lightweight 3B models due to consumer hardware constraints, these findings establish a broader architectural blueprint: large enterprises equipped with dedicated AI compute clusters can fine-tune custom, larger local models to achieve elite domain-specific performance while preserving absolute data sovereignty.
By filtering solver outputs through execution verification and back-translation, specialized local students reach 86% valid code and 72% exact objectives on target logistics domains—outperforming their 14B teacher by +32 points while eliminating API hallucinations.
While our 3B PoC ran locally on a single consumer GPU (AMD RX 7900 XT), enterprises with dedicated on-premise clusters can scale this methodology to much larger proprietary models (e.g., 32B or 70B+). This unlocks near-frontier reasoning capabilities while ensuring that sensitive operational data and proprietary cost matrices never leave corporate infrastructure.
Local fine-tuning acts as a powerful, secure domain compiler for recurring workflows. However, novel zero-shot OOD synthesis remains constrained by parameter scale. Production systems should leverage custom local models for core business operations and reserve cloud escalation for unprecedented edge cases.