<commit_before># A rudimentary AMPL interface in Julia.
# <NAME>, April 2014.

include("ampl_utils.jl")

# Convenience macros and libasl specifics.
jampl = "jampl";
macro jampl_call(func, args...)
  quote
    ccall(($func, $jampl), $(args...))
  end
end

# TODO: When / If Julia allows abstract types with fields,
# AmplModel should inherit from NLPModel.
# include("nlp.jl")  # import NLPModel.

type AmplModel

  # A composite type that represents the optimization problem
  #
  #  optimize   obj(x)
  #  subject to lvar ≤    x    ≤ uvar
  #             lcon ≤ cons(x) ≤ ucon
  #
  # where x        is an nvar-dimensional vector,
  #       obj      is the real-valued objective function,
  #       cons     is the vector-valued constraint function,
  #       optimize is either "minimize" or "maximize".
  #
  # Here, lvar, uvar, lcon and ucon are vectors. Some of their
  # components may be infinite to indicate that the corresponding
  # bound or general constraint is not present.

  __asl :: Ptr{Void}         # pointer to internal ASL structure. Do not touch.

  nvar  :: Int               # number of variables
  x0    :: Array{Float64,1}  # initial guess
  lvar  :: Array{Float64,1}  # vector of lower bounds
  uvar  :: Array{Float64,1}  # vector of upper bounds

  ncon  :: Int               # total number of general constraints
  nlin  :: Int               # number of linear constraints
  nnln  :: Int               # number of nonlinear general constraints
  nnet  :: Int               # number of nonlinear network constraints
  y0    :: Array{Float64,1}  # initial Lagrange multipliers
  lcon  :: Array{Float64,1}  # vector of constraint lower bounds
  ucon  :: Array{Float64,1}  # vector of constraint upper bounds

  lin   :: Range1{Int64}     # indices of linear constraints
  nln   :: Range1{Int64}     # indices of nonlinear constraints
  net   :: Range1{Int64}     # indices of nonlinear network constraints

  nnzj  :: Int               # number of nonzeros in the sparse Jacobian
  nnzh  :: Int               # number of nonzeros in the sparse Hessian

  minimize :: Bool           # true if optimize == minimize
  islp  :: Bool              # true if the problem is a linear program
  name  :: ASCIIString       # problem name

  function AmplModel(stub :: ASCIIString)
    asl = @jampl_call(:jampl_init, Ptr{Void}, (Ptr{Uint8},), stub);
    if asl == C_NULL
      msg = "Error allocating ASL structure"
      error(msg)
    end

    minimize = !bool(@jampl_call(:jampl_objtype, Int32, (Ptr{Void},), asl));
    islp = bool(@jampl_call(:jampl_islp, Int32, (Ptr{Void},), asl));

    nvar = int(@jampl_call(:jampl_nvar, Int32, (Ptr{Void},), asl));
    x0   = pointer_to_array(@jampl_call(:jampl_x0,   Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);
    lvar = pointer_to_array(@jampl_call(:jampl_lvar, Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);
    uvar = pointer_to_array(@jampl_call(:jampl_uvar, Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);

    ncon = int(@jampl_call(:jampl_ncon, Int32, (Ptr{Void},), asl));
    nnet = int(@jampl_call(:jampl_nlnc, Int32, (Ptr{Void},), asl));
    nnln = int(@jampl_call(:jampl_nlc,  Int32, (Ptr{Void},), asl)) - nnet;
    nlin = ncon - nnln - nnet
    y0   = pointer_to_array(@jampl_call(:jampl_y0,   Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);
    lcon = pointer_to_array(@jampl_call(:jampl_lcon, Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);
    ucon = pointer_to_array(@jampl_call(:jampl_ucon, Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);

    nln  = 1 : nnln
    net  = nnln+1 : nnln+nnet
    lin  = nnln+nnet+1 : ncon

    nnzj = int(@jampl_call(:jampl_nnzj, Int32, (Ptr{Void},), asl));
    nnzh = int(@jampl_call(:jampl_nnzh, Int32, (Ptr{Void},), asl));

    nlp = new(asl, nvar, x0, lvar, uvar, ncon, nlin, nnln, nnet, y0, lcon, ucon,
              lin, nln, net, nnzj, nnzh, minimize, islp, stub)

    lagscale(nlp, -1.0)  # Lagrangian L(x,y) = f(x) - ∑ yi ci(x)

    finalizer(nlp, amplmodel_finalize)
    return nlp
  end

end

# Methods associated to AmplModel instances.<commit_msg>Add arrays of bounds and constraints categories.<commit_after># A rudimentary AMPL interface in Julia.
# <NAME>, Vancouver, April 2014.

include("ampl_utils.jl")

# Convenience macros and libasl specifics.
jampl = "jampl";
macro jampl_call(func, args...)
  quote
    ccall(($func, $jampl), $(args...))
  end
end

# TODO: When / If Julia allows abstract types with fields,
# AmplModel should inherit from NLPModel.
# include("nlp.jl")  # import NLPModel.

type AmplModel

  # A composite type that represents the optimization problem
  #
  #  optimize   obj(x)
  #  subject to lvar ≤    x    ≤ uvar
  #             lcon ≤ cons(x) ≤ ucon
  #
  # where x        is an nvar-dimensional vector,
  #       obj      is the real-valued objective function,
  #       cons     is the vector-valued constraint function,
  #       optimize is either "minimize" or "maximize".
  #
  # Here, lvar, uvar, lcon and ucon are vectors. Some of their
  # components may be infinite to indicate that the corresponding
  # bound or general constraint is not present.

  __asl :: Ptr{Void}         # pointer to internal ASL structure. Do not touch.

  nvar  :: Int               # number of variables
  ncon  :: Int               # total number of general constraints
  x0    :: Array{Float64,1}  # initial guess
  y0    :: Array{Float64,1}  # initial Lagrange multipliers

  lvar  :: Array{Float64,1}  # vector of lower bounds
  uvar  :: Array{Float64,1}  # vector of upper bounds

  ifix  :: Array{Int64,1}    # indices of fixed variables
  ilow  :: Array{Int64,1}    # indices of variables with lower bound only
  iupp  :: Array{Int64,1}    # indices of variables with upper bound only
  irng  :: Array{Int64,1}    # indices of variables with lower and upper bound (range)
  ifree :: Array{Int64,1}    # indices of free variables
  iinf  :: Array{Int64,1}    # indices of infeasible bounds

  lcon  :: Array{Float64,1}  # vector of constraint lower bounds
  ucon  :: Array{Float64,1}  # vector of constraint upper bounds

  jfix  :: Array{Int64,1}    # indices of equality constraints
  jlow  :: Array{Int64,1}    # indices of constraints of the form c(x) ≥ cl
  jupp  :: Array{Int64,1}    # indices of constraints of the form c(x) ≤ cu
  jrng  :: Array{Int64,1}    # indices of constraints of the form cl ≤ c(x) ≤ cu
  jfree :: Array{Int64,1}    # indices of "free" constraints (there shouldn't be any)
  jinf  :: Array{Int64,1}    # indices of

  nlin  :: Int               # number of linear constraints
  nnln  :: Int               # number of nonlinear general constraints
  nnet  :: Int               # number of nonlinear network constraints

  lin   :: Range1{Int64}     # indices of linear constraints
  nln   :: Range1{Int64}     # indices of nonlinear constraints
  net   :: Range1{Int64}     # indices of nonlinear network constraints

  nnzj  :: Int               # number of nonzeros in the sparse Jacobian
  nnzh  :: Int               # number of nonzeros in the sparse Hessian

  minimize :: Bool           # true if optimize == minimize
  islp  :: Bool              # true if the problem is a linear program
  name  :: ASCIIString       # problem name

  function AmplModel(stub :: ASCIIString)
    asl = @jampl_call(:jampl_init, Ptr{Void}, (Ptr{Uint8},), stub);
    if asl == C_NULL
      msg = "Error allocating ASL structure"
      error(msg)
    end

    minimize = !bool(@jampl_call(:jampl_objtype, Int32, (Ptr{Void},), asl));
    islp = bool(@jampl_call(:jampl_islp, Int32, (Ptr{Void},), asl));

    nvar = int(@jampl_call(:jampl_nvar, Int32, (Ptr{Void},), asl));
    ncon = int(@jampl_call(:jampl_ncon, Int32, (Ptr{Void},), asl));

    x0   = pointer_to_array(@jampl_call(:jampl_x0,   Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);
    y0   = pointer_to_array(@jampl_call(:jampl_y0,   Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);

    lvar = pointer_to_array(@jampl_call(:jampl_lvar, Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);
    uvar = pointer_to_array(@jampl_call(:jampl_uvar, Ptr{Float64}, (Ptr{Void},), asl),
                            (nvar,), false);

    ifix = find(lvar .== uvar);
    ilow = find((lvar .> -Inf) & (uvar .== Inf));
    iupp = find((lvar .== -Inf) & (uvar .< Inf));
    irng = find((lvar .> -Inf) & (uvar .< Inf) & (lvar .< uvar));
    ifree = find((lvar .== -Inf) & (uvar .== Inf));
    iinf = find(lvar .> uvar);

    lcon = pointer_to_array(@jampl_call(:jampl_lcon, Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);
    ucon = pointer_to_array(@jampl_call(:jampl_ucon, Ptr{Float64}, (Ptr{Void},), asl),
                            (ncon,), false);

    jfix = find(lcon .== ucon);
    jlow = find((lcon .> -Inf) & (ucon .== Inf));
    jupp = find((lcon .== -Inf) & (ucon .< Inf));
    jrng = find((lcon .> -Inf) & (ucon .< Inf) & (lcon .< ucon));
    jfree = find((lcon .== -Inf) & (ucon .== Inf));
    jinf = find(lcon .> ucon);

    nnet = int(@jampl_call(:jampl_nlnc, Int32, (Ptr{Void},), asl));
    nnln = int(@jampl_call(:jampl_nlc,  Int32, (Ptr{Void},), asl)) - nnet;
    nlin = ncon - nnln - nnet

    nln  = 1 : nnln
    net  = nnln+1 : nnln+nnet
    lin  = nnln+nnet+1 : ncon

    nnzj = int(@jampl_call(:jampl_nnzj, Int32, (Ptr{Void},), asl));
    nnzh = int(@jampl_call(:jampl_nnzh, Int32, (Ptr{Void},), asl));

    nlp = new(asl, nvar, ncon, x0, y0,
              lvar, uvar, ifix, ilow, iupp, irng, ifree, iinf,
              lcon, ucon, jfix, jlow, jupp, jrng, jfree, jinf,
              nlin, nnln, nnet, lin, nln, net,
              nnzj, nnzh, minimize, islp, stub)

    lagscale(nlp, -1.0)  # Lagrangian L(x,y) = f(x) - ∑ yi ci(x)

    finalizer(nlp, amplmodel_finalize)
    return nlp
  end

end

# Methods associated to AmplModel instances.
{'fraction_non_alphanumeric': 0.1316337664914195, 'fraction_numerical': 0.018053764507489335, 'mean_word_length': 2.6058655221745353, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5717284', 'n_tokens_mistral': 3706, 'n_tokens_neox': 3486, 'n_words': 990}