Changeset 423


Ignore:
Timestamp:
09/10/10 09:49:46 (6 years ago)
Author:
mmckerns
Message:

added termination test suite (temporarily in wrong dir)
minor bugfixes to termination conditions

Location:
mystic
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • mystic/_math/approx.py

    r259 r423  
    11#!/usr/bin/env python 
    2  
    32""" 
    43tools for measuring equality 
    54""" 
     5#NOTE: need to comapre to numpy.allclose... 
     6 
    67def _float_approx_equal(x, y, tol=1e-18, rel=1e-7): 
    78    if tol is rel is None: 
  • mystic/mystic/termination.py

    r422 r423  
    1717    """cost of last iteration is < tolerance: 
    1818 
    19 cost[-1] < tolerance""" 
     19cost[-1] <= tolerance""" 
    2020    def _VTR(inst): 
    2121         hist = inst.energy_history 
    22          return hist[-1] < tolerance 
     22         return hist[-1] <= tolerance 
    2323    return _VTR 
    2424 
     
    2626    """change in cost is < tolerance over a number of generations: 
    2727 
    28 cost[-g] - cost[-1] < tolerance, where g=generations""" 
     28cost[-g] - cost[-1] <= tolerance, where g=generations""" 
    2929    def _ChangeOverGeneration(inst): 
    3030         hist = inst.energy_history 
    3131         lg = len(hist) 
    3232         if lg <= generations: return False 
    33          return (hist[-generations]-hist[-1]) < tolerance 
     33         return (hist[-generations]-hist[-1]) <= tolerance 
    3434    return _ChangeOverGeneration 
    3535 
     
    4747    return _NormalizedChangeOverGeneration 
    4848               
    49 def CandidateRelativeTolerance(xtol=1e-4, ftol=1e-4): 
     49def CandidateRelativeTolerance(xtol = 1e-4, ftol = 1e-4): 
    5050    """absolute difference in candidates is < tolerance: 
    5151 
     
    7272sum(abs(last_params - current_params)) <= tolerance""" 
    7373    def _SolutionImprovement(inst): 
    74         update = inst.bestSolution - inst.trialSolution #XXX: if inf - inf ? 
     74        best = numpy.array(inst.bestSolution) 
     75        trial = numpy.array(inst.trialSolution) 
     76        update = best - trial #XXX: if inf - inf ? 
    7577        answer = numpy.add.reduce(abs(update)) <= tolerance 
    7678        return answer 
     
    9092             lg = len(hist) 
    9193             #XXX: throws error when hist is shorter than generations ? 
    92              return lg > generations and (hist[-generations]-hist[-1]) < 0 
     94             return lg > generations and (hist[-generations]-hist[-1]) <= 0 
    9395         if not generations and fval == None: return True 
    9496         return abs(inst.bestEnergy-fval) <= abs(tolerance * fval) 
    9597    return _NormalizedCostTarget 
    9698 
    97 def VTRChangeOverGenerations(ftol = 0.005, gtol = 1e-6, generations = 30): 
     99def VTRChangeOverGeneration(ftol = 0.005, gtol = 1e-6, generations = 30): 
    98100    """change in cost is < gtol over a number of generations, 
    99101or cost of last iteration is < ftol: 
    100102 
    101 cost[-g] - cost[-1] < gtol, where g=generations *or* cost[-1] < ftol.""" 
    102     def _VTRChangeOverGenerations(inst): 
     103cost[-g] - cost[-1] <= gtol, where g=generations *or* cost[-1] <= ftol.""" 
     104    def _VTRChangeOverGeneration(inst): 
    103105         hist = inst.energy_history 
    104106         lg = len(hist) 
    105107         #XXX: throws error when hist is shorter than generations ? 
    106          return (lg > generations and (hist[-generations]-hist[-1]) < gtol)\ 
    107                 or ( hist[-1] < ftol ) 
    108     return _VTRChangeOverGenerations 
     108         return (lg > generations and (hist[-generations]-hist[-1]) <= gtol)\ 
     109                or ( hist[-1] <= ftol ) 
     110    return _VTRChangeOverGeneration 
    109111 
    110 def PopulationSpread(tolerance=1e-6): 
     112def PopulationSpread(tolerance = 1e-6): 
    111113    """normalized absolute deviation from best candidate is < tolerance: 
    112114 
    113 abs(params - params[0]) < tolerance""" 
     115abs(params - params[0]) <= tolerance""" 
    114116    def _PopulationSpread(inst): 
    115117         sim = numpy.array(inst.population) 
     
    117119         #    print "Warning: Invalid termination condition (nPop < 2)" 
    118120         #    return True 
    119          return all(abs(sim - sim[0]) <= abs(tolerance * sim[0])) 
     121         return numpy.all(abs(sim - sim[0]) <= abs(tolerance * sim[0])) 
    120122    return _PopulationSpread 
    121123 
    122 def GradientNormTolerance(tolerance=1e-5, norm=Inf):  
     124def GradientNormTolerance(tolerance = 1e-5, norm = Inf):  
    123125    """gradient norm is < tolerance, given user-supplied norm: 
    124126 
    125 sum( abs(gradient)**norm )**(1.0/norm) < tolerance""" 
     127sum( abs(gradient)**norm )**(1.0/norm) <= tolerance""" 
    126128    def _GradientNormTolerance(inst): 
    127129        try: 
Note: See TracChangeset for help on using the changeset viewer.