Changeset 536
- Timestamp:
- 07/29/12 22:10:39 (4 years ago)
- Location:
- mystic/mystic
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/mystic/differential_evolution.py
r519 r536 286 286 287 287 self.energy_history.append(self.bestEnergy) 288 termination(self) #XXX: initialize termination conditions, if needed 288 289 self._stepmon(self.bestSolution[:], self.bestEnergy, id) 289 self.generations = 0 290 self.generations = 0 #XXX: above currently *not* counted as an iteration 290 291 if callback is not None: 291 292 callback(self.bestSolution) … … 493 494 494 495 self.energy_history.append(self.bestEnergy) 496 termination(self) #XXX: initialize termination conditions, if needed 495 497 #FIXME: StepMonitor works for 'pp'? 496 498 self._stepmon(self.bestSolution[:], self.bestEnergy, id) -
mystic/mystic/monitors.py
r480 r536 75 75 def __setattr__(self, name, value): return self 76 76 def __delattr__(self, name): return self 77 def __len__(self): return #XXX ? 77 78 78 79 class Monitor(object): … … 99 100 self._info = [] 100 101 #self._all = all 102 103 def __len__(self): 104 return len(self.x) 101 105 102 106 def info(self, message): -
mystic/mystic/munge.py
r477 r536 1 1 from mystic.tools import list_or_tuple_or_ndarray as sequence 2 from mystic.tools import isNull 2 3 3 4 # logfile reader … … 46 47 47 48 def raw_to_converge(steps, energy): 48 if not sequence(steps[0][0]): 49 steps = [[step] for step in steps] # needed when steps = [1,2,3,...] 50 steps = [zip(*step) for step in steps] # also can be used to revert 'steps' 49 if len(steps) > 0: 50 if not sequence(steps[0][0]): 51 steps = [[step] for step in steps] # needed when steps = [1,2,3,...] 52 steps = [zip(*step) for step in steps] # also can be used to revert 'steps' 51 53 return steps, energy 52 54 … … 58 60 59 61 def write_raw_file(mon,log_file='paramlog.py',**kwds): 62 if isNull(mon): return #XXX: throw error? warning? ??? 60 63 steps, energy = read_monitor(mon) 61 64 f = open(log_file,'w') … … 69 72 70 73 def write_support_file(mon,log_file='paramlog.py'): 74 if isNull(mon): return #XXX: throw error? warning? ??? 71 75 monitor = write_monitor( *raw_to_support( *read_monitor(mon) ) ) 72 76 write_raw_file(monitor,log_file,header="written in 'support' format") … … 74 78 75 79 def write_converge_file(mon,log_file='paramlog.py'): 80 if isNull(mon): return #XXX: throw error? warning? ??? 76 81 monitor = write_monitor( *raw_to_converge( *read_monitor(mon) ) ) 77 82 write_raw_file(monitor,log_file,header="written in 'converge' format") … … 131 136 132 137 def __orig_write_support_file(mon,log_file='paramlog.py'): 138 if isNull(mon): return #XXX: throw error? warning? ??? 133 139 steps, energy = read_monitor(mon) 134 140 log = [] 135 for p in range(len(steps[0])): 136 q = [] 137 for s in range(len(steps)): 138 q.append(steps[s][p]) 139 log.append(q) 141 if len(steps) > 0: 142 for p in range(len(steps[0])): 143 q = [] 144 for s in range(len(steps)): 145 q.append(steps[s][p]) 146 log.append(q) 140 147 monitor = write_monitor(log, energy) 141 148 write_raw_file(monitor,log_file) -
mystic/mystic/scipy_optimize.py
r528 r536 215 215 allvecs = [sim[0]] 216 216 fsim[0] = func(x0) 217 termination(self) #XXX: initialize termination conditions, if needed 217 218 self._stepmon(sim[0], fsim[0], id) # sim = all; "best" is sim[0] 218 219 … … 586 587 self.popEnergy[0] = fval #XXX: pointless? 587 588 self.energy_history.append(self.bestEnergy) 589 termination(self) #XXX: initialize termination conditions, if needed 588 590 self._stepmon(x, fval, id) # get initial values 589 591 -
mystic/mystic/termination.py
r484 r536 7 7 import numpy 8 8 from numpy import absolute 9 from __builtin__ import bool as _bool 9 10 abs = absolute 10 11 Inf = numpy.Inf 12 null = "" 11 13 12 14 # a module level singleton. … … 18 20 19 21 cost[-1] <= tolerance""" 20 def _VTR(inst): 22 doc = "VTR with %s" % {'tolerance':tolerance, 'target':target} 23 def _VTR(inst, bool=True): 24 if bool: bool = _bool 25 else: bool = lambda x:x 21 26 hist = inst.energy_history 22 return abs(hist[-1] - target) <= tolerance 27 if not len(hist): return bool(null) 28 if abs(hist[-1] - target) <= tolerance: msg = doc 29 else: msg = null 30 return bool(msg) 31 #_VTR.__doc__ = "%s(**%s)" % tuple(doc.split(" with ")) 32 _VTR.__doc__ = doc 23 33 return _VTR 24 34 … … 27 37 28 38 cost[-g] - cost[-1] <= tolerance, where g=generations""" 39 doc = "ChangeOverGeneration with %s" % {'tolerance':tolerance, 40 'generations':generations} 29 41 def _ChangeOverGeneration(inst): 30 42 hist = inst.energy_history 31 43 lg = len(hist) 32 if lg <= generations: return False 33 return (hist[-generations]-hist[-1]) <= tolerance 44 if lg <= generations: return "" 45 if (hist[-generations]-hist[-1]) <= tolerance: return doc 46 return "" 47 _ChangeOverGeneration.__doc__ = doc 34 48 return _ChangeOverGeneration 35 49 … … 39 53 (cost[-g] - cost[-1]) / 0.5*(abs(cost[-g]) + abs(cost[-1])) <= tolerance""" 40 54 eta = 1e-20 55 doc = "NormalizedChangeOverGeneration with %s" % {'tolerance':tolerance, 56 'generations':generations} 41 57 def _NormalizedChangeOverGeneration(inst): 42 58 hist = inst.energy_history 43 59 lg = len(hist) 44 if lg <= generations: return False60 if lg <= generations: return "" 45 61 diff = tolerance*(abs(hist[-generations])+abs(hist[-1])) + eta 46 return 2.0*(hist[-generations]-hist[-1]) <= diff 62 if 2.0*(hist[-generations]-hist[-1]) <= diff: return doc 63 return "" 64 _NormalizedChangeOverGeneration.__doc__ = doc 47 65 return _NormalizedChangeOverGeneration 48 66 … … 52 70 abs(xi-x0) <= xtol & abs(fi-f0) <= ftol, where x=params & f=cost""" 53 71 #NOTE: this termination expects nPop > 1 72 doc = "CandidateRelativeTolerance with %s" % {'xtol':xtol, 'ftol':ftol} 54 73 def _CandidateRelativeTolerance(inst): 55 74 sim = numpy.array(inst.population) 56 75 fsim = numpy.array(inst.popEnergy) 57 76 if not len(fsim[1:]): 58 print "Warning: Invalid termination condition (nPop < 2)" 59 return True 77 warn = "Warning: Invalid termination condition (nPop < 2)" 78 print warn 79 return warn 60 80 # raise ValueError, "Invalid termination condition (nPop < 2)" 61 81 #FIXME: abs(inf - inf) will raise a warning... … … 64 84 answer = answer and max(abs(fsim[0]-fsim[1:])) <= ftol 65 85 numpy.seterr(invalid=errdict['invalid']) #FIXME: turn on warnings 66 return answer 86 if answer: return doc 87 return "" 88 _CandidateRelativeTolerance.__doc__ = doc 67 89 return _CandidateRelativeTolerance 68 90 … … 71 93 72 94 sum(abs(last_params - current_params)) <= tolerance""" 95 doc = "SolutionImprovement with %s" % {'tolerance':tolerance} 73 96 def _SolutionImprovement(inst): 74 97 best = numpy.array(inst.bestSolution) … … 76 99 update = best - trial #XXX: if inf - inf ? 77 100 answer = numpy.add.reduce(abs(update)) <= tolerance 78 return answer 101 if answer: return doc 102 return "" 103 _SolutionImprovement.__doc__ = doc 79 104 return _SolutionImprovement 80 105 … … 87 112 # original --> if generations: then return cost[-g] - cost[-1] < 0 88 113 # --> else: return fval != 0 and abs((best - fval)/fval) < tol 114 doc = "NormalizedCostTarget with %s" % {'fval':fval, 'tolerance':tolerance, 115 'generations':generations} 89 116 def _NormalizedCostTarget(inst): 90 117 if generations and fval == None: … … 92 119 lg = len(hist) 93 120 #XXX: throws error when hist is shorter than generations ? 94 return lg > generations and (hist[-generations]-hist[-1]) <= 0 95 if not generations and fval == None: return True 96 return abs(inst.bestEnergy-fval) <= abs(tolerance * fval) 121 if lg > generations and (hist[-generations]-hist[-1]) <= 0: 122 return doc 123 return "" 124 if not generations and fval == None: return doc 125 if abs(inst.bestEnergy-fval) <= abs(tolerance * fval): return doc 126 return "" 127 _NormalizedCostTarget.__doc__ = doc 97 128 return _NormalizedCostTarget 98 129 … … 103 134 104 135 cost[-g] - cost[-1] <= gtol, where g=generations *or* cost[-1] <= ftol.""" 136 doc = "VTRChangeOverGeneration with %s" % {'ftol':ftol, 'gtol':gtol, 137 'generations':generations, 138 'target':target} 105 139 def _VTRChangeOverGeneration(inst): 106 140 hist = inst.energy_history 107 141 lg = len(hist) 108 142 #XXX: throws error when hist is shorter than generations ? 109 return (lg > generations and (hist[-generations]-hist[-1]) <= gtol)\ 110 or ( abs(hist[-1] - target) <= ftol ) 143 if (lg > generations and (hist[-generations]-hist[-1]) <= gtol)\ 144 or ( abs(hist[-1] - target) <= ftol ): return doc 145 return "" 146 _VTRChangeOverGeneration.__doc__ = doc 111 147 return _VTRChangeOverGeneration 112 148 … … 115 151 116 152 abs(params - params[0]) <= tolerance""" 153 doc = "PopulationSpread with %s" % {'tolerance':tolerance} 117 154 def _PopulationSpread(inst): 118 155 sim = numpy.array(inst.population) … … 120 157 # print "Warning: Invalid termination condition (nPop < 2)" 121 158 # return True 122 return numpy.all(abs(sim - sim[0]) <= abs(tolerance * sim[0])) 159 if numpy.all(abs(sim - sim[0]) <= abs(tolerance * sim[0])): return doc 160 return "" 161 _PopulationSpread.__doc__ = doc 123 162 return _PopulationSpread 124 163 … … 127 166 128 167 sum( abs(gradient)**norm )**(1.0/norm) <= tolerance""" 168 doc = "GradientNormTolerance with %s" % {'tolerance':tolerance, 'norm':norm} 129 169 def _GradientNormTolerance(inst): 130 170 try: 131 171 gfk = inst.gfk #XXX: need to ensure that gfk is an array ? 132 172 except: 133 print "Warning: Invalid termination condition (no gradient)" 134 return True 173 warn = "Warning: Invalid termination condition (no gradient)" 174 print warn 175 return warn 135 176 if norm == Inf: 136 177 gnorm = numpy.amax(abs(gfk)) … … 141 182 #XXX: as norm < -large, gnorm approaches amin(abs(gfk)) --> then -inf 142 183 gnorm = numpy.sum(abs(gfk)**norm,axis=0)**(1.0/norm) 143 return gnorm <= tolerance 184 if gnorm <= tolerance: return doc 185 return "" 186 _GradientNormTolerance.__doc__ = doc 144 187 return _GradientNormTolerance 145 188 -
mystic/mystic/tools.py
r470 r536 251 251 from monitors import Null 252 252 253 def isNull(mon): 254 if isinstance(mon, Null): # is Null() 255 return True 256 if mon == Null: # is Null 257 return True 258 return False 259 253 260 254 261 if __name__=='__main__':
Note: See TracChangeset
for help on using the changeset viewer.