Skip to content
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 docs/src/manual/standard_form.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The vector-valued set types implemented in MathOptInterface.jl are:
| [`RelativeEntropyCone(d)`](@ref MathOptInterface.RelativeEntropyCone) | ``\{ (u, v, w) \in \mathbb{R}^{d} : u \ge \sum_i w_i \log (\frac{w_i}{v_i}), v_i \ge 0, w_i \ge 0 \}`` |
| [`HyperRectangle(l, u)`](@ref MathOptInterface.HyperRectangle) | ``\{x \in \bar{\mathbb{R}}^d: x_i \in [l_i, u_i] \forall i=1,\ldots,d\}`` |
| [`NormCone(p, d)`](@ref MathOptInterface.NormCone) | ``\{ (t,x) \in \mathbb{R}^{d} : t \ge \left(\sum\limits_i \lvert x_i \rvert^p\right)^{\frac{1}{p}} \}`` |
| [`VectorNonlinearOracle`](@ref MathOptInterface.VectorNonlinearOracle)| ``\{x \in \mathbb{R}^{dimension}: l \le f(x) \le u \}`` |

## Matrix cones

Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/standard_form.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ ACTIVATE_ON_ONE
Complements
HyperRectangle
Scaled
VectorNonlinearOracle
```

## Constraint programming sets
Expand Down
26 changes: 26 additions & 0 deletions src/Test/test_basic_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,31 @@ function _set(::Type{T}, ::Type{MOI.HyperRectangle}) where {T}
return MOI.HyperRectangle(zeros(T, 3), ones(T, 3))
end

function _set(::Type{T}, ::Type{MOI.VectorNonlinearOracle}) where {T}
set = MOI.VectorNonlinearOracle(;
dimension = 3,
l = T[0, 0],
u = T[1, 0],
eval_f = (ret, x) -> begin
ret[1] = x[2]^2
ret[2] = x[3]^2 + x[4]^3 - x[1]
return
end,
jacobian_structure = [(1, 2), (2, 1), (2, 3), (2, 4)],
eval_jacobian = (ret, x) -> begin
ret[1] = T(2) * x[2]
ret[2] = -T(1)
ret[3] = T(2) * x[3]
ret[4] = T(3) * x[4]^2
return
end,
)
x, ret_f, ret_J = T[1, 2, 3, 4, 5], T[0, 0], T[0, 0, 0, 0]
set.eval_f(ret_f, x)
set.eval_jacobian(ret_J, x)
return set
end

function _test_function_modification(
model::MOI.ModelLike,
config::Config{T},
Expand Down Expand Up @@ -392,6 +417,7 @@ for s in [
:Table,
:Path,
:HyperRectangle,
:VectorNonlinearOracle,
]
S = getfield(MOI, s)
functions = if S <: MOI.AbstractScalarSet
Expand Down
159 changes: 159 additions & 0 deletions src/Test/test_nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2223,3 +2223,162 @@ function setup_test(
end

version_added(::typeof(test_nonlinear_quadratic_4)) = v"1.35.0"

function test_vector_nonlinear_oracle(
model::MOI.ModelLike,
config::Config{T},
) where {T}
@requires _supports(config, MOI.optimize!)
@requires MOI.supports_constraint(
model,
MOI.VectorOfVariables,
MOI.VectorNonlinearOracle{T},
)
set = MOI.VectorNonlinearOracle(;
dimension = 5,
l = T[0, 0],
u = T[0, 0],
eval_f = (ret, x) -> begin
@test length(ret) == 2
@test length(x) == 5
ret[1] = x[1]^2 - x[4]
ret[2] = x[2]^2 + x[3]^3 - x[5]
return
end,
jacobian_structure = [(1, 1), (2, 2), (2, 3), (1, 4), (2, 5)],
eval_jacobian = (ret, x) -> begin
@test length(ret) == 5
@test length(x) == 5
ret[1] = T(2) * x[1]
ret[2] = T(2) * x[2]
ret[3] = T(3) * x[3]^2
ret[4] = -T(1)
ret[5] = -T(1)
return
end,
hessian_lagrangian_structure = [(1, 1), (2, 2), (3, 3)],
eval_hessian_lagrangian = (ret, x, u) -> begin
@test length(ret) == 3
@test length(x) == 5
@test length(u) == 2
ret[1] = T(2) * u[1]
ret[2] = T(2) * u[2]
ret[3] = T(6) * x[3] * u[2]
return
end,
)
@test MOI.dimension(set) == 5
x = T[1, 2, 3, 4, 5]
ret = T[0, 0]
set.eval_f(ret, x)
@test ret == T[-3, 26]
ret = T[0, 0, 0, 0, 0]
set.eval_jacobian(ret, x)
@test ret == T[2, 4, 27, -1, -1]
ret = T[0, 0, 0]
set.eval_hessian_lagrangian(ret, x, T[2, 3])
@test ret == T[4, 6, 54]
x, y = MOI.add_variables(model, 3), MOI.add_variables(model, 2)
MOI.add_constraints.(model, x, MOI.EqualTo.(T(1):T(3)))
c = MOI.add_constraint(model, MOI.VectorOfVariables([x; y]), set)
MOI.optimize!(model)
x_v = MOI.get.(model, MOI.VariablePrimal(), x)
y_v = MOI.get.(model, MOI.VariablePrimal(), y)
@test ≈(y_v, [x_v[1]^2, x_v[2]^2 + x_v[3]^3], config)
@test ≈(MOI.get(model, MOI.ConstraintPrimal(), c), [x_v; y_v], config)
@test ≈(MOI.get(model, MOI.ConstraintDual(), c), zeros(T, 5), config)
return
end

function setup_test(
::typeof(test_vector_nonlinear_oracle),
model::MOIU.MockOptimizer,
config::Config{T},
) where {T}
MOI.Utilities.set_mock_optimize!(
model,
mock -> MOI.Utilities.mock_optimize!(
mock,
config.optimal_status,
T[1, 2, 3, 1, 31],
(MOI.VectorOfVariables, MOI.VectorNonlinearOracle{T}) =>
[zeros(T, 5)],
),
)
model.eval_variable_constraint_dual = false
return () -> model.eval_variable_constraint_dual = true
end

version_added(::typeof(test_vector_nonlinear_oracle)) = v"1.46.0"

function test_vector_nonlinear_oracle_no_hessian(
model::MOI.ModelLike,
config::Config{T},
) where {T}
@requires _supports(config, MOI.optimize!)
@requires MOI.supports_constraint(
model,
MOI.VectorOfVariables,
MOI.VectorNonlinearOracle{T},
)
set = MOI.VectorNonlinearOracle(;
dimension = 5,
l = T[0, 0],
u = T[0, 0],
eval_f = (ret, x) -> begin
ret[1] = x[1]^2 - x[4]
ret[2] = x[2]^2 + x[3]^3 - x[5]
return
end,
jacobian_structure = [(1, 1), (2, 2), (2, 3), (1, 4), (2, 5)],
eval_jacobian = (ret, x) -> begin
ret[1] = T(2) * x[1]
ret[2] = T(2) * x[2]
ret[3] = T(3) * x[3]^2
ret[4] = -T(1)
ret[5] = -T(1)
return
end,
)
@test MOI.dimension(set) == 5
x = T[1, 2, 3, 4, 5]
ret = T[0, 0]
set.eval_f(ret, x)
@test ret == T[-3, 26]
ret = T[0, 0, 0, 0, 0]
set.eval_jacobian(ret, x)
@test ret == T[2, 4, 27, -1, -1]
@test isempty(set.hessian_lagrangian_structure)
@test set.eval_hessian_lagrangian === nothing
x, y = MOI.add_variables(model, 3), MOI.add_variables(model, 2)
MOI.add_constraints.(model, x, MOI.EqualTo.(T(1):T(3)))
c = MOI.add_constraint(model, MOI.VectorOfVariables([x; y]), set)
MOI.optimize!(model)
x_v = MOI.get.(model, MOI.VariablePrimal(), x)
y_v = MOI.get.(model, MOI.VariablePrimal(), y)
@test ≈(y_v, [x_v[1]^2, x_v[2]^2 + x_v[3]^3], config)
@test ≈(MOI.get(model, MOI.ConstraintPrimal(), c), [x_v; y_v], config)
@test ≈(MOI.get(model, MOI.ConstraintDual(), c), zeros(T, 5), config)
return
end

function setup_test(
::typeof(test_vector_nonlinear_oracle_no_hessian),
model::MOIU.MockOptimizer,
config::Config{T},
) where {T}
MOI.Utilities.set_mock_optimize!(
model,
mock -> MOI.Utilities.mock_optimize!(
mock,
config.optimal_status,
T[1, 2, 3, 1, 31],
(MOI.VectorOfVariables, MOI.VectorNonlinearOracle{T}) =>
[zeros(T, 5)],
),
)
model.eval_variable_constraint_dual = false
return () -> model.eval_variable_constraint_dual = true
end

version_added(::typeof(test_vector_nonlinear_oracle_no_hessian)) = v"1.46.0"
1 change: 1 addition & 0 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ const EqualToIndicatorZero{T} =
MOI.Table,
MOI.BinPacking,
MOI.HyperRectangle,
MOI.VectorNonlinearOracle,
),
(MOI.ScalarNonlinearFunction,),
(MOI.ScalarAffineFunction, MOI.ScalarQuadraticFunction),
Expand Down
Loading
Loading