Skip to content

Fix BridgeableConstraint with model_convert#4183

Open
blegat wants to merge 1 commit into
masterfrom
bl/model_convert
Open

Fix BridgeableConstraint with model_convert#4183
blegat wants to merge 1 commit into
masterfrom
bl/model_convert

Conversation

@blegat

@blegat blegat commented Jun 27, 2026

Copy link
Copy Markdown
Member

Tried fixing jump-dev/SumOfSquares.jl#444 with Claude but he got really confused.
The root cause it that the way model_convert works is a bit unclear and inconsistent.
When you call @constraint(model, ...), we first create an AbstractConstraint with build_constraint. There we don't have the model type so we cannot convert yet.
Then, in the macro code itself we call model_convert:

code = if is_vectorized
quote
$parse_code
# These broadcast calls need to be nested so that the operators
# are fused. Some broadcasted errors result if you put them on
# different lines.
add_constraint.(
$model,
model_convert.($model, $build_call),
$name_expr,
)
end
else
quote
$parse_code
build = model_convert($model, $build_call)
add_constraint($model, build, $name_expr)
end
end

Then we pass the converted AbstractConstraint to add_constraint which then also calls model_convert:
con = model_convert(model, con)

It a bit redundant. I checked and both were added in github.com//pull/3385.
The advantages of doing it in add_constraint are that 1) it's easier to maintain method implementations that macro code and 2) we don't need to do a broadcast in case of the vectorization.
In PolyJuMP, we create a constraint outside the macro so we rely on the model_convert in add_constraint.
https://github.com/jump-dev/PolyJuMP.jl/blob/571a9874547ea44efe38822e073a3aecffdb127c/src/constraint.jl#L282-L288
However, we didn't call model_convert there so that's an issue.

So there are a few ways to fix the issue in SumOfSquares. In all of them, we need the fix in model_convert of BridgeableConstraint. The question is, where should model_convert be called.
We should decide who's responsibility it is to call model_convert, is it

  1. the caller of add_constraint or
  2. the implementation of add_constraint.

In case

  1. we should probably remove the model_convert in add_constraint(::AbstractModel, ::AbstractConstraint) (we may consider it breaking but that would only be for users that weren't using the macro so unclear if it's a big deal) and in case
  2. we should add model_convert in add_constraint(::GenericModel, ::BridgeableConstraint) like done in this PR and then also remove the model_convert called from the macro.

@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.93%. Comparing base (44397c7) to head (c4d70a7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4183   +/-   ##
=======================================
  Coverage   99.93%   99.93%           
=======================================
  Files          42       42           
  Lines        6230     6231    +1     
=======================================
+ Hits         6226     6227    +1     
  Misses          4        4           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread test/test_model.jl
# `model_convert` normalizes the bridge coefficient type to the value type of
# the model (`Float64`), even though `BridgeMe` requested `Int`.
@test NonnegativeBridge{Float64} in model.bridge_types
@test !(NonnegativeBridge{Int} in model.bridge_types)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@test !(NonnegativeBridge{Int} in model.bridge_types)
@test !(NonnegativeBridge{Int} in model.bridge_types)
return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants