Skip to content

Commit 455541d

Browse files
joaquimgJoaquim Garcia
andauthored
Prepare integration with DiffOpt (#161)
* POI + DiffOpt = S2 * fix POI (pp), add reverse mode, add tests * fix format * add more tests * fix test for correct quadratic constraint interpretation * fix test * add format * fix test * format diff tests * add tests * remove GLPK * fix format * concentrate changes * remove diffopt from poi * remove from tests * format * add tests for new attributes * Update Project.toml * Document attributes --------- Co-authored-by: Joaquim Garcia <[email protected]>
1 parent 4e3757e commit 455541d

File tree

4 files changed

+450
-26
lines changed

4 files changed

+450
-26
lines changed

src/MOI_wrapper.jl

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ function MOI.is_empty(model::Optimizer)
9999
#
100100
isempty(model.multiplicative_parameters) &&
101101
isempty(model.dual_value_of_parameters) &&
102-
model.number_of_parameters_in_model == 0
102+
model.number_of_parameters_in_model == 0 &&
103+
isempty(model.ext)
103104
end
104105

105106
function MOI.empty!(model::Optimizer{T}) where {T}
@@ -133,6 +134,7 @@ function MOI.empty!(model::Optimizer{T}) where {T}
133134
empty!(model.dual_value_of_parameters)
134135
#
135136
model.number_of_parameters_in_model = 0
137+
empty!(model.ext)
136138
return
137139
end
138140

@@ -1124,24 +1126,161 @@ end
11241126
# Special Attributes
11251127
#
11261128

1129+
"""
1130+
ParametricObjectiveType <: MOI.AbstractModelAttribute
1131+
1132+
A model attribute for the type `P` of the ParametricOptInterface's parametric
1133+
function type in the objective function. The value os `P` can be `Nothing` if
1134+
the objective function is not parametric. The parametric function type can be
1135+
queried using the [`ParametricObjectiveFunction{P}`](@ref) attribute. The type
1136+
`P` can be `ParametricAffineFunction{T}` or `ParametricQuadraticFunction{T}`.
1137+
"""
1138+
struct ParametricObjectiveType <: MOI.AbstractModelAttribute end
1139+
1140+
function MOI.get(model::Optimizer{T}, ::ParametricObjectiveType) where {T}
1141+
if model.quadratic_objective_cache !== nothing
1142+
return ParametricQuadraticFunction{T}
1143+
elseif model.affine_objective_cache !== nothing
1144+
return ParametricAffineFunction{T}
1145+
end
1146+
return Nothing
1147+
end
1148+
1149+
"""
1150+
ParametricObjectiveFunction{P} <: MOI.AbstractModelAttribute
1151+
1152+
A model attribute for the parametric objective function of type `P`. The type
1153+
`P` can be `ParametricAffineFunction{T}` or `ParametricQuadraticFunction{T}`.
1154+
"""
1155+
struct ParametricObjectiveFunction{T} <: MOI.AbstractModelAttribute end
1156+
1157+
function MOI.get(
1158+
model::Optimizer{T},
1159+
::ParametricObjectiveFunction{ParametricQuadraticFunction{T}},
1160+
) where {T}
1161+
if model.quadratic_objective_cache === nothing
1162+
error("
1163+
There is no parametric quadratic objective function in the model.
1164+
")
1165+
end
1166+
return model.quadratic_objective_cache
1167+
end
1168+
1169+
function MOI.get(
1170+
model::Optimizer{T},
1171+
::ParametricObjectiveFunction{ParametricAffineFunction{T}},
1172+
) where {T}
1173+
if model.affine_objective_cache === nothing
1174+
error("
1175+
There is no parametric affine objective function in the model.
1176+
")
1177+
end
1178+
return model.affine_objective_cache
1179+
end
1180+
1181+
"""
1182+
ListOfParametricConstraintTypesPresent()
1183+
1184+
A model attribute for the list of tuples of the form `(F,S,P)`, where `F` is a
1185+
MOI function type, `S` is a set type and `P` is a ParametricOptInterface
1186+
parametric function type indicating that the attribute
1187+
[`DictOfParametricConstraintIndicesAndFunctions{F,S,P}`](@ref) returns a
1188+
non-empty dictionary.
1189+
"""
1190+
struct ListOfParametricConstraintTypesPresent <: MOI.AbstractModelAttribute end
1191+
1192+
function MOI.get(
1193+
model::Optimizer{T},
1194+
::ListOfParametricConstraintTypesPresent,
1195+
) where {T}
1196+
output = Set{Tuple{DataType,DataType,DataType}}()
1197+
for (F, S) in MOI.Utilities.DoubleDicts.nonempty_outer_keys(
1198+
model.affine_constraint_cache,
1199+
)
1200+
push!(output, (F, S, ParametricAffineFunction{T}))
1201+
end
1202+
for (F, S) in MOI.Utilities.DoubleDicts.nonempty_outer_keys(
1203+
model.vector_affine_constraint_cache,
1204+
)
1205+
push!(output, (F, S, ParametricVectorAffineFunction{T}))
1206+
end
1207+
for (F, S) in MOI.Utilities.DoubleDicts.nonempty_outer_keys(
1208+
model.quadratic_constraint_cache,
1209+
)
1210+
push!(output, (F, S, ParametricQuadraticFunction{T}))
1211+
end
1212+
return collect(output)
1213+
end
1214+
1215+
"""
1216+
DictOfParametricConstraintIndicesAndFunctions{F,S,P}
1217+
1218+
A model attribute for a dictionary mapping constraint indices to parametric
1219+
functions. The key is a constraint index with scalar function type `F`
1220+
and set type `S` and the value is a parametric function of type `P`.
1221+
"""
1222+
struct DictOfParametricConstraintIndicesAndFunctions{F,S,P} <:
1223+
MOI.AbstractModelAttribute end
1224+
1225+
function MOI.get(
1226+
model::Optimizer,
1227+
::DictOfParametricConstraintIndicesAndFunctions{F,S,P},
1228+
) where {F,S,P<:ParametricAffineFunction}
1229+
return model.affine_constraint_cache[F, S]
1230+
end
1231+
1232+
function MOI.get(
1233+
model::Optimizer,
1234+
::DictOfParametricConstraintIndicesAndFunctions{F,S,P},
1235+
) where {F,S,P<:ParametricVectorAffineFunction}
1236+
return model.vector_affine_constraint_cache[F, S]
1237+
end
1238+
1239+
function MOI.get(
1240+
model::Optimizer,
1241+
::DictOfParametricConstraintIndicesAndFunctions{F,S,P},
1242+
) where {F,S,P<:ParametricQuadraticFunction}
1243+
return model.quadratic_constraint_cache[F, S]
1244+
end
1245+
1246+
"""
1247+
NumberOfPureVariables
1248+
1249+
A model attribute for the number of pure variables in the model.
1250+
"""
11271251
struct NumberOfPureVariables <: MOI.AbstractModelAttribute end
11281252

11291253
function MOI.get(model::Optimizer, ::NumberOfPureVariables)
11301254
return length(model.variables)
11311255
end
11321256

1257+
"""
1258+
ListOfPureVariableIndices
1259+
1260+
A model attribute for the list of pure variable indices in the model.
1261+
"""
11331262
struct ListOfPureVariableIndices <: MOI.AbstractModelAttribute end
11341263

11351264
function MOI.get(model::Optimizer, ::ListOfPureVariableIndices)
11361265
return collect(keys(model.variables))::Vector{MOI.VariableIndex}
11371266
end
11381267

1268+
"""
1269+
NumberOfParameters
1270+
1271+
A model attribute for the number of parameters in the model.
1272+
"""
11391273
struct NumberOfParameters <: MOI.AbstractModelAttribute end
11401274

11411275
function MOI.get(model::Optimizer, ::NumberOfParameters)
11421276
return length(model.parameters)
11431277
end
11441278

1279+
"""
1280+
ListOfParameterIndices
1281+
1282+
A model attribute for the list of parameter indices in the model.
1283+
"""
11451284
struct ListOfParameterIndices <: MOI.AbstractModelAttribute end
11461285

11471286
function MOI.get(model::Optimizer, ::ListOfParameterIndices)

src/ParametricOptInterface.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ mutable struct Optimizer{T,OT<:MOI.ModelLike} <: MOI.AbstractOptimizer
166166
number_of_parameters_in_model::Int64
167167
constraints_interpretation::ConstraintsInterpretationCode
168168
save_original_objective_and_constraints::Bool
169+
170+
# extension data
171+
ext::Dict{Symbol,Any}
169172
function Optimizer(
170173
optimizer::OT;
171174
evaluate_duals::Bool = true,
@@ -223,6 +226,7 @@ mutable struct Optimizer{T,OT<:MOI.ModelLike} <: MOI.AbstractOptimizer
223226
0,
224227
ONLY_CONSTRAINTS,
225228
save_original_objective_and_constraints,
229+
Dict{Symbol,Any}(),
226230
)
227231
end
228232
end

0 commit comments

Comments
 (0)