# Optimization errors with simple JuMP function using GLPK Optimizer

**URL:** <https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652>\
**Category:** Optimization (Mathematical)\
**Created:** [September 15, 2020, 6:40pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652 "2020-09-15T18:40:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![standon41](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@standon41](https://discourse.julialang.org/u/standon41)\
**Post date:** [September 15, 2020, 6:40pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/1 "2020-09-15T18:40:47Z")

</div>

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:

```julia
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)  

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 15, 2020, 7:37pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/2 "2020-09-15T19:37:38Z")

</div>

You have an old version of JuMP for some reason

Run

```nohighlight
] 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`.

---

<div class="post-metadata">

**Author:** ![standon41](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@standon41](https://discourse.julialang.org/u/standon41)\
**Post date:** [September 15, 2020, 7:51pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/3 "2020-09-15T19:51:17Z")

</div>

Here is the output for `]st`

```julia
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.

```julia
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…

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 15, 2020, 8:13pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/4 "2020-09-15T20:13:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![standon41](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@standon41](https://discourse.julialang.org/u/standon41)\
**Post date:** [September 15, 2020, 8:27pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/5 "2020-09-15T20:27:40Z")

</div>

Yes I restarted Julia, Jupyter and my laptop

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 15, 2020, 8:47pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/6 "2020-09-15T20:47:30Z")

</div>

I take it you’re using [my tutorial from the Grid Science Winter School](https://github.com/lanl-ansi/tutorial-grid-science-2019)? 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

```nohighlight
(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](https://github.com/jump-dev/JuMPTutorials.jl)

---

<div class="post-metadata">

**Author:** ![standon41](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@standon41](https://discourse.julialang.org/u/standon41)\
**Post date:** [September 16, 2020, 7:00pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/7 "2020-09-16T19:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![standon41](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@standon41](https://discourse.julialang.org/u/standon41)\
**Post date:** [September 16, 2020, 8:11pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/8 "2020-09-16T20:11:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 16, 2020, 8:24pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/9 "2020-09-16T20:24:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [September 16, 2020, 8:53pm UTC](https://discourse.julialang.org/t/optimization-errors-with-simple-jump-function-using-glpk-optimizer/46652/10 "2020-09-16T20:53:07Z")

</div>

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/](https://julialang.github.io/Pkg.jl/v1/environments/)
