Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Mar 28, 2024
1 parent 3d8ad8d commit 48012e9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
34 changes: 25 additions & 9 deletions src/objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
)
Expand Down Expand Up @@ -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]),
)
Expand Down
24 changes: 18 additions & 6 deletions test/test_objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]),
)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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]),
)
Expand Down

0 comments on commit 48012e9

Please sign in to comment.