juliajulia-jump

Why is this code outputting to the console when it shouldn't?


I'm trying to write a function that computes the value P(α, β) for a given adjacency matrix representing a graph G, and for two given vertices α and β of G.

This is what I wrote :

function P(g::Matrix{Int}, source::Int, target::Int)::Tuple{Int, Matrix{Int}}
    # Size of the matrix
    n = size(g, 1)

    # Create an optimization model
    model = JuMP.Model(Cbc.Optimizer)

    # The decision variables x[i, j], representing the flow on the arc (i, j) in the graph. 
    # The variables are non-negative and bounded by the capacity g[i, j] of the corresponding arc.
    @variable(model, 0 <= x[i = 1:n, j = 1:n] <= g[i, j])

    # For each vertex i (excluding the source and target), this constraint ensures that the sum of flow entering i is equal to the sum of flow leaving i. 
    for i in 1:n
        if i != source && i != target
            @constraint(model, sum(x[j, i] for j in 1:n if j != i) == sum(x[i, j] for j in 1:n if j != i))
        end
    end
    # This constraint ensures that the sum of flow entering the source vertex is zero.
    @constraint(model, sum(x[j, source] for j in 1:n) == 0)
    # This constraint ensures that the sum of flow leaving the target vertex is zero.
    @constraint(model, sum(x[target, j] for j in 1:n) == 0)

    # The objective is to maximize the total flow leaving the source vertex (source).
    @objective(model, Max, sum(x[source, j] for j in 1:n))

    # Solve the optimization problem
    MOI.set(model, MOI.Silent(), true)
    optimize!(model)

    # Check solution status
    @assert termination_status(model) == MOI.OPTIMAL "No optimal solution found for the maximum flow problem"

    # Retrieve maximum flow value and flow distribution
    flow_value = round(Int, JuMP.objective_value(model))
    flow_distribution = round.(Int, JuMP.value.(x))

    return flow_value, flow_distribution
end

Note the line MOI.set(model, MOI.Silent(), true) which should silence all output from the solver.

Yet when I run this code (with julia .\tests.jl) :

using Test
include("functions.jl")

G3 = [
    0 1 1 0;
    0 0 1 1;
    1 0 0 1;
    1 1 0 0
]

@test P(G3, 1, 2)[1] == 2
@test P(G3, 2, 3)[1] == 2
@test P(G3, 3, 4)[1] == 2
@test P(G3, 4, 1)[1] == 2

It prints out all of this :

Presolve 0 (-4) rows, 0 (-16) columns and 0 (-20) elements
Optimal - objective value 2
After Postsolve, objective 2, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2 - 0 iterations time 0.002, Presolve 0.00
Presolve 0 (-4) rows, 0 (-16) columns and 0 (-20) elements
Optimal - objective value 2
After Postsolve, objective 2, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2 - 0 iterations time 0.022, Presolve 0.01
Presolve 0 (-4) rows, 0 (-16) columns and 0 (-20) elements
Optimal - objective value 2
After Postsolve, objective 2, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2 - 0 iterations time 0.002, Presolve 0.00
Presolve 0 (-4) rows, 0 (-16) columns and 0 (-20) elements
Optimal - objective value 2
After Postsolve, objective 2, infeasibilities - dual 0 (0), primal 0 (0)
Optimal objective 2 - 0 iterations time 0.002, Presolve 0.00

Why is that? And how do I stop it ? I'm on Windows, but ideally, I'd like to suppress it in all systems..

Edit :

I tried :

set_optimizer_attribute(model, "OUTPUTLOG", 0)
set_optimizer_attribute(model, "logLevel", 0)
JuMP.set_silent(model)

These don't suppress the output.

and :

redirect_stdout(Sys.isunix() ? "/dev/null" : "NUL") do
    optimize!(model)
end

This doesn't even run for some reason.


Solution

  • Finally found something that helped, I changed this :

    @variable(model, 0 <= x[i = 1:n, j = 1:n] <= g[i, j])
    

    into this :

    @variable(model, 0 <= x[i = 1:n, j = 1:n] <= g[i, j], Bin)
    

    and used :

    MOI.set(model, MOI.Silent(), true)
    

    Why does specifying that the variables are binary affect the output being suppressed or not? I have no clue...

    If anyone can offer an explanation, I'd be thankful.