Changeset 1091
- Timestamp:
- 07/03/08 15:21:41 (2 months ago)
- Files:
-
- trunk/openopt/scikits/openopt/Kernel/BaseAlg.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/Kernel/BaseProblem.py (modified) (15 diffs)
- trunk/openopt/scikits/openopt/Kernel/GLP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/LLSP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/LP.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/LSP.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/Kernel/MILP.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/MMP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/NLP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/NLSP.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/NSP.py (modified) (3 diffs)
- trunk/openopt/scikits/openopt/Kernel/QP.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/Residuals.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/Kernel/objFunRelated.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/Kernel/ooCheck.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/Kernel/runProbSolver.py (modified) (5 diffs)
- trunk/openopt/scikits/openopt/Kernel/setDefaultIterFuncs.py (modified) (6 diffs)
- trunk/openopt/scikits/openopt/solvers/BrasilOpt/ALGENCAN_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/BrasilOpt/algencan_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/CVXOPT/cvxopt_glpk_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/CVXOPT/cvxopt_lp_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/CVXOPT/cvxopt_qp_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/CVXOPT/glpk_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/CoinOr/ipopt_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/Standalone/bvls_oo.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/solvers/Standalone/galileo_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/Standalone/toms587_oo.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/ShorEllipsoid_oo.py (modified) (2 diffs)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/goldenSection_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/lincher_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/nsmm_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/nssolve_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/lp_solve/lpSolve_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_cobyla_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_fminbound_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_lbfgsb_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_slsqp_oo.py (modified) (1 diff)
- trunk/openopt/scikits/openopt/solvers/scipy_optim/scipy_tnc_oo.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/BaseAlg.py
r1089 r1091 11 11 __homepage__ = 'Undefined. Use web search' 12 12 __info__ = 'None' 13 __constraintsThatCannotBeHandled__ = ['c', 'h', 'Aeq', 'A', 'lb', 'ub'] 13 14 # TODO: remove it 15 __constraintsThatCannotBeHandled__ = [] 16 17 __optionalDataThatCanBeHandled__ = [] 14 18 __isIterPointAlwaysFeasible__ = lambda self, p: p.isUC#TODO: provide possibility of simple True, False 15 19 __iterfcnConnected__ = False trunk/openopt/scikits/openopt/Kernel/BaseProblem.py
r1089 r1091 3 3 from oologfcn import * 4 4 from ooGraphics import Graphics 5 from setDefaultIterFuncs import setDefaultIterFuncs 5 from setDefaultIterFuncs import setDefaultIterFuncs, IS_MAX_FUN_EVALS_REACHED 6 6 from objFunRelated import objFunRelated 7 7 from Residuals import Residuals … … 12 12 13 13 ProbDefaults = {'diffInt': 1e-7, 'xtol': 1e-6, 'noise': 0} 14 15 14 from runProbSolver import runProbSolver 16 15 17 class Check: 18 def __init__(self): 19 self.df = 0# check numerical & 20 self.dh = 0# user-supplied gradients 21 self.dc = 0 22 # lines with difference less than maxViolation will be not shown 23 self.maxViolation = 1e-5 16 17 24 18 25 19 class user: 26 20 def __init__(self): 27 21 pass 28 29 class Parallel:30 def __init__(self):31 self.f = False# 0 - don't use parallel calclations, 1 - use32 self.c = False33 self.h = False34 #others are under development35 #TODO: add paralell func!36 #self.parallel.fun = dfeval37 22 38 23 class oomatrix: … … 46 31 #return asarray(x) * asarray(y) 47 32 48 class args:49 def __init__(self): pass50 f, c, h = (), (), ()51 52 33 class autocreate: 53 34 def __init__(self): pass 54 35 55 class BaseProblem(oomatrix, objFunRelated, Residuals, ooTextOutput, args):36 class BaseProblem(oomatrix, Residuals, ooTextOutput): 56 37 def __init__(self): 57 38 self.isObjFunValueASingleNumber = True … … 61 42 self.name = 'unnamed' 62 43 63 self.check = Check() 64 self.parallel = Parallel() 44 65 45 self.graphics = Graphics() 66 46 self.user = user() 67 self.args = args()68 47 69 48 self.plot = False # draw picture or not … … 95 74 self.data4TextOutput = ['objFunVal', 'log10(maxResidual)'] 96 75 97 #TODO: remove it?98 self.useScaling = 0#if 1, ScaleFactor or TypicalX must be provided, else x0 will be used as TypicalX. However, OpenOpt automatic scaling isn't tested properly yet.99 76 100 77 self.debug = 0 … … 106 83 107 84 self.maxIter = 400 108 self.maxFunEvals = 10000 85 self.maxFunEvals = 10000 # TODO: move it to NinLinProblem class? 109 86 self.maxCPUTime = inf 110 87 self.maxTime = inf 111 self.maxLineSearch = 500 112 self.xtol = ProbDefaults['xtol'] 113 self.gradtol = 1e-6 88 self.maxLineSearch = 500 # TODO: move it to NinLinProblem class? 89 self.xtol = ProbDefaults['xtol'] # TODO: move it to NinLinProblem class? 90 self.gradtol = 1e-6 # TODO: move it to NinLinProblem class? 114 91 self.ftol = 1e-6 115 92 self.contol = 1e-6 … … 121 98 self.binVars = [] # for problems like MILP 122 99 123 self.noise = ProbDefaults['noise'] 124 self.consMode = 'all' 125 126 #TODO: move it to non-lin funcs classes only 127 self.prevVal = {} 128 for fn in ['f', 'c', 'h', 'df', 'dc', 'dh', 'd2f', 'd2c', 'd2h']: 129 self.prevVal[fn] = {'key':None, 'val':None} 100 self.noise = ProbDefaults['noise'] # TODO: move it to NinLinProblem class? 101 130 102 131 103 # A * x <= b inequalities … … 147 119 # 0 : <Awhole, x> [j] = bwhole[j] 148 120 149 #finite-difference gradient aproximation step150 self.diffInt = ProbDefaults['diffInt']151 152 121 self.scale = None 153 154 self.isVectoriezed = 0# isn't tested properly yet155 156 #f0 = None#TODO: handle me properly157 158 #non-linear constraints159 self.c = None # c(x)<=0160 self.h = None # h(x)=0161 122 162 123 self.goal = None# should be redefined by child class 163 124 # possible values: 'maximum', 'min', 'max', 'minimum', 'minimax' etc 164 125 self.showGoal = False# can be redefined by child class, used for text & graphic output 165 166 # TODO: implement in a field self classification:167 # 'minimax', 'multiobjective'(or goalattain?), etc168 169 self.fPattern = None170 self.cPattern = None171 self.hPattern = None172 173 ##primal.* fields are for OpenOpt developers only, not common users174 #primal.f = None175 #primal.df = None176 #primal.d2f = None177 #primal.c = None178 #primal.h = None179 #primal.dc = None180 #primal.dh = None181 #primal.d2c = None182 #primal.d2h = None183 126 184 127 self.color = 'b' # blue, color for plotting … … 199 142 # so it may be ignored with some solvers not closely connected to OO Kernel 200 143 201 self.kernelIterFuncs = setDefaultIterFuncs()202 203 144 self.callback = [] 204 145 … … 208 149 209 150 self.special = autocreate() 151 152 self.optionalData = []#string names of optional data like 'c', 'h', 'Aeq' etc 153 154 def __finalize__(self): 155 pass 210 156 211 157 def objFunc(self, x): … … 224 170 return self.b.size ==0 and self.beq.size==0 and not self.userProvided.c and not self.userProvided.h 225 171 226 def __1stBetterThan2nd__(self, f1, f2, r1=None, r2=None):227 if self.isUC:228 #TODO: check for goal = max/maximum229 return f1 < f2230 else:#then r1, r2 should be defined231 return (r1 < r2 and self.contol < r2) or (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 < f2)232 233 def __1stCertainlyBetterThan2ndTakingIntoAcoountNoise__(self, f1, f2, r1=None, r2=None):234 if self.isUC:235 #TODO: check for goalType = max236 return f1 + self.noise < f2 - self.noise237 else:238 #return (r1 + self.noise < r2 - self.noise and self.contol < r2) or \239 return (r1 < r2 and self.contol < r2) or \240 (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 + self.noise < f2 - self.noise)172 # def __1stBetterThan2nd__(self, f1, f2, r1=None, r2=None): 173 # if self.isUC: 174 # #TODO: check for goal = max/maximum 175 # return f1 < f2 176 # else:#then r1, r2 should be defined 177 # return (r1 < r2 and self.contol < r2) or (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 < f2) 178 # 179 # def __1stCertainlyBetterThan2ndTakingIntoAcoountNoise__(self, f1, f2, r1=None, r2=None): 180 # if self.isUC: 181 # #TODO: check for goalType = max 182 # return f1 + self.noise < f2 - self.noise 183 # else: 184 # #return (r1 + self.noise < r2 - self.noise and self.contol < r2) or \ 185 # return (r1 < r2 and self.contol < r2) or \ 186 # (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 + self.noise < f2 - self.noise) 241 187 242 188 … … 244 190 return runProbSolver(self, solvers, *args, **kwargs) 245 191 192 193 # TODO: remove it 246 194 def auxFunc(self, x, *args, **kwargs): 247 195 r = self.connectedIterFuncField(x, *args, **kwargs) … … 253 201 return r 254 202 203 # TODO: simplify or remove it 255 204 def connectIterFcn(self, funcname, calledByUser = True): 256 205 #this function could be called by user or by RunProbSolver.py … … 271 220 self.err('OpenOpt error: this function should be overdetermined by child class') 272 221 222 def inspire(self, newProb): 223 # fills some fields of new prob with old prob values 224 225 #TODO: hold it in single place 226 for key in ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'contol', 'xtol', 'ftol', 'gradtol', 'iprint', 'plot', 'maxIter', 'maxTime', 'maxCPUTime','fEnough', 'goal', 'color', 'debug'] : 227 if hasattr(self, key): setattr(newProb, key, getattr(self, key)) 228 229 230 class MatrixProblem(BaseProblem): 231 __baseClassName__ = 'Matrix' 232 def __init__(self): 233 BaseProblem.__init__(self) 234 self.kernelIterFuncs = setDefaultIterFuncs('Matrix') 235 236 def __prepare__(self): 237 pass 238 239 class Check: 240 def __init__(self): 241 self.df = 0# check numerical & 242 self.dh = 0# user-supplied gradients 243 self.dc = 0 244 # lines with difference less than maxViolation will be not shown 245 self.maxViolation = 1e-5 246 247 class Parallel: 248 def __init__(self): 249 self.f = False# 0 - don't use parallel calclations, 1 - use 250 self.c = False 251 self.h = False 252 #TODO: add paralell func! 253 #self.parallel.fun = dfeval 254 255 class args: 256 def __init__(self): pass 257 f, c, h = (), (), () 258 259 class NonLinProblem(BaseProblem, objFunRelated, args): 260 __baseClassName__ = 'NonLin' 261 def __init__(self): 262 BaseProblem.__init__(self) 263 self.check = Check() 264 self.args = args() 265 self.consMode = 'all' # TODO: remove it? 266 #self.parallel = Parallel() 267 268 self.prevVal = {} 269 for fn in ['f', 'c', 'h', 'df', 'dc', 'dh', 'd2f', 'd2c', 'd2h']: 270 self.prevVal[fn] = {'key':None, 'val':None} 271 272 #finite-difference gradient aproximation step 273 self.diffInt = ProbDefaults['diffInt'] 274 275 #self.isVectoriezed = False 276 277 #non-linear constraints 278 self.c = None # c(x)<=0 279 self.h = None # h(x)=0 280 281 # self.fPattern = None 282 # self.cPattern = None 283 # self.hPattern = None 284 self.kernelIterFuncs = setDefaultIterFuncs('NonLin') 285 273 286 def checkdf(self, *args, **kwargs): 274 287 return ooCheckGradient(self, 'df', *args, **kwargs) … … 289 302 if type(v) != type(()): setattr(self.args, j, (v,)) 290 303 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)) 304 def __finalize__(self): 305 if (self.userProvided.c and any(isnan(self.c(self.xf)))) or (self.userProvided.h and any(isnan(self.h(self.xf)))): 306 if self.iprint >= -1: self.warn('some non-linear constraints are equal to NaN') 307 308 def __prepare__(self): 309 self.__makeCorrectArgs__() 310 for s in ('f', 'df', 'd2f', 'c', 'dc', 'd2c', 'h', 'dh', 'd2h'): 311 if hasattr(self, s) and getattr(self, s) is not None: 312 setattr(self.userProvided, s, True) 313 self.nEvals[s] = 0 314 A = getattr(self,s) 315 316 if callable(A): #TODO: add or ndarray(A)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 317 A = (A,)#make tuple 318 setattr(self.user, s, A) 319 else: 320 setattr(self.userProvided, s, False) 321 #setattr(p.user, s, (None,)) 322 setattr(self, s, getattr(self, 'user_' + s)) 323 324 #initialization, getting nf, nc, nh etc: 325 for s in ['c', 'h']: 326 if getattr(self.userProvided, s): 327 getattr(self, s)(self.x0) 328 else: 329 setattr(self, 'n'+s, 0) 330 331 if hasattr(self, 'delayedConnectIterFcn'): 332 if self.solver.__iterfcnConnected__: self.warn('solver ' + self.solver.__name__ + ' has native iterfcn, user-defined connection via p.connectIterFcn will be ignored') 333 else: 334 if self.delayedConnectIterFcn == 'df': self.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') 335 self.connectIterFcn('df') 336 else: 337 if not self.solver.__iterfcnConnected__: 338 self.connectIterFcn('df', calledByUser = False) trunk/openopt/scikits/openopt/Kernel/GLP.py
r1040 r1091 1 1 from ooMisc import assignScript 2 from BaseProblem import BaseProblem2 from BaseProblem import NonLinProblem 3 3 from numpy import asarray, ones, inf, array, iterable 4 4 from NLP import nlp_init 5 5 6 class GLP(BaseProblem): 6 class GLP(NonLinProblem): 7 __optionalData__ = ['lb', 'ub'] 7 8 def __init__(self, *args, **kwargs): 8 9 if len(args) > 1: self.err('incorrect args number for GLP constructor, must be 0..1 + (optionaly) some kwargs') … … 10 11 kwargs2 = kwargs.copy() 11 12 if len(args) > 0: kwargs2['f'] = args[0] 12 BaseProblem.__init__(self)13 NonLinProblem.__init__(self) 13 14 14 15 glp_init(self, kwargs2) trunk/openopt/scikits/openopt/Kernel/LLSP.py
r1040 r1091 1 1 from ooMisc import assignScript 2 from BaseProblem import BaseProblem3 from numpy import asfarray, ones, inf, dot 2 from BaseProblem import MatrixProblem 3 from numpy import asfarray, ones, inf, dot, nan, zeros 4 4 from numpy.linalg import norm 5 import NLP 5 6 6 class LLSP(BaseProblem): 7 class LLSP(MatrixProblem): 8 __optionalData__ = ['damp', 'xd', 'c'] 7 9 def __init__(self, *args, **kwargs): 8 10 if len(args) > 2: self.err('incorrect args number for LLSP constructor, must be 0..2 + (optionaly) some kwargs') 9 11 if len(args) > 0: kwargs['C'] = args[0] 10 12 if len(args) > 1: kwargs['d'] = args[1] 11 BaseProblem.__init__(self) 13 14 MatrixProblem.__init__(self) 12 15 llsp_init(self, kwargs) 13 16 14 17 def objFunc(self, x): 15 return norm(dot(self.C, x) - self.d) 18 r = norm(dot(self.C, x) - self.d) ** 2 / 2.0 19 if self.damp != 0: r += self.damp * norm(x-self.xd)**2 / 2.0 20 if any(self.f): r += dot(self.f, x) 21 return r 22 23 def llsp2nlp(self, solver, **kwargs): 24 if hasattr(self,'x0'): p = NLP.NLP(ff, self.x0, df=dff) 25 else: p = NLP(ff, zeros(self.n), df=dff) 26 p.args.f = self # DO NOT USE p.args = self IN PROB ASSIGNMENT! 27 self.inspire(p) 28 r = p.solve(solver, **kwargs) 29 return r 30 31 16 32 17 33 def llsp_init(prob, kwargs): … … 26 42 prob.lb = -inf * ones(prob.n) 27 43 prob.ub = inf * ones(prob.n) 44 if not kwargs.has_key('damp'): kwargs['damp'] = 0 45 if not kwargs.has_key('xd'): kwargs['xd'] = zeros(prob.n) 46 if not kwargs.has_key('f'): kwargs['f'] = zeros(prob.n) 47 48 if prob.x0 is nan: prob.x0 = zeros(prob.n) 28 49 29 50 return assignScript(prob, kwargs) 30 51 52 #def ff(x, LLSPprob): 53 # r = dot(LLSPprob.C, x) - LLSPprob.d 54 # return dot(r, r) 55 ff = lambda x, LLSPprob: LLSPprob.objFunc(x) 56 dff = lambda x, LLSPprob: 2 * (dot(LLSPprob.C.T, dot(LLSPprob.C,x) - LLSPprob.d)) 57 trunk/openopt/scikits/openopt/Kernel/LP.py
r1040 r1091 1 1 from ooMisc import assignScript 2 from BaseProblem import BaseProblem3 from numpy import asarray, ones, inf 2 from BaseProblem import MatrixProblem 3 from numpy import asarray, ones, inf, dot, nan, zeros 4 4 5 5 6 class LP(BaseProblem): 6 class LP(MatrixProblem): 7 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 7 8 def __init__(self, *args, **kwargs): 8 9 kwargs2 = kwargs.copy() … … 10 11 if len(args) > 1: self.err('incorrect args number for LP constructor, must be 0..1 + (optionaly) some kwargs') 11 12 self.probType = 'LP' 12 BaseProblem.__init__(self)13 MatrixProblem.__init__(self) 13 14 lp_init(self, kwargs2) 14 15 15 16 16 17 def objFunc(self, x): 17 return self.dotmult(p.f, x)18 return dot(self.f, x) 18 19 19 20 def lp_init(prob, kwargs): … … 27 28 28 29 prob.n = len(f) 30 if prob.x0 is nan: prob.x0 = zeros(prob.n) 29 31 prob.lb = -inf * ones(prob.n) 30 32 prob.ub = inf * ones(prob.n) trunk/openopt/scikits/openopt/Kernel/LSP.py
r830 r1091 1 from BaseProblem import BaseProblem1 from BaseProblem import NonLinProblem 2 2 from NLP import nlp_init 3 3 4 class LSP(BaseProblem): 4 class LSP(NonLinProblem): 5 __optionalData__ = [] 5 6 def __init__(self, *args, **kwargs): 6 7 if len(args) > 2: self.err('incorrect args number for LSP constructor, must be 0..2 + (optionaly) some kwargs') 7 8 8 9 kwargs2 = kwargs.copy() 9 10 if len(args) > 0: kwargs2['f'] = args[0] 10 11 if len(args) > 1: kwargs2['x0'] = args[1] 11 12 BaseProblem.__init__(self)12 13 NonLinProblem.__init__(self) 13 14 self.probType = 'LSP' 15 self.allowedGoals = ['minimum', 'min'] 14 16 self.isObjFunValueASingleNumber = False 15 17 16 18 return nlp_init(self, kwargs2) 17 19 trunk/openopt/scikits/openopt/Kernel/MILP.py
r1040 r1091 1 1 2 2 from ooMisc import assignScript 3 from BaseProblem import BaseProblem4 from numpy import asarray, ones, inf 3 from BaseProblem import MatrixProblem 4 from numpy import asarray, ones, inf, dot, nan, zeros 5 5 6 6 from LP import lp_init … … 8 8 9 9 10 class MILP(BaseProblem): 10 class MILP(MatrixProblem): 11 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 11 12 def __init__(self, *args, **kwargs): 12 13 if len(args) > 2: self.err('incorrect args number for MILP constructor, must be 0..2 + (optionaly) some kwargs') … … 16 17 if len(args) > 1: kwargs2['intVars'] = args[1] 17 18 self.probType = 'MILP' 18 BaseProblem.__init__(self)19 MatrixProblem.__init__(self) 19 20 lp_init(self, kwargs2) 21 22 def objFunc(self, x): 23 return dot(self.f, x) 20 24 21 25 22 26 23 24 trunk/openopt/scikits/openopt/Kernel/MMP.py
r1040 r1091 1 from BaseProblem import BaseProblem1 from BaseProblem import NonLinProblem 2 2 from NLP import nlp_init 3 3 from numpy import max 4 4 5 class MMP( BaseProblem):5 class MMP(NonLinProblem): 6 6 """ 7 7 Mini-Max Problem 8 8 """ 9 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 9 10 def __init__(self, *args, **kwargs): 10 11 if len(args) > 2: self.err('incorrect args number for MMP constructor, must be 0..2 + (optionaly) some kwargs') … … 13 14 if len(args) > 0: kwargs2['f'] = args[0] 14 15 if len(args) > 1: kwargs2['x0'] = args[1] 15 BaseProblem.__init__(self)16 NonLinProblem.__init__(self) 16 17 17 self.allowedGoals = ['minim um', 'min', 'maximum', 'max']18 self.allowedGoals = ['minimax'] 18 19 19 20 nlp_init(self, kwargs2) trunk/openopt/scikits/openopt/Kernel/NLP.py
r1040 r1091 1 1 from ooMisc import assignScript 2 from BaseProblem import BaseProblem2 from BaseProblem import NonLinProblem 3 3 from numpy import asarray, ones, inf, array, iterable 4 4 5 5 6 class NLP(BaseProblem): 6 class NLP(NonLinProblem): 7 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 7 8 def __init__(self, *args, **kwargs): 8 9 if len(args) > 2: self.err('incorrect args number for NLP constructor, must be 0..2 + (optionaly) some kwargs') … … 11 12 if len(args) > 0: kwargs2['f'] = args[0] 12 13 if len(args) > 1: kwargs2['x0'] = args[1] 13 BaseProblem.__init__(self)14 NonLinProblem.__init__(self) 14 15 15 16 self.probType = 'NLP' trunk/openopt/scikits/openopt/Kernel/NLSP.py
r1040 r1091 1 from BaseProblem import BaseProblem1 from BaseProblem import NonLinProblem 2 2 from NLP import nlp_init 3 3 from numpy.linalg import norm … … 5 5 from setDefaultIterFuncs import FVAL_IS_ENOUGH 6 6 7 class NLSP(BaseProblem): 7 class NLSP(NonLinProblem): 8 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 8 9 def __init__(self, *args, **kwargs): 9 10 if len(args) > 2: self.err('incorrect args number for NLSP constructor, must be 0..2 + (optionaly) some kwargs') … … 12 13 if len(args) > 0: kwargs2['f'] = args[0] 13 14 if len(args) > 1: kwargs2['x0'] = args[1] 14 BaseProblem.__init__(self)15 NonLinProblem.__init__(self) 15 16 16 17 self.isObjFunValueASingleNumber = False trunk/openopt/scikits/openopt/Kernel/NSP.py
r1040 r1091 1 1 from ooMisc import assignScript 2 from BaseProblem import BaseProblem2 from BaseProblem import NonLinProblem 3 3 from numpy import asarray, ones, inf 4 4 … … 7 7 8 8 9 class NSP(BaseProblem): 9 class NSP(NonLinProblem): 10 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 10 11 def __init__(self, *args, **kwargs): 11 12 if len(args) > 2: self.err('incorrect args number for NSP constructor, must be 0..2 + (optionaly) some kwargs') … … 14 15 if len(args) > 0: kwargs2['f'] = args[0] 15 16 if len(args) > 1: kwargs2['x0'] = args[1] 16 BaseProblem.__init__(self)17 NonLinProblem.__init__(self) 17 18 18 19 trunk/openopt/scikits/openopt/Kernel/QP.py
r1089 r1091 6 6 7 7 from ooMisc import assignScript 8 from BaseProblem import BaseProblem8 from BaseProblem import MatrixProblem 9 9 from numpy import asfarray, ones, inf, dot, asfarray, nan, zeros 10 10 11 11 12 class QP(BaseProblem): 12 class QP(MatrixProblem): 13 __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 13 14 def __init__(self, *args, **kwargs): 14 15 self.probType = 'QP' … … 17 18 if len(args) > 1: kwargs2['f'] = args[1] 18 19 if len(args) > 2: self.err('incorrect args number for QP constructor, must be 0..1 + (optionaly) some kwargs') 19 BaseProblem.__init__(self)20 MatrixProblem.__init__(self) 20 21 21 22 return qp_init(self, kwargs2) trunk/openopt/scikits/openopt/Kernel/Residuals.py
r1089 r1091 36 36 # TODO: add quadratic constraints 37 37 r = EmptyClass() 38 r.c = self.__get_nonLinInEq_Residuals__(x) 39 r.h = self.__get_nonLinEq_Residuals__(x) 38 # TODO: simplify it! 39 if self.__baseClassName__ == 'NonLin': 40 r.c = self.__get_nonLinInEq_Residuals__(x) 41 r.h = self.__get_nonLinEq_Residuals__(x) 42 else: 43 r.c = r.h = 0 40 44 r.A = self.__get_AX_Less_B_Residuals__(x) 41 45 r.Aeq= self.__get_AeqX_eq_Beq_Residuals__(x) trunk/openopt/scikits/openopt/Kernel/objFunRelated.py
r1009 r1091 167 167 def wrapped_1st_derivatives(p, funcType, x, ind=None, ignorePrev=False): 168 168 derivativesType = 'd'+funcType 169 funcs = getattr(p.user, derivativesType)169 #funcs = getattr(p.user, derivativesType) 170 170 prevKey = p.prevVal[derivativesType]['key'] 171 171 if prevKey is not None and p.iter > 0 and all(x == prevKey) and ind is None and not ignorePrev: trunk/openopt/scikits/openopt/Kernel/ooCheck.py
r1064 r1091 11 11 p.err('goal '+ p.goal+' is not available for the '+ p.probType + ' class (at least not implemented yet)') 12 12 13 13 # for fn in p.__optionalData__: 14 # if not fn in p.solver.__optionalDataThatCanBeHandled__: 15 # p.err('the solver ' + p.solverName + ' cannot handle ' + "'" + fn + "' data") 14 16 15 17 if p.solver.__constraintsThatCannotBeHandled__ != []: … … 20 22 p.err('the solver ' + p.solverName + ' cannot handle ' + "'" + fn + "' constraints") 21 23 22 for fn in ('df', 'dc', 'dh'):23 if getattr(p.check, fn):24 p.info('the option p.check.'+fn+' is obsolete, use p.check'+fn+'() or p.check'+fn+'(x) instead')25 24 26 25 return nErrors trunk/openopt/scikits/openopt/Kernel/runProbSolver.py
r1090 r1091 84 84 if p.graphics.xlabel == 'nf': p.iterValues.nf = [] # iter ObjFunc evaluation number 85 85 86 p.__ makeCorrectArgs__()86 p.__prepare__() 87 87 88 88 if p.probType in ('LP', 'MILP', 'QP') and p.plot: … … 93 93 if hasattr(p, 'x0'): p.x0 = atleast_1d(asfarray(p.x0).copy()) 94 94 for fn in ['lb', 'ub', 'b', 'beq', 'scale', 'diffInt']: 95 fv = getattr(p, fn) 96 if fv != None:# and fv != []: 97 setattr(p, fn, asfarray(fv, dtype='float').flatten()) 98 elif fn != 'scale': 99 setattr(p, fn, asfarray([])) 100 101 102 if p.scale is not None: 95 if hasattr(p, fn): 96 fv = getattr(p, fn) 97 if fv != None:# and fv != []: 98 setattr(p, fn, asfarray(fv, dtype='float').flatten()) 99 elif fn != 'scale': 100 setattr(p, fn, asfarray([])) 101 102 103 if p.scale is not None and hasattr(p, 'diffInt'): 103 104 if p.diffInt.size>1 or p.diffInt[0] != ProbDefaults['diffInt']: 104 105 p.info('using both non-default scale & diffInt is not recommended. diffInt = diffInt/scale will be used') … … 125 126 p.stopdict = {} 126 127 127 for s in ('f', 'df', 'd2f', 'c', 'dc', 'd2c', 'h', 'dh', 'd2h', 'l', 'dl', 'd2l'):128 if not (s == 'f' and p.probType in ('LP', 'MILP', 'QP')):129 if hasattr(p, s) and getattr(p, s) is not None:130 setattr(p.userProvided, s, True)131 p.nEvals[s] = 0132 A = getattr(p,s)133 134 if callable(A): #TODO: add or ndarray(A)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!135 A = (A,)#make tuple136 setattr(p.user, s, A)137 else:138 setattr(p.userProvided, s, False)139 setattr(p.user, s, (None,))140 setattr(p, s, getattr(p, 'user_' + s))141 142 #initialization, getting nf, nc, nh etc:143 for s in ['c', 'h']:144 if getattr(p.userProvided, s):145 getattr(p, s)(p.x0)146 else:147 setattr(p, 'n'+s, 0)148 128 for s in ['b','beq']: 149 129 if hasattr(p, s): setattr(p, 'n'+s, len(getattr(p, s))) … … 158 138 nErr = ooCheck(p) 159 139 if nErr: p.err("prob check results: " +str(nErr) + "ERRORS!")#however, I guess this line will be never reached. 160 161 if hasattr(p, 'delayedConnectIterFcn'):162 if p.solver.__iterfcnConnected__: p.warn('solver ' + p.solver.__name__ + ' has native iterfcn, user-defined connection via p.connectIterFcn will be ignored')163 else:164 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')165 p.connectIterFcn('df')166 else:167 if not p.solver.__iterfcnConnected__:168 p.connectIterFcn('df', calledByUser = False)169 140 170 141 if not p.solver.__iterfcnConnected__: … … 226 197 else: p.iterfcn(p.xf, p.ff) 227 198 228 if (p.userProvided.c and any(isnan(p.c(p.xf)))) or (p.userProvided.h and any(isnan(p.h(p.xf)))): 229 #p. 230 if p.iprint >= -1: p.warn('some non-linear constraints are equal to NaN') 199 p.__finalize__() 231 200 232 201 r = OpenOptResult() trunk/openopt/scikits/openopt/Kernel/setDefaultIterFuncs.py
r960 r1091 13 13 IS_NAN_IN_X = -4 14 14 IS_LINE_SEARCH_FAILED = -5 15 IS_MAX_ITER_REACHED = -7 15 IS_MAX_ITER_REACHED = -7 16 16 IS_MAX_CPU_TIME_REACHED = -8 17 17 IS_MAX_TIME_REACHED = -9 … … 20 20 21 21 FAILED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON = -1000 22 22 23 23 def stopcase(arg): 24 24 if hasattr(arg, 'istop'): istop = arg.istop 25 25 else: istop = arg 26 26 27 27 if istop > 0: return 1 28 28 elif istop in [IS_MAX_ITER_REACHED, IS_MAX_CPU_TIME_REACHED, IS_MAX_TIME_REACHED, IS_MAX_FUN_EVALS_REACHED]: return 0 29 29 else: return -1 30 31 30 32 31 33 def setDefaultIterFuncs(): 32 33 def setDefaultIterFuncs(className): 34 34 d = dict() 35 35 36 36 # positive: 37 37 d[SMALL_DF] = lambda p: small_df(p) … … 46 46 d[IS_MAX_CPU_TIME_REACHED] = lambda p: isMaxCPUTimeReached(p) 47 47 d[IS_MAX_TIME_REACHED] = lambda p: isMaxTimeReached(p) 48 d[IS_MAX_FUN_EVALS_REACHED] = lambda p: isMaxFunEvalsReached(p)48 if className == 'NonLin': d[IS_MAX_FUN_EVALS_REACHED] = lambda p: isMaxFunEvalsReached(p) 49 49 return d 50 50 51 51 def small_df(p): 52 if not hasattr(p, '_df') or p._df == None or p.norm(p._df) >= p.gradtol: return False 52 if not hasattr(p, '_df') or p._df == None or p.norm(p._df) >= p.gradtol: return False 53 53 return (True, '|| gradient F(X[k]) || < gradtol') 54 54 #return False if not hasattr(p, 'dF') or p.dF == None or p.norm(p.dF) > p.gradtol else True 55 55 56 56 def small_deltaX(p): 57 57 if p.iter == 0: return False … … 61 61 else: return (True, '|| X[k] - X[k-1] || < xtol') 62 62 #r = False if p.norm(p.xk - p.x_prev) > p.xtol else True 63 64 63 64 65 65 def small_deltaF(p): 66 66 if p.iter == 0: return False 67 67 #r = False if p.norm(p.fk - p.f_prev) > p.ftol else True 68 if p.norm(p.iterValues.f[-1] - p.iterValues.f[-2]) >= p.ftol: # or (p.iterValues.r[-1] > p.contol and p.iterValues.r[-1] - p.iterValues.r[-2] < p.contol): 68 if p.norm(p.iterValues.f[-1] - p.iterValues.f[-2]) >= p.ftol: # or (p.iterValues.r[-1] > p.contol and p.iterValues.r[-1] - p.iterValues.r[-2] < p.contol): 69 69 return False 70 70 else: return (True, '|| F[k] - F[k-1] || < ftol') 71 71 72 72 73 73 def isEnough(p): … … 80 80 return (True, 'NaN in X[k] coords has been obtained') 81 81 else: return False 82 82 83 83 84 84 def isMaxIterReached(p): … … 102 102 def isMaxFunEvalsReached(p): 103 103 #if not hasattr(p, 'nFunEvals'): p.warn('no nFunEvals field'); return 0 104 if p.nEvals['f'] < p.maxFunEvals: 104 if p.nEvals['f'] >= p.maxFunEvals: 105 return (True, 'max objfunc evals limit has been reached') 106 else: 105 107 return False 106 else:107 return (True, 'max objfunc evals limit has been reached')108 108 109 109 trunk/openopt/scikits/openopt/solvers/BrasilOpt/ALGENCAN_oo.py
