|
26 | 26 |
|
27 | 27 | nconstraints(f::Union{SVF, SAF}, s) = 1 |
28 | 28 | nconstraints(f::VVF, s) = length(f.variables) |
29 | | -nconstraints(f::VAF, s) = length(f.constant) |
| 29 | +nconstraints(f::VAF, s) = MOIU.moilength(f) |
30 | 30 |
|
31 | 31 | MOIU.canallocateconstraint(::SOItoMOIBridge{T}, f::Type{<:Union{VF, AF{T}}}, ::Type{<:SupportedSets}) where T = true |
32 | 32 | function _allocateconstraint!(m::SOItoMOIBridge, f, s) |
@@ -55,35 +55,36 @@ function loadslacks!(m::SOItoMOIBridge, cs) |
55 | 55 | end |
56 | 56 | end |
57 | 57 |
|
58 | | -_row(f::SAF, i) = 1 |
59 | | -_row(f::VAF, i) = f.outputindex[i] |
| 58 | +output_index(::MOI.ScalarAffineTerm) = 1 |
| 59 | +output_index(t::MOI.VectorAffineTerm) = t.output_index |
| 60 | +scalar_term(t::MOI.ScalarAffineTerm) = t |
| 61 | +scalar_term(t::MOI.VectorAffineTerm) = t.scalar_term |
60 | 62 |
|
61 | 63 | _getconstant(m::SOItoMOIBridge, s::MOI.AbstractScalarSet) = MOIU.getconstant(s) |
62 | 64 | _getconstant(m::SOItoMOIBridge{T}, s::MOI.AbstractSet) where T = zero(T) |
63 | 65 |
|
64 | 66 | function loadcoefficients!(m::SOItoMOIBridge, cs::UnitRange, f::AF, s) |
65 | 67 | f = MOIU.canonical(f) # sum terms with same variables and same outputindex |
66 | 68 | if !isempty(cs) |
67 | | - rhs = _getconstant(m, s) - f.constant |
68 | | - for i in 1:length(f.variables) |
69 | | - val = f.coefficients[i] |
70 | | - if !iszero(val) |
71 | | - row = _row(f, i) |
72 | | - c = cs[row] |
73 | | - for (blk, i, j, coef, shift) in varmap(m, f.variables[i]) |
| 69 | + rhs = _getconstant(m, s) - MOIU._constant(f) |
| 70 | + for t in f.terms |
| 71 | + st = scalar_term(t) |
| 72 | + if !iszero(st.coefficient) |
| 73 | + c = cs[output_index(t)] |
| 74 | + for (blk, i, j, coef, shift) in varmap(m, st.variable_index) |
74 | 75 | if !iszero(blk) |
75 | 76 | @assert !iszero(coef) |
76 | | - setconstraintcoefficient!(m.sdoptimizer, val*coef, c, blk, i, j) |
| 77 | + setconstraintcoefficient!(m.sdoptimizer, st.coefficient*coef, c, blk, i, j) |
77 | 78 | end |
78 | 79 | if isa(rhs, Vector) |
79 | | - rhs[row] -= val * shift |
| 80 | + rhs[output_index(t)] -= st.coefficient * shift |
80 | 81 | else |
81 | | - rhs -= val * shift |
| 82 | + rhs -= st.coefficient * shift |
82 | 83 | end |
83 | 84 | end |
84 | 85 | end |
85 | 86 | end |
86 | | - for j in 1:length(f.constant) |
| 87 | + for j in 1:MOIU.moilength(f) |
87 | 88 | c = cs[j] |
88 | 89 | setconstraintconstant!(m.sdoptimizer, rhs[j], c) |
89 | 90 | end |
|
0 commit comments