|
1 |
| -abstract type SolutionTable end |
2 |
| - |
3 |
| -Tables.istable(::Type{<:SolutionTable}) = true |
4 |
| -Tables.rowaccess(::Type{<:SolutionTable}) = true |
5 |
| - |
6 |
| -rows(t::SolutionTable) = t |
7 |
| -names(t::SolutionTable) = getfield(t, :names) |
8 |
| -lookup(t::SolutionTable) = getfield(t, :lookup) |
9 |
| - |
10 |
| -Base.eltype(::SolutionTable) = SolutionRow |
11 |
| -Base.length(t::SolutionTable) = length(t.var) |
12 |
| - |
13 |
| -struct SolutionRow <: Tables.AbstractRow |
14 |
| - index_vals::Any |
15 |
| - sol_val::Float64 |
16 |
| - source::SolutionTable |
17 |
| -end |
18 |
| - |
19 |
| -function Tables.getcolumn(s::SolutionRow, i::Int) |
20 |
| - if i > length(getfield(s, :index_vals)) |
21 |
| - return getfield(s, :sol_val) |
22 |
| - end |
23 |
| - return getfield(s, :index_vals)[i] |
24 |
| -end |
25 |
| - |
26 |
| -function Tables.getcolumn(s::SolutionRow, nm::Symbol) |
27 |
| - i = lookup(getfield(s, :source))[nm] |
28 |
| - if i > length(getfield(s, :index_vals)) |
29 |
| - return getfield(s, :sol_val) |
30 |
| - end |
31 |
| - return getfield(s, :index_vals)[i] |
32 |
| -end |
33 |
| - |
34 |
| -Tables.columnnames(s::SolutionRow) = names(getfield(s, :source)) |
35 |
| - |
36 |
| -struct SolutionTableSparse <: SolutionTable |
37 |
| - names::Vector{Symbol} |
38 |
| - lookup::Dict{Symbol,Int} |
39 |
| - var::SparseVarArray |
40 |
| -end |
41 |
| - |
42 |
| -SolutionTableSparse(v::SparseVarArray) = SolutionTableSparse(v, Symbol(v.name)) |
43 |
| - |
44 |
| -function SolutionTableSparse(v::SparseVarArray, name) |
45 |
| - if length(v) > 0 && !has_values(first(v.data).model) |
46 |
| - error("No solution values available for variable") |
47 |
| - end |
48 |
| - names = vcat(v.index_names, name) |
49 |
| - lookup = Dict(nm => i for (i, nm) in enumerate(names)) |
50 |
| - return SolutionTableSparse(names, lookup, v) |
51 |
| -end |
52 |
| - |
53 |
| -function Base.iterate(t::SolutionTableSparse, state = nothing) |
54 |
| - next = |
55 |
| - isnothing(state) ? iterate(keys(t.var.data)) : |
56 |
| - iterate(keys(t.var.data), state) |
57 |
| - next === nothing && return nothing |
58 |
| - return SolutionRow(next[1], JuMP.value(t.var[next[1]]), t), next[2] |
59 |
| -end |
60 |
| - |
61 |
| -table(var::SparseVarArray) = SolutionTableSparse(var) |
62 |
| -table(var::SparseVarArray, name) = SolutionTableSparse(var, name) |
63 |
| - |
64 |
| -struct SolutionTableDense <: SolutionTable |
65 |
| - names::Vector{Symbol} |
66 |
| - lookup::Dict{Symbol,Int} |
67 |
| - index_lookup::Dict |
68 |
| - var::Containers.DenseAxisArray |
69 |
| -end |
70 |
| - |
71 |
| -function SolutionTableDense( |
72 |
| - v::Containers.DenseAxisArray{VariableRef,N,Ax,L}, |
73 |
| - name, |
74 |
| - colnames..., |
75 |
| -) where {N,Ax,L} |
76 |
| - if length(colnames) < length(axes(v)) |
77 |
| - error("Not enough column names provided") |
78 |
| - end |
79 |
| - if length(v) > 0 && !has_values(first(v).model) |
80 |
| - error("No solution values available for variable") |
| 1 | +function _rows(x::Union{SparseArray,SparseVarArray,IndexedVarArray}) |
| 2 | + return zip(eachindex(x.data), keys(x.data)) |
| 3 | +end |
| 4 | + |
| 5 | +# The rowtable functions should be moved to the JuMP.Containers namespace |
| 6 | +# when Tables support is available in JuMP |
| 7 | +function rowtable( |
| 8 | + f::Function, |
| 9 | + x::AbstractSparseArray; |
| 10 | + header::Vector{Symbol} = Symbol[], |
| 11 | +) |
| 12 | + if isempty(header) |
| 13 | + header = Symbol[Symbol("x$i") for i in 1:ndims(x)] |
| 14 | + push!(header, :y) |
81 | 15 | end
|
82 |
| - names = vcat(colnames..., name) |
83 |
| - lookup = Dict(nm => i for (i, nm) in enumerate(names)) |
84 |
| - index_lookup = Dict() |
85 |
| - for (i, ax) in enumerate(v.axes) |
86 |
| - index_lookup[i] = collect(ax) |
| 16 | + got, want = length(header), ndims(x) + 1 |
| 17 | + if got != want |
| 18 | + error( |
| 19 | + "Invalid number of column names provided: Got $got, expected $want.", |
| 20 | + ) |
87 | 21 | end
|
88 |
| - return SolutionTableDense(names, lookup, index_lookup, v) |
| 22 | + names = tuple(header...) |
| 23 | + return [NamedTuple{names}((args..., f(x[i]))) for (i, args) in _rows(x)] |
89 | 24 | end
|
90 | 25 |
|
91 |
| -function Base.iterate(t::SolutionTableDense, state = nothing) |
92 |
| - next = |
93 |
| - isnothing(state) ? iterate(eachindex(t.var)) : |
94 |
| - iterate(eachindex(t.var), state) |
95 |
| - next === nothing && return nothing |
96 |
| - index = next[1] |
97 |
| - index_vals = [t.index_lookup[i][index[i]] for i in 1:length(index)] |
98 |
| - return SolutionRow(index_vals, JuMP.value(t.var[next[1]]), t), next[2] |
| 26 | +function rowtable(f::Function, x::IndexedVarArray, col_header::Symbol) |
| 27 | + header = Symbol[k for k in keys(x.index_names)] |
| 28 | + push!(header, col_header) |
| 29 | + return rowtable(f, x; header = header) |
99 | 30 | end
|
100 | 31 |
|
101 |
| -function table( |
102 |
| - var::Containers.DenseAxisArray{VariableRef,N,Ax,L}, |
103 |
| - name, |
104 |
| - colnames..., |
105 |
| -) where {N,Ax,L} |
106 |
| - return SolutionTableDense(var, name, colnames...) |
| 32 | +function rowtable(f::Function, x::IndexedVarArray) |
| 33 | + header = Symbol[k for k in keys(x.index_names)] |
| 34 | + push!(header, Symbol(f)) |
| 35 | + return rowtable(f, x; header = header) |
107 | 36 | end
|
0 commit comments