Changeset 735
- Timestamp:
- 07/29/14 08:33:29 (22 months ago)
- Location:
- mystic/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/tests/solver_test_suite.py
r713 r735 13 13 # should report clock-time, # of iterations, and # of function evaluations 14 14 15 import sys 16 from StringIO import StringIO 15 17 import unittest 16 18 from math import * 17 19 from mystic.math import almostEqual 18 20 19 disp = True # Flag for whether to display number of iterations and 20 # function evaluations 21 disp = False # Flag for whether to display number of iterations 22 # and function evaluations. 23 verbosity = 2 # Verbosity setting for unittests (default is 1). 24 25 def trap_stdout(): #XXX: better with contextmanager? 26 "temporarily trap stdout; return original sys.stdout" 27 orig, sys.stdout = sys.stdout, StringIO() 28 return orig 29 30 def release_stdout(orig): 31 "release stdout; return any trapped output as a string" 32 out = sys.stdout.getvalue() 33 sys.stdout.close() 34 sys.stdout = orig 35 return out 36 21 37 22 38 class TestZimmermann(unittest.TestCase): … … 36 52 def _run_solver(self, iter_limit=False, local=False, **kwds): 37 53 from mystic.monitors import Monitor 38 import numpy39 54 from mystic.tools import random_seed 40 55 random_seed(321) # Number of failures is quite dependent on random seed! 41 56 esow = Monitor() 42 57 ssow = Monitor() 43 44 if iter_limit: # CRT with solvers that don't use population.45 print '\nA warning should print:'46 58 47 59 solver = self.solver … … 50 62 solver.SetEvaluationMonitor(esow) 51 63 solver.SetGenerationMonitor(ssow) 64 _stdout = trap_stdout() 52 65 solver.Solve(self.costfunction, self.term, **kwds) 66 out = release_stdout(_stdout) 53 67 sol = solver.Solution() 54 68 #print '\nsol:', sol … … 64 78 if iter_limit: 65 79 self.assertTrue(solver.generations < 2) 80 warn = "Warning: Invalid termination condition (nPop < 2)" 81 self.assertTrue(warn in out) 66 82 return 67 83 … … 247 263 def _run_solver(self, iter_limit=False, **kwds): 248 264 from mystic.monitors import Monitor 249 import numpy250 265 from mystic.tools import random_seed 251 266 #random_seed(123) … … 260 275 solver.SetEvaluationMonitor(esow) 261 276 solver.SetGenerationMonitor(ssow) 277 _stdout = trap_stdout() 262 278 solver.Solve(self.costfunction, self.term, **kwds) 279 out = release_stdout(_stdout) 263 280 sol = solver.Solution() 264 281 … … 273 290 if iter_limit: 274 291 self.assertTrue(solver.generations < 2) 292 warn = "Warning: Invalid termination condition (nPop < 2)" 293 self.assertTrue(warn in out) 275 294 return 276 295 … … 448 467 def _run_solver(self, iter_limit=False, **kwds): 449 468 from mystic.monitors import Monitor 450 import numpy451 469 from mystic.tools import random_seed 452 470 random_seed(123) … … 460 478 solver.SetEvaluationMonitor(esow) 461 479 solver.SetGenerationMonitor(ssow) 480 _stdout = trap_stdout() 462 481 solver.Solve(self.costfunction, self.term, **kwds) 482 out = release_stdout(_stdout) 463 483 sol = solver.Solution() 464 484 … … 473 493 if iter_limit: 474 494 self.assertTrue(solver.generations < 2) 495 warn = "Warning: Invalid termination condition (nPop < 2)" 496 self.assertTrue(warn in out) 475 497 return 476 498 … … 649 671 def _run_solver(self, iter_limit=False, **kwds): 650 672 from mystic.monitors import Monitor 651 import numpy652 673 from mystic.tools import random_seed 653 674 #random_seed(123) … … 661 682 solver.SetEvaluationMonitor(esow) 662 683 solver.SetGenerationMonitor(ssow) 684 _stdout = trap_stdout() 663 685 solver.Solve(self.costfunction, self.term, **kwds) 686 out = release_stdout(_stdout) 664 687 sol = solver.Solution() 665 688 … … 674 697 if iter_limit: 675 698 self.assertTrue(solver.generations < 2) 699 warn = "Warning: Invalid termination condition (nPop < 2)" 700 self.assertTrue(warn in out) 676 701 return 677 702 … … 848 873 def _run_solver(self, iter_limit=False, **kwds): 849 874 from mystic.monitors import Monitor 850 import numpy851 875 from mystic.tools import random_seed 852 876 random_seed(123) … … 860 884 solver.SetEvaluationMonitor(esow) 861 885 solver.SetGenerationMonitor(ssow) 886 _stdout = trap_stdout() 862 887 solver.Solve(self.costfunction, self.term, **kwds) 888 out = release_stdout(_stdout) 863 889 sol = solver.Solution() 864 890 … … 873 899 if iter_limit: 874 900 self.assertTrue(solver.generations < 2) 901 warn = "Warning: Invalid termination condition (nPop < 2)" 902 self.assertTrue(warn in out) 875 903 return 876 904 … … 1048 1076 def _run_solver(self, iter_limit = False, **kwds): 1049 1077 from mystic.monitors import Monitor 1050 import numpy1051 1078 from mystic.tools import random_seed 1052 1079 #random_seed(123) … … 1060 1087 solver.SetEvaluationMonitor(esow) 1061 1088 solver.SetGenerationMonitor(ssow) 1089 _stdout = trap_stdout() 1062 1090 solver.Solve(self.costfunction, self.term, **kwds) 1091 out = release_stdout(_stdout) 1063 1092 sol = solver.Solution() 1064 1093 … … 1073 1102 if iter_limit: 1074 1103 self.assertTrue(solver.generations < 2) 1104 warn = "Warning: Invalid termination condition (nPop < 2)" 1105 self.assertTrue(warn in out) 1075 1106 return 1076 1107 … … 1249 1280 def _run_solver(self, iter_limit=False, **kwds): 1250 1281 from mystic.monitors import Monitor 1251 import numpy1252 1282 from mystic.tools import random_seed 1253 1283 #random_seed(123) … … 1261 1291 solver.SetEvaluationMonitor(esow) 1262 1292 solver.SetGenerationMonitor(ssow) 1293 _stdout = trap_stdout() 1263 1294 solver.Solve(self.costfunction, self.term, **kwds) 1295 out = release_stdout(_stdout) 1264 1296 sol = solver.Solution() 1265 1297 … … 1274 1306 if iter_limit: 1275 1307 self.assertTrue(solver.generations < 2) 1308 warn = "Warning: Invalid termination condition (nPop < 2)" 1309 self.assertTrue(warn in out) 1276 1310 return 1277 1311 … … 1462 1496 def _run_solver(self, iter_limit=False, **kwds): 1463 1497 from mystic.monitors import Monitor 1464 import numpy1465 1498 from mystic.tools import random_seed 1466 1499 #random_seed(123) … … 1474 1507 solver.SetEvaluationMonitor(esow) 1475 1508 solver.SetGenerationMonitor(ssow) 1509 _stdout = trap_stdout() 1476 1510 solver.Solve(self.costfunction, self.term, **kwds) 1511 out = release_stdout(_stdout) 1477 1512 sol = solver.Solution() 1478 1513 … … 1487 1522 if iter_limit: 1488 1523 self.assertTrue(solver.generations < 2) 1524 warn = "Warning: Invalid termination condition (nPop < 2)" 1525 self.assertTrue(warn in out) 1489 1526 return 1490 1527 … … 1674 1711 def _run_solver(self, iter_limit=False, **kwds): 1675 1712 from mystic.monitors import Monitor 1676 import numpy1677 1713 from mystic.tools import random_seed 1678 1714 #random_seed(123) … … 1686 1722 solver.SetEvaluationMonitor(esow) 1687 1723 solver.SetGenerationMonitor(ssow) 1724 _stdout = trap_stdout() 1688 1725 solver.Solve(self.costfunction, self.term, **kwds) 1726 out = release_stdout(_stdout) 1689 1727 sol = solver.Solution() 1690 1728 … … 1703 1741 if iter_limit: 1704 1742 self.assertTrue(solver.generations < 2) 1743 warn = "Warning: Invalid termination condition (nPop < 2)" 1744 self.assertTrue(warn in out) 1705 1745 return 1706 1746 … … 1862 1902 1863 1903 def setUp(self): 1904 import numpy 1864 1905 def schwefel(x): 1865 1906 """'Schwefel's function [Sch81] is deceptive in that the global minimum is … … 1883 1924 def _run_solver(self, iter_limit=False, **kwds): 1884 1925 from mystic.monitors import Monitor 1885 import numpy1886 1926 from mystic.tools import random_seed 1887 1927 random_seed(123) … … 1895 1935 solver.SetEvaluationMonitor(esow) 1896 1936 solver.SetGenerationMonitor(ssow) 1937 _stdout = trap_stdout() 1897 1938 solver.Solve(self.costfunction, self.term, **kwds) 1939 out = release_stdout(_stdout) 1898 1940 sol = solver.Solution() 1899 1941 … … 1908 1950 if iter_limit: 1909 1951 self.assertTrue(solver.generations < 2) 1952 warn = "Warning: Invalid termination condition (nPop < 2)" 1953 self.assertTrue(warn in out) 1910 1954 return 1911 1955 … … 2090 2134 def _run_solver(self, iter_limit=False, **kwds): 2091 2135 from mystic.monitors import Monitor 2092 import numpy2093 2136 from mystic.tools import random_seed 2094 2137 random_seed(123) … … 2102 2145 solver.SetEvaluationMonitor(esow) 2103 2146 solver.SetGenerationMonitor(ssow) 2147 _stdout = trap_stdout() 2104 2148 solver.Solve(self.costfunction, self.term, **kwds) 2149 out = release_stdout(_stdout) 2105 2150 sol = solver.Solution() 2106 2151 … … 2115 2160 if iter_limit: 2116 2161 self.assertTrue(solver.generations < 2) 2162 warn = "Warning: Invalid termination condition (nPop < 2)" 2163 self.assertTrue(warn in out) 2117 2164 return 2118 2165 … … 2302 2349 def _run_solver(self, iter_limit=False, **kwds): 2303 2350 from mystic.monitors import Monitor 2304 import numpy2305 2351 from mystic.tools import random_seed 2306 2352 random_seed(123) … … 2314 2360 solver.SetEvaluationMonitor(esow) 2315 2361 solver.SetGenerationMonitor(ssow) 2362 _stdout = trap_stdout() 2316 2363 solver.Solve(self.costfunction, self.term, **kwds) 2364 out = release_stdout(_stdout) 2317 2365 sol = solver.Solution() 2318 2366 … … 2327 2375 if iter_limit: 2328 2376 self.assertTrue(solver.generations < 2) 2377 warn = "Warning: Invalid termination condition (nPop < 2)" 2378 self.assertTrue(warn in out) 2329 2379 return 2330 2380 … … 2490 2540 2491 2541 def setUp(self): 2542 import numpy 2492 2543 def ackley(x): 2493 2544 """Ackley's Path function. … … 2509 2560 def _run_solver(self, iter_limit=False, **kwds): 2510 2561 from mystic.monitors import Monitor 2511 import numpy2512 2562 from mystic.tools import random_seed 2513 2563 random_seed(123) … … 2521 2571 solver.SetEvaluationMonitor(esow) 2522 2572 solver.SetGenerationMonitor(ssow) 2573 _stdout = trap_stdout() 2523 2574 solver.Solve(self.costfunction, self.term, **kwds) 2575 out = release_stdout(_stdout) 2524 2576 sol = solver.Solution() 2525 2577 … … 2534 2586 if iter_limit: 2535 2587 self.assertTrue(solver.generations < 2) 2588 warn = "Warning: Invalid termination condition (nPop < 2)" 2589 self.assertTrue(warn in out) 2536 2590 return 2537 2591 … … 2698 2752 2699 2753 def setUp(self): 2754 import numpy 2700 2755 def rastrigin(x): 2701 2756 """Rastrigin's function. Global minimum at xi=0, f(x)=0. Contains … … 2715 2770 def _run_solver(self, iter_limit=False, **kwds): 2716 2771 from mystic.monitors import Monitor 2717 import numpy2718 2772 from mystic.tools import random_seed 2719 2773 random_seed(123) … … 2727 2781 solver.SetEvaluationMonitor(esow) 2728 2782 solver.SetGenerationMonitor(ssow) 2783 _stdout = trap_stdout() 2729 2784 solver.Solve(self.costfunction, self.term, **kwds) 2785 out = release_stdout(_stdout) 2730 2786 sol = solver.Solution() 2731 2787 … … 2740 2796 if iter_limit: 2741 2797 self.assertTrue(solver.generations < 2) 2798 warn = "Warning: Invalid termination condition (nPop < 2)" 2799 self.assertTrue(warn in out) 2742 2800 return 2743 2801 … … 2922 2980 def _run_solver(self, iter_limit=False, **kwds): 2923 2981 from mystic.monitors import Monitor 2924 import numpy2925 2982 from mystic.tools import random_seed 2926 2983 random_seed(123) … … 2934 2991 solver.SetEvaluationMonitor(esow) 2935 2992 solver.SetGenerationMonitor(ssow) 2993 _stdout = trap_stdout() 2936 2994 solver.Solve(self.costfunction, self.term, **kwds) 2995 out = release_stdout(_stdout) 2937 2996 sol = solver.Solution() 2938 2997 … … 2947 3006 if iter_limit: 2948 3007 self.assertTrue(solver.generations < 2) 3008 warn = "Warning: Invalid termination condition (nPop < 2)" 3009 self.assertTrue(warn in out) 2949 3010 return 2950 3011 … … 3133 3194 def _run_solver(self, iter_limit=False, **kwds): 3134 3195 from mystic.monitors import Monitor 3135 import numpy3136 3196 from mystic.tools import random_seed 3137 3197 random_seed(123) … … 3145 3205 solver.SetEvaluationMonitor(esow) 3146 3206 solver.SetGenerationMonitor(ssow) 3207 _stdout = trap_stdout() 3147 3208 solver.Solve(self.costfunction, self.term, **kwds) 3209 out = release_stdout(_stdout) 3148 3210 sol = solver.Solution() 3149 3211 #print '\n', sol … … 3159 3221 if iter_limit: 3160 3222 self.assertTrue(solver.generations < 2) 3223 warn = "Warning: Invalid termination condition (nPop < 2)" 3224 self.assertTrue(warn in out) 3161 3225 return 3162 3226 … … 3348 3412 def _run_solver(self, iter_limit=False, **kwds): 3349 3413 from mystic.monitors import Monitor 3350 import numpy3351 3414 from mystic.tools import random_seed 3352 3415 random_seed(123) … … 3360 3423 solver.SetEvaluationMonitor(esow) 3361 3424 solver.SetGenerationMonitor(ssow) 3425 _stdout = trap_stdout() 3362 3426 solver.Solve(self.costfunction, self.term, **kwds) 3427 out = release_stdout(_stdout) 3363 3428 sol = solver.Solution() 3364 3429 #print '\n', sol … … 3374 3439 if iter_limit: 3375 3440 self.assertTrue(solver.generations < 2) 3441 warn = "Warning: Invalid termination condition (nPop < 2)" 3442 self.assertTrue(warn in out) 3376 3443 return 3377 3444 … … 3554 3621 # Comment out suites in the list below to test specific test cost functions only 3555 3622 # (Testing all the problems will take some time) 3556 #allsuites = unittest.TestSuite([suite1, suite2, suite3, suite4, suite5, \ 3557 # suite6, suite7, suite8, suite9, suite10, \ 3558 # suite11, suite12, suite13, suite14,\ 3559 # suite15, suite16, suite17]) 3560 allsuites = unittest.TestSuite([suite1]) 3561 unittest.TextTestRunner(verbosity=2).run(allsuites) 3623 allsuites = unittest.TestSuite([suite1, # Zimmermann 3624 # suite2, # Rosenbrock 3625 # suite3, # Corana 3626 # suite4, # Quartic 3627 # suite5, # Shekel 3628 # suite6, # Step 3629 # suite7, # Griewangk 3630 # suite8, # Peaks 3631 # suite9, # Venkataraman91 3632 # suite10, # Schwefel 3633 # suite11, # Easom 3634 suite12, # RotatedEllipsoid 3635 # suite13, # Ackley 3636 # suite14, # Rastrigin 3637 # suite15, # GoldsteinPrice 3638 # suite16, # MathematicaDoc 3639 # suite17, # Paviani 3640 ]) 3641 unittest.TextTestRunner(verbosity=verbosity).run(allsuites) 3642 3643 # EOF -
mystic/tests/test_solver_sanity.py
r734 r735 14 14 from math import * 15 15 from mystic.math import almostEqual 16 17 verbosity = 1 # Verbosity setting for unittests (default is 1). 16 18 17 19 def trap_stdout(): #XXX: better with contextmanager? … … 254 256 suite1 = unittest.TestLoader().loadTestsFromTestCase(TestRosenbrock) 255 257 allsuites = unittest.TestSuite([suite1]) 256 # runner = unittest.TextTestRunner(verbosity=2) 257 runner = unittest.TextTestRunner() 258 runner = unittest.TextTestRunner(verbosity=verbosity) 258 259 259 260 my_maxiter = 0
Note: See TracChangeset
for help on using the changeset viewer.