Skip to content

Commit 20f09ec

Browse files
authored
[Test] fix nonlinear tests by adding starting point (#2585)
1 parent 4395bdf commit 20f09ec

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Test/test_nonlinear.jl

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,21 +1820,20 @@ function test_nonlinear_with_scalar_quadratic_function_with_off_diag(
18201820
@requires _supports(config, MOI.optimize!)
18211821
F = MOI.ScalarNonlinearFunction
18221822
@requires MOI.supports_constraint(model, F, MOI.EqualTo{T})
1823-
for (a, b, status) in [
1824-
(1, 2, config.optimal_status),
1825-
(1, 3, config.infeasible_status),
1826-
(2, 3, config.optimal_status),
1827-
(2, 4, config.infeasible_status),
1828-
]
1823+
test_cases = [(1, 2, true), (1, 3, false), (2, 3, true), (2, 4, false)]
1824+
for (a, b, status) in test_cases
18291825
MOI.empty!(model)
18301826
x, _ = MOI.add_constrained_variable(model, MOI.EqualTo(T(2)))
1827+
MOI.set(model, MOI.VariablePrimalStart(), x, T(2))
18311828
y, _ = MOI.add_constrained_variable(model, MOI.EqualTo(T(3)))
1829+
MOI.set(model, MOI.VariablePrimalStart(), y, T(3))
18321830
g = T(a) * x * y
18331831
@test g isa MOI.ScalarQuadraticFunction{T}
18341832
f = MOI.ScalarNonlinearFunction(:sqrt, Any[g])
18351833
MOI.add_constraint(model, f, MOI.GreaterThan(T(b)))
18361834
MOI.optimize!(model)
1837-
@test MOI.get(model, MOI.TerminationStatus()) == status
1835+
term_status = MOI.get(model, MOI.TerminationStatus())
1836+
@test (term_status == config.optimal_status) == status
18381837
end
18391838
return
18401839
end
@@ -1866,15 +1865,20 @@ function test_nonlinear_constraint_log(
18661865
x = MOI.add_variable(model)
18671866
t = MOI.add_variable(model)
18681867
MOI.add_constraint(model, x, MOI.LessThan(T(2)))
1868+
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
18691869
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
18701870
f = 1.0 * t
1871+
# max t
1872+
# x <= 2
1873+
# log(x) >= t
18711874
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
18721875
g = MOI.ScalarNonlinearFunction(
18731876
:-,
18741877
Any[MOI.ScalarNonlinearFunction(:log, Any[x]), t],
18751878
)
18761879
c = MOI.add_constraint(model, g, MOI.GreaterThan(T(0)))
18771880
MOI.optimize!(model)
1881+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
18781882
x_val = MOI.get(model, MOI.VariablePrimal(), x)
18791883
t_val = MOI.get(model, MOI.VariablePrimal(), t)
18801884
@test (x_val, T(2), config)
@@ -1983,7 +1987,9 @@ function test_nonlinear_quadratic_1(
19831987
#
19841988
# -> x = y = 1/sqrt(2)
19851989
x = MOI.add_variable(model)
1990+
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
19861991
y = MOI.add_variable(model)
1992+
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
19871993
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
19881994
f = T(1) * x + T(1) * y
19891995
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@@ -1993,6 +1999,7 @@ function test_nonlinear_quadratic_1(
19931999
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
19942000
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
19952001
MOI.optimize!(model)
2002+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
19962003
@test (MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
19972004
@test (MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
19982005
@test (MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
@@ -2029,7 +2036,9 @@ function test_nonlinear_quadratic_2(
20292036
#
20302037
# -> x = y = 1/sqrt(2)
20312038
x = MOI.add_variable(model)
2039+
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
20322040
y = MOI.add_variable(model)
2041+
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
20332042
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
20342043
f = T(1) * x + T(1) * y
20352044
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@@ -2039,6 +2048,7 @@ function test_nonlinear_quadratic_2(
20392048
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
20402049
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
20412050
MOI.optimize!(model)
2051+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
20422052
@test (MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
20432053
@test (MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
20442054
@test (MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
@@ -2075,7 +2085,9 @@ function test_nonlinear_quadratic_3(
20752085
#
20762086
# -> x = y = 1/sqrt(2)
20772087
x = MOI.add_variable(model)
2088+
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
20782089
y = MOI.add_variable(model)
2090+
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
20792091
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
20802092
f = T(1) * x + T(1) * y
20812093
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@@ -2085,6 +2097,7 @@ function test_nonlinear_quadratic_3(
20852097
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
20862098
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
20872099
MOI.optimize!(model)
2100+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
20882101
@test (MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
20892102
@test (MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
20902103
@test (MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)
@@ -2120,7 +2133,9 @@ function test_nonlinear_quadratic_4(
21202133
#
21212134
# -> x = y = 1/sqrt(2)
21222135
x = MOI.add_variable(model)
2136+
MOI.set(model, MOI.VariablePrimalStart(), x, T(1))
21232137
y = MOI.add_variable(model)
2138+
MOI.set(model, MOI.VariablePrimalStart(), y, T(1))
21242139
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
21252140
f = T(1) * x + T(1) * y
21262141
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@@ -2130,6 +2145,7 @@ function test_nonlinear_quadratic_4(
21302145
g = MOI.ScalarNonlinearFunction(:sqrt, Any[f3])
21312146
c = MOI.add_constraint(model, g, MOI.LessThan(T(1)))
21322147
MOI.optimize!(model)
2148+
@test MOI.get(model, MOI.TerminationStatus()) == config.optimal_status
21332149
@test (MOI.get(model, MOI.ObjectiveValue()), 2 / sqrt(2), config)
21342150
@test (MOI.get(model, MOI.VariablePrimal(), x), 1 / sqrt(2), config)
21352151
@test (MOI.get(model, MOI.VariablePrimal(), y), 1 / sqrt(2), config)

0 commit comments

Comments
 (0)