其他分享
首页 > 其他分享> > LINGO在图论和网络模型中的应用(二)

LINGO在图论和网络模型中的应用(二)

作者:互联网

1.动态规划解最短路:

model:
  sets:
    cities/A,B,C,D,E,F,G/: FL;
    roads(cities,cities)/A,B A,C B,D B,E B,F C,D C,E C,F D,G E,G F,G/:W,P;
  endsets
  data:
     W=2 4 3 3 1 2 3 1 1 3 4;
  enddata
     N=@SIZE(CITIES);
     FL(N)=0; 
     @for(cities(i) |i #LT# N:FL(i)=@MIN(roads(i,j):W(i,j)+FL(j)));
     @for(roads(i,j):P(i,j)=@IF(FL(i) #EQ# W(i,j)+FL(j),1,0));
  end

2.TSP

3.最小生成树和最优连线:

如果图的边有权,则权的总和达到最小的生成树成为最小生成树(minimal spanning tree,MST),最小生成树不一定唯一。

model:
 sets:
 city/1..7/:u;
 link(city,city): dist,x;
endsets
 n=@size(city);
 data:
    dist=0 3 4 7 100 100 100
         3 0 3 2 4 100 100
         4 3 0 100 5 7 100
         7 2 100 0 2 100 6
         100 4 5 2 0 1 4
         100 100 7 100 1 0 2
         100 100 100 6 4 2 0;
 enddata
min=@SUM(link:dist*x);
 U(1)=0;
@for(link:@bin(x));!定义x为0-1变量;
@for(city(K)|K#GT#1 :@SUM(city(I)|I#ne#k:x(I,K))=1;
@for(city(J)|J#GT#1 #and# J#ne#K: u(J)>=u(K)+X(K,J)-(N-2)*(1-X(K,J))+(N-3)*X(J,K);););
@sum(city(J)|J#GT#1 :x(1,J))>=1;
@for(city(K)|K#GT#1: U(K)>=1;U(K)<=N-1-(N-2)*X(1,K););
end

4.最大流问题(最小费用)

Ford-Fulkerson标号算法和Edmonds-Karp算法

model:
   sets:
     CHSH/1..6/;
     LINKS(CHSH,CHSH)/1,2 1,3 2,4 2,5 3,4 3,5 4,6 5,6 6,1/:C,F;
   endsets
   data:
      C=16,20,10,10,6,6,10,16,1000;
   enddata
   max=F(6,1);
   @for(LINKS(I,J):F(I,J)<=C(I,J));
   @FOR(CHSH(I):@SUM(LINKS(J,I):F(J,I))=@SUM(LINKS(I,J):F(I,J)));
end

  

标签:city,图论,GT,dist,模型,100,cities,FL,LINGO
来源: https://www.cnblogs.com/kmxojer/p/12395150.html