|
1 |
| -abstract type SolutionTable end |
2 | 1 |
|
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 |
| -function Base.iterate(t::SolutionTableSparse, state = nothing) |
37 |
| - next = |
38 |
| - isnothing(state) ? iterate(keys(t.var.data)) : |
39 |
| - iterate(keys(t.var.data), state) |
40 |
| - next === nothing && return nothing |
41 |
| - return SolutionRow(next[1], JuMP.value(t.var[next[1]]), t), next[2] |
42 |
| -end |
43 |
| - |
44 |
| -struct SolutionTableDense <: SolutionTable |
45 |
| - names::Vector{Symbol} |
46 |
| - lookup::Dict{Symbol,Int} |
47 |
| - index_lookup::Dict |
48 |
| - var::Containers.DenseAxisArray |
49 |
| -end |
50 |
| - |
51 |
| -function SolutionTableDense( |
52 |
| - v::Containers.DenseAxisArray{VariableRef,N,Ax,L}, |
53 |
| - name, |
54 |
| - colnames..., |
55 |
| -) where {N,Ax,L} |
56 |
| - if length(colnames) < length(axes(v)) |
57 |
| - error("Not enough column names provided") |
58 |
| - end |
59 |
| - if length(v) > 0 && !has_values(first(v).model) |
60 |
| - error("No solution values available for variable") |
61 |
| - end |
62 |
| - names = vcat(colnames..., name) |
63 |
| - lookup = Dict(nm => i for (i, nm) in enumerate(names)) |
64 |
| - index_lookup = Dict() |
65 |
| - for (i, ax) in enumerate(v.axes) |
66 |
| - index_lookup[i] = collect(ax) |
67 |
| - end |
68 |
| - return SolutionTableDense(names, lookup, index_lookup, v) |
69 |
| -end |
70 |
| - |
71 |
| -function Base.iterate(t::SolutionTableDense, state = nothing) |
72 |
| - next = |
73 |
| - isnothing(state) ? iterate(eachindex(t.var)) : |
74 |
| - iterate(eachindex(t.var), state) |
75 |
| - next === nothing && return nothing |
76 |
| - index = next[1] |
77 |
| - index_vals = [t.index_lookup[i][index[i]] for i in 1:length(index)] |
78 |
| - return SolutionRow(index_vals, JuMP.value(t.var[next[1]]), t), next[2] |
79 |
| -end |
80 |
| - |
81 |
| -function table( |
82 |
| - var::Containers.DenseAxisArray{VariableRef,N,Ax,L}, |
83 |
| - name, |
84 |
| - colnames..., |
85 |
| -) where {N,Ax,L} |
86 |
| - return SolutionTableDense(var, name, colnames...) |
87 |
| -end |
0 commit comments