Changeset 1176

Show
Ignore:
Timestamp:
08/03/08 07:47:22 (4 months ago)
Author:
dmitrey.kroshko
Message:

some changes for ralg + some code cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py

    r1174 r1176  
    2222    showLS = False 
    2323    show_nnan = False 
     24    doBackwardSearch = 1 
    2425    needRej = lambda self, p, b, g_dilated, g: norm(g_dilated)  < 1e-10*norm(g) 
    2526    #checkTurnByGradient = True 
     
    4546        x0 = atleast_1d(T(copy(p.x0))) 
    4647 
    47         ############################################# 
    48         # Shor r-alg engine 
    49         ############################################# 
     48 
     49        """                            Shor r-alg engine                           """ 
     50 
    5051        x = x0.copy() 
    51         xPrevIter = x0.copy() 
    5252        iterPoint = p.point(x) 
    5353        prevIterPoint = p.point(x) 
     
    6565#        else: b = B_constr 
    6666 
    67         for itn in xrange(int(p.maxIter+2)): 
     67 
     68        """                         Ralg main cycle                                   """ 
     69 
     70        for itn in xrange(p.maxIter+2): 
    6871            doDilation = True 
    6972 
     
    7578 
    7679            x_0 = x.copy() 
    77             doBackwardSearch = 1 
     80 
     81            """                           Forward line search                          """ 
     82 
    7883            for ls in xrange(p.maxLineSearch): 
    7984                ls1 += 1 
     
    101106                    break 
    102107 
    103             if ls >= p.maxLineSearch-1: 
     108            if ls == p.maxLineSearch-1: 
    104109                p.istop,  p.msg = IS_LINE_SEARCH_FAILED,  'maxLineSearch (' + str(p.maxLineSearch) + ') has been exceeded' 
    105110                return 
    106111 
    107112            iterPoint  = newPoint 
     113 
     114            if itn != 0: g2 = self.__getRalgDirection__(iterPoint) 
     115 
     116            """                          Backward line search                          """ 
    108117            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                     """ 
    139146 
    140147            x = iterPoint.x.copy() 
    141  
    142148            if ls <= 0: hs *= q1 
    143  
    144             prevIterPointIsFeasible = p.iterValues.r[-1] <= p.contol # p.iterValues.r is max residual defined in ooIter.py 
    145             currIterPointIsFeasible = iterPoint.isFeas() 
    146  
    147149            if itn == 0: 
    148150                g2 = self.__getRalgDirection__(newPoint) 
    149151                p.debugmsg('hs: ' + str(hs)) 
    150152                p.debugmsg('ls: ' + str(ls)) 
    151  
    152153            if self.showLS: p.info('ls: ' + str(ls)) 
    153154            if self.show_nnan: p.info('nnan: ' + str(iterPoint.__nnan__())) 
    154155 
    155             # set dilation direction: 
     156 
     157            """                         Set dilation direction                            """ 
     158 
    156159            if sum(p.dotmult(g, g2))>0: 
    157160                p.debugmsg('ralg warning: slope angle less than pi/2. Mb dilation for the iter will be omitted.') 
    158161                #doDilation = False 
     162 
     163            prevIterPointIsFeasible = prevIterPoint.isFeas() 
     164            currIterPointIsFeasible = iterPoint.isFeas() 
     165 
    159166            if prevIterPointIsFeasible == currIterPointIsFeasible == True: 
    160167                g1 = g2 - g 
    161168            elif prevIterPointIsFeasible == currIterPointIsFeasible == False: 
    162                 #doDilation = False 
    163169                g1 = g2-g 
    164170            elif prevIterPointIsFeasible: 
    165171                g1 = g2.copy() 
    166172            else: 
    167                 g1 = -g.copy() 
     173                g1 = -g.copy() # signum doesn't matter here 
    168174 
    169175#            #pass-by-ref! not copy! 
    170176#            if currIterPointIsFeasible: b = B_f 
    171177#            else: b = B_constr 
     178 
     179 
     180            """                             Perform dilation                               """ 
    172181 
    173182            g = self.__economyMult__(b.T, g1) 
     
    184193                p.debugmsg('matrix B restoration in ralg solver') 
    185194                b = diag(ones(n)) 
    186                 hs = p.norm(xPrevIter - iterPoint.x) 
     195                hs = p.norm(prevIterPoint.x - iterPoint.x) 
    187196 
    188197            cond_same_point = all(iterPoint.x == p.xk) 
     198 
     199 
     200            """                               Call OO iterfcn                                """ 
     201 
    189202            p.iterfcn(iterPoint) 
    190203            if cond_same_point and not p.istop: 
     
    192205                p.msg = '|| X[k] - X[k-1] || < xtol' 
    193206                p.stopdict[SMALL_DELTA_X] = True 
     207 
     208 
     209            """                             Check stop criteria                           """ 
    194210 
    195211            s2 = 0 
     
    215231                p.istop = s2 
    216232 
     233 
     234            """                                If stop required                                """ 
     235 
    217236            if p.istop: 
    218237                if newPoint.betterThan(oldPoint): optimIterPoint = newPoint 
     
    221240                return 
    222241 
     242 
     243            """                Some final things for ralg main cycle                """ 
     244 
    223245            g = g2.copy() 
    224  
    225             xPrevIter = iterPoint.x.copy() 
    226  
    227246            prevIterPoint, iterPoint = iterPoint, None 
    228247