且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用scipy.optimize.linprog进行线性编程

更新时间:2022-12-01 13:15:50

optimize.linprog总是最小化目标函数.如果要最大化,可以使用max(f(x)) == -min(-f(x))

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'
)

这将为您带来预期的结果,值为-f(x) = -11.0

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