From 20f09ec774495aee08668abf9e92ee1b18989377 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Wed, 4 Dec 2024 13:02:11 +1300 Subject: [PATCH] [Test] fix nonlinear tests by adding starting point (#2585) --- src/Test/test_nonlinear.jl | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/Test/test_nonlinear.jl b/src/Test/test_nonlinear.jl index 3f62da547e..987d0492d5 100644 --- a/src/Test/test_nonlinear.jl +++ b/src/Test/test_nonlinear.jl @@ -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 @@ -1866,8 +1865,12 @@ 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( :-, @@ -1875,6 +1878,7 @@ function test_nonlinear_constraint_log( ) 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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)