Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| personal:blog:2017:0203_jump_for_gams_users [2017/02/03 15:44] – antonello | personal:blog:2017:0203_jump_for_gams_users [2025/05/02 09:41] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 15: | Line 15: | ||
| You have plenty of development environment to choose from (e.g. Jupiter, Juno), a clear modern language, the possibility to interface your model with third party libraries.. all of this basically for free.\\ | You have plenty of development environment to choose from (e.g. Jupiter, Juno), a clear modern language, the possibility to interface your model with third party libraries.. all of this basically for free.\\ | ||
| It is also, at least for my user case, much faster than GAMS. Aside the preparation of the model to pass to the solver, where it is roughly equivalent, in the solver execution I can benefit of having on my system a version of IPOPT compiled with the much more performing ma27 linear solver, while for GAMS I would have to rely on the embedded version that is compiled with the MUMPS linear solver. That's part of the flexibility you gain in using JuMP in place of GAMS. | It is also, at least for my user case, much faster than GAMS. Aside the preparation of the model to pass to the solver, where it is roughly equivalent, in the solver execution I can benefit of having on my system a version of IPOPT compiled with the much more performing ma27 linear solver, while for GAMS I would have to rely on the embedded version that is compiled with the MUMPS linear solver. That's part of the flexibility you gain in using JuMP in place of GAMS. | ||
| - | That's said, for people that don't need such flexibility, | + | That's said, for people that don't need such flexibility, |
| Line 30: | Line 30: | ||
| Run, only once, the following code to install JuMP language and a couple of open source solvers: | Run, only once, the following code to install JuMP language and a couple of open source solvers: | ||
| - | < | + | <code julia> |
| - | Pkg.update() | + | using Pkg # Load the package manager |
| - | Pkg.add(" | + | Pkg.update() |
| - | Pkg.add(" | + | Pkg.add(" |
| - | Pkg.add(" | + | Pkg.add(" |
| - | Pkg.add(" | + | Pkg.add(" |
| + | Pkg.add(" | ||
| + | Pkg.add(" | ||
| </ | </ | ||
| Line 42: | Line 44: | ||
| ==== Importing the libraries ==== | ==== Importing the libraries ==== | ||
| - | You will need to import as a minima the '' | + | You will need to import as a minima the '' |
| - | < | + | < |
| - | # Import of the JuMP and DataFrames modules (the latter | + | # Import of the JuMP, GLPK, CSV and DataFrames modules (the latter |
| - | using JuMP, DataFrames | + | using CSV, DataFrames, GLPK, JuMP |
| </ | </ | ||
| Line 56: | Line 58: | ||
| <code julia> | <code julia> | ||
| - | ## Define sets ## | + | # Define sets # |
| # Sets | # Sets | ||
| # | # | ||
| Line 70: | Line 72: | ||
| <code julia> | <code julia> | ||
| - | ## Define parameters | + | # Define parameters # |
| # | # | ||
| # | # | ||
| Line 94: | Line 96: | ||
| # seattle | # seattle | ||
| # san-diego | # san-diego | ||
| - | d_table = wsv""" | + | d_table = CSV.read(IOBuffer(""" |
| plants | plants | ||
| seattle | seattle | ||
| san_diego | san_diego | ||
| - | """ | + | """ |
| d = Dict( (r[: | d = Dict( (r[: | ||
| # Here we are converting the table in a " | # Here we are converting the table in a " | ||
| Line 130: | 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 '' |
| - | 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 |
| - | '' | + | '' |
| <code julia> | <code julia> | ||
| - | # Model declaration | + | # Model declaration |
| - | trmodel = Model() | + | trmodel = Model(GLPK.Optimizer) |
| </ | </ | ||
| Line 190: | Line 192: | ||
| <code julia> | <code julia> | ||
| print(trmodel) | print(trmodel) | ||
| - | </code | + | </code> |
| ==== 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 (": | + | 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 ('' |
| <code julia> | <code julia> | ||
| - | status = solve(trmodel) | + | optimize!(trmodel) |
| + | status = termination_status(trmodel) | ||
| </ | </ | ||
| Line 205: | Line 208: | ||
| <code julia> | <code julia> | ||
| - | if status == :Optimal | + | if status == MOI.OPTIMAL |
| - | println(" | + | println(" |
| - | println(getvalue(x)) | + | println(" |
| + | println(value.(x)) | ||
| println(" | println(" | ||
| - | [println(" | + | [println(" |
| println(" | println(" | ||
| - | [println(" | + | [println(" |
| + | |||
| else | else | ||
| println(" | println(" | ||
| Line 221: | 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 '' | + | If you are using instead the Julia terminal, you can run the script as '' |
| ===== Further help ===== | ===== Further help ===== | ||
| - | Documentation of JuMP is available from [[https:// | + | Documentation of JuMP is available from [[https:// |
| Happy modelling with JuMP ;-) | Happy modelling with JuMP ;-) | ||
| Line 234: | Line 239: | ||
| Here is the complete script: | Here is the complete script: | ||
| - | < | + | < |
| - | #= | + | # Transport example |
| - | Transposition in JuMP of the basic transport model used in the GAMS tutorial | + | |
| - | + | ||
| - | This problem finds a least cost shipping schedule that meets | + | |
| - | requirements at markets and supplies at factories. | + | |
| - | + | ||
| - | - Original formulation: | + | |
| - | Princeton University Press, Princeton, New Jersey, 1963. | + | |
| - | - Gams implementation: | + | |
| - | Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide. | + | |
| - | The Scientific Press, Redwood City, California, 1988. | + | |
| - | - JuMP implementation: | + | |
| - | =# | + | |
| - | + | ||
| - | using JuMP, DataFrames | + | |
| + | # Transposition in JuMP of the basic transport model used in the GAMS tutorial | ||
| + | # | ||
| + | # This problem finds a least cost shipping schedule that meets | ||
| + | # requirements at markets and supplies at factories. | ||
| + | # | ||
| + | # - Original formulation: | ||
| + | # Princeton University Press, Princeton, New Jersey, 1963. | ||
| + | # - Gams implementation: | ||
| + | # Rosenthal, R E, Chapter 2: A GAMS Tutorial. In GAMS: A User's Guide. | ||
| + | # The Scientific Press, Redwood City, California, 1988. | ||
| + | # - JuMP implementation: | ||
| + | |||
| + | using CSV, DataFrames, GLPK, JuMP | ||
| + | |||
| # Sets | # Sets | ||
| plants | plants | ||
| markets = [" | markets = [" | ||
| + | |||
| # Parameters | # Parameters | ||
| a = Dict( # capacity of plant i in cases | a = Dict( # capacity of plant i in cases | ||
| Line 265: | Line 270: | ||
| " | " | ||
| ) | ) | ||
| + | |||
| # distance in thousands of miles | # distance in thousands of miles | ||
| - | d_table = wsv""" | + | d_table = CSV.read(IOBuffer(""" |
| plants | plants | ||
| seattle | seattle | ||
| san_diego | san_diego | ||
| - | """ | + | """ |
| d = Dict( (r[: | d = Dict( (r[: | ||
| + | |||
| f = 90 # freight in dollars per case per thousand miles | f = 90 # freight in dollars per case per thousand miles | ||
| + | |||
| c = Dict() # transport cost in thousands of dollars per case ; | c = Dict() # transport cost in thousands of dollars per case ; | ||
| [ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | [ c[p,m] = f * d[p,m] / 1000 for p in plants, m in markets] | ||
| + | |||
| # Model declaration | # Model declaration | ||
| - | trmodel = Model() # transport model | + | trmodel = Model(GLPK.Optimizer) # transport model |
| + | |||
| # Variables | # Variables | ||
| @variables trmodel begin | @variables trmodel begin | ||
| x[p in plants, m in markets] >= 0 # shipment quantities in cases | x[p in plants, m in markets] >= 0 # shipment quantities in cases | ||
| end | end | ||
| + | |||
| # Constraints | # Constraints | ||
| @constraints trmodel begin | @constraints trmodel begin | ||
| Line 294: | Line 299: | ||
| sum(x[p,m] for p in plants) | sum(x[p,m] for p in plants) | ||
| end | end | ||
| + | |||
| # Objective | # Objective | ||
| @objective trmodel Min begin | @objective trmodel Min begin | ||
| sum(c[p, | sum(c[p, | ||
| end | end | ||
| + | |||
| print(trmodel) | print(trmodel) | ||
| + | |||
| + | optimize!(trmodel) | ||
| + | status = termination_status(trmodel) | ||
| - | status = solve(trmodel) | + | if status == MOI.OPTIMAL |
| - | + | println(" | |
| - | if status == :Optimal | + | |
| - | println(" | + | |
| println(" | println(" | ||
| - | println(getvalue(x)) | + | println(value.(x)) |
| println(" | println(" | ||
| - | [println(" | + | [println(" |
| println(" | println(" | ||
| - | [println(" | + | [println(" |
| + | |||
| else | else | ||
| println(" | println(" | ||
| println(status) | println(status) | ||
| end | end | ||
| - | + | ||
| # Expected result: | # Expected result: | ||
| # obj= 153.675 | # obj= 153.675 | ||
