Changeset 1504
- Timestamp:
- 09/27/08 15:04:17 (2 months ago)
- Files:
-
- trunk/openopt/scikits/openopt/Kernel/NLSP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/Point.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/examples/nlsp_1.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/examples/nlsp_constrained.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/NLSP.py
r1102 r1504 2 2 from NLP import nlp_init 3 3 from numpy.linalg import norm 4 from numpy import inf, asfarray, atleast_1d 4 from numpy import inf, asfarray, atleast_1d, dot 5 5 from setDefaultIterFuncs import FVAL_IS_ENOUGH 6 import NLP 7 #from Function import oofun 6 8 7 9 class NLSP(NonLinProblem): … … 30 32 return norm(atleast_1d(asfarray(fv)), inf) 31 33 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 177 177 178 178 if not self.p.isNaNInConstraintsAllowed: 179 if oldPoint.__n nan__() > self.__nnan__(): return True180 elif oldPoint.__n nan__() < self.__nnan__(): return False179 if oldPoint.__nNaNs__() > self.__nNaNs__(): return True 180 elif oldPoint.__nNaNs__() < self.__nNaNs__(): return False 181 181 # TODO: check me 182 if mr <= self.p.contol and oldPointResidual <= self.p.contol and self.__n nan__() != 0: return mr < oldPointResidual182 if mr <= self.p.contol and oldPointResidual <= self.p.contol and self.__nNaNs__() != 0: return mr < oldPointResidual 183 183 184 184 if mr < oldPointResidual and self.p.contol < oldPointResidual: return True … … 216 216 return True 217 217 218 def __n nan__(self):218 def __nNaNs__(self): 219 219 # returns number of nans in constraints 220 220 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]) 227 224 return r 228 225 trunk/openopt/scikits/openopt/examples/nlsp_1.py
r925 r1504 9 9 from numpy import asfarray, zeros, cos, sin 10 10 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: 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 # 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: 14 15 #f = lambda x: [x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5] 15 16 #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)) … … 25 26 df[2,2] = -sin(x[2]) 26 27 return df 27 28 28 29 x0 = [8,15, 80] 29 30 30 #w/o gradient: 31 #w/o gradient: 31 32 #p = NLSP(f, x0) 32 33 p = NLSP(f, x0, df = DF) … … 43 44 44 45 #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): 48 r = p.solve('nlp:ralg') 46 49 47 50 print 'solution:', r.xf trunk/openopt/scikits/openopt/examples/nlsp_constrained.py
r1067 r1504 21 21 22 22 # 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)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) 25 25 # Python list, numpy.array are allowed as well: 26 26 #f = lambda x: [x[0]**3+x[1]**3-9, x[0]-0.5*x[1], cos(x[2])+x[0]-1.5] … … 65 65 #p.checkdc() 66 66 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): 69 r = p.solve('nlp:ralg') 68 70 69 71 print 'solution:', r.xf 70 72 print '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
