Changeset 1176
- Timestamp:
- 08/03/08 07:47:22 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py
r1174 r1176 22 22 showLS = False 23 23 show_nnan = False 24 doBackwardSearch = 1 24 25 needRej = lambda self, p, b, g_dilated, g: norm(g_dilated) < 1e-10*norm(g) 25 26 #checkTurnByGradient = True … … 45 46 x0 = atleast_1d(T(copy(p.x0))) 46 47 47 ############################################# 48 # Shor r-alg engine49 ############################################# 48 49 """ Shor r-alg engine """ 50 50 51 x = x0.copy() 51 xPrevIter = x0.copy()52 52 iterPoint = p.point(x) 53 53 prevIterPoint = p.point(x) … … 65 65 # else: b = B_constr 66 66 67 for itn in xrange(int(p.maxIter+2)): 67 68 """ Ralg main cycle """ 69 70 for itn in xrange(p.maxIter+2): 68 71 doDilation = True 69 72 … … 75 78 76 79 x_0 = x.copy() 77 doBackwardSearch = 1 80 81 """ Forward line search """ 82 78 83 for ls in xrange(p.maxLineSearch): 79 84 ls1 += 1 … … 101 106 break 102 107 103 if ls >= p.maxLineSearch-1:108 if ls == p.maxLineSearch-1: 104 109 p.istop, p.msg = IS_LINE_SEARCH_FAILED, 'maxLineSearch (' + str(p.maxLineSearch) + ') has been exceeded' 105 110 return 106 111 107 112 iterPoint = newPoint 113 114 if itn != 0: g2 = self.__getRalgDirection__(iterPoint) 115 116 """ Backward line search """ 108 117 x = iterPoint.x.copy() 109 110 doBackwardSearch = 1 111 if itn != 0: g2 = self.__getRalgDirection__(newPoint) 112 113 if ls == 0 and doBackwardSearch: 114 if oldPoint.betterThan(newPoint): 115 PrevPoint = iterPoint 116 while 1: 117 hs *= self.hmult 118 hs_prev = hs 119 120 if itn == 0: 121 x = p.xk - hs * g1 122 else: 123 x = 0.5*x+0.5*xPrevIter 124 hs /= self.hmult 125 126 newPoint = p.point(x) 127 128 if PrevPoint.betterThan(newPoint) or newPoint.f() > PrevPoint.f() or abs(newPoint.f() - p.fk) < 15 * p.ftol or p.norm(newPoint.x - p.xk) < 15 * p.xtol: 129 iterPoint, hs = PrevPoint, hs_prev 130 break 131 132 PrevPoint = newPoint 133 134 ls -= 1 135 136 if itn != 0: 137 break 138 iterPoint = PrevPoint 118 if ls == 0 and self.doBackwardSearch: 119 PrevPoint = iterPoint 120 while 1: 121 hs *= self.hmult 122 hs_prev = hs 123 124 if itn == 0: 125 x = p.xk - hs * g1 126 else: 127 x = 0.5*x+0.5*prevIterPoint.x 128 hs /= self.hmult 129 130 newPoint = p.point(x) 131 132 if PrevPoint.betterThan(newPoint) or newPoint.f() > PrevPoint.f() or abs(newPoint.f() - p.fk) < 15 * p.ftol or p.norm(newPoint.x - p.xk) < 15 * p.xtol: 133 iterPoint, hs = PrevPoint, hs_prev 134 break 135 136 PrevPoint, newPoint = newPoint, None 137 138 ls -= 1 139 140 if itn != 0: 141 break 142 iterPoint = PrevPoint 143 144 145 """ iterPoint has been obtained """ 139 146 140 147 x = iterPoint.x.copy() 141 142 148 if ls <= 0: hs *= q1 143 144 prevIterPointIsFeasible = p.iterValues.r[-1] <= p.contol # p.iterValues.r is max residual defined in ooIter.py145 currIterPointIsFeasible = iterPoint.isFeas()146 147 149 if itn == 0: 148 150 g2 = self.__getRalgDirection__(newPoint) 149 151 p.debugmsg('hs: ' + str(hs)) 150 152 p.debugmsg('ls: ' + str(ls)) 151 152 153 if self.showLS: p.info('ls: ' + str(ls)) 153 154 if self.show_nnan: p.info('nnan: ' + str(iterPoint.__nnan__())) 154 155 155 # set dilation direction: 156 157 """ Set dilation direction """ 158 156 159 if sum(p.dotmult(g, g2))>0: 157 160 p.debugmsg('ralg warning: slope angle less than pi/2. Mb dilation for the iter will be omitted.') 158 161 #doDilation = False 162 163 prevIterPointIsFeasible = prevIterPoint.isFeas() 164 currIterPointIsFeasible = iterPoint.isFeas() 165 159 166 if prevIterPointIsFeasible == currIterPointIsFeasible == True: 160 167 g1 = g2 - g 161 168 elif prevIterPointIsFeasible == currIterPointIsFeasible == False: 162 #doDilation = False163 169 g1 = g2-g 164 170 elif prevIterPointIsFeasible: 165 171 g1 = g2.copy() 166 172 else: 167 g1 = -g.copy() 173 g1 = -g.copy() # signum doesn't matter here 168 174 169 175 # #pass-by-ref! not copy! 170 176 # if currIterPointIsFeasible: b = B_f 171 177 # else: b = B_constr 178 179 180 """ Perform dilation """ 172 181 173 182 g = self.__economyMult__(b.T, g1) … … 184 193 p.debugmsg('matrix B restoration in ralg solver') 185 194 b = diag(ones(n)) 186 hs = p.norm( xPrevIter- iterPoint.x)195 hs = p.norm(prevIterPoint.x - iterPoint.x) 187 196 188 197 cond_same_point = all(iterPoint.x == p.xk) 198 199 200 """ Call OO iterfcn """ 201 189 202 p.iterfcn(iterPoint) 190 203 if cond_same_point and not p.istop: … … 192 205 p.msg = '|| X[k] - X[k-1] || < xtol' 193 206 p.stopdict[SMALL_DELTA_X] = True 207 208 209 """ Check stop criteria """ 194 210 195 211 s2 = 0 … … 215 231 p.istop = s2 216 232 233 234 """ If stop required """ 235 217 236 if p.istop: 218 237 if newPoint.betterThan(oldPoint): optimIterPoint = newPoint … … 221 240 return 222 241 242 243 """ Some final things for ralg main cycle """ 244 223 245 g = g2.copy() 224 225 xPrevIter = iterPoint.x.copy()226 227 246 prevIterPoint, iterPoint = iterPoint, None 228 247
