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

New model: BMF #134

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/ProblemReductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export Factoring, is_factoring
export Matching, is_matching
export MaximalIS
export PaintShop
export BinaryMatrixFactorization, is_binary_matrix_factorization

# rules
export target_problem, AbstractProblem, ConstraintSatisfactionProblem, solution_size, SolutionSize
Expand Down
69 changes: 69 additions & 0 deletions src/models/BMF.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
$TYPEDEF
BinaryMatrixFactorization{K}(A::AbstractMatrix{Bool}, k::Int)

The Boolean Matrix Factorization (BMF) problem is defined on a binary matrix A in m x n size. Given a positive integer k, we need to determine whether we can factorize the matrix A into two binary matrices U and V such that the boolean product of U and V is equal to A, and the dimensions of U and V are (m x k) and (k x n) respectively. Refer to `Recent developments in boolean matrix factorization.(Miettinen, P., & Neumann, S,2020)` for details.

### Required interfaces
- [`variables`](@ref), the degrees of freedoms in the computational problem.
- [`flavors`](@ref), the flavors (domain) of a degree of freedom.
- [`solution_size`](@ref), the size (the lower the better) of the input configuration.
- [`problem_size`](@ref), the size of the computational problem. e.g. for a graph, it could be `(n_vertices=?, n_edges=?)`.

### Optional interfaces
- [`num_variables`](@ref), the number of variables in the computational problem.
- [`num_flavors`](@ref), the number of flavors (domain) of a degree of freedom.
- [`findbest`](@ref), find the best configurations of the input problem.
"""
struct BinaryMatrixFactorization<: AbstractProblem
A::AbstractMatrix
k::Int
function BinaryMatrixFactorization(A::AbstractMatrix, k::Int) where K
new(A, k)
end
end
Base.:(==)(a::BinaryMatrixFactorization, b::BinaryMatrixFactorization) = a.A == b.A && a.k == b.k

num_variables(bmf::BinaryMatrixFactorization) = size(bmf.A,1) * bmf.k + size(bmf.A,2) * bmf.k
flavors(::Type{<:BinaryMatrixFactorization}) = (0, 1)
problem_size(bmf::BinaryMatrixFactorization) = (; num_rows=size(bmf.A,1), num_cols=size(bmf.A,2), k=bmf.k)
function solution_size(bmf::BinaryMatrixFactorization,b::AbstractMatrix,c::AbstractMatrix)
# Hamming Distance is used to described the solution size, the smaller the better
return sum(bmf.A .!= boolean_product(b,c,bmf.k))
end

function boolean_product(A::AbstractMatrix, B::AbstractMatrix,k)
@assert size(A,2) == size(B,1) == k "Dimension mismatch"
@assert isbitstype(eltype(A)) && isbitstype(eltype(B)) "Only binary matrices are supported"
C = zeros(size(A,1), size(B,2))
for i in 1:size(A,1), j in 1:size(B,2)
C[i,j] = any(x -> x==1,A[i,:] .* B[:,j])
end
return C
end

energy_mode(::Type{<:BinaryMatrixFactorization}) = SmallerSizeIsBetter()

function is_binary_matrix_factorization(bmf::BinaryMatrixFactorization, b::AbstractMatrix, c::AbstractMatrix)
return solution_size(bmf,b,c) == 0
end

"""
function findbest(bmf::BinaryMatrixFactorization, ::BruteForce)
n_rows, n_cols = size(bmf.A)
best = (fill(0, n_rows, bmf.k), fill(0, bmf.k, n_cols))
best_energy = solution_size(bmf, best)
for b in Iterators.product(fill(0:1, n_rows, bmf.k)...)
for c in Iterators.product(fill(0:1, bmf.k, n_cols)...)
energy = solution_size(bmf,best(1),best(2))
if energy < best_energy
best = (b,c)
best_energy = energy
end
end
end
return best
end
"""


1 change: 1 addition & 0 deletions src/models/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,4 @@ include("Factoring.jl")
include("Matching.jl")
include("MaximalIS.jl")
include("Paintshop.jl")
include("BMF.jl")
33 changes: 33 additions & 0 deletions test/models/BMF.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Test, ProblemReductions

@testset "BMF" begin
# test 1
A = fill(1, 3, 3)
k = 2
bmf = BinaryMatrixFactorization(A, k)

@test num_variables(bmf) == 12
@test flavors(BinaryMatrixFactorization) == (0, 1)
@test problem_size(bmf) == (num_rows=3, num_cols=3, k=2)
@test solution_size(bmf, fill(0, 3, 2), fill(0, 2, 3)) == 9
@test energy_mode(BinaryMatrixFactorization) == SmallerSizeIsBetter()
@test is_binary_matrix_factorization(bmf, fill(0, 3, 2), fill(0, 2, 3)) == false
@test is_binary_matrix_factorization(bmf,fill(1, 3, 2), fill(1, 2, 3)) == true
bmf1 = BinaryMatrixFactorization(fill(1,3,3), 3)
@test bmf != bmf1
bmf2 = BinaryMatrixFactorization(fill(1,3,3), 2)
@test bmf == bmf2

# test 2
A = fill(true, 3, 3)
k = 2
bmf = BinaryMatrixFactorization(A, k)

@test num_variables(bmf) == 12
@test flavors(BinaryMatrixFactorization) == (0, 1)
@test problem_size(bmf) == (num_rows=3, num_cols=3, k=2)
@test solution_size(bmf, fill(0, 3, 2), fill(0, 2, 3)) == 9
@test energy_mode(BinaryMatrixFactorization) == SmallerSizeIsBetter()
@test is_binary_matrix_factorization(bmf, fill(0, 3, 2), fill(0, 2, 3)) == false
@test is_binary_matrix_factorization(bmf,fill(1, 3, 2), fill(1, 2, 3)) == true
end
4 changes: 4 additions & 0 deletions test/models/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ end

@testset "Paintshop" begin
include("Paintshop.jl")
end

@testset "BMF" begin
include("BMF.jl")
end
Loading