Code execution problem

Hi @dty, welcome to Julia!

Please provide as much context as possible to your question, formulate it as a minimal working example, and do not use screen-shots but actual text (so people can copy paste to try it out). Remember, when answering questions, people are gifting their time to you; so you should make it as easy as possible for them to help you!

A good read is also Please read: make it easier to help you

1 Like

@variable is from JuMP, no? So you probably need using JuMP.

yes.I have installed jump,but @variable is also error.

OK,thanks

As @kristoffer.carlsson said , before defining your function you need to write explicitly:

using JuMP

function blablabla()

end

it is not enought to have the package installed, you have to execute using JuMP to actually have access to its functions.

1 Like

i have try it.but it still error

Execute only the using JuMP line first. Then the rest.

Your example is also using an old version of JuMP.

Read the documentation Introduction · JuMP
For the new syntax. The quick start guide is a good starting point.

it still same error:pensive:

Hi dty:

You have received some suggestions, but it is difficult to help if you just show us screenshots, remember to read the advice given to you on the first answer.

Try to please update your packages using ]up in the REPL.
Check the documentation given by @odow and adapt your code.

If it still doesn’t work, please copy here your code (not a screenshot) so that somebody else can try and run it.

Also, please tell us with detail how are you running it (are you sending the whole block to the REPL?, does using JuMP work without any error message?)

this is my code:

using Pkg
Pkg.add("MathOptInterface")
using Pkg
Pkg.add("JuMP")
using Pkg
Pkg.add("Interact")
using Pkg
Pkg.add("Gadfly")
const g_max = [1000,1000]
const g_min = [0,300]
const c_g = [50,100]
const c_g0 = [1000,0]
const c_w = 50
const d = 1500
const w_f = 200
function opt_ed(g_max,g_min,c_g,c_w,d,w_f)
ed = Model()
@defVar(ed,0 < g[i=1:2] <= g_max[i])
@defVar(ed,0 <= w <= w_f)
@setObjective(ed,Min,sum(c_g[i]*g[i],i = 1:2) + c_w*w)
for i in 1:2
    @addConstraint(ed,g[i] <= g_max[i])
    @addConstraint(ed,g[i] >= g_min[i])
@addConstraint(ed,w <= w_f)
@addConstraint(ed,sum(g([i],i = 1:2) + w == d))
end
solve(ed)
(g_opt,w_opt,ws_opt,obj) = solved(g_max,g_min,c_g,c_w,d,w_f);
println("\n")
println("Dispatch of Generators: ", g_opt[i=1:2], " MW")
println("Dispatch of Wind: ", w_opt, " MW")
println("Wind spillage: ", w_f-w_opt, " MW")
println("\n")
println("Total cost: ", obj, "\$")
end

i’m using the IDE is Juno

You still don’t have a using JuMP in your code – Pkg.adding a package isn’t enough to use it.

so,firstly, add (“JuMP”) in Pkg model,in addition to this,what should i do?

You do not have to add packages several times, you could replace

using Pkg
Pkg.add("MathOptInterface")
using Pkg
Pkg.add("JuMP")
using Pkg
Pkg.add("Interact")
using Pkg
Pkg.add("Gadfly")

with

using Pkg
Pkg.add("MathOptInterface")
Pkg.add("JuMP")
Pkg.add("Interact")
Pkg.add("Gadfly")

and run it only once. Then, it should be installed in your computer, and you do not need it more. In your code, you must add at the beginning:

using JuMP
using MathOptInterface
using Interact
using Gadfly

Please, read the documentation about modules. To use a module in your code you must use

using PackageName

and then, you can use the functions, macros defined in the package.

I suggest you to see learning materials in Get started with Julia or in https://juliaacademy.com/ the free Julia Introduction course (with videos and examples), that includes this type of contents.

2 Likes