zyg
zyg

Reputation: 29

Why ortools maxflow sample program failed to calculate maxflow when I try it?

I didn't change anything,Retained the source code:

// From Taha 'Introduction to Operations Research', example 6.4-2."""
#include <cstdint>
#include "ortools/graph/max_flow.h"

namespace operations_research {
    // MaxFlow simple interface example.
    void SimpleMaxFlowProgram() {
        // Instantiate a SimpleMaxFlow solver.
        SimpleMaxFlow max_flow;

        // Define three parallel arrays: start_nodes, end_nodes, and the capacities
        // between each pair. For instance, the arc from node 0 to node 1 has a
        // capacity of 20.
        std::vector<int64_t> start_nodes = { 0, 0, 0, 1, 1, 2, 2, 3, 3 };
        std::vector<int64_t> end_nodes = { 1, 2, 3, 2, 4, 3, 4, 2, 4 };
        std::vector<int64_t> capacities = { 20, 30, 10, 40, 30, 10, 20, 5, 20 };

        // Add each arc.
        for (int i = 0; i < start_nodes.size(); ++i) {
            max_flow.AddArcWithCapacity(start_nodes[i], end_nodes[i], capacities[i]);
        }

        // Find the maximum flow between node 0 and node 4.
        int status = max_flow.Solve(0, 4);

        if (status == MaxFlow::OPTIMAL) {
            LOG(INFO) << "Max flow: " << max_flow.OptimalFlow();
            LOG(INFO) << "";
            LOG(INFO) << "  Arc    Flow / Capacity";
            for (std::size_t i = 0; i < max_flow.NumArcs(); ++i) {
                LOG(INFO) << max_flow.Tail(i) << " -> " << max_flow.Head(i) << "  "
                    << max_flow.Flow(i) << "  / " << max_flow.Capacity(i);
            }
        }
        else {
            LOG(INFO) << "Solving the max flow problem failed. Solver status: "
                << status;
        }
    }

}  // namespace operations_research

int main() {
    operations_research::SimpleMaxFlowProgram();
    return EXIT_SUCCESS;
}

the output should be:

Max flow: 60
  Arc    Flow / Capacity
0 -> 1    20  /  20
0 -> 2    30  /  30
0 -> 3    10  /  10
1 -> 2     0  /  40
1 -> 4    20  /  30
2 -> 3    10  /  10
2 -> 4    20  /  20
3 -> 2     0  /   5
3 -> 4    20  /  20
Source side min-cut: [0]
Sink side min-cut: [4, 1]

My output is:

Solving the max flow problem failed. Solver status:0  

Why did this happened?When I use maxflow on my own code it happened in the same way.I pasted the sample code without any modification, but there were errors.Didn't I install or usr ortools correctly?

Upvotes: 2

Views: 150

Answers (1)

Mizux
Mizux

Reputation: 9329

After digging a little: Actually there are two enum Status

class SimpleMaxFlow {
 public:
 ...
 enum Status {
    OPTIMAL,
    ...

ref: https://github.com/google/or-tools/blob/49b6301e1e1e231d654d79b6032e79809868a70e/ortools/graph/max_flow.h#L180-L195

and a second one:

class MaxFlowStatusClass {
 public:
  enum Status {
    NOT_SOLVED,
    OPTIMAL,
    ...

ref: https://github.com/google/or-tools/blob/49b6301e1e1e231d654d79b6032e79809868a70e/ortools/graph/max_flow.h#L296-L309

I guess here you may want to use SimpleMaxFlow::OPTIMAL and not MaxFlow::OPTIMAL

todo(mizux): Need to check if we could merge both to avoid ambiguity in the future...

Upvotes: 2

Related Questions