# JuMP, start the optimization with a vector

**URL:** <https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [May 17, 2018, 10:24am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951 "2018-05-17T10:24:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrapo/32/3911_2.png) [@mrapo](https://discourse.julialang.org/u/mrapo)\
**Post date:** [May 17, 2018, 10:24am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/1 "2018-05-17T10:24:11Z")

</div>

I’m trying to minimize a function with JuMP. The variable of the objective function is a 30 element vector x and the optimization is supposed to start from a given vector x0.

**How can I give vector x0 as the start?** If i define the x variable as follows:

```
@variable(model, x, start = x0)

```

I get the following error:

```
ERROR: LoadError: MethodError: no method matching constructvariable!(::JuMP.Model,
::JuMP.#_error#103{Tuple{Symbol,Symbol,Expr}}, ::Float64, ::Float64, ::Symbol, ::String, ::Array{Float64,1})
Closest candidates are:
constructvariable!(::JuMP.Model, ::Function, ::Number, ::Number, ::Symbol, ::Number, ::Array{T,1}
where T, ::Array{Float64,1}, ::AbstractString, ::Number; extra_kwargs...) at C:\Users\mra136\.julia\
v0.6\JuMP\src\macros.jl:824
constructvariable!(::JuMP.Model, ::Function, ::Number, ::Number, ::Symbol, ::AbstractString, 
::Number; extra_kwargs...) at C:\Users\mra136\.julia\v0.6\JuMP\src\macros.jl:831

```

Which I understand is saying that the start can only be a single number. Thanks for any help in advance.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 17, 2018, 10:35am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/2 "2018-05-17T10:35:23Z")

</div>

You can either declare with index

```julia
@variable(model, x[i=1:n], start=x0[i])

```

or with `setvalue`

```julia
@variable(model, x[1:n]) # edited
setvalue(x, x0)

```

---

<div class="post-metadata">

**Author:** ![mrapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrapo/32/3911_2.png) [@mrapo](https://discourse.julialang.org/u/mrapo)\
**Post date:** [May 17, 2018, 10:47am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/3 "2018-05-17T10:47:56Z")

</div>

Thanks for the aswer @abelsiqueira. Unfortunately I still get error messages.

If I declare with index:

```
@variable(model, x[i=1:n], start=x0[i])

```

I get:

```
ERROR: LoadError: MethodError: Cannot `convert` an object of type JuMP.GenericQuadExpr{Float64,JuMP.
Variable} to an object of type JuMP.GenericAffExpr{Float64,JuMP.GenericQuadExpr{Float64,JuMP.
Variable}}
This may have arisen from a call to the constructor JuMP.GenericAffExpr{Float64,JuMP.GenericQuadExpr
{Float64,JuMP.Variable}}(...),
since type constructors fall back to convert methods.

```

And if I use setvalue:

```
@variable(model, x)
setvalue(x, x0)

```

I get:

```
ERROR: LoadError: MethodError: no method matching setvalue(::JuMP.Variable, ::Array{Float64,1})
Closest candidates are:
setvalue(::JuMP.Variable, ::Number) at C:\Users\mra136\.julia\v0.6\JuMP\src\JuMP.jl:400
setvalue(::Array{T<:JuMP.AbstractJuMPScalar,N} where N, ::Array) where T<:JuMP.AbstractJuMPScalar
at C:\Users\mra136\.julia\v0.6\JuMP\src\JuMP.jl:926

```

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 17, 2018, 11:02am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/4 "2018-05-17T11:02:05Z")

</div>

Sorry, the second answer was a mistake, it’s actually

```julia
@variable(model, x[1:n])
setvalue(x, x0)

```

Don’t know what’s wrong with the first, though.

---

<div class="post-metadata">

**Author:** ![migarstka](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/migarstka/32/3828_2.png) [@migarstka](https://discourse.julialang.org/u/migarstka)\
**Post date:** [May 17, 2018, 11:06am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/5 "2018-05-17T11:06:06Z")

</div>

Hey,  
you should be able to use the setvalue() function:

```julia
@variable(m, x[1:30])

for i in 1:30
    setvalue(x[i],x0[i])
end

```

---

<div class="post-metadata">

**Author:** ![mrapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrapo/32/3911_2.png) [@mrapo](https://discourse.julialang.org/u/mrapo)\
**Post date:** [May 17, 2018, 11:35am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/6 "2018-05-17T11:35:22Z")

</div>

Thanks @abelsiqueira and @migarstka for your answers, they both make sense. I tried both ways you suggested, and now I get a new error:

```
ERROR: LoadError: MethodError: Cannot `convert` an object of type JuMP.GenericQuadExpr{Float64,JuMP.Variable}
to an object of type JuMP.GenericAffExpr{Float64,JuMP.GenericQuadExpr{Float64,JuMP.Variable}}
This may have arisen from a call to the constructor JuMP.GenericAffExpr{Float64,JuMP.GenericQuadExpr{Float64,
JuMP.Variable}}(...),
since type constructors fall back to convert methods.

```

So there’s still something wrong.

---

<div class="post-metadata">

**Author:** ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)\
**Post date:** [May 17, 2018, 11:43am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/7 "2018-05-17T11:43:34Z")

</div>

Can you post a minimum example? The following works for me

```julia
using JuMP
model = Model()
@variable(model, x[1:2])
setvalue(x, [1.2; 1.0])

```

Also, the JuMP and Julia versions.

---

<div class="post-metadata">

**Author:** ![mrapo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrapo/32/3911_2.png) [@mrapo](https://discourse.julialang.org/u/mrapo)\
**Post date:** [May 17, 2018, 11:52am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/8 "2018-05-17T11:52:57Z")

</div>

@abelsiqueira I now noticed that the error message happens on the line where I define the @objective. I will probably open a new topic about that.

---

<div class="post-metadata">

**Author:** ![Juser](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@Juser](https://discourse.julialang.org/u/Juser)\
**Post date:** [May 27, 2018, 4:00am UTC](https://discourse.julialang.org/t/jump-start-the-optimization-with-a-vector/10951/9 "2018-05-27T04:00:34Z")

</div>

I could be misunderstanding your question, but if you’re trying to do this with an arbitrary user-defined function, then I think you probably want something like [this](https://stackoverflow.com/questions/44710900/juliajump-variable-number-of-arguments-to-function/44711305#44711305).
