Changeset 1089
- Timestamp:
- 07/02/08 02:53:18 (2 months ago)
- Files:
-
- trunk/openopt/scikits/openopt/Kernel/BaseAlg.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/BaseProblem.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/QP.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/Residuals.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/ooGraphics.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/ooIter.py (modified) (4 diffs)
- trunk/openopt/scikits/openopt/Kernel/runProbSolver.py (modified) (5 diffs)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/lapack_dgelss_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/lapack_sgelss_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_cobyla_oo.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/BaseAlg.py
r1049 r1089 1 1 __docformat__ = "restructuredtext en" 2 from numpy import atleast_1d, all, asarray, ndarray 2 from numpy import atleast_1d, all, asarray, ndarray, copy 3 3 4 4 class BaseAlg: … … 53 53 if not fArg: 54 54 p.Fk = p.F(p.xk) 55 p.fk = copy(p.Fk) 55 56 else: 56 57 if asarray(p.fk).size >1: trunk/openopt/scikits/openopt/Kernel/BaseProblem.py
r1040 r1089 56 56 def __init__(self): 57 57 self.isObjFunValueASingleNumber = True 58 self.prev = autocreate() # used for already obtained values store58 #self.prev = autocreate() # used for already obtained values store 59 59 self.iterfcn = lambda *args: ooIter(self, *args)# this parameter is only for OpenOpt developers, not common users 60 60 self.norm = linalg.norm … … 289 289 if type(v) != type(()): setattr(self.args, j, (v,)) 290 290 291 def inspire(self, newProb): 292 # fills some fields of new prob with old prob values 293 294 #TODO: hold it in single place 295 for key in ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'contol', 'xtol', 'ftol', 'gradtol', 'iprint', 'plot', 'maxIter', 'maxTime', 'maxCPUTime','fEnough', 'goal', 'color', 'debug'] : 296 if hasattr(self, key): setattr(newProb, key, getattr(self, key)) trunk/openopt/scikits/openopt/Kernel/QP.py
r1040 r1089 7 7 from ooMisc import assignScript 8 8 from BaseProblem import BaseProblem 9 from numpy import as array, ones, inf, dot9 from numpy import asfarray, ones, inf, dot, asfarray, nan, zeros 10 10 11 11 … … 22 22 23 23 def objFunc(self, x): 24 return asfarray(0.5*dot(x, dot( p.H, x)) + dot(p.f, x).sum()).flatten()24 return asfarray(0.5*dot(x, dot(self.H, x)) + dot(self.f, x).sum()).flatten() 25 25 26 26 … … 32 32 for fn in ('H', 'f'): 33 33 if kwargs.has_key(fn): 34 kwargs[fn] = as array(kwargs[fn], float) # TODO: handle the case in runProbSolver()34 kwargs[fn] = asfarray(kwargs[fn], float) # TODO: handle the case in runProbSolver() 35 35 36 36 37 37 38 38 p.n = kwargs['H'].shape[0] 39 if p.x0 is nan: p.x0 = zeros(p.n) 39 40 p.lb = -inf * ones(p.n) 40 41 p.ub = inf * ones(p.n) trunk/openopt/scikits/openopt/Kernel/Residuals.py
r1065 r1089 1 1 __docformat__ = "restructuredtext en" 2 from numpy import concatenate, asfarray, array, where, argmax, zeros, isfinite 2 from numpy import concatenate, asfarray, array, where, argmax, zeros, isfinite, copy 3 from copy import deepcopy 3 4 empty_arr = asfarray([]) 4 5 … … 31 32 32 33 def __getResiduals__(self, x): 33 if hasattr(self.prev, 'residualsX') and all(x == self.prev.residualsX):34 return self.prev.residualsVal 34 # if self.prevVal['r'].has_key('x') and all(x == self.prevVal['r']['x']): 35 # return self.prevVal['r']['Val'] 35 36 # TODO: add quadratic constraints 36 37 r = EmptyClass() … … 41 42 r.lb = self.__getLbResiduals__(x) 42 43 r.ub = self.__getUbResiduals__(x) 43 self.prev.residualsVal = r 44 # self.prevVal['r']['Val'] = deepcopy(r) 45 # self.prevVal['r']['x'] = copy(x) 44 46 return r 45 47 trunk/openopt/scikits/openopt/Kernel/ooGraphics.py
r1068 r1089 75 75 self.nTrajectories = 0 76 76 self.ghandlers = [] 77 self.solverNames = []77 #self.solverNames = [] 78 78 79 79 #TODO: the condition should be handled somewhere other place, not here. … … 243 243 p2 = pylab.plot([xx[0]], [yy2[0]], color = color, marker = self.specifierStart, markersize = self.markerSize) 244 244 p3 = pylab.plot([xx[0], xx[0]+1e-50], [yy2[0], yy2[0]], color = color, markersize = self.markerSize) 245 if p.legend == '': pylab.legend([p3[0]], [p.solver Name], shadow = True)245 if p.legend == '': pylab.legend([p3[0]], [p.solver.__name__], shadow = True) 246 246 elif type(p.legend) in (tuple, list): pylab.legend([p3[0]], p.legend, shadow = True) 247 247 else: pylab.legend([p3[0]], [p.legend], shadow = True) trunk/openopt/scikits/openopt/Kernel/ooIter.py
r1004 r1089 4 4 from numpy import asfarray, atleast_1d, isreal, all, ndarray 5 5 from ooMisc import isSolved 6 NoneType = type(None) 6 7 7 8 def ooIter(p, *args, **kwargs): … … 22 23 p.lastDrawIter = 0 23 24 24 25 25 p.solver.__decodeIterFcnArgs__(p, *args, **kwargs) 26 p.iterPrint()27 28 26 if p.graphics.xlabel == 'nf': p.iterValues.nf.append(p.nEvals['f']) 29 27 … … 33 31 if type(attr) == list: 34 32 attr.pop(-1) 35 elif type(attr) == ndarray:36 p.warn('Found ndarrayin p.iterValues (Python list expected), it can lead to error(s)!')33 elif type(attr) not in [str, NoneType]: 34 p.warn('Found incorrect type ' + str(type(attr)) +' in p.iterValues (Python list expected), it can lead to error(s)!') 37 35 #TODO: handle case x0 = x1 = x2 = ... 38 36 return 39 37 40 38 p.iterPrint() 41 39 42 40 p.iterCPUTime.append(clock() - p.cpuTimeStart) … … 68 66 p.userStop = True 69 67 70 if p.istop and not p.solver.__iterfcnConnected__ and not p.isFinished and p.solver Name!= 'ipopt':68 if p.istop and not p.solver.__iterfcnConnected__ and not p.isFinished and p.solver.__name__ != 'ipopt': 71 69 raise isSolved 72 70 trunk/openopt/scikits/openopt/Kernel/runProbSolver.py
r1065 r1089 7 7 import os, string 8 8 from ooMisc import isSolved 9 from string import capitalize 9 from string import capitalize, lower 10 10 from BaseProblem import ProbDefaults 11 11 from scikits.openopt.Kernel.ooMisc import __solverPaths__ … … 31 31 else: p.was_involved = True 32 32 33 if ':' in solver_str: 34 probTypeToConvert, solverName = solver_str.split(':', 1) 35 converterName = lower(p.probType)+'2'+probTypeToConvert 36 converter = getattr(p, converterName) 37 return converter(solverName, *args, **kwargs) 38 33 39 if kwargs.has_key('debug'): 34 40 p.debug = kwargs['debug'] … … 43 49 44 50 p.solver = solverClass() 45 p.solverName = p.solver.__name__ 46 setattr(p, p.solverName, EmptyClass()) 47 p.solverLicense = p.solver.__license__ 51 # p.solverName = p.solver.__name__ 52 # setattr(p, p.solverName, EmptyClass()) 48 53 solver = p.solver.__solver__ 49 54 … … 152 157 153 158 if hasattr(p, 'delayedConnectIterFcn'): 154 if p.solver.__iterfcnConnected__: p.warn('solver ' + p.solver Name+ ' has native iterfcn, user-defined connection via p.connectIterFcn will be ignored')159 if p.solver.__iterfcnConnected__: p.warn('solver ' + p.solver.__name__ + ' has native iterfcn, user-defined connection via p.connectIterFcn will be ignored') 155 160 else: 156 161 if p.delayedConnectIterFcn == 'df': p.warn('iterfcn now connects to df automatically by default, you can either use other funcs like d2f to suppress the warning or ommit user-defined connecting to df at all') … … 173 178 p.cpuTimeStart = clock() 174 179 180 175 181 ############################ 176 182 # Start solving problem: 183 177 184 if p.iprint >= 0: 178 185 print '-----------------------------------------------------' 179 s = 'solver: ' + p.solver Name+ ' problem: ' + p.name186 s = 'solver: ' + p.solver.__name__ + ' problem: ' + p.name 180 187 if p.showGoal: s += ' goal: ' + p.goal 181 188 print s 189 190 p.iterfcn(p.x0) 191 182 192 try: 183 193 solver(p) trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py
r1061 r1089 45 45 # Shor r-alg engine 46 46 ############################################# 47 48 startPoint = p.point(p.x0)49 47 x = x0.copy() 50 48 xPrevIter = x0.copy() 51 #p.xk = x0.copy() 52 #f = startPoint.f() 53 #p.fk = f 49 startPoint = p.point(x) 50 PrevIterPoint = p.point(x) 54 51 55 52 g = self.__getRalgDirection__(startPoint) 56 p._df = copy(g) 57 58 p.iterfcn(startPoint) 59 if p.istop: 60 p.xf = x 61 p.ff = f 62 #r.advanced.ralg.hs = hs; 63 return 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 64 60 65 61 g2 = copy(g) 66 if p. rk <= p.contol:62 if p.isFeas(x): 67 63 constrGradPrevIter = zeros(p.n) 68 64 dfPrevIter = g2.copy() trunk/openopt/scikits/openopt/solvers/scipy_optim/lapack_dgelss_oo.py
r1008 r1089 12 12 def __init__(self):pass 13 13 def __solver__(self, p): 14 p.iterfcn(p.x0) 14 15 15 16 v,x,s,rank,info = dgelss(p.C, p.d) trunk/openopt/scikits/openopt/solvers/scipy_optim/lapack_sgelss_oo.py
r1008 r1089 12 12 def __init__(self):pass 13 13 def __solver__(self, p): 14 p.iterfcn(p.x0) 14 15 v,x,s,rank,info = sgelss(p.C, p.d) 15 16 xf = x[:p.C.shape[1]] trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_cobyla_oo.py
r934 r1089 6 6 from numpy import inf, array, copy 7 7 8 class EmptyClass: pass 9 8 class EmptyClass: pass 9 10 10 class scipy_cobyla(BaseAlg): 11 11 __name__ = 'scipy_cobyla' 12 12 __license__ = "BSD" 13 13 __authors__ = """undefined""" 14 __alg__ = "Constrained Optimization BY Linear Approximation" 15 __info__ = 'constrained NLP solver, no user-defined derivatives are handled'#TODO: add '__info__' field to other solvers 14 __alg__ = "Constrained Optimization BY Linear Approximation" 15 __info__ = 'constrained NLP solver, no user-defined derivatives are handled'#TODO: add '__info__' field to other solvers 16 16 __constraintsThatCannotBeHandled__ = [] # empty list means the solver can handle all constraints 17 17 18 18 def __init__(self): pass 19 19 def __solver__(self, p): … … 24 24 if p.userProvided.h: p.cobyla.nh = p.h(p.x0).size 25 25 else: p.cobyla.nh = 0 26 26 27 27 det_arr = cumsum(array((p.cobyla.nc, p.cobyla.nh, p.b.size, p.beq.size, p.cobyla.nh, p.beq.size))) 28 28 29 29 cons = [] 30 30 for i in xrange(det_arr[-1]): 31 if i < det_arr[0]: 31 if i < det_arr[0]: 32 32 c = lambda x, i=i: - p.c(x)[i] # cobyla requires positive constraints! 33 elif det_arr[0] <= i < det_arr[1]: 33 elif det_arr[0] <= i < det_arr[1]: 34 34 j = i - det_arr[0] 35 35 c = lambda x, i=i: p.h(x)[j] 36 elif det_arr[1] <= i < det_arr[2]: 36 elif det_arr[1] <= i < det_arr[2]: 37 37 j = i - det_arr[1] 38 38 #assert 0<= j <p.cobyla.nb … … 42 42 #assert 0<= j <p.cobyla.nbeq 43 43 c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j] 44 elif det_arr[3] <= i < det_arr[4]: 44 elif det_arr[3] <= i < det_arr[4]: 45 45 j = i - det_arr[3] 46 c = lambda x, j=j: - p.h(x)[j] 46 c = lambda x, j=j: - p.h(x)[j] 47 47 elif det_arr[4] <= i < det_arr[5]: 48 48 j = i - det_arr[4] 49 49 #assert 0<= j <p.cobyla.nbeq 50 50 c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j] 51 else: 51 else: 52 52 p.err('error in connection cobyla to openopt') 53 53 cons.append(c) … … 58 58 ## c3 = p.matmult(p.Aeq, x) - p.beq 59 59 ## return hstack((c0, c1, -c1, c2, c3, -c3)) 60 61 62 p.xk = p.x0.copy()63 p.fk = p.f(p.x0)64 65 p.iterfcn()66 if p.istop:67 p.xf = p.xk68 p.ff = p.fk69 return 70 71 60 61 62 # p.xk = p.x0.copy() 63 # p.fk = p.f(p.x0) 64 # 65 # p.iterfcn() 66 # if p.istop: 67 # p.xf = p.xk 68 # p.ff = p.fk 69 # return 70 71 72 72 xf = fmin_cobyla(p.f, p.x0, cons = tuple(cons), iprint = 0, maxfun = p.maxFunEvals, rhoend = p.xtol ) 73 73 74 74 p.xk = xf 75 75 p.fk = p.f(xf) 76 76 p.istop = 1000 77 p.iterfcn()78 p.xf = xf79 p.ff = p.fk80 81 82 77 # p.iterfcn() 78 # p.xf = xf 79 # p.ff = p.fk 80 81 82
