Changeset 1504

Show
Ignore:
Timestamp:
09/27/08 15:04:17 (2 months ago)
Author:
dmitrey.kroshko
Message:

add converter nlsp2nlp

Files:

Legend:

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

    r1102 r1504  
    22from NLP import nlp_init 
    33from numpy.linalg import norm 
    4 from numpy import inf, asfarray, atleast_1d 
     4from numpy import inf, asfarray, atleast_1d, dot 
    55from setDefaultIterFuncs import FVAL_IS_ENOUGH 
     6import NLP 
     7#from Function import oofun 
    68 
    79class NLSP(NonLinProblem): 
     
    3032        return norm(atleast_1d(asfarray(fv)), inf) 
    3133 
     34    def nlsp2nlp(self, solver, **kwargs): 
     35#        FF = oofun(self.f) 
     36#        if hasattr(self, 'df') and self.df is not None: #TODO: replace by userSupplied 
     37#            FF.d = self.df 
     38        ff = lambda x: sum(asfarray(self.f(x))**2) 
     39        if hasattr(self, 'df'): 
     40            dff = lambda x: dot(2*asfarray(self.f(x)), asfarray(self.df(x))) 
     41            p = NLP.NLP(ff, self.x0, df=dff) 
     42        else: 
     43            p = NLP.NLP(ff, self.x0) 
     44        #p = NLP.NLP(FF, self.x0) 
     45        self.inspire(p) 
     46 
     47        def nlsp_iterfcn(p): 
     48            if all(abs(asfarray(self.f(p.xk)))) < p.ftol and self.getMaxResidual(p.xk) < p.contol: 
     49                if self.isUC: msg_contol = '' 
     50                else: msg_contol = 'and contol ' 
     51                self.msg = 'solution with required ftol ' + msg_contol+ 'has been reached' 
     52                return (15, self.msg) 
     53            else: 
     54                return False 
     55 
     56        p.callback = nlsp_iterfcn 
     57        p.goal = 'min' 
     58        r = p.solve(solver, **kwargs) 
     59        return r 
  • trunk/openopt/scikits/openopt/Kernel/Point.py

    r1383 r1504  
    177177 
    178178            if not self.p.isNaNInConstraintsAllowed: 
    179                 if oldPoint.__nnan__()  > self.__nnan__(): return True 
    180                 elif oldPoint.__nnan__()  < self.__nnan__(): return False 
     179                if oldPoint.__nNaNs__()  > self.__nNaNs__(): return True 
     180                elif oldPoint.__nNaNs__()  < self.__nNaNs__(): return False 
    181181                # TODO: check me 
    182                 if mr <= self.p.contol and oldPointResidual <= self.p.contol and self.__nnan__() != 0: return mr < oldPointResidual 
     182                if mr <= self.p.contol and oldPointResidual <= self.p.contol and self.__nNaNs__() != 0: return mr < oldPointResidual 
    183183 
    184184            if mr < oldPointResidual and self.p.contol < oldPointResidual: return True 
     
    216216        return True 
    217217 
    218     def __nnan__(self): 
     218    def __nNaNs__(self): 
    219219        # returns number of nans in constraints 
    220220        r = 0 
    221         c = self.c() 
    222         if any(isnan(c)): 
    223             r += len(where(isnan(c))[0]) 
    224         h = self.h() 
    225         if any(isnan(h)): 
    226             r += len(where(isnan(h))[0]) 
     221        c, h = self.c(), self.h() 
     222        r += len(where(isnan(c))[0]) 
     223        r += len(where(isnan(h))[0]) 
    227224        return r 
    228225 
  • trunk/openopt/scikits/openopt/examples/nlsp_1.py

    r925 r1504  
    99from numpy import asfarray, zeros, cos, sin 
    1010 
    11 #f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) 
    12 f = (lambda x: x[0]**3+x[1]**3-9, lambda x: x[0]-0.5*x[1], lambda x: cos(x[2])+x[0]-1.5) 
    13 # Python list, numpy.array are allowed as well:  
     11f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) 
     12# or: 
     13#f = (lambda x: x[0]**3+x[1]**3-9, lambda x: x[0]-0.5*x[1], lambda x: cos(x[2])+x[0]-1.5) 
     14# Python list, numpy.array are allowed as well: 
    1415#f = lambda x: [x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5] 
    1516#or f = lambda x: asfarray((x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5)) 
     
    2526    df[2,2] = -sin(x[2]) 
    2627    return df 
    27      
     28 
    2829x0 = [8,15, 80] 
    2930 
    30 #w/o gradient:  
     31#w/o gradient: 
    3132#p = NLSP(f, x0) 
    3233p = NLSP(f, x0, df = DF) 
     
    4344 
    4445#r = p.solve('scipy_fsolve') 
    45 r = p.solve('nssolve') 
     46#r = p.solve('nssolve') 
     47#or using converter nlsp2nlp, try to minimize sum(f_i(x)^2): 
     48r = p.solve('nlp:ralg') 
    4649 
    4750print 'solution:', r.xf 
  • trunk/openopt/scikits/openopt/examples/nlsp_constrained.py

    r1067 r1504  
    2121 
    2222# you can define f in several ways: 
    23 #f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) 
    24 f = (lambda x: x[0]**3+x[1]**3-9, lambda x: x[0]-0.5*x[1], lambda x: cos(x[2])+x[0]-1.5) 
     23f = lambda x: (x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5) 
     24#f = (lambda x: x[0]**3+x[1]**3-9, lambda x: x[0]-0.5*x[1], lambda x: cos(x[2])+x[0]-1.5) 
    2525# Python list, numpy.array are allowed as well: 
    2626#f = lambda x: [x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5] 
     
    6565#p.checkdc() 
    6666 
    67 r = p.solve('nssolve') 
     67#r = p.solve('nssolve', debug=0, maxIter=1e9) 
     68# using nlsp2nlp converter, try to minimize sum(f_i(x)^2): 
     69r = p.solve('nlp:ralg') 
    6870 
    6971print 'solution:', r.xf 
    7072print 'max residual:', r.ff 
    71 ############################### 
    72 """with p.c turned off it should print: 
    73 solution: [   1.00000059    1.99999981  156.03243497] 
    74 max residual: 6.8216439208512725e-07 
    75 with p.c turned on it should print: 
    76 solution: [   1.00000013    1.99999996  151.84364467] 
    77 max residual: 3.4728274767026335e-07 
    78 """ 
     73