Skip to content

Commit 2736f70

Browse files
authored
Add generic routing for objective function attributes (#3142)
* Add generic routing for objective function attributes Custom objective attributes currently require extensions such as DiffOpt to duplicate the bridge optimizer dispatch. Add MOI.Bridges.ObjectiveFunctionAttribute{A,F} to route get/set calls to the objective bridge for F, or unwrap the attribute for a native objective. Like ObjectiveFunctionValue, the wrapper is a routing object rather than an AbstractModelAttribute accepted by native optimizers. This avoids ambiguities with generic model attribute storage. Existing objective value routing is unchanged. This additive API can be released before bridge ownership validation, allowing DiffOpt to migrate first. The later ownership change should establish the bridge context in these centralized methods. Validation: the new standalone test file passes all 10 assertions for native, unbridged, slack, and stacked conversion/slack paths. Temporary line/type traces confirm each routing path executes. The DiffOpt bridge test file also passes against this API. JuliaFormatter 2 applied to the repository. * Route ObjectiveValue through ObjectiveFunctionAttribute Use the generic objective attribute dispatcher for ObjectiveValue and ObjectiveFunctionValue. Adapt the wrapped ObjectiveValue to the existing ObjectiveFunctionValue bridge methods, preserving external objective bridge implementations. Validate multiple result indices through slack and stacked conversion/slack bridges. The new objective attribute test file and existing scalar slack, vector slack, and function conversion test files all pass. Temporary line/type markers confirm both entry points and the compatibility adapter execute, including scalar, quadratic, and vector routes. JuliaFormatter 2 applied to the repository.
1 parent a62e186 commit 2736f70

3 files changed

Lines changed: 239 additions & 7 deletions

File tree

‎src/Bridges/Objective/bridge.jl‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ additionally implement:
2020
"""
2121
abstract type AbstractBridge <: MOI.Bridges.AbstractBridge end
2222

23+
# Preserve the existing extension point for objective bridges while using the
24+
# generic objective attribute routing in the bridge optimizer.
25+
function MOI.get(
26+
model::MOI.ModelLike,
27+
attr::MOI.Bridges.ObjectiveFunctionAttribute{MOI.ObjectiveValue,F},
28+
bridge::AbstractBridge,
29+
) where {F<:MOI.AbstractFunction}
30+
return MOI.get(
31+
model,
32+
MOI.Bridges.ObjectiveFunctionValue{F}(attr.attr.result_index),
33+
bridge,
34+
)
35+
end
36+
2337
"""
2438
supports_objective_function(
2539
BT::Type{<:MOI.Bridges.Objective.AbstractBridge},

‎src/Bridges/bridge_optimizer.jl‎

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,13 +1209,76 @@ function MOI.supports(
12091209
end
12101210
end
12111211

1212+
"""
1213+
ObjectiveFunctionAttribute{A,F}(attr::A)
1214+
1215+
Wrap the model attribute `attr` to associate it with the objective function of
1216+
type `F`. Bridge optimizers forward this attribute to the objective bridge for
1217+
`F`, or forward `attr` to the underlying model if that objective is not bridged.
1218+
1219+
Extensions implement `MOI.get(model, attr, bridge)` and
1220+
`MOI.set(model, attr, bridge, value)` for the wrapped attribute and the objective
1221+
bridges they support. The bridge optimizer handles dispatch to these methods;
1222+
bridge implementations do not need to manage the optimizer's bridge context.
1223+
1224+
The function type tracks the position in a chain of objective bridges. When
1225+
forwarding to another objective bridge, wrap the attribute with the target
1226+
objective function type.
1227+
"""
1228+
struct ObjectiveFunctionAttribute{
1229+
A<:MOI.AbstractModelAttribute,
1230+
F<:MOI.AbstractFunction,
1231+
}
1232+
attr::A
1233+
end
1234+
1235+
function MOI.get(model::MOI.ModelLike, attr::ObjectiveFunctionAttribute)
1236+
return MOI.get(model, attr.attr)
1237+
end
1238+
1239+
function MOI.set(model::MOI.ModelLike, attr::ObjectiveFunctionAttribute, value)
1240+
MOI.set(model, attr.attr, value)
1241+
return
1242+
end
1243+
1244+
function MOI.get(
1245+
b::AbstractBridgeOptimizer,
1246+
attr::ObjectiveFunctionAttribute{A,F},
1247+
) where {A<:MOI.AbstractModelAttribute,F<:MOI.AbstractFunction}
1248+
obj_attr = MOI.ObjectiveFunction{F}()
1249+
if is_bridged(b, obj_attr)
1250+
return MOI.get(recursive_model(b), attr, bridge(b, obj_attr))
1251+
else
1252+
return MOI.get(b.model, attr.attr)
1253+
end
1254+
end
1255+
1256+
function MOI.set(
1257+
b::AbstractBridgeOptimizer,
1258+
attr::ObjectiveFunctionAttribute{A,F},
1259+
value,
1260+
) where {A<:MOI.AbstractModelAttribute,F<:MOI.AbstractFunction}
1261+
obj_attr = MOI.ObjectiveFunction{F}()
1262+
if is_bridged(b, obj_attr)
1263+
MOI.set(recursive_model(b), attr, bridge(b, obj_attr), value)
1264+
else
1265+
MOI.set(b.model, attr.attr, value)
1266+
end
1267+
return
1268+
end
1269+
12121270
"""
12131271
struct ObjectiveFunctionValue{F<:MOI.AbstractFunction} end
12141272
12151273
Attribute for the value of the objective function of type `F`. If the objective
12161274
of the objective function does not depend on `F`, the type `F` determines
12171275
whether the computation is redirected to an objective bridge or to the
12181276
underlying model.
1277+
1278+
Bridge optimizer dispatch uses
1279+
[`ObjectiveFunctionAttribute{MOI.ObjectiveValue,F}`](@ref ObjectiveFunctionAttribute).
1280+
This wrapper is retained for compatibility with existing objective bridge
1281+
implementations.
12191282
"""
12201283
struct ObjectiveFunctionValue{F<:MOI.AbstractFunction}
12211284
result_index::Int
@@ -1231,18 +1294,21 @@ function MOI.get(
12311294
b::AbstractBridgeOptimizer,
12321295
attr::ObjectiveFunctionValue{F},
12331296
) where {F<:MOI.AbstractFunction} # Need `<:` to avoid ambiguity
1234-
obj_attr = MOI.ObjectiveFunction{F}()
1235-
if is_bridged(b, obj_attr)
1236-
return MOI.get(recursive_model(b), attr, bridge(b, obj_attr))
1237-
else
1238-
return MOI.get(b.model, MOI.ObjectiveValue(attr.result_index))
1239-
end
1297+
return MOI.get(
1298+
b,
1299+
ObjectiveFunctionAttribute{MOI.ObjectiveValue,F}(
1300+
MOI.ObjectiveValue(attr.result_index),
1301+
),
1302+
)
12401303
end
12411304

12421305
function MOI.get(b::AbstractBridgeOptimizer, attr::MOI.ObjectiveValue)
12431306
if is_objective_bridged(b)
12441307
F = Objective.function_type(Objective.bridges(b))
1245-
return MOI.get(b, ObjectiveFunctionValue{F}(attr.result_index))
1308+
return MOI.get(
1309+
b,
1310+
ObjectiveFunctionAttribute{MOI.ObjectiveValue,F}(attr),
1311+
)
12461312
else
12471313
return MOI.get(b.model, attr)
12481314
end
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Copyright (c) 2026: jump-dev contributors
2+
#
3+
# Use of this source code is governed by an MIT-style license that can be found
4+
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
5+
6+
module TestObjectiveFunctionAttribute
7+
8+
using Test
9+
import MathOptInterface as MOI
10+
11+
struct ObjectiveData <: MOI.AbstractModelAttribute end
12+
13+
function MOI.get(
14+
model::MOI.ModelLike,
15+
::MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData},
16+
bridge::MOI.Bridges.Objective.SlackBridge,
17+
)
18+
return MOI.get(model, MOI.ConstraintPrimalStart(), bridge.constraint)
19+
end
20+
21+
function MOI.set(
22+
model::MOI.ModelLike,
23+
::MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData},
24+
bridge::MOI.Bridges.Objective.SlackBridge,
25+
value,
26+
)
27+
MOI.set(model, MOI.ConstraintPrimalStart(), bridge.constraint, value)
28+
return
29+
end
30+
31+
function MOI.get(
32+
model::MOI.ModelLike,
33+
attr::MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,G},
34+
::MOI.Bridges.Objective.FunctionConversionBridge{T,F,G},
35+
) where {T,F,G}
36+
return MOI.get(
37+
model,
38+
MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,F}(attr.attr),
39+
)
40+
end
41+
42+
function MOI.set(
43+
model::MOI.ModelLike,
44+
attr::MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,G},
45+
::MOI.Bridges.Objective.FunctionConversionBridge{T,F,G},
46+
value,
47+
) where {T,F,G}
48+
MOI.set(
49+
model,
50+
MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,F}(attr.attr),
51+
value,
52+
)
53+
return
54+
end
55+
56+
function test_unbridged()
57+
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
58+
attr =
59+
MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,MOI.VariableIndex}(
60+
ObjectiveData(),
61+
)
62+
for model in (inner, MOI.Bridges.Objective.Slack{Float64}(inner))
63+
MOI.set(model, attr, 1.23)
64+
@test MOI.get(inner, ObjectiveData()) == 1.23
65+
@test MOI.get(model, attr) == 1.23
66+
end
67+
return
68+
end
69+
70+
function test_bridged()
71+
for stacked in (false, true)
72+
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
73+
slack = MOI.Bridges.Objective.Slack{Float64}(inner)
74+
model = if stacked
75+
MOI.Bridges.Objective.SingleBridgeOptimizer{
76+
MOI.Bridges.Objective.FunctionConversionBridge{
77+
Float64,
78+
MOI.ScalarQuadraticFunction{Float64},
79+
},
80+
}(
81+
slack,
82+
)
83+
else
84+
slack
85+
end
86+
x = MOI.add_variable(model)
87+
f = 2.0 * x + 3.0
88+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
89+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
90+
attr = MOI.Bridges.ObjectiveFunctionAttribute{ObjectiveData,typeof(f)}(
91+
ObjectiveData(),
92+
)
93+
MOI.set(model, attr, 4.56)
94+
@test MOI.get(model, attr) == 4.56
95+
F = MOI.get(slack, MOI.ObjectiveFunctionType())
96+
bridge = MOI.Bridges.bridge(slack, MOI.ObjectiveFunction{F}())
97+
@test MOI.get(inner, MOI.ConstraintPrimalStart(), bridge.constraint) ==
98+
4.56
99+
MOI.set(inner, MOI.ConstraintPrimalStart(), bridge.constraint, 7.89)
100+
@test MOI.get(model, attr) == 7.89
101+
end
102+
return
103+
end
104+
105+
function test_objective_value()
106+
for stacked in (false, true)
107+
inner = MOI.Utilities.MockOptimizer(MOI.Utilities.Model{Float64}())
108+
slack = MOI.Bridges.Objective.Slack{Float64}(inner)
109+
model = if stacked
110+
MOI.Bridges.Objective.SingleBridgeOptimizer{
111+
MOI.Bridges.Objective.FunctionConversionBridge{
112+
Float64,
113+
MOI.ScalarQuadraticFunction{Float64},
114+
},
115+
}(
116+
slack,
117+
)
118+
else
119+
slack
120+
end
121+
x = MOI.add_variable(model)
122+
f = 2.0 * x + 3.0
123+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
124+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
125+
MOI.set(inner, MOI.ResultCount(), 2)
126+
MOI.set(inner, MOI.TerminationStatus(), MOI.OPTIMAL)
127+
variables = MOI.get(inner, MOI.ListOfVariableIndices())
128+
for (i, values) in enumerate(([2.0, 1.0], [4.0, 1.0]))
129+
MOI.set(inner, MOI.PrimalStatus(i), MOI.FEASIBLE_POINT)
130+
MOI.set(inner, MOI.VariablePrimal(i), variables, values)
131+
end
132+
@test MOI.get(model, MOI.ObjectiveValue(1)) ≈ 7.0
133+
@test MOI.get(model, MOI.ObjectiveValue(2)) ≈ 11.0
134+
@test MOI.get(model, MOI.Bridges.ObjectiveFunctionValue{typeof(f)}(2)) ≈
135+
11.0
136+
@test MOI.get(
137+
model,
138+
MOI.Bridges.ObjectiveFunctionAttribute{MOI.ObjectiveValue,typeof(f)}(
139+
MOI.ObjectiveValue(2),
140+
),
141+
) ≈ 11.0
142+
end
143+
return
144+
end
145+
146+
@testset "ObjectiveFunctionAttribute" begin
147+
test_unbridged()
148+
test_bridged()
149+
test_objective_value()
150+
end
151+
152+
end

0 commit comments

Comments
 (0)