Changeset 996

Show
Ignore:
Timestamp:
05/30/08 10:28:47 (6 months ago)
Author:
dmitrey.kroshko
Message:

some changes

Files:

Legend:

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

    r964 r996  
    2424    def __init__(self): 
    2525        pass 
    26          
     26 
    2727class Parallel: 
    2828    def __init__(self): 
     
    4343        return x * y 
    4444        #return asarray(x) * asarray(y) 
    45      
     45 
    4646class args: 
    4747    def __init__(self): pass 
    4848    f, c, h = (), (), () 
    49      
     49 
    5050class autocreate: 
    5151    def __init__(self): pass 
    5252 
    5353class BaseProblem(oomatrix, objFunRelated, Residuals, args): 
    54     def __init__(self):  
     54    def __init__(self): 
    5555        self.isObjFunValueASingleNumber = True 
    5656        self.prev = autocreate() # used for already obtained values store 
     
    5858        self.norm = linalg.norm 
    5959        self.name = 'unnamed' 
    60      
     60 
    6161        self.check = Check() 
    6262        self.parallel = Parallel() 
     
    6464        self.user = user() 
    6565        self.args = args() 
    66        
     66 
    6767        self.plot = False # draw picture or not 
    6868        self.show = True # use command pylab.show() after solver finish or not 
    69          
     69 
    7070        self.TimeElapsed = 0. 
    7171        self.iter = 0 
     
    7474        self.cpuTimeElapsedForPlotting = [0.] 
    7575        self.isFinished = False 
    76          
     76        self.invertObjFunc = False # True for goal = 'max' or 'maximum' 
     77 
    7778        #you can redirect these ones 
    7879        self.err = ooerr 
     
    8485        self.hint = oohint 
    8586        self.debugmsg = lambda msg: oodebugmsg(self,  msg) 
    86          
     87 
    8788        #TODO: remove it? 
    8889        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 
    9091        self.debug = 0 
    9192        # graphics.lowerBoundForPlotEstim = 0#for future implement 
    92          
     93 
    9394        self.iprint = 10 
    9495        #if iprint<0 -- no output 
    9596        #if iprint==0 -- final output only 
    96          
     97 
    9798        self.maxIter = 400 
    9899        self.maxFunEvals = 10000 
     
    104105        self.ftol = 1e-6 
    105106        self.contol = 1e-6 
    106          
     107 
    107108        self.userStop = False # becomes True is stopped by user 
    108          
     109 
    109110        self.x0 = nan 
    110111        self.intVars = [] # for problems like MILP 
    111112        self.binVars = [] # for problems like MILP 
    112          
     113 
    113114        self.noise = ProbDefaults['noise'] 
    114115        self.consMode = 'all' 
    115          
     116 
    116117        #TODO: move it to non-lin funcs classes only 
    117118        self.prevVal = {} 
    118119        for fn in ['f', 'c', 'h', 'df', 'dc', 'dh', 'd2f', 'd2c', 'd2h']: 
    119120            self.prevVal[fn] = {'key':None, 'val':None} 
    120          
     121 
    121122        # A * x <= b inequalities 
    122123        self.A = None 
    123124        self.b = None 
    124          
     125 
    125126        # Aeq * x = b equalities 
    126127        self.Aeq = None 
    127128        self.beq = None 
    128          
     129 
    129130        #obsolete, should be removed 
    130131        # Awhole * x {<= | = | >= } b 
    131132        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 
    134135        # descriptors dwhole[j] should be : 
    135136        # 1 : <Awhole, x> [j] greater (or equal) than bwhole[j] 
    136137        # -1 : <Awhole, x> [j] less (or equal) than bwhole[j] 
    137138        # 0 : <Awhole, x> [j] = bwhole[j] 
    138          
     139 
    139140        #finite-difference gradient aproximation step 
    140141        self.diffInt = ProbDefaults['diffInt'] 
    141          
     142 
    142143        self.scale = None 
    143          
     144 
    144145        self.isVectoriezed = 0# isn't tested properly yet 
    145146 
    146147        #f0 = None#TODO: handle me properly 
    147                          
     148 
    148149        #non-linear constraints 
    149150        self.c = None # c(x)<=0 
    150151        self.h = None # h(x)=0 
    151          
     152 
    152153        self.goal = 'minimum' 
    153154        # other possible values: 'maximum' 
    154155        # todo: implement in a field self classification: 
    155156        # 'minimax', 'multiobjective'(or goalattain?), etc 
    156          
     157 
    157158        self.fPattern = None 
    158159        self.cPattern = None 
    159160        self.hPattern = None 
    160          
     161 
    161162        ##primal.* fields are for OpenOpt developers only, not common users 
    162163        #primal.f = None 
     
    169170        #primal.d2c = None 
    170171        #primal.d2h = None 
    171          
     172 
    172173        self.color = 'b' # blue, color for plotting 
    173174        self.specifier = '-'# simple line for plotting 
     
    176177        self.ylim = (nan,  nan) 
    177178        self.legend = '' 
    178          
     179 
    179180        self.fixedCoords = None # numbers of fixed variables, for future implementation 
    180          
    181         self.istop = 0         
    182          
     181 
     182        self.istop = 0 
     183 
    183184        self.fEnough = -inf # if value less than fEnough will be obtained 
    184185        # and all constraints no greater than contol 
     
    186187        # this param is handled in iterfcn of OpenOpt Kernel 
    187188        # so it may be ignored with some solvers not closely connected to OO Kernel 
    188          
     189 
    189190        self.kernelIterFuncs = setDefaultIterFuncs() 
    190          
     191 
    191192        self.callback = [] 
    192          
    193         self.solverParams = autocreate()     
    194          
     193 
     194        self.solverParams = autocreate() 
     195 
    195196        self.userProvided = autocreate() 
    196          
     197 
    197198        self.special = autocreate() 
    198      
     199 
    199200    def __isFiniteBoxBounded__(self): # TODO: make this function 'lazy' 
    200201        return all(isfinite(self.ub)) and all(isfinite(self.lb)) 
    201          
     202 
    202203    def __isUnconstrained__(self): 
    203204        s = ((), [], array([]), None) 
     
    208209        s = ((), [], array([]), None) 
    209210        return self.b.size ==0 and self.beq.size==0 and not self.userProvided.c and not self.userProvided.h 
    210      
     211 
    211212    def __1stBetterThan2nd__(self,  f1, f2,  r1=None,  r2=None): 
    212213        if self.isUC: 
     
    214215            return f1 < f2 
    215216        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 
    218219    def __1stCertainlyBetterThan2ndTakingIntoAcoountNoise__(self,   f1, f2,  r1=None,  r2=None): 
    219220        if self.isUC: 
     
    223224            #return (r1 + self.noise < r2 - self.noise and  self.contol < r2) or \ 
    224225            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) 
    226227 
    227228 
     
    237238##        if self.istop: raise isSolved 
    238239        return r 
    239          
     240 
    240241    def connectIterFcn(self, funcname): 
    241         if not hasattr(self, 'delayedConnectIterFcn'):  
     242        if not hasattr(self, 'delayedConnectIterFcn'): 
    242243            self.delayedConnectIterFcn = funcname 
    243244            return 
     
    247248        setattr(self, funcname, self.auxFunc) 
    248249        self.hasAuxInvolved = False 
    249      
     250 
    250251    def objFuncMultiple2Single(self, f): 
    251252        #this function can be overdetermined by child class 
    252253        if asfarray(f).size != 1: self.err('unexpected f size. The function should be redefined in OO child class, inform OO developers') 
    253254        return f 
    254          
     255 
    255256        self.err('OpenOpt error: this function should be overdetermined by child class') 
    256      
     257 
    257258    def checkdf(self, *args,  **kwargs): 
    258259        return ooCheckGradient(self, 'df', *args,  **kwargs) 
    259          
     260 
    260261    def checkdc(self, *args,  **kwargs): 
    261262        return ooCheckGradient(self, 'dc', *args,  **kwargs) 
    262      
     263 
    263264    def checkdh(self, *args,  **kwargs): 
    264265        return ooCheckGradient(self, 'dh', *args,  **kwargs) 
    265      
     266 
    266267    def __makeCorrectArgs__(self): 
    267268        argslist = dir(self.args) 
  • trunk/openopt/scikits/openopt/Kernel/runProbSolver.py

    r994 r996  
    104104 
    105105    if p.goal in ['max','maximum']: p.invertObjFunc = True 
    106     else: p.invertObjFunc = False 
     106 
    107107    #TODO: remove it! 
    108108    p.advanced = EmptyClass() 
  • trunk/openopt/scikits/openopt/examples/badlyScaled.py

    r843 r996  
    22from scikits.openopt import * 
    33 
    4 f = lambda x: (x[0]-20)**2+(1e-8 * x[1] - 80)**2 # objFun 
     4coeff = 1e-8 
     5 
     6f = lambda x: (x[0]-20)**2+(coeff * x[1] - 80)**2 # objFun 
    57c = lambda x: (x[0]-14)**2-1 # non-lin ineq constraint(s) c(x) <= 0 
    68# for the problem involved: f_opt =25, x_opt = [15.0, 8.0e9] 
     
    1618""" 
    1719 for to improve the solution we will use 
    18  changing either p.diffInt from default 1e-7 to [1e-7,  10]  
    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 
    2123 latter (using p.scale) is more recommended 
    22  because it affects xtol for those solvers  
     24 because it affects xtol for those solvers 
    2325 who use OO stop criteria 
    2426 (ralg, lincher, nsmm, nssolve and mb some others) 
    2527  xtol will be compared to scaled x shift: 
    2628 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 
    2931 numpy arrays, matrices, Python lists, tuples 
    3032 """ 
    31 p = NLP(f, x0, c=c, scale = [1,  1e-8], **someModifiedStopCriteria) 
     33p = NLP(f, x0, c=c, scale = [1,  coeff], **someModifiedStopCriteria) 
    3234r = p.solve('ralg') 
    3335print 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  
    1717    hmult = 0.5 
    1818    T = float64 
    19     needRej = lambda self, b, g: norm(g, 1)  < 1e-10 
     19    needRej = lambda self, p, b, g: norm(b.flatten(), 1)  < min(1e-10, 1e-5*p.gradtol) 
    2020    #checkTurnByGradient = True 
    2121 
     
    202202            p._df = g2.copy() 
    203203 
    204             if not self.needRej(b, g): 
     204            if not self.needRej(p, b, g): 
    205205                if all(isfinite(g)) and ng > 1e-50 and doDilation: 
    206206                    g = (g / ng).reshape((-1,1))