JuMP, start the optimization with a vector

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.

You can either declare with index

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

or with setvalue

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

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

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

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

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

1 Like

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

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

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

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.

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

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

Also, the JuMP and Julia versions.

@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.

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.