Lets say we have the following data to solve transportation problem:
A1 A2 A3 Supply
T1 0 600 100 700
T2 500 0 300 800
Demand 500 600 400
I want to solve that transportation problem using Google Optimization Tools Minimum Cost Flows. I'm trying to solve that with the following code:
private static void SolveMinCostFlow()
{
// Define four parallel arrays: sources, destinations, capacities, and unit costs
// between each pair. For instance, the arc from node 0 to node 1 has a
// capacity of 15.
// Problem taken From Taha's 'Introduction to Operations Research',
// example 6.4-2.
int numNodes = 5;
int numArcs = 6;
int[] startNodes = { 0, 0, 0, 1, 1, 1 };
int[] endNodes = { 2, 3, 4, 2, 3, 4};
int[] capacities = { 500, 600, 400, 500, 600, 400 };
int[] unitCosts = { 0, 600, 100, 500, 0, 300 };
// Define an array of supplies at each node.
int[] supplies = { 700, 700, 800, 800, 800 };
// Instantiate a SimpleMinCostFlow solver.
MinCostFlow minCostFlow = new MinCostFlow();
// Add each arc.
for (int i = 0; i < numArcs; ++i)
{
int arc = minCostFlow.AddArcWithCapacityAndUnitCost(startNodes[i], endNodes[i],
capacities[i], unitCosts[i]);
if (arc != i) throw new Exception("Internal error");
}
// Add node supplies.
for (int i = 0; i < numNodes; ++i)
{
minCostFlow.SetNodeSupply(i, supplies[i]);
}
//Console.WriteLine("Solving min cost flow with " + numNodes + " nodes, and " +
// numArcs + " arcs, source=" + source + ", sink=" + sink);
// Find the min cost flow.
int solveStatus = minCostFlow.Solve();
if (solveStatus == MinCostFlow.OPTIMAL)
{
long optimalCost = minCostFlow.OptimalCost();
Console.WriteLine("Minimum cost: " + optimalCost);
Console.WriteLine("");
Console.WriteLine(" Edge Flow / Capacity Cost");
for (int i = 0; i < numArcs; ++i)
{
long cost = minCostFlow.Flow(i) * minCostFlow.UnitCost(i);
Console.WriteLine(minCostFlow.Tail(i) + " -> " +
minCostFlow.Head(i) + " " +
string.Format("{0,3}", minCostFlow.Flow(i)) + " / " +
string.Format("{0,3}", minCostFlow.Capacity(i)) + " " +
string.Format("{0,3}", cost));
}
}
else
{
Console.WriteLine("Solving the min cost flow problem failed. Solver status: " +
solveStatus);
}
}
static void Main(string[] args)
{
SolveMinCostFlow();
Console.Read();
}
But I get error: Solving the min cost flow problem failed. Solver status: 4 What am I doing wrong here? I suppose there should be something with defining parameters at the start of SolveMinCostFlow but can't figure it out.
[700,800,-500,-600,-400]
? – Erwin Kalvelagen Jan 19 '18 at 12:53