-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tables support for IndexedVarArray #30
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2db9b8c
Tables support for IndexedVarArray
trulsf 956742a
Implement Tables support along recent JuMP changes
trulsf 623036a
Format fix
trulsf dba2949
Move rowtable out of JuMP.Containers namespace
trulsf 52efb69
Test with generic column names until new JuMP ver
trulsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,36 @@ | ||
abstract type SolutionTable end | ||
|
||
Tables.istable(::Type{<:SolutionTable}) = true | ||
Tables.rowaccess(::Type{<:SolutionTable}) = true | ||
|
||
rows(t::SolutionTable) = t | ||
names(t::SolutionTable) = getfield(t, :names) | ||
lookup(t::SolutionTable) = getfield(t, :lookup) | ||
|
||
Base.eltype(::SolutionTable) = SolutionRow | ||
Base.length(t::SolutionTable) = length(t.var) | ||
|
||
struct SolutionRow <: Tables.AbstractRow | ||
index_vals::Any | ||
sol_val::Float64 | ||
source::SolutionTable | ||
end | ||
|
||
function Tables.getcolumn(s::SolutionRow, i::Int) | ||
if i > length(getfield(s, :index_vals)) | ||
return getfield(s, :sol_val) | ||
end | ||
return getfield(s, :index_vals)[i] | ||
end | ||
|
||
function Tables.getcolumn(s::SolutionRow, nm::Symbol) | ||
i = lookup(getfield(s, :source))[nm] | ||
if i > length(getfield(s, :index_vals)) | ||
return getfield(s, :sol_val) | ||
end | ||
return getfield(s, :index_vals)[i] | ||
end | ||
|
||
Tables.columnnames(s::SolutionRow) = names(getfield(s, :source)) | ||
|
||
struct SolutionTableSparse <: SolutionTable | ||
names::Vector{Symbol} | ||
lookup::Dict{Symbol,Int} | ||
var::SparseVarArray | ||
end | ||
|
||
SolutionTableSparse(v::SparseVarArray) = SolutionTableSparse(v, Symbol(v.name)) | ||
|
||
function SolutionTableSparse(v::SparseVarArray, name) | ||
if length(v) > 0 && !has_values(first(v.data).model) | ||
error("No solution values available for variable") | ||
end | ||
names = vcat(v.index_names, name) | ||
lookup = Dict(nm => i for (i, nm) in enumerate(names)) | ||
return SolutionTableSparse(names, lookup, v) | ||
end | ||
|
||
function Base.iterate(t::SolutionTableSparse, state = nothing) | ||
next = | ||
isnothing(state) ? iterate(keys(t.var.data)) : | ||
iterate(keys(t.var.data), state) | ||
next === nothing && return nothing | ||
return SolutionRow(next[1], JuMP.value(t.var[next[1]]), t), next[2] | ||
end | ||
|
||
table(var::SparseVarArray) = SolutionTableSparse(var) | ||
table(var::SparseVarArray, name) = SolutionTableSparse(var, name) | ||
|
||
struct SolutionTableDense <: SolutionTable | ||
names::Vector{Symbol} | ||
lookup::Dict{Symbol,Int} | ||
index_lookup::Dict | ||
var::Containers.DenseAxisArray | ||
end | ||
|
||
function SolutionTableDense( | ||
v::Containers.DenseAxisArray{VariableRef,N,Ax,L}, | ||
name, | ||
colnames..., | ||
) where {N,Ax,L} | ||
if length(colnames) < length(axes(v)) | ||
error("Not enough column names provided") | ||
end | ||
if length(v) > 0 && !has_values(first(v).model) | ||
error("No solution values available for variable") | ||
function _rows(x::Union{SparseArray,SparseVarArray,IndexedVarArray}) | ||
return zip(eachindex(x.data), keys(x.data)) | ||
end | ||
|
||
# The rowtable functions should be moved to the JuMP.Containers namespace | ||
# when Tables support is available in JuMP | ||
function rowtable( | ||
f::Function, | ||
x::AbstractSparseArray; | ||
header::Vector{Symbol} = Symbol[], | ||
) | ||
if isempty(header) | ||
header = Symbol[Symbol("x$i") for i in 1:ndims(x)] | ||
push!(header, :y) | ||
end | ||
names = vcat(colnames..., name) | ||
lookup = Dict(nm => i for (i, nm) in enumerate(names)) | ||
index_lookup = Dict() | ||
for (i, ax) in enumerate(v.axes) | ||
index_lookup[i] = collect(ax) | ||
got, want = length(header), ndims(x) + 1 | ||
if got != want | ||
error( | ||
"Invalid number of column names provided: Got $got, expected $want.", | ||
) | ||
end | ||
return SolutionTableDense(names, lookup, index_lookup, v) | ||
names = tuple(header...) | ||
return [NamedTuple{names}((args..., f(x[i]))) for (i, args) in _rows(x)] | ||
end | ||
|
||
function Base.iterate(t::SolutionTableDense, state = nothing) | ||
next = | ||
isnothing(state) ? iterate(eachindex(t.var)) : | ||
iterate(eachindex(t.var), state) | ||
next === nothing && return nothing | ||
index = next[1] | ||
index_vals = [t.index_lookup[i][index[i]] for i in 1:length(index)] | ||
return SolutionRow(index_vals, JuMP.value(t.var[next[1]]), t), next[2] | ||
function rowtable(f::Function, x::IndexedVarArray, col_header::Symbol) | ||
header = Symbol[k for k in keys(x.index_names)] | ||
push!(header, col_header) | ||
return rowtable(f, x; header = header) | ||
end | ||
|
||
function table( | ||
var::Containers.DenseAxisArray{VariableRef,N,Ax,L}, | ||
name, | ||
colnames..., | ||
) where {N,Ax,L} | ||
return SolutionTableDense(var, name, colnames...) | ||
function rowtable(f::Function, x::IndexedVarArray) | ||
header = Symbol[k for k in keys(x.index_names)] | ||
push!(header, Symbol(f)) | ||
return rowtable(f, x; header = header) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be
AbstractSparseArray
? I guess we are about to delete SparseVarArray in another PR soon.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The implementation of _rows assumes a
data
property exists which is not available in general for AbstractSparseArray. Agree that SparseVarArray just can be removed here.