Changeset 1168

Show
Ignore:
Timestamp:
07/29/08 09:34:36 (4 months ago)
Author:
dmitrey.kroshko
Message:

some code cleanup + minor change for ralg

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openopt/scikits/openopt/Kernel/Function.py

    r1156 r1168  
    1414        assert len(args) == 0 
    1515        self.fun = fun 
     16 
     17        #TODO: modify for cases where output can be partial 
     18        self.evals = 0 
     19        self.same = 0 
     20 
    1621        for key, item in kwargs.iteritems(): 
    1722            #assert key in self.__allowedFields__ # TODO: make set comparison 
     
    4853 
    4954        if not hasattr(self, 'f_key_prev') or any(self.f_key_prev != key_to_compare): 
     55            self.evals += 1 
    5056#            if self.fun.__module__ == 'scikits.openopt.Kernel.Function': 
    5157#                setattr(fun, 'x', x) 
     
    5460            else: 
    5561                self.f_val_prev = self.fun(Input) 
     62        else: 
     63            self.same += 1 
     64 
    5665        self.f_key_prev = copy(key_to_compare) 
    5766        return deepcopy(self.f_val_prev) 
  • trunk/openopt/scikits/openopt/Kernel/Point.py

    r1158 r1168  
    178178                else: # f(newPoint) is not NaN 
    179179                    return True 
     180 
     181    def isFeas(self): 
     182        return self.__isFeas__() 
     183 
     184    def __isFeas__(self): 
     185        contol = self.p.contol 
     186        if hasattr(self, '_mr'): 
     187            if self._mr > contol: return False 
     188        else: 
     189            #TODO: simplify it! 
     190            #for fn in Residuals: (...) 
     191            if any(self.lb() > contol): return False 
     192            if any(self.ub() > contol): return False 
     193            if any(abs(self.beq()) > contol): return False 
     194            if any(self.b() > contol): return False 
     195            if any(abs(self.h()) > contol): return False 
     196            if any(self.c() > contol): return False 
     197        return True 
  • trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py

    r1158 r1168  
    4848        xPrevIter = x0.copy() 
    4949        startPoint = p.point(x) 
    50         PrevIterPoint = p.point(x) 
     50        prevIterPoint = p.point(x) 
    5151 
    5252        g = self.__getRalgDirection__(startPoint) 
    53  
    54 #        p.iterfcn(startPoint) 
    55 #        if p.istop: 
    56 #            p.xf = x 
    57 #            p.ff = f 
    58 #            #r.advanced.ralg.hs = hs; 
    59 #            return 
     53        if not any(g): 
     54            p.istop = SMALL_DF 
     55            p.msg = '|| gradient F(X[k]) || < gradtol' 
     56            return 
    6057 
    6158        g2 = copy(g) 
    62         if p.isFeas(x): 
    63             constrGradPrevIter = zeros(p.n) 
    64             dfPrevIter = g2.copy() 
    65         else: 
    66             constrGradPrevIter = g2.copy() 
    67             dfPrevIter = zeros(p.n) 
    6859 
    6960#        #pass-by-ref! not copy! 
     
    9889                #if not self.checkTurnByGradient: 
    9990                if newPoint.betterThan(oldPoint): 
    100                     #TODO: handle possible noise here 
    101 #                    if ls>1500: 
    102 #                        if oldPoint.f() >=  newPoint.f()  and max(oldPoint.mr(), p.contol) >=  newPoint.mr(): 
    103 #                            oldPoint, newPoint = newPoint,  None 
    104 #                            continue 
    105 #                        else: 
    106 #                            doDilation = False 
    107 #                            # and go to label "Line Search Finished" 
    108 #                    else: 
     91                    #TODO: 1. handle possible noise here; 2. handle the case 
     92#                    if ls>15: ... 
    10993                    oldPoint, newPoint = newPoint,  None 
    110                     continue 
    111  
    112                 #Label "Line Search Finished" 
    113                 break 
     94                else: 
     95                    break 
    11496 
    11597            if ls >= p.maxLineSearch-1: 
     
    118100 
    119101            iterPoint  = newPoint 
    120             x = iterPoint.x 
     102            x = iterPoint.x.copy() 
    121103 
    122104            doBackwardSearch = 1 
     
    150132                    iterPoint = PrevPoint 
    151133 
    152             x = newPoint.x.copy() 
     134            x = iterPoint.x.copy() 
    153135 
    154136            if ls <= 0: hs *= q1 
    155137 
    156138            prevIterPointIsFeasible = p.iterValues.r[-1] <= p.contol # p.iterValues.r is max residual defined in ooIter.py 
    157             currIterPointIsFeasible = p.isFeas(x
     139            currIterPointIsFeasible = iterPoint.isFeas(
    158140 
    159141            if itn == 0: g2 = self.__getRalgDirection__(newPoint) 
     142 
     143            # set dilation direction: 
    160144            if prevIterPointIsFeasible == currIterPointIsFeasible == True: 
    161145                g1 = g2 - g 
    162146            elif prevIterPointIsFeasible == currIterPointIsFeasible == False: 
    163                 if not any(constrGradPrevIter-g2): 
    164                     g1 = zeros(p.n) 
    165                     if all(isfinite(dfPrevIter)): 
    166                         g1 = -p.df(xPrevIter) 
    167                     df = p.df(x) 
    168                     if sum(p.dotmult(p.df(xPrevIter), df))>0: 
    169                         doDilation = False 
    170                     if all(isfinite(df)): 
    171                         g1 += df 
    172                 else: 
    173                     g1 = g2-constrGradPrevIter 
    174                     if sum(p.dotmult(constrGradPrevIter, g2))>0: 
    175                         #TODO: add debugmsg here 
    176                         doDilation = False 
     147                #doDilation = False 
     148                g1 = g2-g 
     149                if sum(p.dotmult(g, g2))>0: 
     150                    p.debugmsg('warning! same gradient for constraints!') 
     151                    doDilation = False 
    177152            elif prevIterPointIsFeasible: 
    178153                g1 = g2.copy() 
    179154            else: 
    180                 #doDilation = False 
    181155                g1 = -g.copy() 
    182156 
     
    198172                p.debugmsg('matrix B restoration in ralg solver') 
    199173                b = diag(ones(n)) 
    200                 hs = p.norm(xPrevIter - x) 
    201                 #hs = max(p.norm(xPrevIter - x), hsmin) 
    202  
     174                hs = p.norm(xPrevIter - iterPoint.x) 
     175 
     176            cond_same_point = all(iterPoint.x == p.xk) 
    203177            p.iterfcn(iterPoint) 
     178            if cond_same_point and not p.istop: 
     179                p.istop = SMALL_DELTA_X 
     180                p.msg = '|| X[k] - X[k-1] || < xtol' 
     181                p.stopdict[SMALL_DELTA_X] = True 
    204182 
    205183            s2 = 0 
     
    212190                    p.stopdict.pop(SMALL_DELTA_F) 
    213191                if p.stopdict.has_key(SMALL_DELTA_X): 
    214                     #if currIterPointIsFeasible or not prevIterPointIsFeasible: s2 = p.istop 
    215                     if currIterPointIsFeasible or not prevIterPointIsFeasible: s2 = p.istop 
     192                    if currIterPointIsFeasible or not prevIterPointIsFeasible or cond_same_point: s2 = p.istop 
    216193                    p.stopdict.pop(SMALL_DELTA_X) 
    217194                if not s2 and any(p.stopdict.values()): 
     
    229206 
    230207            g = g2.copy() 
    231             xPrevIter = x.copy() 
    232             if currIterPointIsFeasible: 
    233                 constrGradPrevIter = zeros(p.n) 
    234                 dfPrevIter = g2.copy() 
    235             else: 
    236                 constrGradPrevIter = g2.copy() 
    237                 dfPrevIter =zeros(p.n)# p.df(x) 
     208 
     209            xPrevIter = iterPoint.x.copy() 
     210 
     211            prevIterPoint, iterPoint = iterPoint, None 
     212 
    238213 
    239214    def __getRalgDirection__(self, point):