-
-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Tables.jl support to containers (#3104)
- Loading branch information
Showing
6 changed files
with
306 additions
and
0 deletions.
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 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 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
_rows(x::Array) = zip(eachindex(x), Iterators.product(axes(x)...)) | ||
|
||
_rows(x::DenseAxisArray) = zip(vec(eachindex(x)), Iterators.product(axes(x)...)) | ||
|
||
_rows(x::SparseAxisArray) = zip(eachindex(x.data), keys(x.data)) | ||
|
||
""" | ||
rowtable([f::Function=identity,] x; [header::Vector{Symbol} = Symbol[]]) | ||
Applies the function `f` to all elements of the variable container `x`, | ||
returning the result as a `Vector` of `NamedTuple`s, where `header` is a vector | ||
containing the corresponding axis names. | ||
If `x` is an `N`-dimensional array, there must be `N+1` names, so that the last | ||
name corresponds to the result of `f(x[i])`. | ||
If `header` is left empty, then the default header is `[:x1, :x2, ..., :xN, :y]`. | ||
!!! info | ||
A `Vector` of `NamedTuple`s implements the [Tables.jl](https://github.com/JuliaData/Tables.jl) | ||
interface, and so the result can be used as input for any function | ||
that consumes a 'Tables.jl' compatible source. | ||
## Example | ||
```jldoctest; setup=:(using JuMP) | ||
julia> model = Model(); | ||
julia> @variable(model, x[i=1:2, j=i:2] >= 0, start = i+j); | ||
julia> Containers.rowtable(start_value, x; header = [:i, :j, :start]) | ||
3-element Vector{NamedTuple{(:i, :j, :start), Tuple{Int64, Int64, Float64}}}: | ||
(i = 1, j = 2, start = 3.0) | ||
(i = 1, j = 1, start = 2.0) | ||
(i = 2, j = 2, start = 4.0) | ||
julia> Containers.rowtable(x) | ||
3-element Vector{NamedTuple{(:x1, :x2, :y), Tuple{Int64, Int64, VariableRef}}}: | ||
(x1 = 1, x2 = 2, y = x[1,2]) | ||
(x1 = 1, x2 = 1, y = x[1,1]) | ||
(x1 = 2, x2 = 2, y = x[2,2]) | ||
``` | ||
""" | ||
function rowtable( | ||
f::Function, | ||
x::Union{Array,DenseAxisArray,SparseAxisArray}; | ||
header::Vector{Symbol} = Symbol[], | ||
) | ||
if isempty(header) | ||
header = Symbol[Symbol("x$i") for i in 1:ndims(x)] | ||
push!(header, :y) | ||
end | ||
got, want = length(header), ndims(x) + 1 | ||
if got != want | ||
error( | ||
"Invalid number of column names provided: Got $got, expected $want.", | ||
) | ||
end | ||
names = tuple(header...) | ||
return [NamedTuple{names}((args..., f(x[i]))) for (i, args) in _rows(x)] | ||
end | ||
|
||
rowtable(x; kwargs...) = rowtable(identity, x; kwargs...) |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
module TestTableInterface | ||
|
||
using JuMP | ||
using Test | ||
|
||
function runtests() | ||
for name in names(@__MODULE__; all = true) | ||
if startswith("$(name)", "test_") | ||
@testset "$(name)" begin | ||
getfield(@__MODULE__, name)() | ||
end | ||
end | ||
end | ||
return | ||
end | ||
|
||
function test_denseaxisarray() | ||
model = Model() | ||
@variable(model, x[i = 4:10, j = 2002:2022] >= 0, start = 0.0) | ||
@test typeof(x) <: Containers.DenseAxisArray | ||
start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) | ||
T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} | ||
@test start_table isa Vector{T} | ||
@test length(start_table) == length(x) | ||
row = first(start_table) | ||
@test row == (i1 = 4, i2 = 2002, i3 = 0.0) | ||
x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) | ||
@test x_table[1] == (i1 = 4, i2 = 2002, i3 = x[4, 2002]) | ||
return | ||
end | ||
|
||
function test_array() | ||
model = Model() | ||
@variable(model, x[1:10, 1:5] >= 0, start = 0.0) | ||
@test typeof(x) <: Array{VariableRef} | ||
start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) | ||
T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} | ||
@test start_table isa Vector{T} | ||
@test length(start_table) == length(x) | ||
row = first(start_table) | ||
@test row == (i1 = 1, i2 = 1, i3 = 0.0) | ||
x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) | ||
@test x_table[1] == (i1 = 1, i2 = 1, i3 = x[1, 1]) | ||
return | ||
end | ||
|
||
function test_sparseaxisarray() | ||
model = Model() | ||
@variable(model, x[i = 1:10, j = 1:5; i + j <= 8] >= 0, start = 0) | ||
@test typeof(x) <: Containers.SparseAxisArray | ||
start_table = Containers.rowtable(start_value, x; header = [:i1, :i2, :i3]) | ||
T = NamedTuple{(:i1, :i2, :i3),Tuple{Int,Int,Float64}} | ||
@test start_table isa Vector{T} | ||
@test length(start_table) == length(x) | ||
@test (i1 = 1, i2 = 1, i3 = 0.0) in start_table | ||
x_table = Containers.rowtable(x; header = [:i1, :i2, :i3]) | ||
@test (i1 = 1, i2 = 1, i3 = x[1, 1]) in x_table | ||
return | ||
end | ||
|
||
function test_col_name_error() | ||
model = Model() | ||
@variable(model, x[1:2, 1:2]) | ||
@test_throws ErrorException Containers.rowtable(x; header = [:y, :a]) | ||
@test_throws( | ||
ErrorException, | ||
Containers.rowtable(x; header = [:y, :a, :b, :c]), | ||
) | ||
@test Containers.rowtable(x; header = [:y, :a, :b]) isa Vector{<:NamedTuple} | ||
return | ||
end | ||
|
||
# Mockup of custom variable type | ||
struct _MockVariable <: JuMP.AbstractVariable | ||
var::JuMP.ScalarVariable | ||
end | ||
|
||
struct _MockVariableRef <: JuMP.AbstractVariableRef | ||
vref::VariableRef | ||
end | ||
|
||
JuMP.name(v::_MockVariableRef) = JuMP.name(v.vref) | ||
|
||
JuMP.owner_model(v::_MockVariableRef) = JuMP.owner_model(v.vref) | ||
|
||
JuMP.start_value(v::_MockVariableRef) = JuMP.start_value(v.vref) | ||
|
||
struct _Mock end | ||
|
||
function JuMP.build_variable(::Function, info::JuMP.VariableInfo, _::_Mock) | ||
return _MockVariable(JuMP.ScalarVariable(info)) | ||
end | ||
|
||
function JuMP.add_variable(model::Model, x::_MockVariable, name::String) | ||
variable = JuMP.add_variable(model, x.var, name) | ||
return _MockVariableRef(variable) | ||
end | ||
|
||
function test_custom_variable() | ||
model = Model() | ||
@variable( | ||
model, | ||
x[i = 1:3, j = 100:102] >= 0, | ||
_Mock(), | ||
container = Containers.DenseAxisArray, | ||
start = 0.0, | ||
) | ||
@test typeof(x) <: Containers.DenseAxisArray | ||
start_table = Containers.rowtable(start_value, x) | ||
T = NamedTuple{(:x1, :x2, :y),Tuple{Int,Int,Float64}} | ||
@test start_table isa Vector{T} | ||
@test length(start_table) == length(x) | ||
@test (x1 = 1, x2 = 100, y = 0.0) in start_table | ||
x_table = Containers.rowtable(x) | ||
@test (x1 = 1, x2 = 100, y = x[1, 100]) in x_table | ||
return | ||
end | ||
|
||
end | ||
|
||
TestTableInterface.runtests() |