Search code examples
optimizationjuliamathematical-optimizationmodelingjulia-jump

AMPL to JuMP (Julia)


I need to transform a AMPL code to JuMP.

param f;

set R := 1..N;
set R_OK := 1..M;
set V := 1..N;

param tMax;
set T := 1..tMax;

var primary{R,V}, binary;
var SendPrepReq{T,R,V}, binary;

"param f" would be an int. The varibles I know how to do. But what about the sets? What is its equivalent in JuMP?


Solution

  • One of the most relevant pieces of documentation may be the Quickstart guide to get the basics of how JuMP works.

    For your example, you can just declare your parameters directly:

    using JuMP
    
    # declare some parameters
    f = 3
    N = 10
    M = 5
    R = 1:N
    V = 1:N
    R_OK = 1:M
    
    Tmax = 33
    T = 1:Tmax
    
    # create the model
    m = Model()
    # add variables
    @variable(m, primary[R,V], Bin)
    @variable(m, SendPrepReq[T,R,V], Bin)
    

    EDIT

    One might want to provide parameters independently from the model declaration as in AMLP. The most straightforward way in Julia will be to build and solve the model in a function taking the problem parameters in argument:

    function build_model(f, N, M, Tmax)
        R = 1:N
        V = 1:N
        R_OK = 1:M
        T = 1:Tmax
    
        # create the model
        m = Model()
        # add variables
        @variable(m, primary[R,V], Bin)
        @variable(m, SendPrepReq[T,R,V], Bin)
    
        return (m, primary, SendPrepReq)
    end