Changeset 405

Show
Ignore:
Timestamp:
01/30/10 19:30:00 (1 month ago)
Author:
dmitrey
Message:

add export LP/MILP to MPS + minor code cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • PythonPackages/OpenOpt/openopt/kernel/DFP.py

    r288 r405  
    5 5  
    6 6 class DFP(NonLinProblem): 
    7       __optionalData__ = ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'c', 'h'] 
      7     _optionalData = ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'c', 'h'] 
    8 8     probType = 'DFP' 
    9 9     expectedArgs = ['f', 'x0', 'X', 'Y'] 
  • PythonPackages/OpenOpt/openopt/kernel/GLP.py

    r262 r405  
    6 6 class GLP(NonLinProblem): 
    7 7     probType = 'GLP' 
    8       __optionalData__ = ['lb', 'ub', 'c', 'A', 'b'] 
      8     _optionalData = ['lb', 'ub', 'c', 'A', 'b'] 
    9 9     expectedArgs = ['f', 'x0'] 
    10 10     allowedGoals = ['minimum', 'min', 'maximum', 'max'] 
  • PythonPackages/OpenOpt/openopt/kernel/LLAVP.py

    r333 r405  
    6 6  
    7 7 class LLAVP(MatrixProblem): 
    8       __optionalData__ = ['damp', 'X', 'c'] 
      8     _optionalData = ['damp', 'X', 'c'] 
    9 9     allowedGoals = ['minimum', 'min'] 
    10 10     def __init__(self, *args, **kwargs): 
  • PythonPackages/OpenOpt/openopt/kernel/LLSP.py

    r334 r405  
    6 6  
    7 7 class LLSP(MatrixProblem): 
    8       __optionalData__ = ['damp', 'X', 'c'] 
      8     _optionalData = ['damp', 'X', 'c'] 
    9 9     expectedArgs = ['C', 'd']# for FD it should be Cd and x0 
    10 10     probType = 'LLSP' 
  • PythonPackages/OpenOpt/openopt/kernel/LP.py

    r255 r405  
    1 1 from ooMisc import assignScript 
      2 from nonOptMisc import isspmatrix 
    2 3 from baseProblem import MatrixProblem 
    3   from numpy import asarray, ones, inf, dot, nan, zeros, isnan, any 
      4 from numpy import asarray, ones, inf, dot, nan, zeros, isnan, any, vstack, array, asfarray 
    4 5 import NLP 
    5 6  
    6 7 class LP(MatrixProblem): 
    7       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
      8     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
    8 9     expectedArgs = ['f', 'x0'] 
    9 10     goal = 'minimum' 
     
    11 12     allowedGoals = ['minimum', 'min', 'max', 'maximum'] 
    12 13     showGoal = True 
      14     _lp_prepared = False 
    13 15  
    14 16     def __init__(self, *args, **kwargs): 
     
    18 20  
    19 21     def __prepare__(self): 
      22         if self._lp_prepared: return 
      23         self._lp_prepared = True 
    20 24         MatrixProblem.__prepare__(self) 
    21 25         if self.x0 is None: self.x0 = zeros(self.n) 
     
    77 81         return r 
    78 82  
      83     def exportToMPS(self, filename): 
      84         try: from lp_solve import lpsolve 
      85         except ImportError: self.err('To export LP/MILP in files you should have lpsolve and its Python binding properly installed') 
      86         handler = self.get_lpsolve_handler() 
      87          
      88         # TODO: uncomment it 
      89         ext = 'mps' if not filename.endswith('MPS') and not filename.endswith('mps') else '' 
      90         if ext != '': filename += '.' + ext 
      91          
      92         r = lpsolve('write_mps', handler, filename)  
      93         if r != True:  
      94             self.warn('Failed to write MPS file, maybe read-only filesystem, incorrect path or write access is absent') 
      95              
      96         lpsolve('delete_lp', handler)  
      97         return r 
    79 98  
      99     def get_lpsolve_handler(self): 
      100         try: from lp_maker import lp_maker, lpsolve 
      101         except ImportError: self.err('To export LP/MILP in files you should have lpsolve and its Python binding properly installed') 
      102         self.__prepare__() 
      103         from ooMisc import LinConst2WholeRepr 
      104         LinConst2WholeRepr(self) 
      105          
      106         # set goal to  min/max 
      107         f, minim = (-asfarray(self.f), 0) if self.goal in ['max', 'maximum'] else (asfarray(self.f), 1) 
      108          
      109         lp_handle = lp_maker(List(f.flatten()), List(self.Awhole), List(self.bwhole.flatten()), List(self.dwhole.flatten()), \ 
      110         List(self.lb), List(self.ub), (1+asarray(self.intVars)).tolist(), 0,minim) 
      111         
      112         #lp_handle = lpsolve('make_lp', len(self.beq)+len(self.b), self.n)  
      113         L = lambda action, *args: lpsolve(action, lp_handle, *args) 
      114          
      115         #L(goal)  
      116         # set objective 
      117         #L('set_obj', List(f)) 
      118         # set name 
      119         L('set_lp_name', self.name) 
      120          
      121         # set integer values if present 
      122         if len(self.intVars)>0: 
      123             intVars = [False]*self.n 
      124             for iv in self.intVars: 
      125                 intVars[iv] = True 
      126             L('set_int', intVars) 
      127          
      128         # set boolean values if present 
      129         #if len(self.boolVars)>0: 
      130             #assert self.boolVars[0] in [True, False] 
      131             #L('set_binary', self.boolVars) 
      132          
      133         # set variables names 
      134         if self.isFDmodel: 
      135             assert not isinstance(self.optVars, set), 'error in openopt kernel, inform developers' 
      136             assert len(self.optVars) == self.n, 'not implemented yet for oovars of size > 1' 
      137             names = [oov.name for oov in self.optVars] 
      138             r = lpsolve('set_col_name', names)  
      139          
      140         # lb, ub 
      141         #L('set_bounds', List(self.lb), List(self.lb)) 
      142         #L('', List(self.lb), List(self.lb)) 
      143          
      144 #        if isspmatrix(self.A) or isspmatrix(self.Aeq):  
      145 #            s = 'export LP/MILP to file: sparse matrices will be temporary cast to dense, direct operation is not implemented yet' 
      146 #            self.pWarn(s) 
      147 #        M = vstack((self.A.A if isspmatrix(self.A) else self.A, self.Aeq.A if isspmatrix(self.Aeq) else self.Aeq)) 
      148          
      149         # set constraints matrix 
      150         # L('set_mat', List(M)) 
      151         # set lower bounds for constraints matrix 
      152         #L('set_lowbo', [-1e100]*len(self.b)+[0.0]*len(self.beq)) 
      153         # set upper bounds for constraints matrix 
      154         #L('set_upbo', ([0.0]*(len(self.b)+len(self.beq)))) 
      155          
      156          
      157         return lp_handle 
      158          
      159 def List(x): 
      160     if isinstance(x, list): return x 
      161     elif x == None or x.size == 0: return None 
      162     else: return x.tolist()         
    80 163  
    81    
    82    
  • PythonPackages/OpenOpt/openopt/kernel/MILP.py

    r301 r405  
    9 9  
    10 10 class MILP(LP): 
    11       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'intVars', 'boolVars'] 
      11     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'intVars', 'boolVars'] 
    12 12     storeIterPoints = False 
    13 13     probType = 'MILP' 
     
    16 16     allowedGoals = ['minimum', 'min', 'max', 'maximum'] 
    17 17     showGoal = True 
      18     _milp_prepared = False 
    18 19      
    19 20     def __prepare__(self): 
      21         if self._milp_prepared: return 
      22         self._milp_prepared = True 
    20 23         LP.__prepare__(self) 
    21 24         r = [] 
  • PythonPackages/OpenOpt/openopt/kernel/MINLP.py

    r185 r405  
    4 4  
    5 5 class MINLP(NonLinProblem): 
    6       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h', 'discreteVars'] 
      6     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h', 'discreteVars'] 
    7 7     goal = 'minimum' 
    8 8     probType = 'MINLP' 
  • PythonPackages/OpenOpt/openopt/kernel/MMP.py

    r339 r405  
    8 8     Mini-Max Problem 
    9 9     """ 
    10       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
      10     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
    11 11     expectedArgs = ['f', 'x0'] 
    12 12     allowedGoals = ['minimax'] 
  • PythonPackages/OpenOpt/openopt/kernel/NLLSP.py

    r339 r405  
    4 4  
    5 5 class NLLSP(NonLinProblem): 
    6       __optionalData__ = ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'c', 'h'] 
      6     _optionalData = ['lb', 'ub', 'A', 'Aeq', 'b', 'beq', 'c', 'h'] 
    7 7     showGoal = False 
    8 8     goal = 'minimum' 
  • PythonPackages/OpenOpt/openopt/kernel/NLP.py

    r185 r405  
    5 5  
    6 6 class NLP(NonLinProblem): 
    7       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
      7     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
    8 8     expectedArgs = ['f', 'x0'] 
    9 9     goal = 'minimum' 
  • PythonPackages/OpenOpt/openopt/kernel/NLSP.py

    r249 r405  
    8 8  
    9 9 class NLSP(NonLinProblem): 
    10       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
      10     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
    11 11     probType = 'NLSP' 
    12 12     goal = 'minimize residual' 
  • PythonPackages/OpenOpt/openopt/kernel/NSP.py

    r185 r405  
    4 4  
    5 5 class NSP(NonLinProblem): 
    6       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
      6     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'c', 'h'] 
    7 7     expectedArgs = ['f', 'x0'] 
    8 8     goal = 'minimum' 
  • PythonPackages/OpenOpt/openopt/kernel/QP.py

    r206 r405  
    15 15     allowedGoals = ['minimum', 'min'] 
    16 16     showGoal = False 
    17       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
      17     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
    18 18     expectedArgs = ['H', 'f'] 
    19 19      
  • PythonPackages/OpenOpt/openopt/kernel/SDP.py

    r207 r405  
    11 11  
    12 12 class SDP(MatrixProblem): 
    13       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'S', 'd'] 
      13     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub', 'S', 'd'] 
    14 14     expectedArgs = ['f'] 
    15 15     goal = 'minimum' 
  • PythonPackages/OpenOpt/openopt/kernel/SLE.py

    r379 r405  
    22 22     FuncDesignerSign = 'C' 
    23 23     solver = 'defaultSLEsolver' 
    24       __optionalData__ = [] 
      24     _optionalData = [] 
    25 25     _isPrepared = False 
    26 26      
  • PythonPackages/OpenOpt/openopt/kernel/SOCP.py

    r379 r405  
    12 12 class SOCP(MatrixProblem): 
    13 13     probType = 'SOCP' 
    14       __optionalData__ = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
      14     _optionalData = ['A', 'Aeq', 'b', 'beq', 'lb', 'ub'] 
    15 15     goal = 'minimum' 
    16 16     allowedGoals = ['minimum', 'min'] 
  • PythonPackages/OpenOpt/openopt/kernel/baseProblem.py

    r380 r405  
    105 105     Aeq = None 
    106 106     beq = None 
    107    
      107      
    108 108     scale = None 
    109 109  
     
    162 162         self.constraints = [] # used in isFDmodel 
    163 163  
    164            
    165 164         self.callback = [] # user-defined callback function(s) 
    166 165          
     
    268 267              
    269 268             setStartVectorAndTranslators(self) 
    270               lb, ub = array([-inf] * self.n), array([inf] * self.n) 
      269              
      270             lb, ub = -inf*ones(self.n), inf*ones(self.n) 
    271 271  
    272 272             # TODO: get rid of start c, h = None, use [] instead 
    273 273             A, b, Aeq, beq = [], [], [], [] 
      274              
    274 275             Z = self._vector2point(zeros(self.n)) 
      276              
    275 277             if hasattr(self.constraints, 'isConstraint'): 
    276 278                 self.constraints = [self.constraints] 
     
    391 393             else: 
    392 394                 setattr(self, fn, asfarray([]).reshape(0, self.n)) 
    393    
      395          
    394 396         nA, nAeq = prod(self.A.shape), prod(self.Aeq.shape)  
    395 397         SizeThreshold = 2 ** 15 
     
    404 406              
    405 407         self._baseProblemIsPrepared = True 
      408          
    406 409  
    407 410  
  • PythonPackages/OpenOpt/openopt/kernel/check.py

    r170 r405  
    14 14 #            p.err('the solver ' + p.solverName + ' cannot handle ' + "'" + fn + "' data") 
    15 15  
    16       for fn in p.__optionalData__
      16     for fn in p._optionalData
    17 17         if hasattr(p, fn): 
    18 18             attr = getattr(p, fn) 
  • PythonPackages/OpenOpt/openopt/kernel/ooMisc.py

    r301 r405  
    1 1 __docformat__ = "restructuredtext en" 
    2   from numpy import zeros, ones, copy, isfinite, where, asarray, inf, array, asfarray, dot 
      2 from numpy import zeros, ones, copy, isfinite, where, asarray, inf, array, asfarray, dot, ndarray 
    3 3  
    4 4 def Len(arg): 
    5       if arg == None or arg == [] or (arg.size==1 and arg == array(None, dtype=object)): 
      5     if arg == None or arg == [] or (isinstance(arg, ndarray) and arg.size==1 and arg == array(None, dtype=object)): 
    6 6         return 0 
    7 7     elif type(arg) in [int, float]: