# What is the best way to introduce multiple variables in column generation scheme?

**URL:** https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [February 29, 2024, 4:23pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963 "2024-02-29T16:23:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lucasximenes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasximenes/32/207381_2.png) [@lucasximenes](https://discourse.julialang.org/u/lucasximenes)
#### Post date: [February 29, 2024, 4:23pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963/1 "2024-02-29T16:23:38Z")

</div>

Hello!

I was wondering what is the optimal way to introduce a group of new variables into an existing variable container, and then update their values in the objective function. My main concern is that my current implementation is not vectorized, so I’d imagine it leaves some performance gains on the table, but I can’t find a way to introduce them all at once

The function I’m working with is below:

```julia
function updateModel!(SP::SPFormulation, newColumns::Vector{RouteInfo})
    println("======== Updating model =========")
    varCount = length(SP.model[:λ])
    for (i, column) in enumerate(newColumns)
        push!(SP.model[:λ], @variable(SP.model, base_name="λ[$(varCount + i)]", lower_bound=0))
        set_objective_coefficient(SP.model, SP.model[:λ][varCount + i], column.cost)
        for node in column.route
            if node == 1
                set_normalized_coefficient(SP.model[:depot], SP.model[:λ][varCount + i], 1)
            else
                set_normalized_coefficient(SP.model[:con][node], SP.model[:λ][varCount + i], 1)
            end
        end
    end
end

```

---

<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 29, 2024, 6:01pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963/2 "2024-02-29T18:01:55Z")

</div>

Hi @lucasximenes, welcome to the forum!

What you have is the suggested approach.

Have you benchmarked it and found that adding the columns is a bottleneck?

> My main concern is that my current implementation is not vectorized, so I’d imagine it leaves some performance gains on the table

Julia is not Python. You don’t really need to worry about vectorization.

---

<div class="post-metadata">

### Author: ![lucasximenes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucasximenes/32/207381_2.png) [@lucasximenes](https://discourse.julialang.org/u/lucasximenes)
#### Post date: [February 29, 2024, 7:53pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963/3 "2024-02-29T19:53:47Z")

</div>

Thank you for the quick answer, Oscar! I haven’t set up a proper benchmark to see if this model update is in fact problematic, but I wanted to be sure that the implementation was correct.

---

<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 29, 2024, 8:09pm UTC](https://discourse.julialang.org/t/what-is-the-best-way-to-introduce-multiple-variables-in-column-generation-scheme/110963/4 "2024-02-29T20:09:56Z")

</div>

> I wanted to be sure that the implementation was correct

It looks like you have a good handle on things.

It’s a matter of style (should have minimal performance difference), but I might write your function like this:

```julia
function update_model(SP::SPFormulation, new_columns::Vector{RouteInfo})
    println("======== Updating model =========")
    λ, depot, con = SP.model[:λ], SP.model[:depot], SP.model[:con]
    i = length(λ)
    for column in new_columns
        i += 1
        push!(λ, @variable(SP.model, base_name = "λ[$i]", lower_bound = 0))
        set_objective_coefficient(SP.model, λ[end], column.cost)
        for node in column.route
            set_normalized_coefficient(node == 1 ? depot : con[node], λ[end], 1)
        end
    end
    return
end

```
