Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MOI.add_constrained_variable when creating variables #3863

Merged
merged 7 commits into from
Nov 1, 2024

Conversation

odow
Copy link
Member

@odow odow commented Oct 31, 2024

Replacement for #3862

See also jump-dev/HiGHS.jl#245

src/variables.jl Show resolved Hide resolved
Copy link

codecov bot commented Oct 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.55%. Comparing base (c63b19c) to head (a79859c).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3863      +/-   ##
==========================================
+ Coverage   97.54%   97.55%   +0.01%     
==========================================
  Files          44       44              
  Lines        6029     6059      +30     
==========================================
+ Hits         5881     5911      +30     
  Misses        148      148              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@odow odow force-pushed the od/add-constrained-variable branch from 83076c9 to 636191e Compare November 1, 2024 01:24
src/variables.jl Outdated Show resolved Hide resolved
@odow
Copy link
Member Author

odow commented Nov 1, 2024

Before

julia> using JuMP, HiGHS

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, 1 <= x[i in 1:10_000] <= 2)
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.025593 seconds (30.06 k allocations: 2.307 MiB)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 198 samples with 1 evaluation.
 Range (min  max):  24.018 ms   31.237 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     25.140 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   25.339 ms ± 981.654 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

       ▁▂▂▄ ▁▂█▇▆▄                                              
  ▃▁▁▆▆████▅████████▃▄▁▃▆▁▃▁▃▆▁▁▁▃▃▃▃▃▃▁▁▃▁▁▁▁▁▃▁▃▁▁▃▁▁▁▁▁▁▃▁▃ ▃
  24 ms           Histogram: frequency by time         29.2 ms <

 Memory estimate: 2.31 MiB, allocs estimate: 30056.

(hgh) pkg> st
Status `/private/tmp/hgh/Project.toml`
  [87dc4568] HiGHS v1.10.2
  [4076af6c] JuMP v1.23.3

After

julia> using JuMP, HiGHS

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, 1 <= x[i in 1:10_000] <= 2)
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.016782 seconds (30.06 k allocations: 2.307 MiB)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 284 samples with 1 evaluation.
 Range (min  max):  16.344 ms  29.127 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     17.092 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   17.609 ms ±  1.672 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%

  ▄█▆▆▆▆▄▄▃▁▂ ▁                                                
  ██████████████▆█▆▆▆▁▄▆▆▆▆▄▄▁▄▄▄▁▁▁▁▆▄▁▄▄▁▁▄▁▁▁▁▁▄▁▄▁▁▁▁▁▁▁▄ ▆
  16.3 ms      Histogram: log(frequency) by time      25.5 ms <

 Memory estimate: 2.31 MiB, allocs estimate: 30056.

(hgh2) pkg> st
Status `/private/tmp/hgh2/Project.toml`
  [87dc4568] HiGHS v1.10.2 `https://github.com/jump-dev/HiGHS.jl.git#master`
  [4076af6c] JuMP v1.23.3 `https://github.com/jump-dev/JuMP.jl.git#od/add-constrained-variable`

Change

So from 24ms to 16ms, which is about 1/3 faster, or exactly what we predicted might be the case on the call.

Single-sided bounds

Single-side bounds are twice as fast. From 16ms to 8ms:

julia> using JuMP, HiGHS

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, x[i in 1:10_000] >= 0)
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.049627 seconds (41.81 k allocations: 3.119 MiB, 62.90% compilation time)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 274 samples with 1 evaluation.
 Range (min  max):  16.755 ms   22.301 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     18.170 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   18.237 ms ± 583.379 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

                      ▂  ▄▁█▁▃▄▆▄▃▂ ▄▁▃   ▁▂                    
  ▄▁▁▁▁▁▁▁▃▄▁▄▃▅▅▆▇█▆▅█▇▇██████████▇████▅▅██▆▆▇▃▃▅▃▃▄▃▄▄▄▁▄▁▁▄ ▄
  16.8 ms         Histogram: frequency by time         19.6 ms <

 Memory estimate: 2.31 MiB, allocs estimate: 30056.

to

julia> using JuMP, HiGHS

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, x[i in 1:10_000] >= 0)
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.041924 seconds (41.62 k allocations: 3.094 MiB, 73.63% compilation time)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 468 samples with 1 evaluation.
 Range (min  max):   8.908 ms  375.817 ms  ┊ GC (min  max): 0.00%  93.33%
 Time  (median):      9.863 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   10.672 ms ±  16.926 ms  ┊ GC (mean ± σ):  7.02% ±  4.31%

     ▃▁█▃▅▆  ▁  ▂ ▁▃ ▄ ▆▁▇▃ ▂▄▁▃▃▁                              
  ▃▃▆██████▅██▇▇█▇████▇████▅██████▅▄▆▆▃▇▆▄▄▃▃▃▄▃▄▁▃▁▃▃▃▃▃▁▃▃▁▃ ▄
  8.91 ms         Histogram: frequency by time         11.7 ms <

 Memory estimate: 2.31 MiB, allocs estimate: 30056.

@odow
Copy link
Member Author

odow commented Nov 1, 2024

So this is an initial backwards compatible minimal change that is a good performance improvement, but only partially solves the problem.

@joaquimg, you could always write your variables as x in MOI.Interval(l, u) to get another speedup from

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, x[i in 1:10_000] in MOI.Interval(0.0, 1.0))
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.098376 seconds (82.31 k allocations: 7.882 MiB, 63.35% compilation time)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 156 samples with 1 evaluation.
 Range (min  max):  30.089 ms  36.249 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     31.996 ms              ┊ GC (median):    0.00%
 Time  (mean ± σ):   32.061 ms ±  1.038 ms  ┊ GC (mean ± σ):  0.00% ± 0.00%

           ▃  ▃▃▅▅ ▂ ▃▆█ ▆ ▆▃                                  
  ▄▄▅▅▄▇▇▅▄█▅███████████▅████▇██▇▇█▅▅▅▄▁▄▁▄▄▁▁▄▄▁▁▁▁▄▁▄▁▁▁▁▁▄ ▄
  30.1 ms         Histogram: frequency by time        35.5 ms <

to

julia> using JuMP, HiGHS

julia> function main()
           model = direct_model(HiGHS.Optimizer())
           set_silent(model)
           set_string_names_on_creation(model, false)
           @variable(model, x[i in 1:10_000] in MOI.Interval(0.0, 1.0))
           return model
       end
main (generic function with 1 method)

julia> @time main();
  0.087588 seconds (67.48 k allocations: 6.796 MiB, 82.41% compilation time)

julia> using BenchmarkTools

julia> @benchmark main()
BenchmarkTools.Trial: 323 samples with 1 evaluation.
 Range (min  max):  12.555 ms  297.110 ms  ┊ GC (min  max):  0.00%  92.31%
 Time  (median):     13.754 ms               ┊ GC (median):     0.00%
 Time  (mean ± σ):   15.470 ms ±  16.230 ms  ┊ GC (mean ± σ):  10.89% ± 12.55%

  ▄▆▇█▇▄                                                        
  ███████▇▄▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▅▅▇▄▁▅▄▁▄▁▁▁▁▁▄ ▆
  12.6 ms       Histogram: log(frequency) by time      33.9 ms <

 Memory estimate: 4.90 MiB, allocs estimate: 40064.

@odow odow merged commit be7d350 into master Nov 1, 2024
11 checks passed
@odow odow deleted the od/add-constrained-variable branch November 1, 2024 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants