一.常规模式:针对特定数据类型的运用
% f :目标函数的系数矩阵
% intcon :整数所在位置
% A :不等式约束的变量系数矩阵
% B:不等式约束的资源数
% Aeq :等式约束的变量系数矩阵
% Beq :等式约束的资源数
% lb :变量约束下限
% ub :变量约束上限
% [x,fval] = intlinprog(f,intcon,A,B,Aeq,Beq,lb,ub); % fval代表最优解处的函数值
% x,fval = fval % 返回目标量最小值的结果
clc;
clear;
gonghuofang=3;
users=4;
distance=[1500,1200,5000;
1600,6100,2100;
1000,1200,900;
4900,1500,2000];
peisongfei=[0.16,0.23,0.15;
0.14,0.13,0.21;
0.16,0.26,0.17;
0.21,0.27,0.19];
T=distance.*peisongfei;
for j=1:3
for i=1:4
f(1,4*(j-1)+i)=T(i,j);
number(i,j)=4*(j-1)+i;
end
end
for i=1:4
for k=1:12
for j=1:3
if k==number(i,j)
A1(i,k)=1;
break;
else
A1(i,k)=0;
continue;
end
end
end
end
b1=[2000;5000;3000;6000];
for i=1:4
for k=1:12
for j=1:3
if k==number(i,j)
A2(i,k)=-1;
break;
else
A2(i,k)=0;
continue;
end
end
end
end
b2=[-1000;-2500;-1500;-3000];
for j=1:3
for k=1:12
for i=1:4
if k==number(i,j)
A3(j,k)=1;
break;
else
A3(j,k)=0;
continue;
end
end
end
end
b3=[7000;5000;4000];
intcon=1:12;
A=[A1;A2;A3];
B=[b1;b2;b3];
lb=zeros(1,12);
for k=1:12
ub(k)=inf;
end
[X,fval]=intlinprog(f,intcon,A,B,[],[],lb,ub);
X,fval
二.交互式程序
% f :目标函数的系数矩阵
% intcon :整数所在位置
% A :不等式约束的变量系数矩阵
% B:不等式约束的资源数
% Aeq :等式约束的变量系数矩阵
% Beq :等式约束的资源数
% lb :变量约束下限
% ub :变量约束上限
% [x,fval] = intlinprog(f,intcon,A,B,Aeq,Beq,lb,ub); % fval代表最优解处的函数值
% x,fval = fval % 返回目标量最小值的结果
clc;
clear;
gonghuofangnumber=input('供货方数量:');
usernumber=input('用户数量:');
for i=1:usernumber
for j=1:gonghuofangnumber
distance(i,j)=input('请输入(从左往右从上往下)各用户与第各供应方之间的距离:');
end
end
for i=1:usernumber
for j=1:gonghuofangnumber
peisongfei(i,j)=input('请输入(从左往右从上往下)各用户与第各供应方之间的单位配送费:');
end
end
for i=1:(usernumber*2+gonghuofangnumber)
B(i)=input('请输入(从左往右)不等式约束的资源数:');
end
% b1=[2000;5000;3000;6000];b2=[-1000;-2500;-1500;-3000];b3=[7000;5000;4000];B=[b1;b2;b3];
% distance=[1500,1200,5000;
% 1600,6100,2100;
% 1000,1200,900;
% 4900,1500,2000];
% peisongfei=[0.16,0.23,0.15;
% 0.14,0.13,0.21;
% 0.16,0.26,0.17;
% 0.21,0.27,0.19];
T=distance.*peisongfei;
for j=1:gonghuofangnumber
for i=1:usernumber
f(1,usernumber*(j-1)+i)=T(i,j);
number(i,j)=usernumber*(j-1)+i;
end
end
for i=1:usernumber
for k=1:(gonghuofangnumber*usernumber)
for j=1:gonghuofangnumber
if k==number(i,j)
A1(i,k)=1;
break;
else
A1(i,k)=0;
continue;
end
end
end
end
for i=1:usernumber
for k=1:(gonghuofangnumber*usernumber)
for j=1:gonghuofangnumber
if k==number(i,j)
A2(i,k)=-1;
break;
else
A2(i,k)=0;
continue;
end
end
end
end
for j=1:gonghuofangnumber
for k=1:(gonghuofangnumber*usernumber)
for i=1:usernumber
if k==number(i,j)
A3(j,k)=1;
break;
else
A3(j,k)=0;
continue;
end
end
end
end
intcon=1:usernumber*gonghuofangnumber;
A=[A1;A2;A3];
lb=zeros(1,usernumber*gonghuofangnumber);
for k=1:usernumber*gonghuofangnumber
ub(k)=inf;
end
[X,fval]=intlinprog(f,intcon,A,B,[],[],lb,ub);
X,fval
三.混合式程序
% f :目标函数的系数矩阵
% intcon :整数所在位置
% A :不等式约束的变量系数矩阵
% B:不等式约束的资源数
% Aeq :等式约束的变量系数矩阵
% Beq :等式约束的资源数
% lb :变量约束下限
% ub :变量约束上限
% [x,fval] = intlinprog(f,intcon,A,B,Aeq,Beq,lb,ub); % fval代表最优解处的函数值
% x,fval = fval % 返回目标量最小值的结果
clc;
clear;
usernumber=4;
t=input('是否有新增的供货方(是的话请输入Y,没有请输入N);','s');
if t=='Y'
s=input('新增的供货方数量(为正整数)为:');
gonghuofangnumber=3+s;
for i=1:usernumber
for j=1:s
xinzengdistance(i,j)=input('请输入(从左往右从上往下)各用户与第各供应方之间的距离:');
end
end
for i=1:usernumber
for j=1:s
xinzengpeisongfei(i,j)=input('请输入(从左往右从上往下)各用户与第各供应方之间的单位配送费:');
end
end
for i=1:s
xinzengB(i,1)=input('请输入(从左往右各新增供应方之间的资源约束:');
end
elseif t=='N'
s=0;
gonghuofangnumber=3;
else
disp('出现操作错误,请重新运行并输入相关参数');
end
%--------------------------------------------------------------------------------------------------------------------------
distance=[1500,1200,5000;
1600,6100,2100;
1000,1200,900;
4900,1500,2000];
peisongfei=[0.16,0.23,0.15;
0.14,0.13,0.21;
0.16,0.26,0.17;
0.21,0.27,0.19];
b1=[2000;5000;3000;6000];b2=[-1000;-2500;-1500;-3000];b3=[7000;5000;4000];
if s==0
distance=distance;
peisongfei=peisongfei;
B=[b1;b2;b3];
else
distance=[distance,xinzengdistance];
peisongfei=[peisongfei,xinzengpeisongfei];
B=[b1;b2;b3;xinzengB];
end
T=distance.*peisongfei;
for j=1:gonghuofangnumber
for i=1:usernumber
f(1,usernumber*(j-1)+i)=T(i,j);
number(i,j)=usernumber*(j-1)+i;
end
end
for i=1:usernumber
for k=1:(gonghuofangnumber*usernumber)
for j=1:gonghuofangnumber
if k==number(i,j)
A1(i,k)=1;
break;
else
A1(i,k)=0;
continue;
end
end
end
end
for i=1:usernumber
for k=1:(gonghuofangnumber*usernumber)
for j=1:gonghuofangnumber
if k==number(i,j)
A2(i,k)=-1;
break;
else
A2(i,k)=0;
continue;
end
end
end
end
for j=1:gonghuofangnumber
for k=1:(gonghuofangnumber*usernumber)
for i=1:usernumber
if k==number(i,j)
A3(j,k)=1;
break;
else
A3(j,k)=0;
continue;
end
end
end
end
A=[A1;A2;A3];
intcon=1:usernumber*gonghuofangnumber;
lb=zeros(1,usernumber*gonghuofangnumber);
for k=1:usernumber*gonghuofangnumber
ub(k)=inf;
end
[X,fval]=intlinprog(f,intcon,A,B,[],[],lb,ub);
X,fval