Repository navigation
Expand file tree
/
Copy pathtest_openopt.py
More file actions
168 lines (138 loc) · 5.07 KB
/
Copy pathtest_openopt.py
File metadata and controls
168 lines (138 loc) · 5.07 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
#!/usr/bin/env python
"""
Simple test script to verify OpenOpt basic functionality.
Tests for compatibility with Python 3 and NumPy 2.x
"""
import sys
import os
import traceback
# Add OpenOpt to path
sys.path.insert(0, 'OpenOpt')
def test_import():
"""Test basic imports"""
print("Testing basic imports...")
try:
import openopt
print("✓ OpenOpt imported successfully")
return True
except Exception as e:
print(f"✗ Failed to import OpenOpt: {e}")
traceback.print_exc()
return False
def test_simple_nlp():
"""Test a simple NLP problem"""
print("\nTesting simple NLP problem...")
try:
from openopt import NLP
import numpy as np
# Simple quadratic problem
f = lambda x: (x[0]-1)**2 + (x[1]-2)**2
x0 = [0, 0]
p = NLP(f, x0, iprint=-1, maxIter=100)
# Try with ralg solver (should be built-in)
try:
r = p.solve('ralg')
print(f"✓ NLP solved with ralg: solution={r.xf}, f(x)={r.ff:.6f}")
success = True
except Exception as e:
print(f"✗ ralg solver failed: {e}")
success = False
return success
except Exception as e:
print(f"✗ Failed to solve NLP: {e}")
traceback.print_exc()
return False
def test_numpy_compatibility():
"""Test NumPy compatibility issues"""
print("\nTesting NumPy compatibility...")
try:
import numpy as np
print(f"NumPy version: {np.__version__}")
# Check for common NumPy 2.x issues
if int(np.__version__.split('.')[0]) >= 2:
print("⚠ NumPy 2.x detected - may have compatibility issues")
# Test basic array operations
arr = np.array([1, 2, 3])
print(f"✓ Basic array creation works: {arr}")
# Test common deprecated functions
try:
# In NumPy 2.x, np.product is removed in favor of np.prod
result = np.prod(arr)
print(f"✓ np.prod works: {result}")
except:
print("✗ np.prod failed")
return True
except Exception as e:
print(f"✗ NumPy compatibility test failed: {e}")
return False
def run_test_suite():
"""Run the test suite from OpenOpt tests directory"""
print("\nAttempting to run OpenOpt test suite...")
try:
os.chdir('OpenOpt')
sys.path.insert(0, '.')
test_files = ['nlp1', 'nlp2', 'nlsp1']
results = {}
for test_name in test_files:
print(f"\nRunning test: {test_name}...")
try:
test_module = __import__(f'openopt.tests.{test_name}', fromlist=['test'])
if hasattr(test_module, 'test'):
isPassed, r, p = test_module.test(iprint=-1)
results[test_name] = isPassed
print(f" {'✓' if isPassed else '✗'} {test_name}: {'PASSED' if isPassed else 'FAILED'}")
else:
print(f" ⚠ {test_name}: No test() function found")
except Exception as e:
results[test_name] = False
print(f" ✗ {test_name}: Failed with error - {str(e)[:100]}")
os.chdir('..')
return results
except Exception as e:
print(f"✗ Test suite failed: {e}")
traceback.print_exc()
return {}
def main():
print("=" * 60)
print("OpenOpt Test Suite")
print(f"Python version: {sys.version}")
print("=" * 60)
# Run tests
tests_passed = []
# Basic import test
if test_import():
tests_passed.append("import")
# NumPy compatibility
if test_numpy_compatibility():
tests_passed.append("numpy")
# Simple NLP test
if test_simple_nlp():
tests_passed.append("simple_nlp")
# Full test suite
suite_results = run_test_suite()
# Summary
print("\n" + "=" * 60)
print("SUMMARY")
print("=" * 60)
print(f"Basic tests passed: {len(tests_passed)}/3")
for test in ["import", "numpy", "simple_nlp"]:
status = "✓" if test in tests_passed else "✗"
print(f" {status} {test}")
if suite_results:
print(f"\nTest suite results: {sum(suite_results.values())}/{len(suite_results)} passed")
for test, passed in suite_results.items():
print(f" {'✓' if passed else '✗'} {test}")
print("\n" + "=" * 60)
# Overall status
overall_success = len(tests_passed) >= 2
if overall_success:
print("✓ OpenOpt is partially working but has issues")
print(" - Python 2 to 3 conversion appears mostly successful")
print(" - NumPy 2.x compatibility has some issues")
print(" - Some solvers may not be available or working")
else:
print("✗ OpenOpt has significant issues")
return overall_success
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)