## Problem related to linear programming, when the supply and demand exchange, the code doesn't work

The code works perfectly for the first time:

``````Proc IML ;
*Define the routes between the nodes;
arcs = { 'AB' 'AC' 'AE' 'BC' 'BD' 'CE' 'CH' 'DE' 'DF' 'EF' 'EH' 'GF' 'GH' }; /* decision variables */
nodes = {'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H'};
*Input the constraint matrix;
A={ 1 1 -1 0 0 0 0 0 0 0 0 0 0,
-1 0 0 1 1 0 0 0 0 0 0 0 0,
0 -1 0 1 0 -1 1 0 0 0 0 0 0,
0 0 0 0 -1 0 0 1 1 0 0 0 0,
0 0 1 0 0 1 0 0 0 1 -1 0 0,
0 0 0 0 0 0 0 0 1 -1 0 1 0,
0 0 0 0 0 0 0 0 0 0 0 -1 1,
0 0 0 0 0 0 -1 0 0 0 1 0 1};
end;
print 'The constraint matrix of the direction of flows' ;
print A[r=nodes c=arcs l={'The Network Constraint Matrix'}] ;
COST = {28.259407 41.255207 67.664184 39.863161 34.92524 35.860331 41.266789 27.098869 36.723808 23.638669 29.807257 24.900754 42.253725 };
print cost [c=arcs l={'The cost/distance for each internode route'}] ;
*The supply and demand at each node;
supply = { 0 -2 12 -2 -2 0 -3 -3 };
print supply[c=nodes l={'Supply and Demand at each node'}] ;
*Force all demand and supply to even out by making all constraints equalities
operators: 'L' for <=, 'G' for >=, 'E' for =;
ops = j(8,1,'E') ;
cntl = j(1,13,.); /* control vector */
cntl = 1; /* 1 for minimum; -1 for maximum */
call lpsolve(rc, value, x, dual, redcost,cost, A, supply, cntl, ops);
print value[L='Minimum Cost'];
print x[r=arcs L='Optimal Flow'];
quit;
run;``````

After I exchange the demand and supply, it doesn't work, which in specific, I change this line of code:

`supply = { 0 2 -12 2 2 0 3 3 };`

Based on the graph in description, the transportation can surely work both forward and backwards. I'm not sure whether I have made the correct modifications.

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: Problem related to linear programming, when the supply and demand exchange, the code doesn't wor

This is not my area of expertise, but I am guessing that you also need to reverse the direction of flow by multiplying the matrix A by -1.  The "end;" statement in your code should also be removed as this is generating an error.

2 REPLIES 2

## Re: Problem related to linear programming, when the supply and demand exchange, the code doesn't wor

This is not my area of expertise, but I am guessing that you also need to reverse the direction of flow by multiplying the matrix A by -1.  The "end;" statement in your code should also be removed as this is generating an error.

## Re: Problem related to linear programming, when the supply and demand exchange, the code doesn't wor

Thank you so much!!!!! I got the right results after applying your suggestions! You are a lifesaver!!!

From The DO Loop