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: implement findmax and findmin interfaces #90

Merged
merged 1 commit into from
Dec 13, 2024
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GenericTensorNetworks"
uuid = "3521c873-ad32-4bb4-b63d-f4f178f42b49"
authors = ["GiggleLiu <[email protected]> and contributors"]
version = "3.0.0"
version = "3.1.0"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
3 changes: 3 additions & 0 deletions src/GenericTensorNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export SetCovering, is_set_covering
# Interfaces
export solve, SizeMax, SizeMin, PartitionFunction, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single, AllConfigs

# ProblemReductions API
export GTNSolver

# Utilities
export save_configs, load_configs, hamming_distribution, save_sumproduct, load_sumproduct

Expand Down
15 changes: 15 additions & 0 deletions src/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,18 @@ for GP in [:IndependentSet, :MaxCut, :DominatingSet, :Satisfiability, :Coloring,
end
size_all_negative(::SpinGlass) = false
size_all_positive(::SpinGlass) = false

# NOTE: `findmin` and `findmax` are required by `ProblemReductions.jl`
Base.@kwdef struct GTNSolver
optimizer::OMEinsum.CodeOptimizer = TreeSA()
usecuda::Bool = false
T::Type = Float64
end
function Base.findmin(problem::AbstractProblem, solver::GTNSolver)
res = collect(solve(GenericTensorNetwork(problem; optimizer=solver.optimizer), ConfigsMin(; tree_storage=true); usecuda=solver.usecuda, T=solver.T)[].c)
return map(x -> ProblemReductions.id_to_config(problem, Int.(x) .+ 1), res)
end
function Base.findmax(problem::AbstractProblem, solver::GTNSolver)
res = collect(solve(GenericTensorNetwork(problem; optimizer=solver.optimizer), ConfigsMax(; tree_storage=true); usecuda=solver.usecuda, T=solver.T)[].c)
return map(x -> ProblemReductions.id_to_config(problem, Int.(x) .+ 1), res)
end
12 changes: 10 additions & 2 deletions test/interfaces.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GenericTensorNetworks
using GenericTensorNetworks, GenericTensorNetworks.ProblemReductions
using Graphs, Test, Random

@testset "independent set problem" begin
Expand Down Expand Up @@ -245,4 +245,12 @@ end
@test_throws ArgumentError solve(gp, ConfigsMin(2))
@test solve(gp, ConfigsMin(Single)) isa Array
@test_throws AssertionError ConfigsMin(0)
end
end

@testset "GTNSolver" begin
sg = SpinGlass(smallgraph(:petersen), rand(15), rand(10))
solver1 = GTNSolver(; optimizer=TreeSA(ntrials=1))
solver2 = BruteForce()
@test Set(findmin(sg, solver1)) == Set(findmin(sg, solver2))
@test Set(findmax(sg, solver1)) == Set(findmax(sg, solver2))
end
Loading