Changeset 996
- Timestamp:
- 05/30/08 10:28:47 (6 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openopt/scikits/openopt/Kernel/BaseProblem.py
r964 r996 24 24 def __init__(self): 25 25 pass 26 26 27 27 class Parallel: 28 28 def __init__(self): … … 43 43 return x * y 44 44 #return asarray(x) * asarray(y) 45 45 46 46 class args: 47 47 def __init__(self): pass 48 48 f, c, h = (), (), () 49 49 50 50 class autocreate: 51 51 def __init__(self): pass 52 52 53 53 class BaseProblem(oomatrix, objFunRelated, Residuals, args): 54 def __init__(self): 54 def __init__(self): 55 55 self.isObjFunValueASingleNumber = True 56 56 self.prev = autocreate() # used for already obtained values store … … 58 58 self.norm = linalg.norm 59 59 self.name = 'unnamed' 60 60 61 61 self.check = Check() 62 62 self.parallel = Parallel() … … 64 64 self.user = user() 65 65 self.args = args() 66 66 67 67 self.plot = False # draw picture or not 68 68 self.show = True # use command pylab.show() after solver finish or not 69 69 70 70 self.TimeElapsed = 0. 71 71 self.iter = 0 … … 74 74 self.cpuTimeElapsedForPlotting = [0.] 75 75 self.isFinished = False 76 76 self.invertObjFunc = False # True for goal = 'max' or 'maximum' 77 77 78 #you can redirect these ones 78 79 self.err = ooerr … … 84 85 self.hint = oohint 85 86 self.debugmsg = lambda msg: oodebugmsg(self, msg) 86 87 87 88 #TODO: remove it? 88 89 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. 89 90 90 91 self.debug = 0 91 92 # graphics.lowerBoundForPlotEstim = 0#for future implement 92 93 93 94 self.iprint = 10 94 95 #if iprint<0 -- no output 95 96 #if iprint==0 -- final output only 96 97 97 98 self.maxIter = 400 98 99 self.maxFunEvals = 10000 … … 104 105 self.ftol = 1e-6 105 106 self.contol = 1e-6 106 107 107 108 self.userStop = False # becomes True is stopped by user 108 109 109 110 self.x0 = nan 110 111 self.intVars = [] # for problems like MILP 111 112 self.binVars = [] # for problems like MILP 112 113 113 114 self.noise = ProbDefaults['noise'] 114 115 self.consMode = 'all' 115 116 116 117 #TODO: move it to non-lin funcs classes only 117 118 self.prevVal = {} 118 119 for fn in ['f', 'c', 'h', 'df', 'dc', 'dh', 'd2f', 'd2c', 'd2h']: 119 120 self.prevVal[fn] = {'key':None, 'val':None} 120 121 121 122 # A * x <= b inequalities 122 123 self.A = None 123 124 self.b = None 124 125 125 126 # Aeq * x = b equalities 126 127 self.Aeq = None 127 128 self.beq = None 128 129 129 130 #obsolete, should be removed 130 131 # Awhole * x {<= | = | >= } b 131 132 self.Awhole = None # matrix m x n, n = len(x) 132 self.bwhole = None # vector, size = m x 1 133 self.dwhole = None #vector of descriptors, size = m x 1 133 self.bwhole = None # vector, size = m x 1 134 self.dwhole = None #vector of descriptors, size = m x 1 134 135 # descriptors dwhole[j] should be : 135 136 # 1 : <Awhole, x> [j] greater (or equal) than bwhole[j] 136 137 # -1 : <Awhole, x> [j] less (or equal) than bwhole[j] 137 138 # 0 : <Awhole, x> [j] = bwhole[j] 138 139 139 140 #finite-difference gradient aproximation step 140 141 self.diffInt = ProbDefaults['diffInt'] 141 142 142 143 self.scale = None 143 144 144 145 self.isVectoriezed = 0# isn't tested properly yet 145 146 146 147 #f0 = None#TODO: handle me properly 147 148 148 149 #non-linear constraints 149 150 self.c = None # c(x)<=0 150 151 self.h = None # h(x)=0 151 152 152 153 self.goal = 'minimum' 153 154 # other possible values: 'maximum' 154 155 # todo: implement in a field self classification: 155 156 # 'minimax', 'multiobjective'(or goalattain?), etc 156 157 157 158 self.fPattern = None 158 159 self.cPattern = None 159 160 self.hPattern = None 160 161 161 162 ##primal.* fields are for OpenOpt developers only, not common users 162 163 #primal.f = None … … 169 170 #primal.d2c = None 170 171 #primal.d2h = None 171 172 172 173 self.color = 'b' # blue, color for plotting 173 174 self.specifier = '-'# simple line for plotting … … 176 177 self.ylim = (nan, nan) 177 178 self.legend = '' 178 179 179 180 self.fixedCoords = None # numbers of fixed variables, for future implementation 180 181 self.istop = 0 182 181 182 self.istop = 0 183 183 184 self.fEnough = -inf # if value less than fEnough will be obtained 184 185 # and all constraints no greater than contol … … 186 187 # this param is handled in iterfcn of OpenOpt Kernel 187 188 # so it may be ignored with some solvers not closely connected to OO Kernel 188 189 189 190 self.kernelIterFuncs = setDefaultIterFuncs() 190 191 191 192 self.callback = [] 192 193 self.solverParams = autocreate() 194 193 194 self.solverParams = autocreate() 195 195 196 self.userProvided = autocreate() 196 197 197 198 self.special = autocreate() 198 199 199 200 def __isFiniteBoxBounded__(self): # TODO: make this function 'lazy' 200 201 return all(isfinite(self.ub)) and all(isfinite(self.lb)) 201 202 202 203 def __isUnconstrained__(self): 203 204 s = ((), [], array([]), None) … … 208 209 s = ((), [], array([]), None) 209 210 return self.b.size ==0 and self.beq.size==0 and not self.userProvided.c and not self.userProvided.h 210 211 211 212 def __1stBetterThan2nd__(self, f1, f2, r1=None, r2=None): 212 213 if self.isUC: … … 214 215 return f1 < f2 215 216 else:#then r1, r2 should be defined 216 return (r1 < r2 and self.contol < r2) or (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 < f2) 217 217 return (r1 < r2 and self.contol < r2) or (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 < f2) 218 218 219 def __1stCertainlyBetterThan2ndTakingIntoAcoountNoise__(self, f1, f2, r1=None, r2=None): 219 220 if self.isUC: … … 223 224 #return (r1 + self.noise < r2 - self.noise and self.contol < r2) or \ 224 225 return (r1 < r2 and self.contol < r2) or \ 225 (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 + self.noise < f2 - self.noise) 226 (((r1 <= self.contol and r2 <= self.contol) or r1==r2) and f1 + self.noise < f2 - self.noise) 226 227 227 228 … … 237 238 ## if self.istop: raise isSolved 238 239 return r 239 240 240 241 def connectIterFcn(self, funcname): 241 if not hasattr(self, 'delayedConnectIterFcn'): 242 if not hasattr(self, 'delayedConnectIterFcn'): 242 243 self.delayedConnectIterFcn = funcname 243 244 return … … 247 248 setattr(self, funcname, self.auxFunc) 248 249 self.hasAuxInvolved = False 249 250 250 251 def objFuncMultiple2Single(self, f): 251 252 #this function can be overdetermined by child class 252 253 if asfarray(f).size != 1: self.err('unexpected f size. The function should be redefined in OO child class, inform OO developers') 253 254 return f 254 255 255 256 self.err('OpenOpt error: this function should be overdetermined by child class') 256 257 257 258 def checkdf(self, *args, **kwargs): 258 259 return ooCheckGradient(self, 'df', *args, **kwargs) 259 260 260 261 def checkdc(self, *args, **kwargs): 261 262 return ooCheckGradient(self, 'dc', *args, **kwargs) 262 263 263 264 def checkdh(self, *args, **kwargs): 264 265 return ooCheckGradient(self, 'dh', *args, **kwargs) 265 266 266 267 def __makeCorrectArgs__(self): 267 268 argslist = dir(self.args) trunk/openopt/scikits/openopt/Kernel/runProbSolver.py
r994 r996 104 104 105 105 if p.goal in ['max','maximum']: p.invertObjFunc = True 106 else: p.invertObjFunc = False 106 107 107 #TODO: remove it! 108 108 p.advanced = EmptyClass() trunk/openopt/scikits/openopt/examples/badlyScaled.py
r843 r996 2 2 from scikits.openopt import * 3 3 4 f = lambda x: (x[0]-20)**2+(1e-8 * x[1] - 80)**2 # objFun 4 coeff = 1e-8 5 6 f = lambda x: (x[0]-20)**2+(coeff * x[1] - 80)**2 # objFun 5 7 c = lambda x: (x[0]-14)**2-1 # non-lin ineq constraint(s) c(x) <= 0 6 8 # for the problem involved: f_opt =25, x_opt = [15.0, 8.0e9] … … 16 18 """ 17 19 for to improve the solution we will use 18 changing either p.diffInt from default 1e-7 to [1e-7, 1 0]19 or p.scale from default None to [1, 1e- 8]20 20 changing either p.diffInt from default 1e-7 to [1e-7, 1] 21 or p.scale from default None to [1, 1e-7] 22 21 23 latter (using p.scale) is more recommended 22 because it affects xtol for those solvers 24 because it affects xtol for those solvers 23 25 who use OO stop criteria 24 26 (ralg, lincher, nsmm, nssolve and mb some others) 25 27 xtol will be compared to scaled x shift: 26 28 is || (x[k] - x[k-1]) * scale || < xtol 27 28 You can define scale and diffInt as 29 30 You can define scale and diffInt as 29 31 numpy arrays, matrices, Python lists, tuples 30 32 """ 31 p = NLP(f, x0, c=c, scale = [1, 1e-8],**someModifiedStopCriteria)33 p = NLP(f, x0, c=c, scale = [1, coeff], **someModifiedStopCriteria) 32 34 r = p.solve('ralg') 33 35 print r.ff, r.xf # "24.999996490694787 [ 1.50000004e+01 8.00004473e+09]" - much better trunk/openopt/scikits/openopt/solvers/UkrOpt/ralg_oo.py
r994 r996 17 17 hmult = 0.5 18 18 T = float64 19 needRej = lambda self, b, g: norm(g, 1) < 1e-1019 needRej = lambda self, p, b, g: norm(b.flatten(), 1) < min(1e-10, 1e-5*p.gradtol) 20 20 #checkTurnByGradient = True 21 21 … … 202 202 p._df = g2.copy() 203 203 204 if not self.needRej( b, g):204 if not self.needRej(p, b, g): 205 205 if all(isfinite(g)) and ng > 1e-50 and doDilation: 206 206 g = (g / ng).reshape((-1,1))
