Optimization errors with simple JuMP function using GLPK Optimizer

Hello,

I am a novice to Julia programming. Going though a tutorial I see myself running into this error and I am not quite sure what it means. Can someone who is an expert in JuMP and GLPK advise?

Error:
MethodError: no method matching Model(::Type{GLPK.Optimizer})

Code:

using Interact
using Plots
using JuMP, GLPK
# Define some input data about the test system
# Maximum power output of generators
const GENERATION_MAX = [1000, 1000]
# Minimum power output of generators
const GENERATION_MIN = [0, 300]
# Incremental cost of generators 
const COST_GENERATION = [50, 100]
# Incremental cost of wind generators
const COST_WIND = 50
# Total demand
const DEMAND = 1500
# Wind forecast
const WIND_MAX = 200;

function solve_economic_dispatch(;
        cost_of_thermal = COST_GENERATION, 
        cost_of_wind = COST_WIND)

    economic_dispatch = Model((GLPK.Optimizer))
    
    # Define decision variables    
    @variables(economic_dispatch, begin
        g[i=1:2]  # Thermal generation (MW).
        w >= 0  # Wind power (MW).
    end)

    # Define the objective function
    @objective(economic_dispatch, Min, 
        sum(cost_of_thermal[i] * g[i] for i in 1:2) + cost_of_wind * w
    )

    # Define the constraint on the maximum and minimum power output of each generator.
    for i in 1:2
        @constraint(economic_dispatch, g[i] <= GENERATION_MAX[i])
        @constraint(economic_dispatch, g[i] >= GENERATION_MIN[i])
    end
    
    @constraints(economic_dispatch, begin
        # Define the constraint on the wind power injection
        w <= WIND_MAX
        # Define the power balance constraint
        sum(g[i] for i in 1:2) + w == DEMAND
    end)

    # Solve statement
    optimize!(economic_dispatch)
    
    # Return the optimal value of the objective function and its minimizers
    # as a NamedTuple.
    return (
        generation = value.(g), 
        wind_generation = value(w),
        wind_spillage = WIND_MAX - value(w),
        cost = objective_value(economic_dispatch)
    )
end

# Solve the economic dispatch problem
solution = solve_economic_dispatch()

println("Dispatch")
println("   Generators: ", solution.generation, " MW")
println("         Wind: ", solution.wind_generation, " MW")
println("Wind spillage: ", solution.wind_spillage, " MW") 
println("----------------------------------")
println("Total cost: \$", solution.cost)  

You have an old version of JuMP for some reason

Run

] up
] add JuMP@0.21

Then close Julia (or VS code, etc), re-open Julia, and try again. If you encounter any errors, post the full text, as well as the output of ] st.

1 Like

Here is the output for ]st

Status `~/.julia/environments/v1.5/Project.toml`
  [60bf3e95] GLPK v0.14.1
  [7073ff75] IJulia v1.21.3
  [b6b21f68] Ipopt v0.6.3
  [4076af6c] JuMP v0.21.4
  [c36e90e8] PowerModels v0.17.3 `https://github.com/lanl-ansi/PowerModels.jl#master`

Unfortunately, updating those modules did little. Here is the full text of the error.

MethodError: no method matching Model(::Type{GLPK.Optimizer})
Closest candidates are:
  Model(::Any, !Matched::Symbol, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Int64, !Matched::Array{String,1}, !Matched::Array{String,1}, !Matched::Array{Float64,1}, !Matched::Array{Float64,1}, !Matched::Array{Symbol,1}, !Matched::Array{T,1} where T, !Matched::Array{Tuple{Symbol,Any},1}, !Matched::Any, !Matched::Any, !Matched::Array{Float64,1}, !Matched::Array{Float64,1}, !Matched::Array{Float64,1}, !Matched::Array{Float64,1}, !Matched::Array{Array{Int64,1},1}, !Matched::Array{Array{Tuple{Int64,Int64},1},1}, !Matched::Any, !Matched::MathProgBase.SolverInterface.AbstractMathProgSolver, !Matched::Bool, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Array{T,1} where T, !Matched::JuMP.IndexedVector{Float64}, !Matched::Any, !Matched::Bool, !Matched::Dict{Symbol,Any}, !Matched::IdDict, !Matched::Int64, !Matched::Int64, !Matched::Dict{Symbol,Any}) at /Users/shubhamtandon/.julia/packages/JuMP/I7whV/src/JuMP.jl:87
  Model(::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any) at /Users/shubhamtandon/.julia/packages/JuMP/I7whV/src/JuMP.jl:87
  Model(; solver, simplify_nonlinear_expressions) at /Users/shubhamtandon/.julia/packages/JuMP/I7whV/src/JuMP.jl:167

Stacktrace:
 [1] solve_economic_dispatch(; cost_of_thermal::Array{Int64,1}, cost_of_wind::Int64) at ./In[4]:5
 [2] solve_economic_dispatch() at ./In[4]:5
 [3] top-level scope at In[4]:45
 [4] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

I don’t understand as I am simply running whats given in the tutorial…

Did you close Julia and restart it? Whenever you update packages, you need to restart it for the changes to take effect.

Yes I restarted Julia, Jupyter and my laptop

I take it you’re using my tutorial from the Grid Science Winter School? Did you follow the installation instructions and run the first cell?

Because your example is using Interact and Plots, but they aren’t in your output of ] st.

If I use your versions, it works for me

(pm) pkg> st
Status `/private/tmp/pm/Project.toml`
  [60bf3e95] GLPK v0.14.1
  [7073ff75] IJulia v1.21.3
  [b6b21f68] Ipopt v0.6.3
  [4076af6c] JuMP v0.21.4
  [c36e90e8] PowerModels v0.17.3 `https://github.com/lanl-ansi/PowerModels.jl.git#master`

julia> using JuMP, GLPK

julia> Model(GLPK.Optimizer)
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: EMPTY_OPTIMIZER
Solver name: GLPK

You could also try these tutorials to get started https://github.com/jump-dev/JuMPTutorials.jl

1 Like

Sorry for the late response. I really appreciate your quick replies.

Installing Interact and Plots gives me the same issue. Same with the Pkg instantiation. I might reinstall Juila, because this error shouldn’t really be happening.

Thnaks again

No luck yet Oscar. Do you know why this error might occur?

I’ll send you a private message. This error has appeared a few times recently, and it’d be nice to understand the root cause.

For posterity, the issue was a different environment was being used in the IJulia notebook, which contained Pavito, causing JuMP to downgrade.

For others finding this, here is the documentation on Pkg environments:
https://julialang.github.io/Pkg.jl/v1/environments/

1 Like