You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mycode/intLP.m

50 lines
882 B

%整数规划
%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);