Skip to content

Commit 09629d9

Browse files
authored
Reenable support for JuMP.Containers.rowtable (#35)
* 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]>
1 parent cff8629 commit 09629d9

File tree

6 files changed

+52
-18
lines changed

6 files changed

+52
-18
lines changed

NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ TimeStructures release notes
22
===================================
33

44

5+
Version 0.7.1 (2022-10-29)
6+
--------------------------
7+
* Extend JuMP.Containers.rowtable for `Tables.jl` support added in JuMP v1.4.0
8+
59
Version 0.7.0 (2022-10-25)
610
--------------------------
711
* Major cleanup and improved test coverage

Project.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "SparseVariables"
22
uuid = "2749762c-80ed-4b14-8f33-f0736679b02b"
33
authors = ["Truls Flatberg <[email protected]>", "Lars Hellemo <[email protected]>"]
4-
version = "0.7.0"
4+
version = "0.7.1"
55

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

1212
[compat]
1313
Dictionaries = "0.3"
14-
JuMP = "1"
14+
JuMP = "1.4.0"
1515
SnoopPrecompile = "1"
1616
julia = "1.6"

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,29 @@ end
7575
@constraint(m, sum(z[endswith("a"), iseven]) >= 1)
7676
```
7777

78+
## Solution information
79+
80+
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:
81+
82+
```julia
83+
using HiGHS
84+
85+
# Solve m
86+
set_optimizer(m, HiGHS.Optimizer)
87+
optimize!(m)
88+
89+
# Fetch solution
90+
tab = JuMP.Containers.rowtable(value, y)
91+
92+
# Save to CSV
93+
using CSV
94+
CSV.write("result.csv", tab)
95+
96+
# Convert to DataFrame
97+
using DataFrames
98+
DataFrame(tab)
99+
100+
# Pretty print
101+
using PrettyTables
102+
pretty_table(tab)
103+
```

src/indexedarray.jl

-6
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,6 @@ function Containers.container(
188188
)
189189
end
190190

191-
# Fallback when no names are provided
192-
function Containers.container(f::Function, indices, D::Type{IndexedVarArray})
193-
index_vars = Symbol.("i$i" for i in 1:length(indices.prod.iterators))
194-
return Containers.container(f, indices, D, index_vars)
195-
end
196-
197191
function Base.firstindex(sa::IndexedVarArray, d)
198192
return first(sort(sa.index_names[d]))
199193
end

src/tables.jl

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ function _rows(x::Union{SparseArray,IndexedVarArray})
22
return zip(eachindex(x.data), keys(x.data))
33
end
44

5-
# The rowtable functions should be moved to the JuMP.Containers namespace
6-
# when Tables support is available in JuMP
7-
function rowtable(
5+
function JuMP.Containers.rowtable(
86
f::Function,
97
x::AbstractSparseArray;
108
header::Vector{Symbol} = Symbol[],
@@ -23,14 +21,18 @@ function rowtable(
2321
return [NamedTuple{names}((args..., f(x[i]))) for (i, args) in _rows(x)]
2422
end
2523

26-
function rowtable(f::Function, x::IndexedVarArray, col_header::Symbol)
24+
function JuMP.Containers.rowtable(
25+
f::Function,
26+
x::IndexedVarArray,
27+
col_header::Symbol,
28+
)
2729
header = Symbol[k for k in keys(x.index_names)]
2830
push!(header, col_header)
2931
return rowtable(f, x; header = header)
3032
end
3133

32-
function rowtable(f::Function, x::IndexedVarArray)
34+
function JuMP.Containers.rowtable(f::Function, x::IndexedVarArray)
3335
header = Symbol[k for k in keys(x.index_names)]
3436
push!(header, Symbol(f))
35-
return rowtable(f, x; header = header)
37+
return JuMP.Containers.rowtable(f, x; header = header)
3638
end

test/runtests.jl

+12-4
Original file line numberDiff line numberDiff line change
@@ -243,15 +243,15 @@ end
243243
set_optimizer_attribute(m, MOI.Silent(), true)
244244
optimize!(m)
245245

246-
tab = SparseVariables.rowtable(value, y)
246+
tab = JuMP.Containers.rowtable(value, y)
247247

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

251251
@test length(tab) == 3
252252
r = tab[1]
253-
@test r.i1 == "ford"
254-
@test r.i2 == 2002
253+
@test r.car == "ford"
254+
@test r.year == 2002
255255
@test r.value == 300.0
256256
end
257257

@@ -265,6 +265,14 @@ end
265265
@test length(x) == 1
266266
unsafe_insertvar!(x, 2, 102)
267267
@test length(x) == 2
268+
269+
# When no names are provided
270+
@variable(m, y[1:3, 100:102] >= 0, container = IndexedVarArray)
271+
@test length(y) == 0
272+
insertvar!(y, 1, 100)
273+
@test length(y) == 1
274+
unsafe_insertvar!(y, 2, 102)
275+
@test length(y) == 2
268276
end
269277

270278
# Mockup of custom variable type

0 commit comments

Comments
 (0)