|
| 1 | +"""Train and evaluate the ARC solver in Kaggle/Colab environments. |
| 2 | +
|
| 3 | +This script provides a minimal end-to-end pipeline for training the neural |
| 4 | +guidance classifier and producing Kaggle-compatible submission files. When |
| 5 | +ground-truth solutions are provided, it also reports accuracy and per-task |
| 6 | +differences between predictions and targets. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import argparse |
| 12 | +import json |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | +from typing import Any, Dict, List, Optional, Tuple |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +# Ensure repository root is on the path so arc_solver can be imported when this |
| 20 | +# script runs in Kaggle/Colab notebooks. |
| 21 | +sys.path.append(str(Path(__file__).parent.parent)) |
| 22 | + |
| 23 | +from arc_solver.solver import ARCSolver |
| 24 | +from arc_solver.grid import to_array, eq |
| 25 | +from arc_solver.io_utils import save_submission |
| 26 | +from train_guidance import ( |
| 27 | + load_training_data, |
| 28 | + extract_training_features_and_labels, |
| 29 | + train_classifier, |
| 30 | + save_classifier, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +def train_guidance_model( |
| 35 | + train_json: str, |
| 36 | + solutions_json: Optional[str], |
| 37 | + model_path: str, |
| 38 | + epochs: int = 100, |
| 39 | +) -> str: |
| 40 | + """Train the neural guidance classifier. |
| 41 | +
|
| 42 | + Args: |
| 43 | + train_json: Path to the ARC training challenges JSON. |
| 44 | + solutions_json: Optional path to training solutions for supervised labels. |
| 45 | + model_path: Where to persist the trained classifier. |
| 46 | + epochs: Number of training epochs. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + Path to the saved model. |
| 50 | + """ |
| 51 | + tasks = load_training_data(train_json, solutions_json) |
| 52 | + features, labels = extract_training_features_and_labels(tasks) |
| 53 | + classifier = train_classifier(features, labels, epochs) |
| 54 | + Path(model_path).parent.mkdir(parents=True, exist_ok=True) |
| 55 | + save_classifier(classifier, model_path) |
| 56 | + return model_path |
| 57 | + |
| 58 | + |
| 59 | +def evaluate_solver( |
| 60 | + test_json: str, |
| 61 | + model_path: str, |
| 62 | + solutions_json: Optional[str], |
| 63 | + out_path: str, |
| 64 | +) -> Tuple[float, Dict[str, List[List[List[int]]]]]: |
| 65 | + """Run the solver on evaluation tasks and optionally score against solutions. |
| 66 | +
|
| 67 | + Args: |
| 68 | + test_json: Path to evaluation challenges JSON. |
| 69 | + model_path: Path to trained guidance model. |
| 70 | + solutions_json: Optional path to ground-truth solutions for scoring. |
| 71 | + out_path: Where to write the Kaggle submission JSON. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + Tuple of overall accuracy (0-1) and a mapping of task ids to diff grids. |
| 75 | + """ |
| 76 | + solver = ARCSolver(use_enhancements=True, guidance_model_path=model_path) |
| 77 | + |
| 78 | + with open(test_json, "r") as f: |
| 79 | + test_tasks: Dict[str, Any] = json.load(f) |
| 80 | + |
| 81 | + solutions: Dict[str, Any] = {} |
| 82 | + if solutions_json and Path(solutions_json).exists(): |
| 83 | + with open(solutions_json, "r") as f: |
| 84 | + solutions = json.load(f) |
| 85 | + |
| 86 | + predictions: Dict[str, Dict[str, List[List[List[int]]]]] = {} |
| 87 | + diffs: Dict[str, List[List[List[int]]]] = {} |
| 88 | + correct = 0 |
| 89 | + total = 0 |
| 90 | + |
| 91 | + for task_id, task in test_tasks.items(): |
| 92 | + result = solver.solve_task(task) |
| 93 | + predictions[task_id] = result |
| 94 | + |
| 95 | + if task_id in solutions: |
| 96 | + target_grids = [pair["output"] for pair in solutions[task_id]["test"]] |
| 97 | + pred_grids = result["attempt_1"] |
| 98 | + diff_grids: List[List[List[int]]] = [] |
| 99 | + all_match = True |
| 100 | + |
| 101 | + for pred, target in zip(pred_grids, target_grids): |
| 102 | + pa = to_array(pred) |
| 103 | + ta = to_array(target) |
| 104 | + all_match &= eq(pa, ta) |
| 105 | + diff_grids.append((pa != ta).astype(int).tolist()) |
| 106 | + |
| 107 | + if all_match: |
| 108 | + correct += 1 |
| 109 | + diffs[task_id] = diff_grids |
| 110 | + total += 1 |
| 111 | + |
| 112 | + save_submission(predictions, out_path) |
| 113 | + accuracy = correct / total if total else 0.0 |
| 114 | + return accuracy, diffs |
| 115 | + |
| 116 | + |
| 117 | +def main() -> None: |
| 118 | + parser = argparse.ArgumentParser(description="Train and evaluate ARC solver") |
| 119 | + parser.add_argument("--train-json", help="Path to training challenges JSON") |
| 120 | + parser.add_argument( |
| 121 | + "--train-solutions", help="Path to training solutions JSON", default=None |
| 122 | + ) |
| 123 | + parser.add_argument( |
| 124 | + "--model-path", |
| 125 | + default="neural_guidance_model.json", |
| 126 | + help="Where to save or load the guidance model", |
| 127 | + ) |
| 128 | + parser.add_argument("--test-json", required=True, help="Path to evaluation JSON") |
| 129 | + parser.add_argument( |
| 130 | + "--test-solutions", |
| 131 | + help="Path to evaluation solutions JSON for scoring", |
| 132 | + default=None, |
| 133 | + ) |
| 134 | + parser.add_argument( |
| 135 | + "--out", default="submission.json", help="Output path for submission JSON" |
| 136 | + ) |
| 137 | + parser.add_argument("--epochs", type=int, default=100, help="Training epochs") |
| 138 | + |
| 139 | + args = parser.parse_args() |
| 140 | + |
| 141 | + if args.train_json: |
| 142 | + train_guidance_model( |
| 143 | + args.train_json, args.train_solutions, args.model_path, args.epochs |
| 144 | + ) |
| 145 | + |
| 146 | + accuracy, diffs = evaluate_solver( |
| 147 | + args.test_json, args.model_path, args.test_solutions, args.out |
| 148 | + ) |
| 149 | + |
| 150 | + if args.test_solutions: |
| 151 | + print(f"Accuracy: {accuracy * 100:.2f}%") |
| 152 | + for task_id, diff in diffs.items(): |
| 153 | + if any(np.any(np.array(d)) for d in diff): |
| 154 | + status = "incorrect" |
| 155 | + else: |
| 156 | + status = "correct" |
| 157 | + print(f"Task {task_id}: {status}") |
| 158 | + |
| 159 | + print(f"Submission file written to {args.out}") |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + main() |
| 164 | + |
0 commit comments