-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfast_simplex_2d_test.py
More file actions
424 lines (333 loc) · 11.5 KB
/
fast_simplex_2d_test.py
File metadata and controls
424 lines (333 loc) · 11.5 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
================================================================================
FAST SIMPLEX 2D - Test Suite v3.0
================================================================================
Comprehensive tests for Fast Simplex 2D v3.0 (Angular Algorithm).
EDA Team: Gemini · Claude · Alex
License: MIT
Version: 3.0
================================================================================
"""
import numpy as np
import sys
from fast_simplex_2d import FastSimplex2D
def test_1_basic_initialization():
"""Test 1: Basic initialization"""
print("\n" + "="*80)
print("TEST 1: Basic Initialization")
print("="*80)
try:
engine = FastSimplex2D(k_neighbors=18)
print("✓ PASS: Engine initialized successfully")
print(f" - K neighbors: {engine.k}")
return True
except Exception as e:
print(f"✗ FAIL: {e}")
return False
def test_2_data_loading():
"""Test 2: Data loading and validation"""
print("\n" + "="*80)
print("TEST 2: Data Loading")
print("="*80)
np.random.seed(42)
x = np.random.rand(100) * 10
y = np.random.rand(100) * 10
z = x + y
data = np.column_stack([x, y, z])
try:
engine = FastSimplex2D()
engine.fit(data)
if len(engine.points) == 100:
print("✓ PASS: 100 points loaded correctly")
return True
else:
print(f"✗ FAIL: Expected 100 points, got {len(engine.points)}")
return False
except Exception as e:
print(f"✗ FAIL: {e}")
return False
def test_3_wrong_shape():
"""Test 3: Wrong data shape rejection"""
print("\n" + "="*80)
print("TEST 3: Wrong Shape Rejection")
print("="*80)
wrong_data = np.random.rand(10, 2)
try:
engine = FastSimplex2D()
engine.fit(wrong_data)
print("✗ FAIL: Should have raised error for wrong shape")
return False
except (ValueError, IndexError) as e:
print(f"✓ PASS: Correctly rejected wrong shape")
print(f" - Error type: {type(e).__name__}")
return True
except Exception as e:
print(f"✗ FAIL: Wrong exception type: {e}")
return False
def test_4_simple_prediction():
"""Test 4: Simple prediction on linear function"""
print("\n" + "="*80)
print("TEST 4: Simple Prediction (z = x + y)")
print("="*80)
np.random.seed(42)
x = np.random.rand(500) * 10
y = np.random.rand(500) * 10
z = x + y
data = np.column_stack([x, y, z])
engine = FastSimplex2D()
engine.fit(data)
test_point = np.array([5.0, 5.0])
expected = 10.0
pred = engine.predict(test_point)
if pred is not None:
error = abs(pred - expected)
if error < 0.5:
print(f"✓ PASS: Prediction accurate")
print(f" - Expected: {expected}")
print(f" - Predicted: {pred:.4f}")
print(f" - Error: {error:.6f}")
return True
else:
print(f"✗ FAIL: Error too large")
print(f" - Expected: {expected}")
print(f" - Predicted: {pred:.4f}")
print(f" - Error: {error:.6f}")
return False
else:
print("✗ FAIL: Prediction returned None")
return False
def test_5_success_rate():
"""Test 5: Success rate validation"""
print("\n" + "="*80)
print("TEST 5: Success Rate (v3.0 target: 99%+ on 1K, 99.5%+ on large datasets)")
print("="*80)
np.random.seed(42)
x = np.random.rand(1000) * 10
y = np.random.rand(1000) * 10
z = x + y
data = np.column_stack([x, y, z])
engine = FastSimplex2D()
engine.fit(data)
test_points = np.random.rand(200, 2) * 10
predictions = [engine.predict(p) for p in test_points]
success_count = sum([1 for p in predictions if p is not None])
success_rate = success_count / len(predictions) * 100
print(f"Success rate: {success_rate:.1f}%")
print(f"Successful: {success_count}/{len(predictions)}")
if success_rate >= 95.0:
print("✓ PASS: Success rate ≥ 95%")
return True
else:
print(f"✗ FAIL: Success rate below 95%")
return False
def test_6_batch_predictions():
"""Test 6: Batch predictions"""
print("\n" + "="*80)
print("TEST 6: Batch Predictions")
print("="*80)
np.random.seed(42)
x = np.random.rand(300) * 10
y = np.random.rand(300) * 10
z = x + y
data = np.column_stack([x, y, z])
engine = FastSimplex2D()
engine.fit(data)
test_points = np.array([
[2.5, 2.5],
[5.0, 5.0],
[7.5, 7.5]
])
results = []
for point in test_points:
pred = engine.predict(point)
results.append(pred)
success_count = sum([1 for r in results if r is not None])
if success_count == len(test_points):
print(f"✓ PASS: All {len(test_points)} predictions successful")
for i, (point, pred) in enumerate(zip(test_points, results)):
expected = point[0] + point[1]
error = abs(pred - expected)
print(f" - Point {i+1}: pred={pred:.4f}, expected={expected:.4f}, error={error:.4f}")
return True
else:
print(f"✗ FAIL: Only {success_count}/{len(test_points)} predictions successful")
return False
def test_7_exact_match():
"""Test 7: Exact match with dataset point"""
print("\n" + "="*80)
print("TEST 7: Exact Match")
print("="*80)
data = np.array([
[0, 0, 0],
[1, 0, 1],
[0, 1, 1],
[1, 1, 2]
])
engine = FastSimplex2D()
engine.fit(data)
test_point = np.array([1.0, 1.0])
expected = 2.0
pred = engine.predict(test_point)
if pred is not None:
error = abs(pred - expected)
if error < 1e-6:
print("✓ PASS: Exact match prediction")
print(f" - Expected: {expected}")
print(f" - Predicted: {pred:.10f}")
print(f" - Error: {error:.2e}")
return True
else:
print(f"✗ FAIL: Error too large for exact match: {error}")
return False
else:
print("✗ FAIL: Prediction returned None")
return False
def test_8_performance():
"""Test 8: Performance benchmark"""
print("\n" + "="*80)
print("TEST 8: Performance")
print("="*80)
import time
np.random.seed(42)
N = 5000
x = np.random.rand(N) * 10
y = np.random.rand(N) * 10
z = x + y
data = np.column_stack([x, y, z])
# Measure fit time
start = time.perf_counter()
engine = FastSimplex2D()
engine.fit(data)
fit_time = (time.perf_counter() - start) * 1000
# Measure predict time (100 queries)
queries = np.random.rand(100, 2) * 10
start = time.perf_counter()
for q in queries:
engine.predict(q)
predict_time = (time.perf_counter() - start) * 1000
throughput = 100 / (predict_time / 1000)
print(f"✓ PASS: Performance measured")
print(f" - Fit time ({N} points): {fit_time:.2f} ms")
print(f" - Predict time (100 queries): {predict_time:.2f} ms")
print(f" - Throughput: {throughput:.0f} pred/s")
if throughput > 5000:
print(f" - ✓ Excellent performance (>5000 pred/s)")
return True
def test_9_curved_function():
"""Test 9: Non-linear function (curved)"""
print("\n" + "="*80)
print("TEST 9: Curved Function (z = sin(x) * cos(y))")
print("="*80)
np.random.seed(42)
N = 1000
x = np.random.rand(N) * 10
y = np.random.rand(N) * 10
z = np.sin(x) * np.cos(y)
data = np.column_stack([x, y, z])
engine = FastSimplex2D()
engine.fit(data)
# Test points
test_points = np.random.rand(100, 2) * 10
true_values = np.sin(test_points[:, 0]) * np.cos(test_points[:, 1])
predictions = []
for p in test_points:
pred = engine.predict(p)
predictions.append(pred if pred is not None else np.nan)
predictions = np.array(predictions)
valid = ~np.isnan(predictions)
if np.sum(valid) > 0:
errors = np.abs(predictions[valid] - true_values[valid])
mean_error = np.mean(errors)
max_error = np.max(errors)
success_rate = np.sum(valid) / len(predictions) * 100
print(f"✓ PASS: Curved function tested")
print(f" - Success rate: {success_rate:.1f}%")
print(f" - Mean error: {mean_error:.6f}")
print(f" - Max error: {max_error:.6f}")
if mean_error < 0.01:
print(f" - ✓ Excellent precision on curves")
return True
else:
print("✗ FAIL: No valid predictions")
return False
def test_10_large_dataset():
"""Test 10: Large dataset scalability"""
print("\n" + "="*80)
print("TEST 10: Large Dataset Scalability")
print("="*80)
np.random.seed(42)
N = 50000
x = np.random.rand(N) * 10
y = np.random.rand(N) * 10
z = x + y
data = np.column_stack([x, y, z])
import time
start = time.perf_counter()
engine = FastSimplex2D()
engine.fit(data)
fit_time = (time.perf_counter() - start) * 1000
queries = np.random.rand(1000, 2) * 10
start = time.perf_counter()
predictions = [engine.predict(q) for q in queries]
query_time = (time.perf_counter() - start) * 1000
success_rate = sum([1 for p in predictions if p is not None]) / len(predictions) * 100
throughput = 1000 / (query_time / 1000)
print(f"✓ PASS: Large dataset handled")
print(f" - Dataset size: {N:,} points")
print(f" - Fit time: {fit_time:.2f} ms")
print(f" - Query time (1000): {query_time:.2f} ms")
print(f" - Throughput: {throughput:.0f} pred/s")
print(f" - Success rate: {success_rate:.1f}%")
if success_rate >= 99.0:
print(f" - ✓ Excellent success rate on large dataset")
return True
def run_all_tests():
"""Run complete test suite"""
print("\n" + "="*80)
print("FAST SIMPLEX 2D v3.0 - COMPREHENSIVE TEST SUITE")
print("="*80)
print("EDA Team: Gemini · Claude · Alex")
print("="*80)
tests = [
test_1_basic_initialization,
test_2_data_loading,
test_3_wrong_shape,
test_4_simple_prediction,
test_5_success_rate,
test_6_batch_predictions,
test_7_exact_match,
test_8_performance,
test_9_curved_function,
test_10_large_dataset
]
results = []
for test in tests:
try:
result = test()
results.append(result)
except Exception as e:
print(f"\n✗ CRASH: {test.__name__} crashed with: {e}")
import traceback
traceback.print_exc()
results.append(False)
# Summary
print("\n" + "="*80)
print("TEST SUMMARY")
print("="*80)
passed = sum(results)
total = len(results)
for i, (test, result) in enumerate(zip(tests, results), 1):
status = "✓ PASS" if result else "✗ FAIL"
print(f"{status} | Test {i}: {test.__name__}")
print("="*80)
print(f"TOTAL: {passed}/{total} tests passed")
if passed == total:
print("🎉 ALL TESTS PASSED! 🎉")
print("\n✅ Fast Simplex v3.0 is READY for production!")
else:
print(f"⚠️ {total - passed} test(s) failed")
print("="*80)
return passed == total
if __name__ == "__main__":
success = run_all_tests()
sys.exit(0 if success else 1)