Skip to content
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

Reenable support for JuMP.Containers.rowtable #35

Merged
merged 7 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ TimeStructures release notes
===================================


Version 0.7.1 (2022-10-29)
--------------------------
* Extend JuMP.Containers.rowtable for `Tables.jl` support added in JuMP v1.4.0

Version 0.7.0 (2022-10-25)
--------------------------
* Major cleanup and improved test coverage
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SparseVariables"
uuid = "2749762c-80ed-4b14-8f33-f0736679b02b"
authors = ["Truls Flatberg <[email protected]>", "Lars Hellemo <[email protected]>"]
version = "0.7.0"
version = "0.7.1"

[deps]
Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4"
Expand All @@ -11,6 +11,6 @@ SnoopPrecompile = "66db9d55-30c0-4569-8b51-7e840670fc0c"

[compat]
Dictionaries = "0.3"
JuMP = "1"
JuMP = "1.4.0"
SnoopPrecompile = "1"
julia = "1.6"
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,29 @@ end
@constraint(m, sum(z[endswith("a"), iseven]) >= 1)
```

## Solution information

The [Tables.jl](https://github.com/JuliaData/Tables.jl) support has now been [upstreamed to JuMP](https://github.com/jump-dev/JuMP.jl/pull/3104), and is also supported for `IndexedVarArray`s:

```julia
using HiGHS

# Solve m
set_optimizer(m, HiGHS.Optimizer)
optimize!(m)

# Fetch solution
tab = JuMP.Containers.rowtable(value, y)

# Save to CSV
using CSV
CSV.write("result.csv", tab)

# Convert to DataFrame
using DataFrames
DataFrame(tab)

# Pretty print
using PrettyTables
pretty_table(tab)
```
6 changes: 0 additions & 6 deletions src/indexedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,6 @@ function Containers.container(
)
end

# Fallback when no names are provided
function Containers.container(f::Function, indices, D::Type{IndexedVarArray})
index_vars = Symbol.("i$i" for i in 1:length(indices.prod.iterators))
return Containers.container(f, indices, D, index_vars)
end

function Base.firstindex(sa::IndexedVarArray, d)
return first(sort(sa.index_names[d]))
end
Expand Down
14 changes: 8 additions & 6 deletions src/tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ function _rows(x::Union{SparseArray,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(
function JuMP.Containers.rowtable(
f::Function,
x::AbstractSparseArray;
header::Vector{Symbol} = Symbol[],
Expand All @@ -23,14 +21,18 @@ function rowtable(
return [NamedTuple{names}((args..., f(x[i]))) for (i, args) in _rows(x)]
end

function rowtable(f::Function, x::IndexedVarArray, col_header::Symbol)
function JuMP.Containers.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 rowtable(f::Function, x::IndexedVarArray)
function JuMP.Containers.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)
return JuMP.Containers.rowtable(f, x; header = header)
end
16 changes: 12 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ end
set_optimizer_attribute(m, MOI.Silent(), true)
optimize!(m)

tab = SparseVariables.rowtable(value, y)
tab = JuMP.Containers.rowtable(value, y)

T = NamedTuple{(:i1, :i2, :value),Tuple{String,Int,Float64}}
T = NamedTuple{(:car, :year, :value),Tuple{String,Int,Float64}}
@test tab isa Vector{T}

@test length(tab) == 3
r = tab[1]
@test r.i1 == "ford"
@test r.i2 == 2002
@test r.car == "ford"
@test r.year == 2002
@test r.value == 300.0
end

Expand All @@ -265,6 +265,14 @@ end
@test length(x) == 1
unsafe_insertvar!(x, 2, 102)
@test length(x) == 2

# When no names are provided
@variable(m, y[1:3, 100:102] >= 0, container = IndexedVarArray)
@test length(y) == 0
insertvar!(y, 1, 100)
@test length(y) == 1
unsafe_insertvar!(y, 2, 102)
@test length(y) == 2
end

# Mockup of custom variable type
Expand Down