# Convert a VariableRef in JuMP objective function or constraint

**URL:** <https://discourse.julialang.org/t/convert-a-variableref-in-jump-objective-function-or-constraint/43408>\
**Category:** Optimization (Mathematical)\
**Created:** [July 21, 2020, 12:58am UTC](https://discourse.julialang.org/t/convert-a-variableref-in-jump-objective-function-or-constraint/43408 "2020-07-21T00:58:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [July 21, 2020, 12:58am UTC](https://discourse.julialang.org/t/convert-a-variableref-in-jump-objective-function-or-constraint/43408/1 "2020-07-21T00:58:34Z")

</div>

I have a model with decision variables like this

```julia
model = Model(Cbc.Optimizer)

@variable(model, x[1:num_groups, 1:size(data, 1)], Bin)

```

and I need to be able to use the values of `x` to return a submatrix from another matrix `dm`. I’ve tried like this

```julia
@objective(model, Min, sum(sum(dm[BitArray(x[i,:]), BitArray(x[i,:])]) for i in 1:3))

```

but I get:

```julia
julia> @objective(model, Min, sum(sum(dm[BitArray(x[i,:]), BitArray(x[i,:])]) for i in 1:3))
ERROR: MethodError: Cannot `convert` an object of type VariableRef to an object of type Bool

```

I thought I had tricked it into working with this monstrosity because it didn’t throw an error but the objective value always turns out to be 0.0 this way:

```julia
sum([sum(dm[[i for i in 1:size(data,1) if value.(x)[j,:][i] == 1.0], [i for i in 1:size(data,1) if value.(x)[j,:][i] == 1.0]]) for j in 1:num_groups])

```

Is there a way to convert the values of `x` so that I can use them in the same way one would use a `BitArray`?

---

<div class="post-metadata">

**Author:** ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)\
**Post date:** [July 21, 2020, 1:08am UTC](https://discourse.julialang.org/t/convert-a-variableref-in-jump-objective-function-or-constraint/43408/2 "2020-07-21T01:08:42Z")

</div>

JuMP Variables cannot be used as Bool’s.  
You cannot use them to access arrays.  
Once you `optimize!` the problem you might get their optimal value by using the `value` method.  
If you call `value` prior to optimization its either an error or a meaningless number.
