From 48012e9ab9514ea39d0c3922e95890961817586c Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Mar 2024 10:48:49 +1300 Subject: [PATCH] Update --- src/objective.jl | 34 +++++++++++++++++++++++++--------- src/variables.jl | 16 +++++++--------- test/test_constraint.jl | 4 ++-- test/test_objective.jl | 24 ++++++++++++++++++------ 4 files changed, 52 insertions(+), 26 deletions(-) diff --git a/src/objective.jl b/src/objective.jl index e4db2c0e490..f75b645a66b 100644 --- a/src/objective.jl +++ b/src/objective.jl @@ -450,7 +450,10 @@ function set_objective_coefficient( coeff::Real, ) where {T} if _nlp_objective_function(model) !== nothing - error("A nonlinear objective is already set in the model") + error( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ) end coeff_t = convert(T, coeff)::T F = objective_function_type(model) @@ -527,9 +530,14 @@ function set_objective_coefficient( coeffs::AbstractVector{<:Real}, ) where {T} if _nlp_objective_function(model) !== nothing - error("A nonlinear objective is already set in the model") - elseif length(variables) != length(coeffs) - msg = "The number of variables and coefficients must match" + error( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ) + end + n, m = length(variables), length(coeffs) + if !(n == m) + msg = "The number of variables ($n) and coefficients ($m) must match" throw(DimensionMismatch(msg)) end F = objective_function_type(model) @@ -605,7 +613,10 @@ function set_objective_coefficient( coeff::Real, ) where {T} if _nlp_objective_function(model) !== nothing - error("A nonlinear objective is already set in the model") + error( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ) end coeff_t = convert(T, coeff)::T F = moi_function_type(objective_function_type(model)) @@ -685,12 +696,17 @@ function set_objective_coefficient( coeffs::AbstractVector{<:Real}, ) where {T} if _nlp_objective_function(model) !== nothing - error("A nonlinear objective is already set in the model") - elseif !(length(variables_1) == length(variables_2) == length(coeffs)) - msg = "The number of variables and coefficients must match" + error( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ) + end + n1, n2, m = length(variables_1), length(variables_2), length(coeffs) + if !(n1 == n2 == m) + msg = "The number of variables ($n1, $n2) and coefficients ($m) must match" throw(DimensionMismatch(msg)) end - coeffs_t = convert.(T, coeffs)::AbstractVector{<:T} + coeffs_t = convert.(T, coeffs) F = moi_function_type(objective_function_type(model)) _set_objective_coefficient(model, variables_1, variables_2, coeffs_t, F) model.is_model_dirty = true diff --git a/src/variables.jl b/src/variables.jl index d8ec818cb0c..daa7003ff89 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -2607,8 +2607,9 @@ function set_normalized_coefficient( variables::AbstractVector{<:AbstractVariableRef}, coeffs::AbstractVector{<:Number}, ) where {T,F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}}} - if !(length(constraints) == length(variables) == length(coeffs)) - msg = "The number of constraints, variables and coefficients must match" + c, n, m = length(constraints), length(variables), length(coeffs) + if !(c == n == m) + msg = "The number of constraints ($c), variables ($n) and coefficients ($m) must match" throw(DimensionMismatch(msg)) end model = owner_model(first(constraints)) @@ -2764,13 +2765,10 @@ function set_normalized_coefficient( variables_2::AbstractVector{<:AbstractVariableRef}, coeffs::AbstractVector{<:Number}, ) where {T,F<:MOI.ScalarQuadraticFunction{T}} - dimension_match = - length(constraints) == - length(variables_1) == - length(variables_2) == - length(coeffs) - if !dimension_match - msg = "The number of constraints, variables and coefficients must match" + c, m = length(constraints), length(coeffs) + n1, n2 = length(variables_1), length(variables_1) + if !(c == n1 == n2 == m) + msg = "The number of constraints ($c), variables ($n1, $n2) and coefficients ($m) must match" throw(DimensionMismatch(msg)) end new_coeffs = convert.(T, coeffs) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index 52f7ad31874..08c89342fbc 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -1001,7 +1001,7 @@ function test_change_coefficient_batch() @test isequal_canonical(constraint_object(quad_con).func, x^2 + 2x + 7y) @test_throws( DimensionMismatch( - "The number of constraints, variables and coefficients must match", + "The number of constraints (1), variables (2) and coefficients (2) must match", ), set_normalized_coefficient([con_ref], [x, y], [4, 5]), ) @@ -1868,7 +1868,7 @@ function test_set_normalized_coefficient_quadratic_batch() @test normalized_coefficient(con, x[1], x[2]) == 5.0 @test_throws( DimensionMismatch( - "The number of constraints, variables and coefficients must match", + "The number of constraints (1), variables (2, 2) and coefficients (2) must match", ), set_normalized_coefficient([con], [x[1], x[1]], [x[1], x[2]], [4, 5]), ) diff --git a/test/test_objective.jl b/test/test_objective.jl index bbd8500e9a8..553b0865243 100644 --- a/test/test_objective.jl +++ b/test/test_objective.jl @@ -55,7 +55,10 @@ function test_objective_coef_update_linear_objective_error() @variable(model, x[1:2]) @NLobjective(model, Min, x[1] * x[2]) @test_throws( - ErrorException("A nonlinear objective is already set in the model"), + ErrorException( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ), set_objective_coefficient(model, x[1], 2), ) return @@ -66,7 +69,10 @@ function test_objective_coef_update_linear_objective_batch_error() @variable(model, x[1:2]) @NLobjective(model, Min, x[1] * x[2]) @test_throws( - ErrorException("A nonlinear objective is already set in the model"), + ErrorException( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ), set_objective_coefficient(model, [x[1], x[2]], [2, 3]), ) return @@ -78,7 +84,7 @@ function test_objective_coef_update_linear_objective_batch_dimension_error() @objective(model, Min, x[1] * x[2]) @test_throws( DimensionMismatch( - "The number of variables and coefficients must match", + "The number of variables (2) and coefficients (1) must match", ), set_objective_coefficient(model, [x[1], x[2]], [2]), ) @@ -321,7 +327,10 @@ function test_set_objective_coefficient_quadratic_error() @variable(model, x[1:2]) @NLobjective(model, Min, x[1] * x[2]) @test_throws( - ErrorException("A nonlinear objective is already set in the model"), + ErrorException( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ), set_objective_coefficient(model, x[1], x[1], 2), ) return @@ -332,7 +341,10 @@ function test_set_objective_coefficient_quadratic_batch_error() @variable(model, x[1:2]) @NLobjective(model, Min, x[1] * x[2]) @test_throws( - ErrorException("A nonlinear objective is already set in the model"), + ErrorException( + "A nonlinear objective created by the legacy `@NLobjective` is " * + "set in the model. This does not support modification.", + ), set_objective_coefficient(model, [x[1]], [x[1]], [2]), ) return @@ -344,7 +356,7 @@ function test_set_objective_coefficient_quadratic_batch_dimension_error() @objective(model, Min, x[1] * x[2]) @test_throws( DimensionMismatch( - "The number of variables and coefficients must match", + "The number of variables (1, 1) and coefficients (2) must match", ), set_objective_coefficient(model, [x[1]], [x[1]], [2, 3]), )