Changeset 560 for branches/collapse/collapse_termination.py
- Timestamp:
- 09/14/12 16:42:47 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/collapse/collapse_termination.py
r541 r560 1 debug = True #False 2 3 def CLPS_Weight(npts, tolW=0.05, tolG=10e-6, generations=70): 1 debug = False #True 2 3 def CLPS_Weight(npts, tol_weight=0.05, generations=200, axisW = '', \ 4 indexW = ''): 4 5 """ Termination condition for vanishing weights. 5 6 if info = False: … … 7 8 if info = True: 8 9 prints out a message saying for which points the weight vanished 10 11 In this code, weight collapse is the event where the weight of a support 12 point (its maximum accros last given number of generations) has been 13 less than or equal to the tollerance level, tol_weight. 14 15 If axisW and index are not declared, the function returns whenever weight 16 has vanished for any point. If declared (axisW and index are tuple or 17 integer objects) then the function returns only if collapse had occured f 18 or points with given indices. Note that neither or both axisW and indexW 19 must be declared, otherwise an error is printed. 9 20 10 21 npts = (nx, ny, ...) is the tuple of the support point numbers for each 11 22 input.""" 23 # from mystic.tools import isNull 24 from mystic.monitors import Null, Monitor, VerboseMonitor 25 monitor = Monitor() 26 if debug: monitor = VerboseMonitor(2, 2) 27 28 29 def _CLPS_Weight(inst, info=False): 30 ###FIXME: this should probably be in Solver ####### 31 # if isNull(inst._stepmon): inst._stepmon = monitor 32 ################################################### 33 34 ######################## TO BE DELETED################################## 35 #print "HERE I AM and the last 10 best solutions are: " 36 #print inst._stepmon.x[-10:] 37 ######################################################################## 38 39 ############## PARAMETERS (length, and number of emasures) ############# 40 hist_lg = len(inst._stepmon.x) 41 if hist_lg <= generations: return False 42 43 from mystic.math.measures import _nested_split 44 w_bestSolution, x_bestSolution = \ 45 _nested_split(inst.bestSolution, npts) 46 ######################################################################## 47 collapseWeight_msg = '' 48 49 if isinstance(axisW, int) and isinstance(indexW, int): 50 max_weight = 0 51 for n in range(-generations, 0): 52 weights, positions = _nested_split(inst._stepmon.x[n], npts) 53 if max_weight < weights[axisW][indexW]: 54 max_weight = weights[axisW][indexW] 55 if max_weight <= tol_weight: 56 collapseWeight_msg += 'measure: %s & index: %s, ' \ 57 %(axisW,indexW) 58 59 elif isinstance(axisW, tuple) and isinstance(indexW, int): 60 max_weight = [0]*len(axisW) 61 62 for n in range(-generations, 0): 63 weights, positions = _nested_split(inst._stepmon.x[n], npts) 64 for i in range(len(max_weight)): 65 if max_weight[i] < weights[axisW[i]][indexW]: 66 max_weight[i] = weights[axisW[i]][indexW] 67 for k in range(len(max_weight)): 68 if max_weight[k] <= tol_weight: 69 collapseWeight_msg += 'measure: %s & index: %s, ' \ 70 %(axisW[k],indexW) 71 72 elif isinstance(axisW, int) and isinstance(indexW, tuple): 73 max_weight = [0]*len(indexW) 74 75 for n in range(-generations, 0): 76 weights, positions = _nested_split(inst._stepmon.x[n], npts) 77 for i in range(len(max_weight)): 78 if max_weight[i] < weights[axisW][indexW[i]]: 79 max_weight[i] = weights[axisW][indexW[i]] 80 for k in range(len(max_weight)): 81 if max_weight[k] <= tol_weight: 82 collapseWeight_msg += 'measure: %s & index: %s, ' \ 83 %(axisW,indexW[k]) 84 85 elif (isinstance(axisW, tuple) and isinstance(indexW, tuple)) or \ 86 (axisW == '' and indexW == ''): #Note that can't change to not axisW 87 # and not indexW since those can be zero indices 88 if (axisW == '' and indexW == ''): 89 max_weight = [[0] * nx for nx in npts] 90 else: 91 max_weight = [[0]*len(axisW)]*len(indexW) 92 93 for n in range(-generations, 0): 94 weights, positions = _nested_split(inst._stepmon.x[n], npts) 95 96 for i in range(len(max_weight)): 97 for j in range(len(max_weight[i])): 98 if (axisW =='' and indexW == ''): 99 if max_weight[i][j] < weights[i][j]: 100 max_weight[i][j] = weights[i][j] 101 else: 102 if max_weight[i][j] < weights[axisW[i]][index[j]]: 103 max_weight[i][j] = weights[axisW[i]][index[j]] 104 105 for k in range(len(max_weight)): 106 for l in range(len(max_weight[k])): 107 if max_weight[k][l] <= tol_weight: 108 if (axisW == '' and indexW == ''): 109 collapseWeight_msg += 'measure: %s & index: %s, ' \ 110 %(k,l) 111 else: 112 collapseWeight_msg += 'measure: %s & index: %s, ' \ 113 %(axisW[k],indexW[l]) 114 115 else: 116 print 'Error, wrong arguments of CLPS_Weight' 117 return False 118 119 if info: 120 if collapseWeight_msg == '': msg = '' 121 else: 122 msg0 = 'CLPS_Weight with: ' 123 msg = msg0 + collapseWeight_msg 124 msg2 = msg0 + '\n npts = %s, tol_weight = %s, generations = %s: \n' \ 125 % (npts, tol_weight, generations) 126 msg2 += 'vanishing weights of support points: ' 127 msg2 += collapseWeight_msg 128 if debug: print msg2 129 return msg 130 131 if collapseWeight_msg == '': return False 132 else: return True 133 134 return _CLPS_Weight 135 136 137 138 def CLPS_Position(npts, tol_position, generations=200, axisP = '', \ 139 index1 = '', index2 = ''): 140 141 """ Termination condition for collapsing positions of support points. 142 if info = False: 143 returns true if the positions collapse for any 2 support 144 points in a measure 145 if info = True: 146 prints out a message saying for which point-pairs the location collapsed. 147 148 In this code, collapse is the event where the distance between 2 support 149 points (its maximum accros last given number of generations) has been 150 less than the tollerance level, tol_position. 151 152 If axisP and index1 and index2 are not declared, the function returns 153 whenever any 2 points collapse in position. If declared (axisP is a tuple 154 or and integer, index1 and index2 are integers) then the function returns 155 only if collapse had occured for points with given indices. 156 157 npts = (nx, ny, ...) is the tuple of the support point numbers for each 158 input.""" 159 12 160 from mystic.tools import isNull 13 161 from mystic.monitors import Null, Monitor, VerboseMonitor 14 162 monitor = Monitor() 15 if debug: monitor = VerboseMonitor(2) 16 17 def _CLPS_Weight(inst, info=False): 163 if debug: monitor = VerboseMonitor(2, 2) 164 165 # CHECK THAT index1, and index2 are integers if declared, and either none or 166 # both are empty 167 168 def _CLPS_Position(inst, info=False): 18 169 ###FIXME: this should probably be in Solver ####### 19 170 if isNull(inst._stepmon): inst._stepmon = monitor 20 171 ################################################### 21 172 22 lg = len(inst._stepmon) 23 if lg <= generations: return False 24 25 from numpy import array, ones, transpose 26 from mystic.math.dirac_measure import product_measure 27 28 Last_bestSolution = product_measure() 29 Last_bestSolution.load(inst._stepmon.x[-1], npts) 30 31 Gen_bestSolution = product_measure() 32 Gen_bestSolution.load(inst._stepmon.x[-generations], npts) 33 34 dim = len(Last_bestSolution) # Number of measures 35 collapseWeight = '' 36 173 ############## PARAMETERS (length, and number of emasures) ############# 174 hist_lg = len(inst._stepmon) 175 if hist_lg <= generations: return False 176 177 from mystic.math.measures import _nested_split 178 w_bestSolution, x_bestSolution = \ 179 _nested_split(inst.bestSolution, npts) 180 181 if axisP == '': 182 dim = len(w_bestSolution) # Number of measures 183 axis = range(dim) 184 else: 185 axis = axisP 186 if isinstance( axisP, int ): dim = 1 187 else: dim = len(list(axisP)) 188 189 ######################################################################## 190 collapsePairs_msg = '' 191 192 from numpy import array, zeros, ones, transpose 37 193 for i in range(dim): 38 Last_collapseWeight = array(Last_bestSolution[i].weights) <= tolW 39 Gen_collapseWeight = array(Gen_bestSolution[i].weights) <= tolW 40 for k in range(len(Last_collapseWeight)): 41 if all([Last_collapseWeight[k], Gen_collapseWeight[k]]): 42 collapseWeight += ('measure %s: index: %s; ' %(i,k)) 194 if isinstance(axis, int): ax_idx = axis 195 else: ax_idx = axis[i] 196 197 if isinstance(index1, int): 198 if isinstance(index2, int): 199 nx = 1 200 max_measure_diffLoc = 0 201 else: 202 print "Error argument declaration index2 is \ 203 empty while index1 is not" 204 return False #FIXME 205 else: 206 if isinstance(index2, int): 207 print "Error argument declaration index1 is \ 208 empty while index2 is not" 209 return False #FIXME 210 else: 211 nx = npts[ax_idx] 212 max_measure_diffLoc = zeros((nx, nx)) 213 214 for n in range(-generations, 0): 215 weights, positions = _nested_split(inst._stepmon.x[n], npts) 216 if nx == 1: 217 diffLoc = abs(positions[ax_idx][index1] - \ 218 positions[ax_idx][index2]) 219 if max_measure_diffLoc < diffLoc: 220 max_measure_diffLoc= diffLoc 221 else: 222 diffLoc = abs(transpose(positions[ax_idx]*ones((nx, nx))) - \ 223 positions[ax_idx]*ones((nx, nx))) 224 for k in range(nx): 225 for l in range(nx): 226 if max_measure_diffLoc[(k, l)] < diffLoc[(k, l)]: 227 max_measure_diffLoc[(k, l)] = diffLoc[(k, l)] 228 229 if nx ==1 and max_measure_diffLoc <= tol_position[ax_idx]: 230 collapsePairs_msg += ('measure: %s & indices: (%s,%s),'\ 231 %(ax_idx,index1,index2)) 232 233 else: 234 for k in range(nx): 235 for l in range(k,nx): 236 if k != l and max_measure_diffLoc[(k,l)] <= \ 237 tol_position[ax_idx]: 238 collapsePairs_msg += ('measure: %s & indices: (%s,%s),' \ 239 %(ax_idx,k,l)) 240 43 241 if info: 44 if collapseWeight == '': msg = '' 45 else: 46 msg = 'CLPS_Weight:\n' 47 msg += 'npts = %s, tolW = %s, tolG = %s, generations = %s;\n' \ 48 % (npts, tolW, tolG, generations) 49 msg += 'vanishing weights of support points: ' 50 msg += collapseWeight 51 #if debug: print msg 242 if collapsePairs_msg == '': msg = '' 243 else: 244 msg0 = 'CLPS_Position with: ' 245 msg = msg0 + collapsePairs_msg 246 msg2 = msg0 + '\n npts = %s, tol_position = %s, generations = %s:\n' \ 247 % (npts, tol_position, generations) 248 msg2 += 'collapse of support points: ' 249 msg2 += collapsePairs_msg 250 if debug: print msg2 52 251 return msg 53 54 if collapse Weight== '': return False252 253 if collapsePairs_msg == '': return False 55 254 else: return True 56 255 57 return _CLPS_Weight 58 59 60 61 62 def CLPS_Location(npts, tolL, tolG=10e-6, generations=30): 63 64 """ Termination condition for collapsing locations of support points. 65 if info = False: 66 returns true if the locations collapse for any 2 support 67 points in a measure 68 if info = True: 69 prints out a message saying for which point-pairs the location collapsed. 70 71 npts = (nx, ny, ...) is the tuple of the support point numbers for each 72 input.""" 73 from mystic.tools import isNull 74 from mystic.monitors import Null, Monitor, VerboseMonitor 75 monitor = Monitor() 76 if debug: monitor = VerboseMonitor(2) 77 78 def _CLPS_Location(inst, info=False): 79 ###FIXME: this should probably be in Solver ####### 80 if isNull(inst._stepmon): inst._stepmon = monitor 81 ################################################### 82 83 lg = len(inst._stepmon) 84 if lg <= generations: return False 85 86 from numpy import array, ones, transpose 87 from mystic.math.dirac_measure import product_measure 88 89 Last_bestSolution = product_measure() 90 Last_bestSolution.load(inst._stepmon.x[-1], npts) 91 92 Gen_bestSolution = product_measure() 93 Gen_bestSolution.load(inst._stepmon.x[-generations], npts) 94 95 dim = len(Last_bestSolution) # Number of measures 96 collapsePairs = '' 97 98 for i in range(dim): 99 x = array(Last_bestSolution[i].coords) 100 Last_DiffLoc = transpose(x*ones((len(x), len(x)))) - \ 101 x*ones((len(x), len(x))) 102 103 y = array(Gen_bestSolution[i].coords) 104 105 Gen_DiffLoc = transpose(y*ones((len(y), len(y)))) - \ 106 y*ones((len(y), len(y))) 107 108 collapseLoc1 = (abs(Last_DiffLoc) <= tolL[i]) 109 collapseLoc2 = (abs(Gen_DiffLoc) <= tolL[i]) 110 111 for k in range(len(x)): 112 for l in range(len(x)): 113 if k != l and all([collapseLoc1[(k,l)], \ 114 collapseLoc2[(k, l)]]): 115 collapsePairs += ('measure %s: indices (%s,%s); ' \ 116 %(i,k, l)) 117 118 if info: 119 if collapsePairs == '': msg = '' 120 else: 121 msg = 'CLPS_Location:\n' 122 msg += 'npts = %s, tolL = %s, tolG = %s, generations = %s;\n' \ 123 % (npts, tolL, tolG, generations) 124 msg += 'collapse of support points: ' 125 msg += collapsePairs 126 #if debug: print msg 127 return msg 128 129 if collapsePairs == '': return False 130 else: return True 131 132 return _CLPS_Location 133 134 256 return _CLPS_Position 257 258
Note: See TracChangeset
for help on using the changeset viewer.