Skip to content

Commit

Permalink
Reenable support for JuMP.Containers.rowtable (#35)
Browse files Browse the repository at this point in the history
* Reenable support for JuMP.Containers.rowtable
* Updated compat bound for JuMP v1.4.0
* Update tests for JuMP v1.4.0
* Drop unreachable fallback
* Test variable construction without names

Co-authored-by: Lars Hellemo <[email protected]>
  • Loading branch information
hellemo and hellemo authored Oct 29, 2022
1 parent cff8629 commit 09629d9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 18 deletions.
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

2 comments on commit 09629d9

@hellemo
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/71279

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.1 -m "<description of version>" 09629d909534f9ef4935fc8978f5e253e00dc67c
git push origin v0.7.1

Please sign in to comment.