2

Hi guys programming newbie here

I am trying to solve a linear programming problem using Julia and GLPK, with emphasis on trying because so far I have been getting errors no matter what I try. This is the code:

using JuMP, GLPK
m = Model(GLPK.Optimizer)
@variable(m,  x1 >= 0  )
@variable(m,  x2 >= 0  )
@variable(m,  x3 >= 0  )
@objective(m, Max,  200x1 +200x2 + 700x3  )
@constraint(m,  2x1 + x2 + 3x3 <= 22  )
@constraint(m,  x1 + 2x2 + 4x3 <= 20  )
@constraint(m,  x1 + x2 + x3 <= 10  )
optimize!(m)
println("Objective value: ", JuMP.objective_value(m))
println("x1 = ", JuMP.value(x1))
println("x2 = ", JuMP.value(x2))
println("x2 = ", JuMP.value(x3))

From what I can tell the error is in the second line, calling GLPK (or maybe the first one because it doesen't "import" GLPK) The above code is the exact code my lecturer uploaded as a solution to the problem yet it wont run on my Windows PC, I suspected it had something to do with Windows Defender, so tried without, I have uninstalled and reinstalled Julia and Atom (and the packages), I've tried including the code directly in the Julia terminal, as well as doing all of these on my laptop but to no avail. Julia ver.: 1.5.2 and I have also tested 1.0.5

I'm having a hard time figuring out what to do, and I haven't been able to find anything on Google either. I hope my question isn't too dumb, and I will greatly appreciate a solution or info!

Error message:

LoadError: MethodError: no method matching Model(::Type{GLPK.Optimizer})
Closest candidates are:
  Model(::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any) at C:\Users\Peter\.julia\packages\JuMP\iGamg\src\JuMP.jl:126
  Model(; caching_mode, solver) at C:\Users\Peter\.julia\packages\JuMP\iGamg\src\JuMP.jl:161
  Model(!Matched::MathOptInterface.AbstractOptimizer, !Matched::Dict{MathOptInterface.ConstraintIndex,AbstractShape}, !Matched::Set{Any}, !Matched::Any, !Matched::Any, !Matched::Dict{Symbol,Any}, !Matched::Int64, !Matched::Dict{Symbol,Any}) at C:\Users\Peter\.julia\packages\JuMP\iGamg\src\JuMP.jl:126
  ...
in expression starting at C:\Users\Peter\iCloudDrive\BSc\Introduktion til operationsanalyse\Opgaver\Week1Ex1.jl:2
top-level scope at Week1Ex1.jl:2
include_string(::Function, ::Module, ::String, ::String) at loading.jl:1088

Pkg.status():

julia> Pkg.status()
Status `C:\Users\Peter\.julia\environments\v1.5\Project.toml`
  [c52e3926] Atom v0.12.21
  [60bf3e95] GLPK v0.13.0
  [3c7084bd] GLPKMathProgInterface v0.5.0
  [4076af6c] JuMP v0.20.0
  [e5e0dc1b] Juno v0.8.3
Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
frivo9
  • 23
  • 4
  • 1
    Can you add the error stacktrace to your question? – Cameron Bieganek Sep 30 '20 at 18:44
  • 1
    What version of Julia are you using? – Cameron Bieganek Sep 30 '20 at 18:50
  • 1
    It would be easier to read the stacktrace if you edit your question to include the stacktrace as a code chunk. :) – Cameron Bieganek Sep 30 '20 at 19:05
  • Yes I realized :P I have added it to the question now – frivo9 Sep 30 '20 at 19:08
  • 1
    Your code works on my Mac. Try running `using Pkg; Pkg.status()` in your Julia REPL and pasting the output of that into your question. – Cameron Bieganek Sep 30 '20 at 19:15
  • I have `JuMP v0.21.4` and `GLPK v0.14.2` which seems to work with the code in your question. Try running `Pkg.update()` to update your packages. However, note that sometimes the listed compatibility bounds of a different package will limit how far certain packages can be updated. – Cameron Bieganek Sep 30 '20 at 19:21
  • Yeah, it looks like `GLPKMathProgInterface` limits `GLPK` to `v0.13`, so you will probably need to do `Pkg.rm("GLPKMathProgInterface"); Pkg.update()`. – Cameron Bieganek Sep 30 '20 at 19:25
  • I still get the same error after `Pkg.update`, I marked the question below as an answer since using `m = Model(with_optimizer(GLPK.Optimizer))` works. Thanks for you input! – frivo9 Sep 30 '20 at 19:30

2 Answers2

3

Perhaps this example is using an old API version that has changed something like one year ago.

Try (requires at least JuMP 0.21.0):

m = Model(optimizer_with_attributes(GLPK.Optimizer))

There is also a shorter version if you do not plan to add attributes (or have JuMP older than 0.21.0 but at least 0.18.0):

m = Model(with_optimizer(GLPK.Optimizer))

However, I always end up using the first one. One common attribute I always use is how verbose messages I want to see, eg.:

m = Model(optimizer_with_attributes(GLPK.Optimizer,  "msg_lev" => GLPK.GLP_MSG_ALL))

Edit

Looking at your package status do the following:

using Pkg
pkg"rm GLPKMathProgInterface"
pkg"up JuMP"

this should get you sorted out with package versions. When you do this the first version of my code will work (now you have an outdated JuMP version).

Moreover note that Atom is not maintained anymore - consider switching to VS CODE.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • 1
    `optimizer_with_attributes` was added in Jump 0.21.0 (so this is the latest version), the new API was introduced in JuMP 0.18.0 as far as I remember. So it all depends what version you are running! – Przemyslaw Szufel Sep 30 '20 at 19:19
2

Collecting my comments into an answer for easier reading.

The GLPKMathProgInterface.jl package is holding back your version of GLPK.jl. Run the following to update your packages:

Pkg.rm("GLPKMathProgInterface")
Pkg.update()

Alternatively, you can run the equivalent commands in package mode. To enter package mode, type ]. Then in package mode, run the following:

(@v1.5) pkg> rm GLPKMathProgInterface

(@v1.5) pkg> update
Cameron Bieganek
  • 7,208
  • 1
  • 23
  • 40