How to declare a Boolean variable?

I need to declare a Boolean variable but I don’t know how to use Bool. How should I do ?
I need the variable “i” to assume two values (1, 0)

using JuMP, Cbc
Model1 = Model(with_optimizer(Cbc.Optimizer))

@variable(Model1,d[1:i], lower_bound=0, Bool = true)  

julia> a = true

julia> typeof(a)

Alternatively Bool can be called as constructor, Bool(0)

I’m not sure this answers your question though, as you seem to be using i to construct a range for indexing something in the code you posted (i.e. you would have 1:1 if i == true and 1:0 if i == false due to type promotion)


@variable(Model1, d[1:i], Bin)


How will i know if i really is assuming the value [0,1] ?

using JuMP, Cbc, MathOptInterface
Model1 = Model(with_optimizer(Cbc.Optimizer))

@variable(Model1,d[1:i], lower_bound=0, Bin) 
is_binary(d) #I can't compile the is_binary command, to return " true" to binary 


Here is the documentation for a binary variable:
Here is the documentation for variable containers:

Avoiding the i issue, you code will make a vector of binary variables, where d[i] in {0, 1} .

using JuMP
model = Model()
@variable(model, d[1:2], Bin)
is_binary(d[1])  # true
is_binary(d[2])  # true

If you really want i to be the index, and for it to be in {0, 1}, that doesn’t make sense. Do you want 0 variables or 1 variable? If you just want a variable called i to be 0 or 1, use:

using JuMP
model = Model()
@variable(model, i, Bin)

p.s. You should also post in the category if you’re posting about JuMP. It makes the post a little more visible to the people interested in optimization.

1 Like