# Assigning JuMP VariableRef to an array position

**URL:** <https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216>\
**Category:** Optimization (Mathematical)\
**Created:** [July 10, 2019, 11:37am UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216 "2019-07-10T11:37:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 11:37am UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/1 "2019-07-10T11:37:43Z")

</div>

Hi,

Is it possible to assign a decision variable to an array position?  
For example:

```julia
using JuMP
m = Model()

@variable(m, x)

arr = zeros(100)
arr[1] = x

```

x is a decision variable which will be decided by the model when it is run, is it possible to assign it to an array position?

Thanks!

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [July 10, 2019, 12:04pm UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/2 "2019-07-10T12:04:52Z")

</div>

Yes, it is possible but the array type should be `JuMP.VariableRef`. Here `zeros(100)` creates an array of `Float64` and `JuMP.VariableRef` is not convertible to `Float64`.  
You can try

```julia
using JuMP
model = Model()
@variable(model, x)
arr = Vector{JuMP.VariableRef}(undef, 100)
arr[1] = x

```

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 10, 2019, 3:52pm UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/3 "2019-07-10T15:52:06Z")

</div>

Oh okay, but I have a small issue now.

```julia
@variable(m, delta0)

amort = delta0/T

bal = Vector{JuMP.VariableRef}(undef, 100)

bal[1] = delta0
for i = 2:100
	bal[i] = bal[i-1] - amort
end

```

I can now assign a decision variable to the first array position `bal[1]`, but for the following positions from [2] onwards, I need to deduct a certain value `amort`. But I get an error `MethodError: Cannot convert an object of type GenericAffExpr{Float64,VariableRef} to an object of type VariableRef`  
So I asked about this in a previous post and someone advised to use regular `zeros` array, and that allowed me to subtract `amort` from `bal[1]`. So I have this dilemma, where I need a JuMP array for decision variable storage, but also need the properties of a zeros array, and am not sure what to do.  
If someone can please help, it would be greatly appreciated. This is the link to my other related question: [https://discourse.julialang.org/t/for-loop-error/26210/7](https://discourse.julialang.org/t/for-loop-error/26210/7)

Thanks!

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [July 10, 2019, 5:34pm UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/4 "2019-07-10T17:34:23Z")

</div>

Specify your model equality relationships as constraints. Also, use the array-form of the `@variable` and `@constraint` macros for efficiency. See [Variables · JuMP](http://www.juliaopt.org/JuMP.jl/v0.19.2/variables/#Variable-containers-1).

```julia
@variable(m, delta0)

amort = delta0/T

@variable(m, bal[i=1:100])
@constraint(m, bal[1] == delta0)
@constraint(m, [i=2:100], bal[i] == bal[i-1] - amort)

```

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [July 10, 2019, 9:37pm UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/5 "2019-07-10T21:37:49Z")

</div>

Try `bal = Vector{JuMP.AffExpr}(undef, 100)`

---

<div class="post-metadata">

**Author:** ![MuadhF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/muadhf/32/20389_2.png) [@MuadhF](https://discourse.julialang.org/u/MuadhF)\
**Post date:** [July 11, 2019, 5:56am UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/6 "2019-07-11T05:56:14Z")

</div>

Thank you @blegat and @ianfiske, both your suggestions worked!

---

<div class="post-metadata">

**Author:** ![Dota](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@Dota](https://discourse.julialang.org/u/Dota)\
**Post date:** [July 31, 2019, 4:56pm UTC](https://discourse.julialang.org/t/assigning-jump-variableref-to-an-array-position/26216/7 "2019-07-31T16:56:37Z")

</div>

Thank you so much!!! I try to use `Any` previously, I will try that
