使用scipy.optimize.linprog进行线性编程 [英] Linear programming with scipy.optimize.linprog

查看:429
本文介绍了使用scipy.optimize.linprog进行线性编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用scipy.optimize.linprog检查了简单的线性编程问题:

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*x[1] + 0*x[2] >= 1
0*x[1] + 1*x[2] >= 1
1*x[1] + 1*x[2] <= 6

得到非常奇怪的结果,我期望x [1]将为1,x [2]将为5,但是:

And got the very strange result, I expected that x[1] will be 1 and x[2] will be 5, but:

>>> print optimize.linprog([1, 2], A_ub=[[1, 1]], b_ub=[6], bounds=(1, 5), method='simplex')
  status: 0
   slack: array([ 4.,  4.,  4.,  0.,  0.])
 success: True
     fun: 3.0
       x: array([ 1.,  1.])
 message: 'Optimization terminated successfully.'
     nit: 2

谁能解释,为什么我得到这个奇怪的结果?

Can anyone explain, why I got this strange result?

推荐答案

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

这篇关于使用scipy.optimize.linprog进行线性编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆