# JuMP: get (warm)start value of an expression

**URL:** <https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [January 20, 2023, 11:06am UTC](https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259 "2023-01-20T11:06:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_standard](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@john\_standard](https://discourse.julialang.org/u/john_standard)\
**Post date:** [January 20, 2023, 11:06am UTC](https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259/1 "2023-01-20T11:06:51Z")

</div>

# Context

I’m trying to write some utility functions to solve non linear control problems using JuMP. Following the [Rocket control tutorial](https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/rocket_control/) I set my state variables as JuMP variables and the dynamics as constraints.  
However I’ve noticed that often the optimizer will fail if I don’t provide a starting value for the states, that are close to the solution imposed by the dynamics.  
Because my dynamics include a control term, itself a JuMP variable, I’d like to be able to compute the starting values for my states based on the starting values of my control (without having to specify by hand the starting value of my control).

# (simplified) MWE

Basically what I need is to be able to initialize a variable based on a function of previously defined variables:

```julia-auto
using JuMP
using Ipopt

test = Model(Ipopt.Optimizer)

@variables(test,begin
    a[i=1:2], (start=[5.0,5.0][i])
    b, (start=sum(a)) # initialize b as a function (sum) of a
end)

```

Doing this I get the following error:

```julia-auto
ERROR: MethodError: Cannot `convert` an object of type AffExpr to an object of type Float64
Closest candidates are:
  convert(::Type{T}, ::T) where T<:Number at number.jl:6
  convert(::Type{T}, ::Number) where T<:Number at number.jl:7
  convert(::Type{T}, ::Base.TwicePrecision) where T<:Number at twiceprecision.jl:273
  ...

```

So I gathered that I must provide a `Number` to the start keyword. Hence I’ve tried to access explicitly the start value:

```julia-auto
using JuMP
using Ipopt

test = Model(Ipopt.Optimizer)

@variables(test,begin
    a[i=1:2], (start=[5.0,5.0][i])
    b, (start=start_value(sum(a))) # initialize b as a function (sum) of a and explicitly request the start_value
end)

```

and get the following error

```julia-auto
ERROR: MethodError: no method matching start_value(::AffExpr)
Closest candidates are:
  start_value(::ConstraintRef{<:AbstractModel, <:MathOptInterface.ConstraintIndex}) at ~/.julia/packages/JuMP/Z1pVn/src/constraints.jl:190
  start_value(::VariableRef) at ~/.julia/packages/JuMP/Z1pVn/src/variables.jl:999

```

I understand start\_value is only defined for `ConstraintRef` and `VariableRef` objects, is there an equivalent to get the starting value implied by a `AffExpr` (the object returned by `sum(a)`)? I feel this must be implemented somewhere (at least using `value.` during the optimization process) in JuMP? How can I access this but using `start_value`? My problem being a non linear control problem, I assume the resulting object is not an `AffExpr` but rather a `JuMP.NonlinearExpression` type, so any pointers specific to this would also be appreciated!

Thanks a bunch!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 21, 2023, 11:53pm UTC](https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259/2 "2023-01-21T23:53:13Z")

</div>

You don’t need to do everything in the macros. Do instead:

```julia
test = Model()
@variables(test, begin
    a[1:2]
    b
end)
set_start_value.(a, [5.0, 5.0])
set_start_value(b, sum(start_value(a[i]) for i in 1:2))

```

---

<div class="post-metadata">

**Author:** ![john\_standard](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@john\_standard](https://discourse.julialang.org/u/john_standard)\
**Post date:** [February 10, 2023, 3:51pm UTC](https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259/3 "2023-02-10T15:51:28Z")

</div>

Apologies I completely forgot to answer back, thank you for your answer.  
Yes doing things outside the macros may make things clearer but my question was rather is there a way to write something like `start_value(Expression)` but after digging into it I realized it doesn’t and the simplest is indeed in this simple example to use `sum(start_value.(a))` instead of trying to evaluate the resulting AffExpr returned by `sum(a)` by typing `start_value.(sum(a))`.  
I’ve found a way to circumvent this anyway, thanks for your help!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 10, 2023, 8:24pm UTC](https://discourse.julialang.org/t/jump-get-warm-start-value-of-an-expression/93259/4 "2023-02-10T20:24:07Z")

</div>

I haven’t tested this, but you could also try

```julia
@variable(model, b, start = value(start_value, sum(a)))

```
