Ticket #6456: 6456-cvxopt-glpk-interface.patch

File 6456-cvxopt-glpk-interface.patch, 2.4 KB (added by dimpase, 9 years ago)

option to linear_program() to use glpk as the solver

  • sage/numerical/optimize.py

    # HG changeset patch
    # User Dmitrii Pasechnik <dimpase@gmail.com>
    # Date 1289397322 -28800
    # Node ID bab1edefee5568cd8b1f22d8924c119a1eeb5b9b
    # Parent  1f29c94e03ac8c047bd2ac6358d3a649d73724eb
    6456 option to linear_program() to use glpk as the solver
    
    diff -r 1f29c94e03ac -r bab1edefee55 sage/numerical/optimize.py
    a b  
    415415    return vector(RDF,min)
    416416
    417417   
    418 def linear_program(c,G,h,A=None,b=None):
     418def linear_program(c,G,h,A=None,b=None,solver=None):
    419419    """
    420420    Solves the dual linear programs:
    421421
     
    436436    - ``A`` -- a matrix
    437437
    438438    - ``b`` --- a vector
     439
     440    - ``solver`` (optional) --- solver to use. If None, the cvxopt's lp-solver
     441                                is used. If it is 'glpk', then glpk's solver
     442                                is used.
    439443 
    440444    These can be over any field that can be turned into a floating point
    441445    number.
     
    474478        sage: sol=linear_program(v,m,h)
    475479        sage: sol['x']
    476480        (45.000000..., 6.2499999...3, 1.00000000...)
     481        sage: sol=linear_program(v,m,h,solver='glpk')
     482        sage: sol['x']
     483        (45.0..., 6.25, 1.0...)
    477484    """
    478485    from cvxopt.base import matrix as m
    479486    from cvxopt import solvers
    480487    solvers.options['show_progress']=False
     488    if solver=='glpk':
     489        from cvxopt import glpk
     490        glpk.options['LPX_K_MSGLEV'] = 0
    481491    c_=m(c.base_extend(RDF).numpy())
    482492    G_=m(G.base_extend(RDF).numpy())
    483493    h_=m(h.base_extend(RDF).numpy())
    484494    if A!=None and b!=None:
    485495        A_=m(A.base_extend(RDF).numpy())
    486496        b_=m(b.base_extend(RDF).numpy())
    487         sol=solvers.lp(c_,G_,h_,A_,b_)
     497        sol=solvers.lp(c_,G_,h_,A_,b_,solver=solver)
    488498    else:
    489         sol=solvers.lp(c_,G_,h_)
     499        sol=solvers.lp(c_,G_,h_,solver=solver)
     500    status=sol['status']
     501    if status != 'optimal':
     502       return  {'primal objective':None,'x':None,'s':None,'y':None,
     503              'z':None,'status':status}
    490504    x=vector(RDF,list(sol['x']))
    491505    s=vector(RDF,list(sol['s']))
    492506    y=vector(RDF,list(sol['y']))
    493507    z=vector(RDF,list(sol['z']))
    494     return  {'x':x,'s':s,'y':y,'z':z}
     508    return  {'primal objective':sol['primal objective'],'x':x,'s':s,'y':y,
     509               'z':z,'status':status}
    495510
    496511
    497512def find_fit(data, model, initial_guess = None, parameters = None, variables = None, solution_dict = False):