JuMP: How to see output of particular variable and indicies?

Hi there

Is it possible to ask JuMp to just show some particular indicies of varaiables? For instance let’s say we have a binary variable @variable(model, x[i = 2:3, j = 1:2:30, ["A", "B",..."Z"]] >= 0, Bin) and after solving models we are interested to first knowing if x[i,j,"C"] takes value and if so for which combination of i,j?

Thanks

There’s nothing particular in JuMP for this. Just write a function or loop in Julia.

for i in 2:3, j in 1:2:30
    if value(x[i, j, "C"]) > 0.5
        # Do something
    end
end

You can subset with x[:, :, "C"], so perhaps you could do value.(x[:, :, "C"]) .> 0.5.

Or you could use Containers.rowtable:

import DataFrames
df = DataFrames.DataFrame(Containers.rowtable(x; header=[:i, :j, :k, :value]))
df[df[!, :value] .> 0.5, :]
2 Likes

One question: How to fix the ERROR: Invalid number of column names provided: Got 4, expected 2. when calling import DataFrames df = DataFrames.DataFrame(Containers.rowtable(x; header=[:i, :j, :k, :value])) df[df[!, :value] .> 0.5, :]

I’ve tried DataFrames.DataFrame(Containers.rowtable(x; header=[:i, :j, :k])) but now says ERROR: Invalid number of column names provided: Got 3, expected 2.

Even the first one returns a key error. I want to print the value of i,j. For instance:

for i in 2:3, j in 1:2:30
    if value(x[i, j, "C"]) > 0.5
        print((i,j)
    end
end

This returns returns a key error. So, for example something similar to this KeyError: key (2, 10, "C") not found in the above.

Your definition of x must be different to what you had above.

It looks like you have one dimension of tuples, instead of three separate dimensions.

Yes. Could that cause a problem? the way I defined x was similar to

idx = [(i,j,k) for (i,j) in Numbers for k in Letters]
@variable(m, x[idx] >= 0, Bin)

Could that cause a problem?

Yes. The code you write to index a variable needs to match how you defined it. Your first example needs x[1, 2, "C"] and your second example needs x[(1, 2, "C")]. The first has three dimensions, and the second has one.

I think you need something like:

indices = [(i, j) for (i, j, k) in idx if k == "C" && value(x[(i, k, k)]) > 0.5]

The rowtable example is Containers.rowtable(x; header=[:idx, :value]).

1 Like