Balanced transportation models are preferred as there is no confusion about the relational operators for the supply and demand constraints. We can use script file to balance any transportation problem automatically:
reset;
model transportation.mod;
param costFromDummy {DEMAND_NODES} default 0;
param costToDummy {SUPPLY_NODES} default 0;
param difference;
# Add the problem date file here
# e.g., data brewery.dat;
let difference := (sum {s in SUPPLY_NODES} Supply[s])
- (sum {d in DEMAND_NODES} Demand[d]);
if difference > 0 then
{
let DEMAND_NODES := DEMAND_NODES union {'Dummy'};
let Demand['Dummy'] := difference;
let {s in SUPPLY_NODES} Cost[s, 'Dummy'] := costToDummy[s];
}
else if difference < 0 then
{
let SUPPLY_NODES := SUPPLY_NODES union {'Dummy'};
let Supply['Dummy'] := - difference;
let {d in DEMAND_NODES} Cost['Dummy', d] := costFromDummy[d];
}; # else the problem is balanced
# Make sure the problem is balanced
check : sum {s in SUPPLY_NODES} Supply[s] = sum {d in DEMAND_NODES} Demand[d];
option solver cplex;
solve;
display Flow;
Note the check statement to ensure that the balancing has been done properly before solving. Also, note that costToDummy and costFromDummy allow for the definition of costs on any flow from/to a dummy node in the data file.
Both the AMPL model file transportation.mod and script file transportation.run are attached. |