# JuMP.set\_objective\_coefficient performance

**URL:** <https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283>\
**Category:** Performance\
**Tags:** question\
**Created:** [November 17, 2020, 9:55am UTC](https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283 "2020-11-17T09:55:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![omicron](https://avatars.discourse-cdn.com/v4/letter/o/848f3c/32.png) [@omicron](https://discourse.julialang.org/u/omicron)\
**Post date:** [November 17, 2020, 9:55am UTC](https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283/1 "2020-11-17T09:55:36Z")

</div>

Hi, I’m using JuMP to build LP-problems and later I want to update the objective coefficients with the function set\_objective\_coefficient instead of building the whole LP again. Below are the functions I defined and the time it took to run them. They seem to be slow. Please help me make the code faster.

```julia
function BuildFull(Big::Float64)::JuMP.Model

   M = Model(with_optimizer(CPLEX.Optimizer))
   NArea = 57
   NMod = 20
   NKOpt = 168
   InfUB = 1.0E8

   @variable(M,0.0 <= spi[iArea=1:NArea,iMod=1:NMod,k=1:NKOpt] <= InfUB, base_name="spi")
   @objective(M,MathOptInterface.MIN_SENSE, sum(spi[iArea,iMod,k] for iArea=1:NArea for iMod=1:NMod for k=1:NKOpt)) 

   return M
end

function update_coeff!(model::JuMP.Model)
     iMod = 1
     spi = model[:spi]
	  for iArea=1:NArea
        for k=1:NKOpt
		      spi_coeff = 2.0
		      set_objective_coefficient(model, spi[iArea,iMod,k], spi_coeff)
         end
	  end
	  return nothing

end

```

I time the functions with the @time macro

```julia
using JuMP
using CPLEX
using MathOptFormat
using MathOptInterface
const MOI = MathOptInterface

const Big = 1.0E18

model = @time BuildFull(Big)
@time update_coeff!(model)

```

The @time macro returns the following:  
0.575319 seconds (2.38 M allocations: 127.048 MiB, 51.21% gc time)  
20.401694 seconds (94.43 k allocations: 27.333 GiB, 20.58% gc time)

This is the second run. 20 seconds seems a long time.  
Is it possible to speed things up?  
Tips to increase performance of @variable is also appreciated.

These are the package versions I use:

```julia
julia> Pkg.installed()
Dict{String,Union{Nothing, VersionNumber}} with 14 entries:
  "Distributions" => v"0.24.3"
  "Clp" => v"0.8.3"
  "Atom" => v"0.12.25"
  "CPLEX" => v"0.6.2"
  "JuMP" => v"0.20.1"
  "Juno" => v"0.8.4"
  "DelimitedFiles" => nothing
  "MathOptFormat" => v"0.4.0"
  "StatsBase" => v"0.33.2"
  "HDF5" => v"0.12.5"
  "Plots" => v"1.4.3"
  "Cbc" => v"0.7.1"
  "MathOptInterface" => v"0.9.8"
  "Printf" => nothing

```

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [November 17, 2020, 11:16am UTC](https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283/2 "2020-11-17T11:16:29Z")

</div>

A simple speed-up option is to iterate over columns instead of rows in multi-dimensional arrays (as stated in the Julia performance tips). Thus in function `update_coeff!` do

```julia
for k=1:NKOpt
   for iArea=1:NArea        
		spi_coeff = 2.0
	    set_objective_coefficient(model, spi[iArea,iMod,k], spi_coeff)
   end
end

```

But this is still slower that re-building the model (at least on my machine). I get

```julia
0.253191 seconds (2.30 M allocations: 125.016 MiB)
4.129311 seconds (38.30 k allocations: 27.330 GiB, 4.25% gc time).

```

JuMP seems to allocate a lot of memory for the `set_objective_coefficient` function.

---

<div class="post-metadata">

**Author:** ![omicron](https://avatars.discourse-cdn.com/v4/letter/o/848f3c/32.png) [@omicron](https://discourse.julialang.org/u/omicron)\
**Post date:** [November 17, 2020, 12:42pm UTC](https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283/3 "2020-11-17T12:42:59Z")

</div>

Thanks, mike\_k. Good point.

However, changing to iterate over columns hardly makes any difference here.  
I get :

```julia
  0.551281 seconds (2.38 M allocations: 127.002 MiB, 43.35% gc time)
 19.040779 seconds (94.42 k allocations: 27.333 GiB, 14.91% gc time)

```

I see I have more gc time.  
Are yoiu on the same JuMP version as I? JuMP 0.20.1

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [November 17, 2020, 1:27pm UTC](https://discourse.julialang.org/t/jump-set-objective-coefficient-performance/50283/4 "2020-11-17T13:27:38Z")

</div>

Interesting, the exchange I suggested reduced the runtime by 50% on my machine. I am using JuMP 0.21.2
