Skip to content

Commit 9c8fd10

Browse files
committed
Simplify code
1 parent 0cec466 commit 9c8fd10

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

src/constraint.jl

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,24 @@ function MOIU.allocate_constraint(m::SOItoMOIBridge, f::SAF, s::SupportedSets)
1414
_allocate_constraint(m::SOItoMOIBridge, f, s)
1515
end
1616

17-
output_index(::MOI.ScalarAffineTerm) = 1
18-
output_index(t::MOI.VectorAffineTerm) = t.output_index
19-
scalar_term(t::MOI.ScalarAffineTerm) = t
20-
scalar_term(t::MOI.VectorAffineTerm) = t.scalar_term
21-
22-
_getconstant(m::SOItoMOIBridge, s::MOI.AbstractScalarSet) = MOIU.getconstant(s)
23-
_getconstant(m::SOItoMOIBridge{T}, s::MOI.AbstractSet) where T = zero(T)
24-
25-
function loadcoefficients!(m::SOItoMOIBridge, cs::UnitRange, f::SAF, s)
17+
function loadcoefficients!(m::SOItoMOIBridge, cs::UnitRange,
18+
f::MOI.ScalarAffineFunction, s)
2619
f = MOIU.canonical(f) # sum terms with same variables and same outputindex
27-
if !isempty(cs)
28-
rhs = _getconstant(m, s) .- MOI._constant(f)
29-
for t in f.terms
30-
st = scalar_term(t)
31-
if !iszero(st.coefficient)
32-
c = cs[output_index(t)]
33-
for (blk, i, j, coef, shift) in varmap(m, st.variable_index)
34-
if !iszero(blk)
35-
@assert !iszero(coef)
36-
setconstraintcoefficient!(m.sdoptimizer, st.coefficient*coef, c, blk, i, j)
37-
end
38-
if isa(rhs, Vector)
39-
rhs[output_index(t)] -= st.coefficient * shift
40-
else
41-
rhs -= st.coefficient * shift
42-
end
20+
@assert length(cs) == 1
21+
c = first(cs)
22+
rhs = MOIU.getconstant(s) - MOI._constant(f)
23+
for t in f.terms
24+
if !iszero(t.coefficient)
25+
for (blk, i, j, coef, shift) in varmap(m, t.variable_index)
26+
if !iszero(blk)
27+
@assert !iszero(coef)
28+
setconstraintcoefficient!(m.sdoptimizer, t.coefficient*coef, c, blk, i, j)
4329
end
30+
rhs -= t.coefficient * shift
4431
end
4532
end
46-
for j in 1:MOI.output_dimension(f)
47-
c = cs[j]
48-
setconstraintconstant!(m.sdoptimizer, rhs[j], c)
49-
end
5033
end
34+
setconstraintconstant!(m.sdoptimizer, rhs, c)
5135
end
5236

5337
function MOIU.load_constraint(m::SOItoMOIBridge, ci::CI, f::SAF, s::SupportedSets)

src/variable.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ function MOIU.allocate_constraint(m::SOItoMOIBridge{T}, f::VF, s::SupportedSets)
8484
end
8585
end
8686

87+
_getconstant(m::SOItoMOIBridge, s::MOI.AbstractScalarSet) = MOIU.getconstant(s)
88+
_getconstant(m::SOItoMOIBridge{T}, s::MOI.AbstractSet) where T = zero(T)
89+
8790
_var(f::SVF, j) = f.variable
8891
_var(f::VVF, j) = f.variables[j]
8992
function MOIU.load_constraint(m::SOItoMOIBridge, ci::CI, f::VF, s::SupportedSets)

0 commit comments

Comments
 (0)