Skip to content

Commit 189b8fa

Browse files
authored
Cleanup terms getters (#229)
* Fix names of cubic terms * cleanup term access
1 parent 00f7f4f commit 189b8fa

File tree

4 files changed

+58
-46
lines changed

4 files changed

+58
-46
lines changed

src/MOI_wrapper.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function _cache_multiplicative_params!(
7171
model::Optimizer{T},
7272
f::ParametricQuadraticFunction{T},
7373
) where {T}
74-
for term in f.pv
74+
for term in quadratic_parameter_variable_terms(f)
7575
push!(model.multiplicative_parameters_pv, term.variable_1.value)
7676
end
7777
return
@@ -81,7 +81,7 @@ function _cache_multiplicative_params!(
8181
model::Optimizer{T},
8282
f::ParametricVectorQuadraticFunction{T},
8383
) where {T}
84-
for term in f.pv
84+
for term in vector_quadratic_parameter_variable_terms(f)
8585
push!(
8686
model.multiplicative_parameters_pv,
8787
term.scalar_term.variable_1.value,
@@ -94,13 +94,13 @@ function _cache_multiplicative_params!(
9494
model::Optimizer{T},
9595
f::ParametricCubicFunction{T},
9696
) where {T}
97-
for term in f.pv
97+
for term in cubic_parameter_variable_terms(f)
9898
push!(model.multiplicative_parameters_pv, term.variable_1.value)
9999
end
100-
for term in f.pvv
100+
for term in cubic_parameter_variable_variable_terms(f)
101101
push!(model.multiplicative_parameters_pv, term.index_1.value)
102102
end
103-
for term in f.ppv
103+
for term in cubic_parameter_parameter_variable_terms(f)
104104
push!(model.multiplicative_parameters_pv, term.index_1.value)
105105
push!(model.multiplicative_parameters_pv, term.index_2.value)
106106
end
@@ -868,7 +868,8 @@ function _add_constraint_with_parameters_on_function(
868868
) where {T,S}
869869
pf = ParametricAffineFunction(f)
870870
if model.constraints_interpretation == ONLY_BOUNDS
871-
if length(pf.v) == 1 && isone(MOI.coefficient(pf.v[]))
871+
if length(affine_variable_terms(pf)) == 1 &&
872+
isone(MOI.coefficient(affine_variable_terms(pf)[]))
872873
poi_ci = _add_vi_constraint(model, pf, set)
873874
else
874875
error(
@@ -878,7 +879,8 @@ function _add_constraint_with_parameters_on_function(
878879
elseif model.constraints_interpretation == ONLY_CONSTRAINTS
879880
poi_ci = MOI.add_constraint(model, pf, set)
880881
elseif model.constraints_interpretation == BOUNDS_AND_CONSTRAINTS
881-
if length(pf.v) == 1 && isone(MOI.coefficient(pf.v[]))
882+
if length(affine_variable_terms(pf)) == 1 &&
883+
isone(MOI.coefficient(affine_variable_terms(pf)[]))
882884
poi_ci = _add_vi_constraint(model, pf, set)
883885
else
884886
poi_ci = MOI.add_constraint(model, pf, set)
@@ -896,7 +898,7 @@ function MOI.add_constraint(
896898
_update_cache!(pf, model)
897899
inner_ci = MOI.add_constraint(
898900
model.optimizer,
899-
MOI.ScalarAffineFunction{T}(pf.v, 0.0),
901+
MOI.ScalarAffineFunction{T}(affine_variable_terms(pf), 0.0),
900902
_set_with_new_constant(set, pf.current_constant),
901903
)
902904
model.last_affine_added += 1
@@ -919,7 +921,7 @@ function _add_vi_constraint(
919921
_update_cache!(pf, model)
920922
inner_ci = MOI.add_constraint(
921923
model.optimizer,
922-
pf.v[].variable,
924+
affine_variable_terms(pf)[].variable,
923925
_set_with_new_constant(set, pf.current_constant),
924926
)
925927
model.last_affine_added += 1
@@ -1920,7 +1922,7 @@ function MOI.compute_conflict!(model::Optimizer)
19201922
) == MOI.NOT_IN_CONFLICT
19211923
continue
19221924
end
1923-
for term in pf.p
1925+
for term in affine_parameter_terms(pf)
19241926
push!(model.parameters_in_conflict, term.variable)
19251927
end
19261928
end
@@ -1936,14 +1938,14 @@ function MOI.compute_conflict!(model::Optimizer)
19361938
) == MOI.NOT_IN_CONFLICT
19371939
continue
19381940
end
1939-
for term in pf.p
1941+
for term in affine_parameter_terms(pf)
19401942
push!(model.parameters_in_conflict, term.variable)
19411943
end
1942-
for term in pf.pp
1944+
for term in quadratic_parameter_parameter_terms(pf)
19431945
push!(model.parameters_in_conflict, term.variable_1)
19441946
push!(model.parameters_in_conflict, term.variable_2)
19451947
end
1946-
for term in pf.pv
1948+
for term in quadratic_parameter_variable_terms(pf)
19471949
push!(model.parameters_in_conflict, term.variable_1)
19481950
end
19491951
end
@@ -1959,7 +1961,7 @@ function MOI.compute_conflict!(model::Optimizer)
19591961
) == MOI.NOT_IN_CONFLICT
19601962
continue
19611963
end
1962-
for term in pf.p
1964+
for term in vector_affine_parameter_terms(pf)
19631965
push!(
19641966
model.parameters_in_conflict,
19651967
term.scalar_term.variable,

src/duals.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function _compute_parameters_in_ci!(
7171
ci::MOI.ConstraintIndex{F,S},
7272
) where {F,S,T}
7373
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
74-
for term in pf.p
74+
for term in affine_parameter_terms(pf)
7575
model.dual_value_of_parameters[p_val(term.variable)] -=
7676
cons_dual * term.coefficient
7777
end
@@ -84,11 +84,11 @@ function _compute_parameters_in_ci!(
8484
ci::MOI.ConstraintIndex{F,S},
8585
) where {F,S,T}
8686
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
87-
for term in pf.p
87+
for term in affine_parameter_terms(pf)
8888
model.dual_value_of_parameters[p_val(term.variable)] -=
8989
cons_dual * term.coefficient
9090
end
91-
for term in pf.pp
91+
for term in quadratic_parameter_parameter_terms(pf)
9292
mult = cons_dual * term.coefficient
9393
if term.variable_1 == term.variable_2
9494
mult /= 2
@@ -107,7 +107,7 @@ function _compute_parameters_in_ci!(
107107
ci::MOI.ConstraintIndex{F,S},
108108
) where {F<:MOI.VectorAffineFunction{T},S} where {T}
109109
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
110-
for term in pf.p
110+
for term in vector_affine_parameter_terms(pf)
111111
model.dual_value_of_parameters[p_val(term.scalar_term.variable)] -=
112112
cons_dual[term.output_index] * term.scalar_term.coefficient
113113
end
@@ -116,7 +116,7 @@ end
116116

117117
function _update_duals_from_objective!(model::Optimizer{T}, pf) where {T}
118118
is_min = MOI.get(model.optimizer, MOI.ObjectiveSense()) == MOI.MIN_SENSE
119-
for param in pf.p
119+
for param in affine_parameter_terms(pf)
120120
model.dual_value_of_parameters[p_val(param.variable)] +=
121121
ifelse(is_min, 1, -1) * param.coefficient
122122
end
@@ -130,12 +130,12 @@ function _update_duals_from_objective!(
130130
is_min = MOI.get(model.optimizer, MOI.ObjectiveSense()) == MOI.MIN_SENSE
131131
sign = ifelse(is_min, one(T), -one(T))
132132
# p terms: ∂(c·p_i)/∂p_i = c
133-
for term in pf.p
133+
for term in affine_parameter_terms(pf)
134134
model.dual_value_of_parameters[p_val(term.variable)] +=
135135
sign * term.coefficient
136136
end
137137
# pp terms: ∂(c·p_i·p_j)/∂p_i = c·p_j
138-
for term in pf.pp
138+
for term in quadratic_parameter_parameter_terms(pf)
139139
mult = sign * term.coefficient
140140
if term.variable_1 == term.variable_2
141141
mult /= 2
@@ -155,12 +155,12 @@ function _update_duals_from_objective!(
155155
is_min = MOI.get(model.optimizer, MOI.ObjectiveSense()) == MOI.MIN_SENSE
156156
sign = ifelse(is_min, one(T), -one(T))
157157
# p terms: ∂(c·p_i)/∂p_i = c
158-
for term in pf.p
158+
for term in cubic_affine_parameter_terms(pf)
159159
model.dual_value_of_parameters[p_val(term.variable)] +=
160160
sign * term.coefficient
161161
end
162162
# pp terms: ∂(c·p_i·p_j)/∂p_i = c·p_j (diagonal: c/2·2·p_i = c·p_i)
163-
for term in pf.pp
163+
for term in cubic_parameter_parameter_terms(pf)
164164
mult = sign * term.coefficient
165165
if term.variable_1 == term.variable_2
166166
mult /= 2
@@ -171,7 +171,7 @@ function _update_duals_from_objective!(
171171
mult * model.parameters[p_idx(term.variable_1)]
172172
end
173173
# ppp terms: ∂(c·p_i·p_j·p_k)/∂p_i = c·p_j·p_k
174-
for term in pf.ppp
174+
for term in cubic_parameter_parameter_parameter_terms(pf)
175175
coef = sign * term.coefficient
176176
p1_val = model.parameters[p_idx(term.index_1)]
177177
p2_val = model.parameters[p_idx(term.index_2)]
@@ -225,11 +225,11 @@ function _compute_parameters_in_ci!(
225225
ci::MOI.ConstraintIndex{F,S},
226226
) where {F,S,T}
227227
cons_dual = MOI.get(model.optimizer, MOI.ConstraintDual(), ci)
228-
for term in pf.p
228+
for term in vector_affine_parameter_terms(pf)
229229
model.dual_value_of_parameters[p_val(term.scalar_term.variable)] -=
230230
cons_dual[term.output_index] * term.scalar_term.coefficient
231231
end
232-
for t in pf.pp
232+
for t in vector_quadratic_parameter_parameter_terms(pf)
233233
mult = cons_dual[t.output_index] * t.scalar_term.coefficient
234234
if t.scalar_term.variable_1 == t.scalar_term.variable_2
235235
mult /= 2

src/parametric_cubic_function.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ function ParametricCubicFunction(parsed::_ParsedCubicExpression{T}) where {T}
129129
end
130130

131131
# Accessors for cubic terms by type (direct field access)
132-
_cubic_pvv_terms(f::ParametricCubicFunction) = f.pvv
133-
_cubic_ppv_terms(f::ParametricCubicFunction) = f.ppv
134-
_cubic_ppp_terms(f::ParametricCubicFunction) = f.ppp
132+
cubic_affine_parameter_terms(f::ParametricCubicFunction) = f.p
133+
cubic_parameter_variable_terms(f::ParametricCubicFunction) = f.pv
134+
cubic_parameter_parameter_terms(f::ParametricCubicFunction) = f.pp
135+
cubic_parameter_variable_variable_terms(f::ParametricCubicFunction) = f.pvv
136+
cubic_parameter_parameter_variable_terms(f::ParametricCubicFunction) = f.ppv
137+
cubic_parameter_parameter_parameter_terms(f::ParametricCubicFunction) = f.ppp
135138

136139
"""
137140
_effective_param_value(model, pi::ParameterIndex)
@@ -156,15 +159,15 @@ function _parametric_constant(model, f::ParametricCubicFunction{T}) where {T}
156159
constant = f.c
157160

158161
# From affine parameter terms (p)
159-
for term in f.p
162+
for term in cubic_affine_parameter_terms(f)
160163
constant +=
161164
term.coefficient *
162165
_effective_param_value(model, p_idx(term.variable))
163166
end
164167

165168
# From quadratic parameter-parameter terms (pp)
166169
# MOI convention: diagonal C means C/2*p^2, off-diagonal C means C*p1*p2
167-
for term in f.pp
170+
for term in cubic_parameter_parameter_terms(f)
168171
divisor = term.variable_1 == term.variable_2 ? 2 : 1
169172
constant +=
170173
(term.coefficient / divisor) *
@@ -173,7 +176,7 @@ function _parametric_constant(model, f::ParametricCubicFunction{T}) where {T}
173176
end
174177

175178
# From cubic ppp terms (all 3 indices are parameters)
176-
for term in _cubic_ppp_terms(f)
179+
for term in cubic_parameter_parameter_parameter_terms(f)
177180
p1 = term.index_1
178181
p2 = term.index_2
179182
p3 = term.index_3
@@ -202,15 +205,15 @@ function _parametric_affine_terms(
202205

203206
# Add contributions from pv terms (parameter * variable)
204207
# These are always off-diagonal (p != v), so coefficient is used as-is
205-
for term in f.pv
208+
for term in cubic_parameter_variable_terms(f)
206209
var = term.variable_2
207210
coef = term.coefficient
208211
p_val = _effective_param_value(model, p_idx(term.variable_1))
209212
terms_dict[var] = get(terms_dict, var, zero(T)) + coef * p_val
210213
end
211214

212215
# Add contributions from ppv cubic terms
213-
for term in _cubic_ppv_terms(f)
216+
for term in cubic_parameter_parameter_variable_terms(f)
214217
var = term.index_3
215218
p1_val = _effective_param_value(model, p_idx(term.index_1))
216219
p2_val = _effective_param_value(model, p_idx(term.index_2))
@@ -235,7 +238,7 @@ function _parametric_quadratic_terms(
235238
terms_dict = copy(f.quadratic_data)
236239

237240
# Add contributions from pvv cubic terms
238-
for term in _cubic_pvv_terms(f)
241+
for term in cubic_parameter_variable_variable_terms(f)
239242
p = term.index_1
240243
first_is_greater = term.index_2.value > term.index_3.value
241244
v1 = ifelse(first_is_greater, term.index_3, term.index_2)
@@ -314,7 +317,7 @@ function _delta_parametric_constant(
314317
delta = zero(T)
315318

316319
# From p terms
317-
for term in f.p
320+
for term in cubic_affine_parameter_terms(f)
318321
p_i = p_idx(term.variable)
319322
if haskey(model.updated_parameters, p_i) &&
320323
!isnan(model.updated_parameters[p_i])
@@ -325,7 +328,7 @@ function _delta_parametric_constant(
325328
end
326329

327330
# From pp terms
328-
for term in f.pp
331+
for term in cubic_parameter_parameter_terms(f)
329332
pi1 = p_idx(term.variable_1)
330333
pi2 = p_idx(term.variable_2)
331334
updated1 =
@@ -351,7 +354,7 @@ function _delta_parametric_constant(
351354
end
352355

353356
# From ppp cubic terms
354-
for term in _cubic_ppp_terms(f)
357+
for term in cubic_parameter_parameter_parameter_terms(f)
355358
pi1 = p_idx(term.index_1)
356359
pi2 = p_idx(term.index_2)
357360
pi3 = p_idx(term.index_3)
@@ -397,7 +400,7 @@ function _delta_parametric_affine_terms(
397400
delta_dict = Dict{MOI.VariableIndex,T}()
398401

399402
# From pv terms (parameter * variable, always off-diagonal)
400-
for term in f.pv
403+
for term in cubic_parameter_variable_terms(f)
401404
p_i = p_idx(term.variable_1)
402405
if haskey(model.updated_parameters, p_i) &&
403406
!isnan(model.updated_parameters[p_i])
@@ -411,7 +414,7 @@ function _delta_parametric_affine_terms(
411414
end
412415

413416
# From ppv cubic terms
414-
for term in _cubic_ppv_terms(f)
417+
for term in cubic_parameter_parameter_variable_terms(f)
415418
var = term.index_3
416419
pi1 = p_idx(term.index_1)
417420
pi2 = p_idx(term.index_2)
@@ -449,7 +452,7 @@ function _delta_parametric_quadratic_terms(
449452
) where {T}
450453
delta_dict = Dict{Tuple{MOI.VariableIndex,MOI.VariableIndex},T}()
451454

452-
for term in _cubic_pvv_terms(f)
455+
for term in cubic_parameter_variable_variable_terms(f)
453456
p_i = p_idx(term.index_1)
454457
first_is_greater = term.index_2.value > term.index_3.value
455458
v1 = ifelse(first_is_greater, term.index_3, term.index_2)

src/parametric_functions.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,11 @@ function _current_function(f::ParametricQuadraticFunction{T}) where {T}
189189
for (v, c) in f.affine_data_np
190190
push!(affine, MOI.ScalarAffineTerm{T}(c, v))
191191
end
192-
return MOI.ScalarQuadraticFunction{T}(f.vv, affine, f.current_constant)
192+
return MOI.ScalarQuadraticFunction{T}(
193+
quadratic_variable_variable_terms(f),
194+
affine,
195+
f.current_constant,
196+
)
193197
end
194198

195199
function _parametric_constant(
@@ -720,7 +724,7 @@ function _delta_parametric_constant(
720724
delta_constants = zeros(T, length(f.current_constant))
721725

722726
# Handle parameter-only affine terms
723-
for term in f.p
727+
for term in vector_affine_parameter_terms(f)
724728
p_idx_val = p_idx(term.scalar_term.variable)
725729
output_idx = term.output_index
726730

@@ -733,7 +737,7 @@ function _delta_parametric_constant(
733737
end
734738

735739
# Handle parameter-parameter quadratic terms
736-
for term in f.pp
740+
for term in vector_quadratic_parameter_parameter_terms(f)
737741
idx = term.output_index
738742
var1 = term.scalar_term.variable_1
739743
var2 = term.scalar_term.variable_2
@@ -767,7 +771,7 @@ function _delta_parametric_affine_terms(
767771
)
768772

769773
# Handle parameter-variable quadratic terms (px) that become affine (x) when p is updated
770-
for term in f.pv
774+
for term in vector_quadratic_parameter_variable_terms(f)
771775
p_idx_val = p_idx(term.scalar_term.variable_1)
772776
var = term.scalar_term.variable_2
773777
output_idx = term.output_index
@@ -835,7 +839,10 @@ end
835839

836840
function _current_function(f::ParametricVectorQuadraticFunction{T}) where {T}
837841
affine_terms = MOI.VectorAffineTerm{T}[]
838-
sizehint!(affine_terms, length(f.current_constant) + length(f.v))
842+
sizehint!(
843+
affine_terms,
844+
length(f.current_constant) + length(vector_affine_variable_terms(f)),
845+
)
839846
for ((var, idx), coef) in f.current_terms_with_p
840847
push!(
841848
affine_terms,

0 commit comments

Comments
 (0)