Changeset 734


Ignore:
Timestamp:
07/28/14 18:08:15 (22 months ago)
Author:
mmckerns
Message:

mv solver_test_compare and solver_test_sanity to start with test_*;
run all included tests; assert silently; include function to trap_stdout

Location:
mystic
Files:
1 edited
2 moved

Legend:

Unmodified
Added
Removed
  • mystic/mystic/termination.py

    r713 r734  
    291291        sim = numpy.array(inst.population) 
    292292        #if not len(sim[1:]): 
    293         #    print "Warning: Invalid termination condition (nPop < 2)" 
    294         #    return True 
     293        #    warn = "Warning: Invalid termination condition (nPop < 2)" 
     294        #    print warn 
     295        #    return warn 
    295296        if numpy.all(abs(sim - sim[0]) <= abs(tolerance * sim[0])): return info(doc) 
    296297        return info(null) 
  • mystic/tests/test_solver_compare.py

    r713 r734  
    99try: 
    1010  from scipy.optimize import fmin, fmin_powell 
    11   scipy_solvers = ['fmin_powell', 'fmin'] 
    1211except ImportError: 
    13   print "Warning: scipy not installed; comparison tests skipped" 
    14   scipy_solvers = [] 
     12  from mystic._scipyoptimize import fmin, fmin_powell 
     13# print "Warning: scipy not installed; defaulting to local solver copy" 
     14scipy_solvers = ['fmin_powell', 'fmin'] 
    1515 
    1616import mystic.solvers as solvers 
     
    5656# print 'my:', my_x[0:2] 
    5757# print 'sp:', sp_x[0:2] 
    58   if maxiter != 0: # mystic can stop at iter=0, scipy can't 
     58  if my_x[3] == sp_x[-2]: # mystic can stop at iter=0, scipy can't 
    5959    assert almostEqual(my_x[0], sp_x[0]) 
    6060    assert almostEqual(my_x[1], sp_x[1]) 
     
    7676 
    7777  # check solutions versus results based on the random_seed 
    78   print "comparing against known results" 
     78# print "comparing against known results" 
    7979  sol = solvers.diffev(rosen, x0, npop=40, disp=0, full_output=True) 
    8080  assert almostEqual(sol[1], 0.0020640145337293249, tol=3e-3) 
     
    8888  solver2 = 'diffev2' 
    8989  for solver in ['diffev']: 
    90     print "comparing %s and %s from mystic" % (solver, solver2) 
     90#   print "comparing %s and %s from mystic" % (solver, solver2) 
    9191    test_solvers(solver, solver2, x0, npop=40) 
    92 #   test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=0) 
    93 #   test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=1) 
    94 #   test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=2) 
    95 #   test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=9) 
     92    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=0) 
     93    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=1) 
     94    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=2) 
     95    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=9) 
    9696    test_solvers(solver, solver2, x0, npop=40, maxiter=0) 
    9797    test_solvers(solver, solver2, x0, npop=40, maxiter=1) 
     
    100100 
    101101  for solver in scipy_solvers: 
    102     print "comparing %s from mystic and scipy" % (solver) 
     102#   print "comparing %s from mystic and scipy" % (solver) 
    103103    test_compare(solver, x0) 
    104 #   test_compare(solver, x0, maxiter=None, maxfun=0) 
    105 #   test_compare(solver, x0, maxiter=None, maxfun=1) 
    106 #   test_compare(solver, x0, maxiter=None, maxfun=2) 
    107 #   test_compare(solver, x0, maxiter=None, maxfun=9) 
     104    test_compare(solver, x0, maxiter=None, maxfun=0) 
     105    test_compare(solver, x0, maxiter=None, maxfun=1) 
     106    test_compare(solver, x0, maxiter=None, maxfun=2) 
     107    test_compare(solver, x0, maxiter=None, maxfun=9) 
    108108    test_compare(solver, x0, maxiter=0) 
    109109    test_compare(solver, x0, maxiter=1) 
  • mystic/tests/test_solver_sanity.py

    r713 r734  
    99# should report clock-time, # of iterations, and # of function evaluations 
    1010 
     11import sys 
     12from StringIO import StringIO 
    1113import unittest 
    1214from math import * 
    1315from mystic.math import almostEqual 
     16 
     17def trap_stdout(): #XXX: better with contextmanager? 
     18    "temporarily trap stdout; return original sys.stdout" 
     19    orig, sys.stdout = sys.stdout, StringIO() 
     20    return orig 
     21 
     22def release_stdout(orig): 
     23    "release stdout; return any trapped output as a string" 
     24    out = sys.stdout.getvalue() 
     25    sys.stdout.close() 
     26    sys.stdout = orig 
     27    return out 
     28 
    1429 
    1530class TestRosenbrock(unittest.TestCase): 
     
    3651        import numpy 
    3752        from mystic.tools import random_seed 
    38         random_seed(321) 
     53        seed = 111 if self.maxiter is None else 321 #XXX: good numbers... 
     54        random_seed(seed) 
    3955        esow = Monitor() 
    4056        ssow = Monitor()  
     
    4662        if self.useevalmon: solver.SetEvaluationMonitor(esow) 
    4763        if self.usestepmon: solver.SetGenerationMonitor(ssow) 
     64        #### run solver, but trap output 
     65        _stdout = trap_stdout() 
    4866        solver.Solve(self.costfunction, self.term, **kwds) 
     67        out = release_stdout(_stdout) 
     68        ################################ 
    4969        sol = solver.Solution() 
    5070 
     
    6888        if early_terminate: 
    6989            self.assertTrue(solver.generations < 2) 
     90            warn = "Warning: Invalid termination condition (nPop < 2)" 
     91            self.assertTrue(warn in out) 
    7092            return 
    7193 
     
    104126 
    105127        # Verify solution is close to exact 
    106         print sol 
     128       #print sol 
    107129        for i in range(len(sol)): 
    108130            self.assertAlmostEqual(sol[i], self.exact[i], self.precision) 
     
    232254    suite1 = unittest.TestLoader().loadTestsFromTestCase(TestRosenbrock) 
    233255    allsuites = unittest.TestSuite([suite1]) 
     256#   runner = unittest.TextTestRunner(verbosity=2) 
     257    runner = unittest.TextTestRunner() 
     258 
    234259    my_maxiter = 0 
    235 #   my_maxiter = 1 
    236 #   my_maxiter = 2 
    237 #   my_maxiter = None 
    238     unittest.TextTestRunner(verbosity=2).run(allsuites) 
     260    runner.run(allsuites) 
     261    my_maxiter = 1 
     262    runner.run(allsuites) 
     263    my_maxiter = 2 
     264    runner.run(allsuites) 
     265    my_maxiter = None 
     266    runner.run(allsuites) 
     267 
    239268 
    240269# EOF 
Note: See TracChangeset for help on using the changeset viewer.