Skip to content

Commit

Permalink
new implement findmax and findmin interfaces (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
GiggleLiu authored Dec 13, 2024
1 parent 1f91473 commit f707164
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
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

0 comments on commit f707164

Please sign in to comment.