Changeset 1039

Show
Ignore:
Timestamp:
05/08/11 13:28:45 (1 month ago)
Author:
dmitrey
Message:

OO examples: changes toward Python3 compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • PythonPackages/OpenOpt/openopt/examples/llavp_1.py

    r170 r1039  
    1   __docformat__ = "restructuredtext en" 
    2    
    3 1 from numpy import empty, sin, cos, arange, ones 
    4 2 from openopt import LLAVP 
     
    8 6 d =  empty(M) 
    9 7  
    10   for j in xrange(M): 
      8 for j in range(M): 
    11 9     d[j] = 1.5*N+80*sin(j) 
    12 10     C[j] = 8*sin(4.0+arange(N)) + 15*cos(j) 
     
    23 21  
    24 22  
    25   print 'f_opt:', r.ff 
      23 print('f_opt: %f' % r.ff) 
    26 24 #print 'x_opt:', r.xf 
    27 25  
  • PythonPackages/OpenOpt/openopt/examples/llsp_1.py

    r426 r1039  
    8 8 d =  empty(M) 
    9 9  
    10   for j in xrange(M): 
      10 for j in range(M): 
    11 11     d[j] = 1.5*N+80*sin(j) 
    12 12     C[j] = 8*sin(4.0+arange(N)) + 15*cos(j) 
     
    25 25 r = p.solve('lsqr') 
    26 26  
    27   print 'f_opt:', r.ff # 2398301.68347 
      27 print('f_opt: %f' % r.ff) # 2398301.68347 
    28 28 #print 'x_opt:', r.xf 
    29 29  
  • PythonPackages/OpenOpt/openopt/examples/lp_1.py

    r185 r1039  
    27 27 #or p = LP(f=f, A=A, Aeq=Aeq, b=b, beq=beq, lb=lb, ub=ub) 
    28 28  
    29   r = p.solve('cvxopt_lp') # CVXOPT must be installed 
    30   p.debug=1 
    31   #r = p.solve('lpSolve') # lpsolve must be installed 
      29 #r = p.solve('glpk') # CVXOPT must be installed 
      30 r = p.solve('lpSolve') # lpsolve must be installed 
    32 31 #search for max: r = p.solve('glpk', goal='max') # CVXOPT & glpk must be installed 
    33 32 #r = p.solve('nlp:ralg', ftol=1e-7, xtol=1e-7, goal='min', plot=1)  
  • PythonPackages/OpenOpt/openopt/examples/milp_1.py

    r868 r1039  
    22 22  
    23 23 p = MILP(f=f, lb=lb, ub=ub, A=A, b=b, intVars=intVars, goal='min') 
    24   #r = p.solve('lpSolve') 
    25   r = p.solve('glpk'
      24 r = p.solve('lpSolve') 
      25 #r = p.solve('glpk', iprint =-1
    26 26 #r = p.solve('cplex') 
    27   print 'f_opt:', r.ff # 25.801450769161505 
    28   print 'x_opt:', r.xf # [ 15. 10.15072538 -1.5 -1.5 -1.  -1.5 -1.5 15.] 
    29 27  
    30   """ 
    31   if you have installed glpk+cvxopt 1.0 or later  
    32   (with BUILD_GLPK=1 in setup.py file)  
    33   you can handle MILP problems with binary constraints  
    34   (coords x from p.binVars should be in {0, 1}): 
    35    
    36   p = MILP(f=f, lb=lb, ub=ub, A=A, b=b, intVars=intVars, binVars=[1]) 
    37   #intVars, binVars indexing from ZERO! 
    38   r = p.solve('glpk')  
    39    
    40   print 'f_opt:', r.ff # 25.8014507692 
    41   print 'x_opt:', r.xf # [ 15. 10.15072538  -1.5  -1.5  -1. -1.5  -1.5  15.] 
    42   """ 
      28 print('f_opt: %f' % r.ff) # 25.801450769161505 
      29 print('x_opt: %s' % r.xf) # [ 15. 10.15072538 -1.5 -1.5 -1.  -1.5 -1.5 15.] 
  • PythonPackages/OpenOpt/openopt/examples/miqcqp_1.py

    r901 r1039  
    26 26 # or p = QP(H=diag([1,2,3]), f=[15,8,80], ...) 
    27 27  
    28   r = p.solve('cplex', iprint = 0
      28 r = p.solve('cplex', iprint = 0, plot=1
    29 29 f_opt, x_opt = r.ff, r.xf 
    30 30 # x_opt = array([ -2.99999999,   9.5       , -16.        ]) 
  • PythonPackages/OpenOpt/openopt/examples/nllsp_1.py

    r170 r1039  
    44 44 #r = p.solve('scipy_leastsq', plot=1, iprint = -1) 
    45 45 #or using converter lsp2nlp: 
    46   r = p.solve('nlp:ralg', plot=1) 
      46 r = p.solve('nlp:ralg', iprint = 1, plot=1) 
    47 47 #r = p.solve('nlp:ipopt',plot=1), r = p.solve('nlp:algencan'), r = p.solve('nlp:ralg'), etc 
    48 48 #(some NLP solvers require additional installation) 
  • PythonPackages/OpenOpt/openopt/examples/nlp_1.py

    r748 r1039  
    103 103  
    104 104 solver = 'ralg' 
      105 #solver = 'algencan' 
    105 106 #solver = 'ipopt' 
    106 107 #solver = 'scipy_slsqp' 
    107 108  
    108 109 # solve the problem 
    109   r = p.solve(solver) # string argument is solver name 
      110  
      111 r = p.solve(solver, plot=0) # string argument is solver name 
    110 112  
    111 113  
  • PythonPackages/OpenOpt/openopt/examples/nlp_d2f.py

    r170 r1039  
    20 20 # p = NLP(x0 = cos(arange(N)), f = lambda x: ((x-M)**4).sum(), df =  lambda x: 4*(x-M)**3, d2f = lambda x: diag(12*(x-M)**2)) 
    21 21 r = p.solve('scipy_ncg') 
    22   print 'objfunc val:', r.ff # it should be a small positive like 5.23656378549e-08 
      22 print('objfunc val: %e' % r.ff) # it should be a small positive like 5.23656378549e-08 
    23 23  
  • PythonPackages/OpenOpt/openopt/examples/nlsp_1.py

    r179 r1039  
    36 36  
    37 37 #r = p.solve('scipy_fsolve') 
    38   r = p.solve('nssolve') 
      38 #r = p.solve('nssolve') 
    39 39 #or using converter nlsp2nlp, try to minimize sum(f_i(x)^2): 
    40   #r = p.solve('nlp:ralg', plot=1) 
      40 r = p.solve('nlp:ralg', plot=1) 
    41 41  
    42   print 'solution:', r.xf 
    43   print 'max residual:', r.ff 
      42 print('solution: %s' % r.xf) 
      43 print('max residual: %e' % r.ff) 
    44 44 ############################### 
    45 45 #should print: 
    46   #solution: [  1.           2.          55.50147021] 
      46 #solution: [  1.           2.          55.50147021] (3rd coord may differ due to cos is periodic) 
    47 47 #max residual: 2.72366951215e-09 
  • PythonPackages/OpenOpt/openopt/examples/nlsp_constrained.py

    r249 r1039  
    69 69 r = p.solve('nlp:ralg', xlabel='iter', iprint=10, plot=1) 
    70 70  
    71   print 'solution:', r.xf 
    72   print 'max residual:', r.ff 
      71 print('solution: %s' % r.xf) 
      72 print('max residual: %e' % r.ff) 
    73 73  
      74  
  • PythonPackages/OpenOpt/openopt/examples/nsp_1.py

    r181 r1039  
    14 14 N = 75 
    15 15 objFun = lambda x: sum(1.2 ** arange(len(x)) * abs(x)) 
      16  
    16 17 x0 = cos(1+asfarray(range(N))) 
    17 18  
     
    32 33 p.df = lambda x: 1.2 ** arange(len(x)) * sign(x) 
    33 34  
    34    
    35    
    36   #p.plot = 1 
    37   p.xlim = (inf,  5) 
    38   p.ylim = (0, 5000000) 
    39 35 r = p.solve('ralg') # ralg is name of a solver 
    40   print 'x_opt:\n', r.xf 
    41   print 'f_opt:', r.ff  # should print small positive number like 0.00056 
      36 print('x_opt: %s' % r.xf) 
      37 print('f_opt: %f' % r.ff)  # should print small positive number like 0.00056 
  • PythonPackages/OpenOpt/openopt/examples/qcqp_1.py

    r901 r1039  
    23 23 QC = ((diag([1.0, 2.5, 3.0]), [0.1, 0.2, 0.3], -1000), (diag([2.0, 1.0, 3.0]), [0.1, 0.5, 0.3], -1000)) 
    24 24  
    25   p = QP(H, f, A = A, b = b, Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf], QC = QC
      25 p = QP(H, f, A = A, b = b, Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf], QC = QC, name='OpenOpt QCQP example 1'
    26 26 # or p = QP(H=diag([1,2,3]), f=[15,8,80], ...) 
    27 27  
    28   r = p.solve('cplex', iprint = 0
      28 r = p.solve('cplex', iprint = 0, plot=1
    29 29 f_opt, x_opt = r.ff, r.xf 
    30 30 # x_opt = array([ -2.99999999,   9.5       , -16.        ]) 
  • PythonPackages/OpenOpt/openopt/examples/qp_1.py

    r832 r1039  
    6 6 x1 + 2x2 + 3x3 <= 150            (2) 
    7 7 8x1 +  15x2 +  80x3 <= 800    (3) 
    8   x2 - x3 = 25                              (4) 
      8 x2 - x3 = 25.5                           (4) 
    9 9 x1 <= 15                                  (5) 
    10 10 """ 
     
    12 12 from numpy import diag, matrix, inf 
    13 13 from openopt import QP 
    14   p = QP(diag([1,2,3]), [15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf]) 
    15   # or p = QP(H=diag([1,2,3]), f=[15,8,80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25, ub = [15,inf,inf]) 
    16   #r = p.solve('cvxopt_qp', iprint = 0) 
    17   r = p.solve('qlcp', iprint = 0) 
    18   #r = p.solve('nlp:ralg', xtol=1e-7, alp=3.9, plot=1)#, r = p.solve('nlp:algencan') 
      14 p = QP(diag([1, 2, 3]), [15, 8, 80], A = matrix('1 2 3; 8 15 80'), b = [150, 800], Aeq = [0, 1, -1], beq = 25.5, ub = [15,inf,inf]) 
      15 # or p = QP(H=diag([1,2,3]), f=[15,8,80], A= ...) 
      16 r = p._solve('cvxopt_qp', iprint = 0) 
    19 17 f_opt, x_opt = r.ff, r.xf 
    20   # x_opt = array([-14.99999995,  -2.59999996, -27.59999991]) 
    21   # f_opt = -1191.90000013 
      18 # x_opt = array([-15. ,  -2.5, -28. ]) 
      19 # f_opt = -1190.25 
  • PythonPackages/OpenOpt/openopt/examples/sdp_1.py

    r889 r1039  
    25 25 # see /examples/lp_1.py  
    26 26  
    27   r = p.solve('cvxopt_sdp', iprint = 0) 
    28   #r = p.solve('dsdp', iprint = -1) 
      27 #r = p.solve('cvxopt_sdp', iprint = 0) 
      28 r = p.solve('dsdp', iprint = -1) 
    29 29  
    30 30 f_opt, x_opt = r.ff, r.xf 
    31   print 'x_opt:',  x_opt 
    32   print 'f_opt:',  f_opt 
      31 print('x_opt: %s' % x_opt) 
      32 print('f_opt: %s' % f_opt) 
    33 33 #x_opt: [-0.36766609  1.89832827 -0.88755043] 
    34 34 #f_opt: -3.15354478797 
  • PythonPackages/OpenOpt/openopt/examples/sle_1.py

    r266 r1039  
    15 15 r = p.solve() 
    16 16  
    17   print 'max residual:', r.ff  
    18   #print 'solution:', r.xf 
      17 print('max residual: %e' % r.ff) 
      18 #print('solution: %s' % r.xf) 
    19 19