Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for complementarity constraints #144

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AmplNLReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ const libasl = joinpath(dirname(ASL_jll.libasl_path), "libasl." * dlext)

include("ampl_meta.jl")
include("ampl_model.jl")
include("ampl_mpec_model.jl")

end # Module AmplNLReader
11 changes: 11 additions & 0 deletions src/ampl_meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ The following keyword arguments are accepted:
- `minimize`: true if optimize == minimize
- `nlo`: number of nonlinear objectives
- `islp`: true if the problem is a linear program
- `n_cc`: number of complementarity constraints
- `cvar`: indices of variables appearing in complementarity constraints (0 if constraint is regular)
- `name`: problem name
"""
struct AmplNLPMeta <: AbstractNLPModelMeta{Float64, Vector{Float64}}
Expand Down Expand Up @@ -114,6 +116,8 @@ struct AmplNLPMeta <: AbstractNLPModelMeta{Float64, Vector{Float64}}
minimize::Bool
nlo::Int
islp::Bool
n_cc::Int
cvar::Vector{Int}
name::String

function AmplNLPMeta(
Expand Down Expand Up @@ -148,6 +152,8 @@ struct AmplNLPMeta <: AbstractNLPModelMeta{Float64, Vector{Float64}}
minimize = true,
nlo = 1,
islp = false,
n_cc = 0,
cvar = Int[],
name = "Generic",
)
if (nvar < 1) || (ncon < 0)
Expand All @@ -159,6 +165,9 @@ struct AmplNLPMeta <: AbstractNLPModelMeta{Float64, Vector{Float64}}
@lencheck nnnet nnet
@lencheck nlnet lnet
@rangecheck 1 ncon lin nln nnet lnet
if n_cc > 0
@lencheck ncon cvar
end

ifix = findall(lvar .== uvar)
ilow = findall((lvar .> -Inf) .& (uvar .== Inf))
Expand Down Expand Up @@ -226,6 +235,8 @@ struct AmplNLPMeta <: AbstractNLPModelMeta{Float64, Vector{Float64}}
minimize,
nlo,
islp,
n_cc,
cvar,
name,
)
end
Expand Down
13 changes: 13 additions & 0 deletions src/ampl_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
nlvci = Int(@asl_call(:asl_nlvci, Int32, (Ptr{Nothing},), asl))
nlvoi = Int(@asl_call(:asl_nlvoi, Int32, (Ptr{Nothing},), asl))
nwv = Int(@asl_call(:asl_nwv, Int32, (Ptr{Nothing},), asl))
n_cc = Int(@asl_call(:asl_n_cc, Int32, (Ptr{Nothing},), asl))

Check warning on line 84 in src/ampl_model.jl

View check run for this annotation

Codecov / codecov/patch

src/ampl_model.jl#L84

Added line #L84 was not covered by tests

lcon = unsafe_wrap(
Array,
Expand All @@ -95,6 +96,16 @@
own = false,
)

if n_cc > 0
cvar = unsafe_wrap(Array, @asl_call(:asl_cvar, Ptr{Int32}, (Ptr{Nothing},), asl),(ncon,), own=false)

Check warning on line 100 in src/ampl_model.jl

View check run for this annotation

Codecov / codecov/patch

src/ampl_model.jl#L100

Added line #L100 was not covered by tests
# Check complementarity constraints are well specified:
cc_cons = cvar .> 0
@assert all(isinf, ucon[cc_cons])
@assert all(isfinite, lcon[cc_cons])
else
cvar = Int[]
end

nlnet = Int(@asl_call(:asl_lnc, Int32, (Ptr{Nothing},), asl))
nnnet = Int(@asl_call(:asl_nlnc, Int32, (Ptr{Nothing},), asl))
nnln = Int(@asl_call(:asl_nlc, Int32, (Ptr{Nothing},), asl)) - nnnet
Expand Down Expand Up @@ -148,6 +159,8 @@
nlnet = nlnet,
minimize = minimize,
islp = islp,
n_cc = n_cc,
cvar = cvar,
name = split(fname, ".")[1], # do not include path or extension in model name
)

Expand Down
Loading
Loading