-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_inference.py
More file actions
170 lines (134 loc) · 5.24 KB
/
test_inference.py
File metadata and controls
170 lines (134 loc) · 5.24 KB
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
"""
Inference Test Script
======================
This script tests that trained models can be loaded and used for inference
WITHOUT running any training code. This validates the strict separation
between training and inference.
Usage:
python test_inference.py
"""
import os
import sys
# Add package to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_sorting_inference():
"""Test sorting model inference."""
print("=" * 60)
print("TESTING SORTING INFERENCE")
print("=" * 60)
from sorting.recommender import SortingRecommender
# Load model (NO training code runs)
recommender = SortingRecommender()
recommender.load_model() # Uses default models directory
# Test cases
test_cases = [
([5, 2, 8, 1, 9, 3, 7], "Small random"),
(list(range(100)), "Already sorted"),
(list(range(100, 0, -1)), "Reverse sorted"),
([x for x in range(1000)], "Large sorted"),
([float(x % 10) for x in range(500)], "Many duplicates"),
]
print("\nInference results:")
for arr, description in test_cases:
result = recommender.recommend(arr)
print(f" {description:20s} → {result['algorithm']}")
# Execute recommendation
arr = [10, 5, 8, 3, 1]
sorted_arr, algo, explanation = recommender.execute_recommendation(arr)
print(f"\nExecuted sorting:")
print(f" Input: {arr}")
print(f" Output: {sorted_arr}")
print(f" Algorithm: {algo}")
return True
def test_array_search_inference():
"""Test array search model inference."""
print("\n" + "=" * 60)
print("TESTING ARRAY SEARCH INFERENCE")
print("=" * 60)
from searching.array_search.recommender import ArraySearchRecommender
# Load model
recommender = ArraySearchRecommender()
recommender.load_model()
# Test cases
test_cases = [
([1, 2, 3, 4, 5], "Sorted uniform"),
([5, 2, 8, 1, 9], "Unsorted"),
(list(range(0, 1000, 10)), "Large sorted uniform"),
]
print("\nInference results:")
for arr, description in test_cases:
result = recommender.recommend(arr)
print(f" {description:20s} → {result['algorithm']} (valid: {result['valid_algorithms']})")
# Verify correctness constraint
unsorted = [5, 2, 8, 1, 9]
result = recommender.recommend(unsorted)
if result['algorithm'] != 'linear_search':
print(f"\n ✗ ERROR: Unsorted array got {result['algorithm']} instead of linear_search!")
return False
else:
print(f"\n ✓ Correctness constraint verified: unsorted → linear_search only")
return True
def test_graph_search_inference():
"""Test graph search model inference."""
print("\n" + "=" * 60)
print("TESTING GRAPH SEARCH INFERENCE")
print("=" * 60)
from searching.graph_search.recommender import GraphSearchRecommender
# Load model
recommender = GraphSearchRecommender()
recommender.load_model()
# Test cases
test_cases = [
# Unweighted graph
({0: [(1, 1.0), (2, 1.0)], 1: [(3, 1.0)], 2: [(3, 1.0)], 3: []}, False, "Unweighted"),
# Weighted non-negative
({0: [(1, 5.0), (2, 3.0)], 1: [(2, 2.0)], 2: []}, False, "Weighted+"),
# Weighted with negative
({0: [(1, 5.0)], 1: [(2, -3.0)], 2: []}, False, "Negative weight"),
]
print("\nInference results:")
for graph, has_heur, description in test_cases:
result = recommender.recommend(graph, has_heuristic=has_heur)
print(f" {description:20s} → {result['algorithm']} (valid: {result['valid_algorithms']})")
# Verify correctness constraint for negative weights
neg_graph = {0: [(1, 5.0)], 1: [(2, -3.0)], 2: []}
result = recommender.recommend(neg_graph)
if result['algorithm'] != 'bellman_ford':
print(f"\n ✗ ERROR: Negative weight graph got {result['algorithm']}!")
return False
else:
print(f"\n ✓ Correctness constraint verified: negative weights → bellman_ford")
return True
def main():
"""Run all inference tests."""
print("=" * 60)
print("INFERENCE TEST - STRICT TRAINING/INFERENCE SEPARATION")
print("=" * 60)
print("\nThis test verifies that:")
print(" 1. Models can be loaded from .pkl files")
print(" 2. Inference works without running training code")
print(" 3. Correctness constraints are enforced")
try:
sorting_ok = test_sorting_inference()
array_ok = test_array_search_inference()
graph_ok = test_graph_search_inference()
print("\n" + "=" * 60)
print("INFERENCE TEST SUMMARY")
print("=" * 60)
print(f" Sorting: {'✓ PASS' if sorting_ok else '✗ FAIL'}")
print(f" Array Search: {'✓ PASS' if array_ok else '✗ FAIL'}")
print(f" Graph Search: {'✓ PASS' if graph_ok else '✗ FAIL'}")
if sorting_ok and array_ok and graph_ok:
print("\n✅ ALL INFERENCE TESTS PASSED")
print(" Training and inference are properly separated.")
return 0
else:
print("\n❌ SOME TESTS FAILED")
return 1
except Exception as e:
print(f"\n❌ ERROR: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == '__main__':
sys.exit(main())