整数规划(linprog\optimproblem解法),01规划(背包问题)

姚安欣
phzrjyvu9 5 months ago
parent a59d58046e
commit 1c2217abc4

@ -0,0 +1,49 @@
%整数规划
%P23 ej2.5
%optimproblem解法
clc; clear; close all; format long g;
prob=optimproblem;
x=optimvar('x',6,'LowerBound',0,'Type','integer');
prob.Objective=sum(x);
cnt=[35,40,50,45,55,30];
con=optimconstr(6);
con(1)=x(1)+x(6)>=35;
for i=1:5
con(i+1)=x(i)+x(i+1)>=cnt(i+1);
end
prob.Constraints.con=con;
[sol,fval,flag,out]=solve(prob);
X=sol.x;
%linprog解法
clc; clear; close all; format long g;
f=[1,1,1,1,1,1];
intcon=[1:6];
A=zeros(6,6);
A(1,1)=-1;
A(1,6)=-1;
for i=1:5
A(i+1,i)=-1;
A(i+1,i+1)=-1;
end
lb=zeros(6,1);%注意不是lb=0
b=[-35;-40;-50;-45;-55;-30];
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb);
%01规划
%背包问题
f=-[540,200,180,350,60,150,280,450,320,120];
intcon=1:10;
lb=zeros(10);
ub=ones(10);
A=[6,3,4,5,1,2,3,5,4,2];
b=30;
[x,fval]=intlinprog(f,intcon,A,b,[],[],lb,ub);
Loading…
Cancel
Save