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

[Test] fix nonlinear tests by adding starting point #2585

Merged
merged 3 commits into from
Dec 4, 2024
Merged
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
30 changes: 23 additions & 7 deletions src/Test/test_nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1820,21 +1820,20 @@ function test_nonlinear_with_scalar_quadratic_function_with_off_diag(
@requires _supports(config, MOI.optimize!)
F = MOI.ScalarNonlinearFunction
@requires MOI.supports_constraint(model, F, MOI.EqualTo{T})
for (a, b, status) in [
(1, 2, config.optimal_status),
(1, 3, config.infeasible_status),
(2, 3, config.optimal_status),
(2, 4, config.infeasible_status),
]
test_cases = [(1, 2, true), (1, 3, false), (2, 3, true), (2, 4, false)]
for (a, b, status) in test_cases
MOI.empty!(model)
x, _ = MOI.add_constrained_variable(model, MOI.EqualTo(T(2)))
MOI.set(model, MOI.VariablePrimalStart(), x, T(2))
y, _ = MOI.add_constrained_variable(model, MOI.EqualTo(T(3)))
MOI.set(model, MOI.VariablePrimalStart(), y, T(3))
g = T(a) * x * y
@test g isa MOI.ScalarQuadraticFunction{T}
f = MOI.ScalarNonlinearFunction(:sqrt, Any[g])
MOI.add_constraint(model, f, MOI.GreaterThan(T(b)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == status
term_status = MOI.get(model, MOI.TerminationStatus())
@test (term_status == config.optimal_status) == status
end
return
end
Expand Down Expand Up @@ -1866,15 +1865,20 @@ function test_nonlinear_constraint_log(
x = MOI.add_variable(model)
t = MOI.add_variable(model)
MOI.add_constraint(model, x, MOI.LessThan(T(2)))
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = 1.0 * t
# max t
# x <= 2
# log(x) >= t
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
g = MOI.ScalarNonlinearFunction(
:-,
Any[MOI.ScalarNonlinearFunction(:log, Any[x]), t],
)
c = MOI.add_constraint(model, g, MOI.GreaterThan(T(0)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
x_val = MOI.get(model, MOI.VariablePrimal(), x)
t_val = MOI.get(model, MOI.VariablePrimal(), t)
@test ≈(x_val, T(2), config)
Expand Down Expand Up @@ -1983,7 +1987,9 @@ function test_nonlinear_quadratic_1(
#
# -> x = y = 1/sqrt(2)
x = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
y = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = T(1) * x + T(1) * y
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
Expand All @@ -1993,6 +1999,7 @@ function test_nonlinear_quadratic_1(
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
Expand Down Expand Up @@ -2029,7 +2036,9 @@ function test_nonlinear_quadratic_2(
#
# -> x = y = 1/sqrt(2)
x = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
y = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = T(1) * x + T(1) * y
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
Expand All @@ -2039,6 +2048,7 @@ function test_nonlinear_quadratic_2(
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
Expand Down Expand Up @@ -2075,7 +2085,9 @@ function test_nonlinear_quadratic_3(
#
# -> x = y = 1/sqrt(2)
x = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
y = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = T(1) * x + T(1) * y
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
Expand All @@ -2085,6 +2097,7 @@ function test_nonlinear_quadratic_3(
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
Expand Down Expand Up @@ -2120,7 +2133,9 @@ function test_nonlinear_quadratic_4(
#
# -> x = y = 1/sqrt(2)
x = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
y = MOI.add_variable(model)
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
f = T(1) * x + T(1) * y
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
Expand All @@ -2130,6 +2145,7 @@ function test_nonlinear_quadratic_4(
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
@test ≈(MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
@test ≈(MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
Expand Down
Loading