MODEL:

SETS:

nodes/1..9/:V;

arcs(nodes,nodes)/
1 2    1 4    2 3     2 5    3 6
4 5    4 7    5 6     5 8    6 9
7 8    8 9
/:cap,cost,flow,dist, xx1,xx2;

pathset:x,pc;
comm/c1 c2/:b, z,  ss,  tt, w;
mat(arcs,pathset):a1;
mat2(comm,pathset):a2;

ENDSETS

DATA:

pathset= 1..20;

cap=  5 3 3 2 2      1 3 2 5 4     4 2;
cost= 5 3 3 2 2      1 3 2 5 4     4 2;

ss= 1  3;
tt  = 9  7;
b =  4 3;

ENDDATA 

SUBMODEL  MASTER_PROBL:

[MASTEROBJ] MIN=@SUM(pathset(j)| j#LE#npaths :  pc(j)* x(j) ) + @SUM(comm(k):1000*z(k));

@FOR(arcs(i1,i2): [capdual] @SUM(mat(i1,i2,j)| j#LE#npaths: a1(i1,i2,j)* x(j) ) < cap(i1,i2) );

@FOR(comm(k): [demdual] @SUM(mat2(k,j)| j#LE# npaths:  a2(k,j)*x(j)) +z(k) = b(k)     );

ENDSUBMODEL

SUBMODEL  PATH_GENERATOR:

! si calcolano i cammini minimi tramite PL;

[SUBOBJ] MIN=@SUM(arcs(i1,i2):dist(i1,i2)*(xx1(i1,i2)+xx2(i1,i2)));

@FOR(nodes(i)|(i#NE#source) #AND# (i#NE#sink) : @SUM(arcs(i,j): xx1(i,j)-xx2(i,j))-@SUM(arcs(j,i):xx1(j,i)-xx2(j,i))=0);  
 @SUM(arcs(i,j)|i#EQ#source: xx1(i,j)-xx2(i,j))-@SUM(arcs(j,i)|i#EQ#source:xx1(j,i)-xx2(j,i))=1;  
 @SUM(arcs(i,j)|i#EQ#sink: xx1(i,j)-xx2(i,j))-@SUM(arcs(j,i)|i#EQ#sink:xx1(j,i)-xx2(j,i))=-1;  

ENDSUBMODEL

CALC:

npaths=0; oldpaths = -1;
@WHILE((npaths #GT# oldpaths) #AND# (npaths #LT#20)    :
	@DIVERT( 'LINGO.LOG');
	@SOLVE(MASTER_PROBL);
	@DIVERT();
	@WRITE("Master Problem value = ",MASTEROBJ,@NEWLINE(1));
	@FOR(pathset(j)|j#LE#npaths: @IFC(x(j)#GT#0: @WRITE("x(",j,")=",x(j)," ; ")  )  );
	@WRITE(@NEWLINE(1));
	@WRITE("duals",@NEWLINE(1));
	@FOR(arcs(i1,i2): @IFC( @DUAL(capdual(i1,i2))#GT#0: 
          	@WRITE("y(",i1,",",i2,")= ",@ABS(@DUAL(capdual(i1,i2)))," ;  "  )   ));
	@WRITE(@NEWLINE(1));
	@FOR(comm(k):  @WRITE("w(",k,")= ",@ABS(@DUAL(demdual(k)))," ;  "));
	@WRITE(@NEWLINE(1));
	@FOR( arcs(i1,i2): dist(i1,i2) = cost(i1,i2)+@ABS(@DUAL( capdual(i1,i2)  )));
	@FOR(comm(k): w(k) = @ABS(@DUAL(demdual(k))) );
	@FOR(arcs(i1,i2): flow(i1,i2)= @SUM(mat(i1,i2,j)| j#LE#npaths: a1(i1,i2,j)* x(j) ));
	 oldpaths=npaths;
	@FOR(comm(k): 
		source=ss(k);
		sink=tt(k);
		@DIVERT( 'LINGO.LOG');
		@SOLVE(PATH_GENERATOR);
		@DIVERT();
		@WRITE("comm ",k,"   pathvalue= ",SUBOBJ,"    dual w(",k,")= ",w(k),@NEWLINE(1));
		@IFC(SUBOBJ #LT# W(k): 
			npaths=npaths+1;
       			@WRITE("path n. ",npaths,"    : ");
       			@FOR(arcs(i1,i2) : @IFC(xx1(i1,i2)+xx2(i1,i2)#GT#0:@WRITE(i1,"-",i2," ; ") )  );
       			@WRITE(@NEWLINE(1));
       			@FOR(arcs(i1,i2):  a1(i1,i2,npaths)=xx1(i1,i2)+xx2(i1,i2) );
       			@FOR(comm(k2):  
				@IFC( k2#EQ#k:  a2(k2,npaths)=1; @ELSE  a2(k2,npaths)=0  ) );
       			pc(npaths)=@SUM(arcs(i1,i2): cost(i1,i2)*(xx1(i1,i2)+xx2(i1,i2)) );
    		 )
	);
);
@FOR(arcs(i1,i2): 
      @IFC(flow(i1,i2)#GT#0: @WRITE("flow(",i1,",",i2,")=  ",flow(i1,i2));@WRITE(@NEWLINE(1)) ) );

ENDCALC

END