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

Update JuMP syntax to use the new nonlinear interface (new) #339

Merged
merged 9 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
[compat]
DataFrames = "1"
JLD2 = "0.4"
JuMP = "~0.19, ~0.20, ~0.21, ~0.22, 0.23, 1"
JuMP = "^1.15"
Requires = "1"
SpecialFunctions = "2"
julia = "~1"
Expand Down
24 changes: 15 additions & 9 deletions src/ADNLPProblems/allinit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ function allinit(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) wh
lvar = -T(Inf) * ones(T, 4)
uvar = T(Inf) * ones(T, 4)

function c!(cx, x)
cx[1] = x[2] - 1
cx[2] = x[3]
cx[3] = x[4] - 2
return cx
end
#function c!(cx, x)
# cx[3] = x[4]
# cx[1] = x[2]
# cx[2] = x[3]
# return cx
#end
A = T[
0 0 0 1;
0 1 0 0;
0 0 1 0
]

return ADNLPModels.ADNLPModel!(
f,
x0,
lvar,
uvar,
c!,
T[0, -1e+10, 0],
T[Inf, 1, 0],
findnz(sparse(A))...,
(cx, x) -> cx,
T[2, 1, -1e+10],
T[2, Inf, 1],
name = "allinit",
minimize = true;
kwargs...,
Expand Down
15 changes: 9 additions & 6 deletions src/ADNLPProblems/allinitc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ function allinitc(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) w
uvar = T(Inf) * ones(T, 4)

function c!(cx, x)
cx[1] = x[2] - 1
cx[2] = x[3]
cx[3] = x[4] - 2
cx[4] = x[1]^2 + x[2]^2 - 1
cx[1] = x[1]^2 + x[2]^2
return cx
end
A = T[
0 0 0 1;
0 1 0 0;
0 0 1 0
]

return ADNLPModels.ADNLPModel!(
f,
x0,
lvar,
uvar,
sparse(A),
c!,
T[0, -1e+10, 0, -Inf],
T[Inf, 1, 0, 0],
T[2, 1, -1e+10, -Inf],
T[2, Inf, 1, 1],
name = "allinitc",
minimize = true;
kwargs...,
Expand Down
11 changes: 7 additions & 4 deletions src/ADNLPProblems/alsotame.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ function alsotame(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) w
function c!(cx, x)
x, y = x[1], x[2]
cx[1] = sin(-x + y - 1)
cx[2] = x
cx[3] = y
return cx
end
A = T[
1 0 0;
0 1 0
]

return ADNLPModels.ADNLPModel!(
f,
x0,
lvar,
uvar,
findnz(sparse(A))...,
c!,
T[0, -2, -1.5],
T[0, 2, 1.5],
T[-2, -1.5, 0],
T[2, 1.5, 0],
name = "alsotame",
minimize = true;
kwargs...,
Expand Down
120 changes: 5 additions & 115 deletions src/ADNLPProblems/avion2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,123 +179,13 @@ function avion2(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwa
1,
1,
]
A = sparse([1, 2, 3, 2, 4, 3, 1, 4, 4, 4, 4, 5, 5, 7, 10, 14, 6, 8, 14, 14, 6, 13, 15, 7, 7, 8, 15, 9, 15, 10, 15, 11, 15, 12, 15, 13, 15, 9, 14, 11, 14, 12, 14], [1, 1, 2, 5, 5, 6, 7, 7, 8, 9, 10, 10,
19, 20, 20, 20, 22, 22, 22, 23, 24, 26, 31, 33, 34, 35, 35, 37, 37, 38, 38, 39, 39, 40, 40, 41, 41, 47, 47, 48, 48, 49, 49], T[-0.13, -0.7, -1.0, 1.0, -2.0, 1.0, 1.0, -2.0, -2.0, -1.0, 1.0, -20.0, 1.0, -1.0, -0.043, 0.5, -2.0, -0.137, -1.0, 1.0, 1.0, -300.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -35.0, 660.0, -200.0, 95.0, -120.0, 70.0], 15, 49)
function c!(cx, x)
SR,
LR,
PK,
EF,
SX,
LX,
SD,
SK,
ST,
SF,
LF,
AM,
CA,
CB,
SO,
SS,
IMPDER,
IMPK,
IMPFUS,
QI,
PT,
MV,
MC,
MD,
PD,
NS,
VS,
CR,
PM,
DV,
MZ,
VN,
QV,
QF,
IMPTRAIN,
IMPMOT,
IMPNMOT,
IMPPET,
IMPPIL,
IMPCAN,
IMPSNA,
MS,
EL,
DE,
DS,
IMPVOIL,
NM,
NP,
NG = x[1],
x[2],
x[3],
x[4],
x[5],
x[6],
x[7],
x[8],
x[9],
x[10],
x[11],
x[12],
x[13],
x[14],
x[15],
x[16],
x[17],
x[18],
x[19],
x[20],
x[21],
x[22],
x[23],
x[24],
x[25],
x[26],
x[27],
x[28],
x[29],
x[30],
x[31],
x[32],
x[33],
x[34],
x[35],
x[36],
x[37],
x[38],
x[39],
x[40],
x[41],
x[42],
x[43],
x[44],
x[45],
x[46],
x[47],
x[48],
x[49]
cx[1] = SD - (13 // 100) * SR
cx[2] = SX - (7 // 10) * SR
cx[3] = LX - LR
cx[4] = SF - ST - 2 * SD - 2 * SX - 2 * SK
cx[5] = IMPFUS - 20 * SF
cx[6] = MD - 2 * MV
cx[7] = QF - QI - QV
cx[8] = IMPTRAIN - (137 // 1000) * MV
cx[9] = IMPNMOT - 35 * NM
cx[10] = IMPPET - (43 // 1000) * QI
cx[11] = IMPPIL - 200 * NP
cx[12] = IMPCAN - 120 * NG
cx[13] = IMPSNA - 300 * NS - 400
cx[14] = MC - MV + 95 * NP + 70 * NG + 660 * NM + (1 // 2) * QI - 380
cx[15] = MZ - IMPTRAIN + IMPNMOT + IMPPET + IMPPIL + IMPCAN + IMPSNA + 290
return cx
end
lcon = zeros(T, 15)
ucon = zeros(T, 15)
lcon = vcat(zeros(T, 12), 400, 380, -290)
ucon = vcat(zeros(T, 12), 400, 380, -290)
lvar = T[
10,
0,
Expand Down Expand Up @@ -399,7 +289,7 @@ function avion2(::Val{:nlp}; n::Int = default_nvar, type::Type{T} = Float64, kwa
2,
]

return ADNLPModels.ADNLPModel!(f, x0, lvar, uvar, c!, lcon, ucon, name = "avion2"; kwargs...)
return ADNLPModels.ADNLPModel!(f, x0, lvar, uvar, findnz(A)..., c!, lcon, ucon, name = "avion2"; kwargs...)
end

function avion2(::Val{:nls}; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
Expand Down
11 changes: 7 additions & 4 deletions src/ADNLPProblems/booth.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ function booth(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) wher
end
x0 = zeros(T, 2)

A = T[
1 2;
2 1
]
function c!(cx, x)
cx[1] = x[1] + 2 * x[2] - 7
cx[2] = 2 * x[1] + x[2] - 5
return cx
end
return ADNLPModels.ADNLPModel!(
f,
x0,
findnz(sparse(A))...,
c!,
zeros(T, 2),
zeros(T, 2),
T[7, 5],
T[7, 5],
minimize = true,
name = "booth";
kwargs...,
Expand Down
2 changes: 1 addition & 1 deletion src/ADNLPProblems/bqp1var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function bqp1var(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) wh
end
x0 = T[0.25,]
function c!(cx, x)
cx[1] = x[1]
return cx
end
lcon = T[0.0,]
Expand All @@ -15,6 +14,7 @@ function bqp1var(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) wh
return ADNLPModels.ADNLPModel!(
f,
x0,
findnz(sparse(ones(T, 1, 1)))...,
c!,
lcon,
ucon,
Expand Down
6 changes: 3 additions & 3 deletions src/ADNLPProblems/bt1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ function bt1(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where
end
x0 = T[0.08, 0.06]
function c!(cx, x)
cx[1] = x[1]^2 + x[2]^2 - 1
cx[1] = x[1]^2 + x[2]^2
return cx
end
lcon = zeros(T, 1)
ucon = zeros(T, 1)
lcon = T[1]
ucon = T[1]
return ADNLPModels.ADNLPModel!(f, x0, c!, lcon, ucon, name = "bt1"; kwargs...)
end
38 changes: 25 additions & 13 deletions src/ADNLPProblems/camshape.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ function camshape(args...; n::Int = default_nvar, type::Type{T} = Float64, kwarg
return -Ti(R_v * pi / n) * sum(y[i] for i = 1:n)
end
function c!(cx, y::V; n = n, R_max = R_max, R_min = R_min, θ = eltype(y)(θ)) where {V}
cx[1] = R_max - y[n]
cx[2] = y[1] - R_min
for i = 1:(n - 1)
cx[2 + i] = y[i + 1] - y[i]
end
cx[n + 2] = -R_min * y[1] - y[1] * y[2] + 2 * R_min * y[2] * cos(θ)
cx[n + 3] = -R_min^2 - R_min * y[1] + 2 * R_min * y[1] * cos(θ)
cx[n + 4] = -y[n - 1] * y[n] - y[n] * R_max + 2 * y[n - 1] * R_max * cos(θ)
cx[n + 5] = -2 * R_max * y[n] + 2 * y[n]^2 * cos(θ)
cx[1] = -R_min * y[1] - y[1] * y[2] + 2 * R_min * y[2] * cos(θ) # quadratic
cx[2] = -y[n - 1] * y[n] - y[n] * R_max + 2 * y[n - 1] * R_max * cos(θ) # quadratic
cx[3] = -2 * R_max * y[n] + 2 * y[n]^2 * cos(θ) # quadratic
for i = 2:(n - 1)
cx[n + 4 + i] = -y[i - 1] * y[i] - y[i] * y[i + 1] + 2 * y[i - 1] * y[i + 1] * cos(θ)
cx[2 + i] = -y[i - 1] * y[i] - y[i] * y[i + 1] + 2 * y[i - 1] * y[i + 1] * cos(θ) # quadratic
end
return cx
end

lcon = vcat(-T(Inf), T(-α * θ) * ones(T, n + 1), -T(Inf) * ones(T, n + 1))
ucon = vcat(T(0), T(α * θ) * ones(T, n + 1), zeros(T, n + 1))

A = zeros(T, n + 2, n)
A[2,n] = -1
lcon[2] -= R_max
ucon[2] -= R_max
A[3,1] = 1
lcon[3] += R_min
ucon[3] += R_min
for i = 1:(n - 1)
A[3 + i, i + 1] = 1
A[3 + i, i] = -1
end
# cx[n + 3] = -R_min^2 - R_min * y[1] + 2 * R_min * y[1] * cos(θ)
A[1, 1] = -R_min + 2 * R_min * cos(θ)
lcon[1] += R_min^2
ucon[1] += R_min^2

lvar = T(R_min) * ones(T, n)
uvar = T(R_max) * ones(T, n)
lcon = vcat(T(-α * θ) * ones(T, n + 1), -T(Inf) * ones(T, n + 2))
ucon = vcat(T(α * θ) * ones(T, n + 1), zeros(T, n + 2))

x0 = T((R_min + R_max) / 2) * ones(T, n)
return ADNLPModels.ADNLPModel!(f, x0, lvar, uvar, c!, lcon, ucon, name = "camshape", ; kwargs...)
return ADNLPModels.ADNLPModel!(f, x0, lvar, uvar, findnz(sparse(A))..., c!, lcon, ucon, name = "camshape", ; kwargs...)
end
10 changes: 4 additions & 6 deletions src/ADNLPProblems/catenary.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export catenary

function catenary(args...; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
function catenary(args...; n::Int = default_nvar, type::Type{T} = Float64, Bl = 1, FRACT = 0.6, kwargs...) where {T}
(n % 3 == 0) || @warn("catenary: number of variables adjusted to be a multiple of 3")
n = 3 * max(1, div(n, 3))
(n < 6) || @warn("catenary: number of variables adjusted to be greater or equal to 6")
n = max(n, 6)

## Model Parameters
N = div(n, 3) - 2
Bl = 1
FRACT = 0.6
d = Bl * (N + 1) * FRACT

gamma = 9.81
Expand All @@ -26,7 +24,7 @@ function catenary(args...; n::Int = default_nvar, type::Type{T} = Float64, kwarg
cx[i] =
(x[1 + 3 * i] - x[-2 + 3 * i])^2 +
(x[2 + 3 * i] - x[-1 + 3 * i])^2 +
(x[3 + 3 * i] - x[3 * i])^2 - Bl^2
(x[3 + 3 * i] - x[3 * i])^2
end
return cx
end
Expand All @@ -38,8 +36,8 @@ function catenary(args...; n::Int = default_nvar, type::Type{T} = Float64, kwarg
lvar[n - 2] = T(d)
uvar[n - 2] = T(d)

lcon = zeros(T, N + 1)
ucon = zeros(T, N + 1)
lcon = zeros(T, N + 1) .+ Bl^2
ucon = zeros(T, N + 1) .+ Bl^2
x0 = zeros(T, n)

for i = 0:(N + 1)
Expand Down
4 changes: 2 additions & 2 deletions src/ADNLPProblems/elec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ function elec(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where
# Define the constraints on these points (sum of the square of the coordinates = 1)
function c!(cx, x; n = n)
for k = 1:n
cx[k] = x[k]^2 + x[n + k]^2 + x[2n + k]^2 - 1
cx[k] = x[k]^2 + x[n + k]^2 + x[2n + k]^2
end
return cx
end

# bounds on the constraints
lcon = ucon = zeros(T, n)
lcon = ucon = ones(T, n)

# building a feasible x0
range0 = T[i / n for i = 1:n]
Expand Down
Loading
Loading