Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
personal:blog:2017:0203_jump_for_gams_users [2023/12/22 11:15]
antonello [Installation]
personal:blog:2017:0203_jump_for_gams_users [2023/12/22 11:37]
antonello [Further help] COMPLETED UPDATING OF THE PAGE
Line 44: Line 44:
 ==== Importing the libraries ==== ==== Importing the libraries ====
  
-You will need to import as a minima the ''JuMP'' module. If you wish to specify a solver engine rather than letting JuMP select a suitable one, you will need to import also the module relative to the solver, e.g. ''Ipopt'' or  ''GLPKMathProgInterface''+You will need to import as a minima the ''JuMP'' module and a suitable solver. In this case the problem is linear, so we can use ''GLPK'' (''HiGHS'' is another popular alternative). If the problem would have been non-linear, you could have used the ''Ipopt'' solver/package
  
 <code  julia> <code  julia>
-# Import of the JuMP and DataFrames modules (the latter one just to import the data from a header based table, as in the original trasnport example in GAMS  +# Import of the JuMP, GLPK, CSV and DataFrames modules (the latter twos just to import the data from a header based table, as in the original trasnport example in GAMS  
-using JuMP, DataFrames+using CSV, DataFrames, GLPK, JuMP
 </code> </code>
  
Line 96: Line 96:
 #      seattle          2.5           1.7          1.8 #      seattle          2.5           1.7          1.8
 #      san-diego        2.5           1.8          1.4  ; #      san-diego        2.5           1.8          1.4  ;
-d_table = wsv"""+d_table = CSV.read(IOBuffer("""
 plants     new_york  chicago  topeka plants     new_york  chicago  topeka
 seattle    2.5       1.7      1.8 seattle    2.5       1.7      1.8
 san_diego  2.5       1.8      1.4 san_diego  2.5       1.8      1.4
-"""+"""), DataFrame, delim=" ", ignorerepeated=true,copycols=true)
 d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets) d = Dict( (r[:plants],m) => r[Symbol(m)] for r in eachrow(d_table), m in markets)
 # Here we are converting the table in a "(plant, market) => distance" dictionary # Here we are converting the table in a "(plant, market) => distance" dictionary
Line 132: Line 132:
  
 Here we declare a JuML optimisation model and we give it a name. This name will be then passed as first argument to all the subsequent operations, like creation of variables, constraints and objective function.\\ Here we declare a JuML optimisation model and we give it a name. This name will be then passed as first argument to all the subsequent operations, like creation of variables, constraints and objective function.\\
-We can, if we wish, works with several models at the same time.\\ +The solver engine to use is given as argument of the ''Model()'' call.\\ 
-If we do not specify a solver, we let JuML use a suitable solver for the type of problem. Aside to specify the solver, we can also pass it solver-level options, e.g.: +We could pass solver-specific options with the ''set_optimizer_attribute'' function, e.g.: 
-''mymodel = Model(solver=IpoptSolver(print_level=0))''+''set_optimizer_attribute(trmodel, "msg_lev", GLPK.GLP_MSG_ON)''
  
 <code julia> <code julia>
 # Model declaration (transport model) # Model declaration (transport model)
-trmodel = Model()  +trmodel = Model(GLPK.Optimizer
 </code> </code>
  
Line 197: Line 196:
 ==== Resolution of the model ==== ==== Resolution of the model ====
  
-It is at this point that the solver is called and the model is passed to the solver engine for its solution. The return value is the status of the optimisation (":Optimal" if all went fine)+It is at this point that the solver is called and the model is passed to the solver engine for its solution. The return value is the status of the optimisation (''MOI.OPTIMAL'' if all went fine)
  
 <code julia> <code julia>
-status = solve(trmodel)+optimize!(trmodel) 
 +status = termination_status(trmodel)
 </code> </code>
  
Line 208: Line 208:
  
 <code julia> <code julia>
-if status == :Optimal +if status == MOI.OPTIMAL 
-    println("Objective value: ", getobjectivevalue(trmodel)) +    println("Objective value: ", objective_value(trmodel)) 
-    println(getvalue(x))+    println("Shipped quantities: ") 
 +    println(value.(x))
     println("Shadow prices of supply:")     println("Shadow prices of supply:")
-    [println("$p = $(getdual(supply[p]))") for p in plants]+    [println("$p = $(dual(supply[p]))") for p in plants]
     println("Shadow prices of demand:")     println("Shadow prices of demand:")
-    [println("$m = $(getdual(demand[m]))") for m in markets]+    [println("$m = $(dual(demand[m]))") for m in markets] 
 + 
 else else
     println("Model didn't solved")     println("Model didn't solved")
Line 224: Line 226:
 ==== Editing and running the script ==== ==== Editing and running the script ====
 Differently from GAMS you can use whatever editor environment you wish to code a JuMP script. If you don't need debugging features, a simple text editor like Notepad++ (in windows), gedit or kate (in Linux) will suffice. They already have syntax highlight for Julia.\\ Differently from GAMS you can use whatever editor environment you wish to code a JuMP script. If you don't need debugging features, a simple text editor like Notepad++ (in windows), gedit or kate (in Linux) will suffice. They already have syntax highlight for Julia.\\
-If you want advanced features and debugging capabilities you can use a dedicated Julia IDE, like e.g. [[http://junolab.org/|Juno]].+If you want advanced features and debugging capabilities you can use a dedicated Julia IDE, like the [[https://www.julia-vscode.org/|Julia extension for VSCode]].
  
-If you are using instead the Julia console,  you can run the script as ''julia transport.jl''.+If you are using instead the Julia terminal,  you can run the script as ''julia transport.jl''.
  
 ===== Further help ===== ===== Further help =====
-Documentation of JuMP is available from [[https://jump.readthedocs.io/en/latest/|this page]]. However if you want to do serious things with juMPit is most likely that you will have to either look at the source code or consult the [[https://discourse.julialang.org/c/domain/opt|discussion forum]].+Documentation of JuMP is available from [[https://jump.dev/|this page]], and community-based support is available on [[https://discourse.julialang.org/c/domain/opt|the Discourse forum]].
  
 Happy modelling with JuMP ;-) Happy modelling with JuMP ;-)
personal/blog/2017/0203_jump_for_gams_users.txt · Last modified: 2023/12/22 11:39 by antonello
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0