LoadError:MethodError despite (seemingly) using working code

126 views Asked by At

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
2

There are 2 answers

1
Przemyslaw Szufel On BEST ANSWER

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.

0
Cameron Bieganek On

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