|
| 1 | +mutable struct ParametricQuadraticFunction{T} <: ParametricFunction{T} |
| 2 | + # helper to efficiently update affine terms |
| 3 | + affine_data::Dict{MOI.VariableIndex,T} |
| 4 | + affine_data_np::Dict{MOI.VariableIndex,T} |
| 5 | + # constant * parameter * variable (in this order) |
| 6 | + pv::Vector{MOI.ScalarQuadraticTerm{T}} |
| 7 | + # constant * parameter * parameter |
| 8 | + pp::Vector{MOI.ScalarQuadraticTerm{T}} |
| 9 | + # constant * variable * variable |
| 10 | + vv::Vector{MOI.ScalarQuadraticTerm{T}} |
| 11 | + # constant * parameter |
| 12 | + p::Vector{MOI.ScalarAffineTerm{T}} |
| 13 | + # constant * variable |
| 14 | + v::Vector{MOI.ScalarAffineTerm{T}} |
| 15 | + # constant (does not include the set constant) |
| 16 | + c::T |
| 17 | + # to avoid unnecessary lookups in updates |
| 18 | + set_constant::T |
| 19 | + # cache data that is inside the solver to avoid slow getters |
| 20 | + current_terms_with_p::Dict{MOI.VariableIndex,T} |
| 21 | + current_constant::T |
| 22 | +end |
| 23 | + |
| 24 | +function evaluate(f::ParametricQuadraticFunction{T}, x::Vector{T}, θ::Vector{T}) where {T} |
| 25 | + ret = zero(T) |
| 26 | + # constant * parameter * variable |
| 27 | + for (pv, θ) in zip(f.pv, θ) |
| 28 | + ret += pv.coefficient * θ[pv.variable_1.value] * x[pv.variable_2.value] |
| 29 | + end |
| 30 | + # constant * parameter * parameter |
| 31 | + for (pp, θ) in zip(f.pp, θ) |
| 32 | + ret += pp.coefficient * θ[pp.variable_1.value] * θ[pp.variable_2.value] |
| 33 | + end |
| 34 | + # constant * variable * variable |
| 35 | + for (vv, x) in zip(f.vv, x) |
| 36 | + ret += vv.coefficient * x[vv.variable_1.value] * x[vv.variable_2.value] |
| 37 | + end |
| 38 | + # constant * parameter |
| 39 | + for (p, θ) in zip(f.p, θ) |
| 40 | + ret += p.coefficient * θ[p.variable.value] |
| 41 | + end |
| 42 | + # constant * variable |
| 43 | + for (v, x) in zip(f.v, x) |
| 44 | + ret += v.coefficient * x[v.variable.value] |
| 45 | + end |
| 46 | + # constant |
| 47 | + ret += f.c |
| 48 | + |
| 49 | + return ret |
| 50 | +end |
| 51 | + |
| 52 | +struct PQFMatrix{MT,VT,T} |
| 53 | + pv::MT |
| 54 | + pp::MT |
| 55 | + vv::MT |
| 56 | + p::VT |
| 57 | + v::VT |
| 58 | + c::T |
| 59 | +end |
| 60 | + |
| 61 | +function PQFMatrix(f::ParametricQuadraticFunction{T}, x::Vector{MOI.VariableIndex}, θ::Vector{MOI.VariableIndex}) where {T} |
| 62 | + nx = length(x) |
| 63 | + nθ = length(θ) |
| 64 | + # NOTE: we rearrange to p'* pv * v |
| 65 | + pv = zeros(T, nθ, nx) |
| 66 | + for term in f.pv |
| 67 | + pv[term.variable_1.value, term.variable_2.value] += term.coefficient |
| 68 | + end |
| 69 | + pp = zeros(T, nθ, nθ) |
| 70 | + for term in f.pp |
| 71 | + pp[term.variable_1.value, term.variable_2.value] += term.coefficient |
| 72 | + end |
| 73 | + vv = zeros(T, nx, nx) |
| 74 | + for term in f.vv |
| 75 | + vv[term.variable_1.value, term.variable_2.value] += term.coefficient |
| 76 | + end |
| 77 | + p = zeros(T, nθ) |
| 78 | + for term in f.p |
| 79 | + p[term.variable.value] += term.coefficient |
| 80 | + end |
| 81 | + v = zeros(T, nx) |
| 82 | + for term in f.v |
| 83 | + v[term.variable.value] += term.coefficient |
| 84 | + end |
| 85 | + c = f.c |
| 86 | + return PQFMatrix(pv, pp, vv, p, v, c) |
| 87 | +end |
0 commit comments