AMPL和MATLAB结合使用示例

以运输问题为例。

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;


评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值