[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
摘要
:当前大多使用Matlab解决运筹学中的整数规划问题,鲜有使用Python解决类似问题。本文使用Python中cvxpy库求解整数规划问题,并对问题求解过程进行总结说明。
关键词:python;整数规划;cvxpy
<>0.例题
目标函数: M a x z = 3 x 1 + x 2 + 3 x 3 \ Max \ z= 3x_1+x_2+3x_3 Max z=3x1+x
2+3x3
约束条件: { − x 1 + 2 x 2 + x 3 ≤ 4 4 x 2 − 3 x 3 ≤ 2 x 1 − 3 x 2 + 2 x 3 ≤ 3 x
1 , x 2 , x 3 ≥ 0 x 1 , x 2 , x 3 均 为 整 数 \left\{\begin{aligned}
-x_1+2x_2+x_3 \leq 4 \\ 4x_2-3x_3 \leq 2 \\ x_1-3x_2+2x_3 \leq 3\\ x_1,x_2,x_3
\geq 0\\ x_1,x_2,x_3\ 均为整数 \end{aligned}\right.⎩⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎧−x1+2x2+x3≤44x
2−3x3≤2x1−3x2+2x3≤3x1,x2,x3≥0x1,x2,x3 均为整数
<>1.Python代码
#导入numpy import numpy as np #导入numpy import cvxpy as cp #设置目标函数中变量个数 n=3
#输入目标函数的系数 c=np.array([3,1,3]) #输入约束条件的系数矩阵(3×3) a=np.array([[-1,2,1],[0,4,-3],[
1,-3,2]]) #输入b值(3×1) b=np.array([4,2,3]) #创建x,个数是3 x=cp.Variable(n,integer=True)
#明确目标函数(此时c是3×1,x是3×1,但python里面可以相乘) objective=cp.Maximize(cp.sum(c*x))
#明确约束条件,其中a是3×3,x是3×1,a*x=b(b为3×1的矩阵) constriants=[0<=x,a*x<=b] #求解问题 prob=cp.
Problem(objective,constriants) #这里solver必须使用cp.CPLEX,否则计算不出来,而CPLEX需要pip intall
CPLEX(建议使用清华镜像) resluts=prob.solve(solver=cp.CPLEX) #输入结果 print(prob.value)
#目标函数的值 print(x.value)#各x的值
<>2. 结果
<>3.总结
(1)本文只介绍了python解决整数规划问题中最基础的一个问题,希望能对初学者有所帮助。
(2)CPLEX需要pip intall CPLEX,而且是在pip install cvxpy后。而pip install
cvxpy你又会发现cmd报错,需要你安装Visual Studio XX。按照要求安装Visual Studio
XX后,可继续安装cvxpy,安装cvxpy后再安装CPLEX。
(3)Python当然可以解决普通线性规划问题,使用optimize.linprog()即可,不再累赘。