您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > lingo解决线性规划方案问题的程序
!exam_1.lg4源程序max=2*x+3*y;[st_1]x+y350;[st_2]x100;2*x+y600;!决策变量黙以为非负;相称于=;大小写不区别sets:r1/1..3/:a;r2:b;r3:c;link2(r1,r2):x;link3(r1,r2,r3):y;Lingo12软件培训教案Lingo重要用于求解线性规划,整数规划,非线性规划,V10以上版本可编程。例1一种简朴线性规划问题maxz2x3yst.xy350x1002xy600x,y0当规划问题规模很大时,需要定义数组(或称为矩阵),以及下标集(set)下面定义下标集和相应数组三种办法,效果相似::r1=r2=r3,a=b=c.ijijij例2运送问题计算6个发点8个收点最小费用运送问题。产销单位运价如下表。B1B2B3B4B5B6B7B8产量A16267425960A24953858255A35219743351A47673927143A52395726541A65522814352销量3537223241324338解:设决策变量x=第i个发点到第j个售点运货量,i=1,2,…m;j=1,2,…n;记为c=第i个发点到第j个售点运送单价,i=1,2,…m;j=1,2,…n记s=第i个发点产量,i=1,2,…m;记d=第j个售点需求量,j=1,2,…n.其中,m=6;n=8.设目的函数为总成本,约束条件为(1)产量约束;(2)需求约束。于是形成如下规划问题:endsetsdata:ALPHA=0.7;a=111213;r2=1..3;b=111213;c=111213;enddata!exam_2.lg4源程序model:!6发点8收点运送问题;sets:rows/1..6/:s;!发点产量限制;cols/1..8/:d;!售点需求限制;links(rows,cols):c,x;!运送单价,决策运送量;endsets!-------------------------------------;data:s=60,55,51,43,41,52;d=3537223241324338;minzmncxijijst.mj1i1j1xsiji,i1,2,...,mnxiji1d,j1,2,...,njx0,i1,2,...,m,j1,2,...,nij把上述程序翻译成LINGO语言,编制程序如下:c=626742954953858252197433enddata!------------------------------------;min=@sum(links:c*x);!目的函数=运送总成本;@for(rows(i):@sum(cols(j):x(i,j))=s(i));!产量约束;@for(cols(j):@sum(rows(i):x(i,j))=d(j));!需求约束;end!exam_3.lg4源程序model:!6发点8收点运送问题;sets:rows/1..6/:s;!发点产量限制;cols/1..8/:d;!售点需求限制;links(rows,cols):c,x;!运送单价,决策运送量;endsets!==================================;data:s=60,55,51,43,41,52;767392712395726555228143;例3把上述程序进行改进,引进运营子模块和打印运算成果语句:d=3537223241324338;c=626742954953858252197433767392712395726555228143;enddata!==================================;submodeltransfer:min=cost;!目的函数极小化;cost=@sum(links:c*x);!目的函数:运送总成本;@for(rows(i):@sum(cols(j):x(i,j))s(i));!产量约束;@for(cols(j):@sum(rows(i):x(i,j))d(j));!需求约束;endsubmodel!==================================;calc:@solve(transfer);!运营子模块(解线性规划);@divert('transfer_out.txt');!向.txt文献按自定格式输出数据;@write('最小运送成本=',cost,@newline(1),'最优运送方案x=');@for(rows(i):@write(@newline(1));最小运送成本=664最优运送方案x=例4data段编写技巧(1):从txt文献中读取原始数据!exam_3.lg4源程序中data也可以写为:data:s=@file('transfer_data.txt');d=@file('transfer_data.txt');c=@file('transfer_data.txt');enddata其中,transfer_data.txt内容为:!transfer.lg4程序数据;打开transfer_out.txt文献,内容为:019004100010032000001100004000000050383470000000022002730@writefor(cols(j):'',@format(x(i,j),'3.0f')));@divert();!关闭输出文献;endcalcend!产量约束s=;60,55,51,43,41,52~!需求约束d=;3537223241324338~!运送单价c=;626742954953858252197433767392712395726555228143~!注:字符~是数据分割符,若无此符,视所有数据为一种数据块,只赋给一种变量;例5lingo程序3种输入和3种输出办法;!exam_5.lg4源程序;sets:rows/1..3/:;cols/1..4/:;link(rows,cols):a,b,mat1,mat2;endsetsdata:b=1,2,3,4例6程序段中循环和选取构造举例!exam_6.lg4源程序;sets:rows/1..5/:;cols/1..3/:;links(rows,cols):d;endsetsdata:d=0234321324725,6,7,89,10,11,12;!程序内输入;a=@file('a.txt');!外部txt文献输入;mat1=@ole('d:\lingo12\data.xls',mat1);!EXcel文献输入;enddatacalc:@text('a_out.txt')=a;!列向量形式输出数据;@for(link:mat2=2*mat1);@ole('d:\lingo12\data.xls')=mat2;!把mat2输出到xls文献中同名数据块;!向.txt文献按自定格式输出数据(参照前例);Endcalc216;enddatacalc:i=1;@while(i#le#5:a=d(i,1);b=d(i,2;)c=d(i,3);@ifc(a#eq#0:@write('infeasible!',@newline(1));@elsedelta=b^2-4*a*c;sqrt@=sqrt(@if(delta#ge#0,delta,-delta));@ifc(delta#ge#0:@write('x1=',(-b+sqrt)/2/a,'x2=',(-b-sqrt)/2/a,@newline(1));@else@write('x1=',-b/2/a,'+',sqrt/2/a,'i','x2=',-b/2/a,'-',sqrt/2/a,'i',@newline(1)););;)i=i+1;);endcalc本程序中循环构造也可以用@for(rows(i):程序体);进行计算。例7指派问题(n人n任务费用最小)ijij!exam_7.lg4源程序;model:!6人6任务指派问题;sets:rows/1..6/:;!6人6任务;links(rows,rows):c,x;!费用和决策变量;endsets!-------------------------------------;B1B2B3B4B5B6A1626742A2495385A3521974A4767392A5239572A6552281解:设决策变量x=1或0,表达第i个人与否完毕第j项任务,i,j=1,2,…n;记c=第i个人完毕第j项任务费用,i,j=1,2,…n;n=6.设目的函数为总费用,约束条件为(1)每人只完毕一项任务;(2)每项任务只由一人完毕。于是形成如下规划问题:minznncxijijst.nj1ni1i1j1x1,i1,2,...,nijx1,j1,2,...,nijx0或1,i1,2,...,n,j1,2,...,nijdata:c=626742495385521974767392239572552281;enddata!==================================;submodelappointment:min=cost;!目的函数极小化;cost=@sum(links:c*x);!目的函数:总费用;@for(rows(i):@sum(rows(j):x(i,j))=1);!每人完毕一项;@for(rows(j):@sum(rows(i):x(i,j))=1);!每项由一人完毕;@for(links:@bin(x));!0-1变量约束;endsubmodelsubmodelbinVar:@for(links:@bin(x));!0-1变量约束;endsubmodel!==================================;calc:@solve(appointment,binVar);!运营子模块(解线性规划);@divert('appointment_out.txt');!向.txt文献按自定格式输出数据;@write('最小指派费用=',cost,@newline(1),'分派方案x=');@for(rows(i):@write(@newline(1));@writefor(rows(j):'',@format(x(i,j),'3.0f')));@divert();!关闭输出文献;endcalcend例8多目的规划转化为单目的规划问题举例把上述运送问题稍加修改,考虑到运送量可以要取整数,就变成整数规划问题,并且运送问题除了成本最小一种目的以外,有时也要考虑各发点运送量尽量均衡作为另一种目的。本程序解决办法一是两目的加权平均,办法二是只选一种目的,另一种目的转化为约束,从而把多目的改为单目的。!exam_8.lg4源程序;model:!6发点8收点运送问题;sets:rows/1..6/:s;!发点产量限制;cols/1..8/:d;!售点需求限制;links(rows,cols):c,x;!运送单价,决策运送量;endsets!==================================;data:s=60,55,51,43,41,52;d=3537223241324338;c=626742954953858252197433767392712395726555228143;enddata!==================================;submodelobj_1:min=minCost;!目的函数极小化;minCost=@sum(links:c*x);!目的函数:运送总成本;endsubmodelsubmodelobj_2:min=objValue;objValue=0.4*obj1+0.6*obj2;!二目的加权平均;obj1=@sum(links:c*x);!目的函数1:运送总
本文标题:lingo解决线性规划方案问题的程序
链接地址:https://www.777doc.com/doc-8767335 .html