# JuMP set start values in new model with solution of smaller model

**URL:** <https://discourse.julialang.org/t/jump-set-start-values-in-new-model-with-solution-of-smaller-model/90482>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [November 19, 2022, 2:22am UTC](https://discourse.julialang.org/t/jump-set-start-values-in-new-model-with-solution-of-smaller-model/90482 "2022-11-19T02:22:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nlaws](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nlaws/32/22205_2.png) [@nlaws](https://discourse.julialang.org/u/nlaws)\
**Post date:** [November 19, 2022, 2:22am UTC](https://discourse.julialang.org/t/jump-set-start-values-in-new-model-with-solution-of-smaller-model/90482/1 "2022-11-19T02:22:11Z")

</div>

In the JuMP docs it shows how to efficiently set start values using one model:

```julia
x = all_variables(model)
x_solution = value.(x)
set_start_value.(x, x_solution)

```

However, I am looking for an efficient way to do this with two models, where the first model (with a solution) includes a subset of the variables in a second model.

Here is my very slow attempt:

```julia
# ... build model1
optimize!(model1)

x1 = all_variables(model1);
x1_solution = value.(x1);

d = Dict(string(n)=>v for (n,v) in zip(x1, x1_solution))

model2 = JuMP.Model()
# ... create second model that has more variables than model1
# and also shares variable names with model1

x2 = all_variables(model2);

for (n1,v) in d # this loop is very slow, like one variable/second 
    x2_index = findfirst(n2 -> string(n2) == n1, x2)
    if !(isnothing(x2_index))
        set_start_value(x2[x2_index], v)
        @info("Set start value for $(x2[x2_index])")
    end
end

```

I also question if I am doing this correctly?

---

<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:** [November 20, 2022, 6:20pm UTC](https://discourse.julialang.org/t/jump-set-start-values-in-new-model-with-solution-of-smaller-model/90482/2 "2022-11-20T18:20:20Z")

</div>

The problem is this loop:

> ```julia
> for (n1,v) in d # this loop is very slow, like one variable/second 
> x2_index = findfirst(n2 -> string(n2) == n1, x2)
> if !(isnothing(x2_index))
> set_start_value(x2[x2_index], v)
> @info("Set start value for $(x2[x2_index])")
> end
> end
> 
> ```

Remove the `if` block and just look at what it is doing:

> ```julia
> for (n1, v) in d
> x2_index = findfirst(n2 -> string(n2) == n1, x2)
> end
> 
> ```

The outer loop is `O(N)` because it loops over every variable in `model1`.

So for every variable in `model1`, you have a (short-circuited) loop over (potentially) every variable in `x2`. And for every variable in the inner loop, you are converting it to a `string`. So you’re calling `string(n2)` `O(N^2)` times.

I haven’t tested (be wary of typos, etc), but I assume something like this would be faster

```Julia
d1 = Dict{String,Float64}(
    JuMP.name(x) => value for
    (x, value) in zip(x1, x1_solution) if !isempty(JuMP.name(x))    
)
d2 = Dict{String,VariableRef}(
    JuMP.name(x) => x for x in x2 if !isempty(JuMP.name(x))
)
for (name_1, start) in d
    x_2 = get(d2, name_1, nothing)
    if x2_i !== nothing
        set_start_value(x2_i, start)
    end
end

```

or even

```julia
model1 = JuMP.Model()
model2 = JuMP.Model()
x1 = all_variables(model1)
x1_solution = value.(x1)
for (x, start) in zip(x1, x1_solution)
    name = JuMP.name(x)
    if isempty(name)
        continue
    end
    x2 = variable_by_name(model2, name)
    if x2 !== nothing
        set_start_value(x2, start)
    end
end

```
