Changeset 1049

Show
Ignore:
Timestamp:
06/21/08 15:17:50 (2 months ago)
Author:
dmitrey.kroshko
Message:

Point object can be used in iterfcn instead of args xk, fk, rk etc. + minor changes.

Files:

Legend:

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

    r1004 r1049  
    11__docformat__ = "restructuredtext en" 
    2 from numpy import atleast_1d,  all, asarray 
     2from numpy import atleast_1d,  all, asarray, ndarray 
    33 
    44class BaseAlg: 
     
    2727        xArg,  fArg,  rArg = True,  True,  True 
    2828 
    29         if len(args)>0: p.xk = args[0] 
    30         elif kwargs.has_key('xk'): p.xk = kwargs['xk'] 
    31         else: xArg = False 
     29        if len(args) == 1 and type(args[0]) != ndarray: # hence Point 
     30            point = args[0] 
     31            p.xk, p.fk, p.rk = point.x, point.f(), point.mr() 
     32        else: 
     33            if len(args)>0: p.xk = args[0] 
     34            elif kwargs.has_key('xk'): p.xk = kwargs['xk'] 
     35            else: xArg = False 
    3236 
    33         if len(args)>1: p.fk = args[1] 
    34         elif kwargs.has_key('fk'): p.fk = kwargs['fk'] 
    35         else: fArg = False 
     37            if len(args)>1: p.fk = args[1] 
     38            elif kwargs.has_key('fk'): p.fk = kwargs['fk'] 
     39            else: fArg = False 
    3640 
    37         if len(args)>2: p.rk = args[2] 
    38         elif kwargs.has_key('rk'): p.rk = kwargs['rk'] 
    39         else: rArg = False 
     41            if len(args)>2: p.rk = args[2] 
     42            elif kwargs.has_key('rk'): p.rk = kwargs['rk'] 
     43            else: rArg = False 
    4044 
    4145        #TODO: handle kwargs correctly! (decodeIterFcnArgs) 
  • trunk/openopt/scikits/openopt/Kernel/Point.py

    r1022 r1049  
    11# created by Dmitrey 
    2 from numpy import copy, isnan, array, argmax, abs 
     2from numpy import copy, isnan, array, argmax, abs, zeros 
    33__docformat__ = "restructuredtext en" 
    44empty_arr = array(()) 
     
    102102 
    103103    def __dmr(self, retAll = False): 
    104         g = zeros(self.p.n) 
    105         maxResidual, resType, ind = self.mr(retAll=True) 
    106         if resType == 'lb': 
    107             g[ind] -= 1 # N * (-1), -1 = dConstr/dx = d(lb-x)/dx 
    108         elif resType == 'ub': 
    109             g[ind] += 1 # N * (+1), +1 = dConstr/dx = d(x-ub)/dx 
    110         elif resType == 'A': 
    111             g += self.A[ind] 
    112         elif resType == 'Aeq': 
    113             rr = self.p.matmult(self.p.Aeq[ind], self.x)-self.p.beq[ind] 
    114             if rr < 0:  g -= self.p.Aeq[ind] 
    115             else:  g += self.p.Aeq[ind] 
    116         elif resType == 'c': 
    117             dc = self.p.dc(self.x, ind=ind).flatten() 
    118             g += dc 
    119         elif resType == 'h': 
    120             dh = self.p.dh(self.x, ind=ind).flatten() 
    121             if self.p.h(self.x, ind=ind) < 0:  g -= dh#CHECKME!! 
    122             else: g += dh#CHECKME!! 
     104        if not hasattr(self, '_dmr'): 
     105            g = zeros(self.p.n) 
     106            maxResidual, resType, ind = self.mr(retAll=True) 
     107            if resType == 'lb': 
     108                g[ind] -= 1 # N * (-1), -1 = dConstr/dx = d(lb-x)/dx 
     109            elif resType == 'ub': 
     110                g[ind] += 1 # N * (+1), +1 = dConstr/dx = d(x-ub)/dx 
     111            elif resType == 'A': 
     112                g += self.A[ind] 
     113            elif resType == 'Aeq': 
     114                rr = self.p.matmult(self.p.Aeq[ind], self.x)-self.p.beq[ind] 
     115                if rr < 0:  g -= self.p.Aeq[ind] 
     116                else:  g += self.p.Aeq[ind] 
     117            elif resType == 'c': 
     118                dc = self.p.dc(self.x, ind=ind).flatten() 
     119                g += dc 
     120            elif resType == 'h': 
     121                dh = self.p.dh(self.x, ind=ind).flatten() 
     122                if self.p.h(self.x, ind=ind) < 0:  g -= dh#CHECKME!! 
     123                else: g += dh#CHECKME!! 
     124        self._dmr, self._dmrName,  self._dmrInd = g, resType, ind 
    123125        if retAll: 
    124             return g,  resType,  ind 
     126            return copy(self._dmr),  resType,  ind 
    125127        else: 
    126             return g 
     128            return copy(self._dmr) 
    127129 
    128130    def betterThan(self, *args, **kwargs): 
  • trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py

    r1041 r1049  
    4646        ############################################# 
    4747 
     48        startPoint = p.point(p.x0) 
    4849        x = x0.copy() 
    4950        xPrevIter = x0.copy() 
    50         xf = x0.copy() 
    51         xk = x0.copy() 
    52         p.xk = x0.copy() 
    53  
    54         f = p.f(x) 
    55  
    56         fk = f 
    57         ff = f 
    58         p.fk = fk 
    59  
    60         g = self.__getRalgDirection__(x, p) 
     51        #p.xk = x0.copy() 
     52        #f = startPoint.f() 
     53        #p.fk = f 
     54 
     55        g = self.__getRalgDirection__(startPoint) 
    6156        p._df = copy(g) 
    6257 
    63         p.iterfcn(
     58        p.iterfcn(startPoint
    6459        if p.istop: 
    6560            p.xf = x 
     
    6863            return 
    6964 
    70         g2 = self.__getRalgDirection__(x, p
     65        g2 = copy(g
    7166        if p.rk <= p.contol: 
    7267            constrGradPrevIter = zeros(p.n) 
     
    163158            currIterPointIsFeasible = p.isFeas(x) 
    164159 
    165             g2 = self.__getRalgDirection__(x, p
     160            g2 = self.__getRalgDirection__(iterPoint
    166161            if prevIterPointIsFeasible == currIterPointIsFeasible == True: 
    167162                g1 = g2 - g 
     
    240235                dfPrevIter =zeros(p.n)# p.df(x) 
    241236 
    242     def __getRalgDirection55__(self, x, p): 
    243         maxRes = p.getMaxResidual(x) 
    244         if maxRes > p.contol: 
    245             d = -getConstrDirection(p, x, 1e-7) 
    246         else: 
    247             d = p.df(x) 
    248         return d 
    249  
    250     def __getRalgDirection__(self, x, p): 
    251         maxRes = p.getMaxResidual(x) 
    252         if maxRes > p.contol: 
     237    def __getRalgDirection__(self, point): 
     238        maxRes = point.mr() 
     239        if maxRes > point.p.contol: 
     240            dmr = point.dmr() 
    253241            if self.S == 0: 
    254                 d = p.getMaxConstrGradient(x) 
    255             else: 
    256                 d = p.df(x) + self.S*p.getMaxConstrGradient(x) 
    257         else: 
    258             d = p.df(x
     242                d = dmr 
     243            else: 
     244                d = point.df() + self.S*dmr 
     245        else: 
     246            d = point.df(
    259247        return d 
    260248