以运输问题为例。
M脚本文件transp2.m:
m=80;
n=100;
orig=1:m;
dest=1:n;
supply=round(n*rand(1,m)+1);
demand=round(m*rand(1,n)+1);
if sum(supply)>sum(demand)
demand(1)=demand(1)+sum(supply)-sum(demand);
else
supply(1)=supply(1)+sum(demand)-sum(supply);
end
demand=demand*sum(supply)/sum(demand);
supply=[orig(:) supply(:)];
demand=[dest(:) demand(:)];
cost=[orig(:) rand(m,n)];
dlmwrite('orig',orig,' ');
dlmwrite('dest',dest,' ');
dlmwrite('supply',supply,' ');
dlmwrite('demand',demand,' ');
dlmwrite('cost',cost,' ');
!ampl transp2.mod
delete orig
delete dest
delete supply
delete demand
delete cost
AMPL源文件transp2.mod
set ORIG; # origins
set DEST; # destinations
param supply {ORIG} >= 0; # amounts available at origins
param demand {DEST} >= 0; # amounts required at destinations
check: sum {i in ORIG} supply[i] = sum {j in DEST} demand[j];
param cost {ORIG,DEST} >= 0; # shipment costs per unit
var Trans {ORIG,DEST} >= 0; # units to be shipped
minimize Total_Cost:
sum {i in ORIG, j in DEST} cost[i,j] * Trans[i,j];
subject to Supply {i in ORIG}:
sum {j in DEST} Trans[i,j] = supply[i];
subject to Demand {j in DEST}:
sum {i in ORIG} Trans[i,j] = demand[j];
data;
set ORIG:= include orig;
set DEST:= include dest;
param supply :=include supply;
param demand :=include demand;
param cost : include dest :=
include cost;
option solver gurobi;
solve;