Linear programming with scipy.optimize.linprog

后端 未结 1 1221
刺人心
刺人心 2020-12-30 12:32

I\'ve just check the simple linear programming problem with scipy.optimize.linprog:

1*x[1] + 2x[2] -> max

1*x[1] + 0*x[2] <= 5
0*x[1] + 1*x[2] <= 5         


        
相关标签:
1条回答
  • 2020-12-30 13:11

    optimize.linprog always minimizes your target function. If you want to maximize instead, you can use that max(f(x)) == -min(-f(x))

    from scipy import optimize
    
    optimize.linprog(
        c = [-1, -2], 
        A_ub=[[1, 1]], 
        b_ub=[6],
        bounds=(1, 5),
        method='simplex'
    )
    

    This will give you your expected result, with the value -f(x) = -11.0

     slack: array([ 0.,  4.,  0.,  4.,  0.])
     message: 'Optimization terminated successfully.'
         nit: 3
           x: array([ 1.,  5.])
      status: 0
     success: True
         fun: -11.0
    
    0 讨论(0)
提交回复
热议问题