From f70716426c8377b50f36aa39bf23580aeb75afd4 Mon Sep 17 00:00:00 2001 From: Jinguo Liu Date: Sat, 14 Dec 2024 01:39:22 +0800 Subject: [PATCH] new implement findmax and findmin interfaces (#90) --- Project.toml | 2 +- src/GenericTensorNetworks.jl | 3 +++ src/interfaces.jl | 15 +++++++++++++++ test/interfaces.jl | 12 ++++++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 09dd63d..69bb2f3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "GenericTensorNetworks" uuid = "3521c873-ad32-4bb4-b63d-f4f178f42b49" authors = ["GiggleLiu and contributors"] -version = "3.0.0" +version = "3.1.0" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" diff --git a/src/GenericTensorNetworks.jl b/src/GenericTensorNetworks.jl index 36a94c1..325b1d0 100644 --- a/src/GenericTensorNetworks.jl +++ b/src/GenericTensorNetworks.jl @@ -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 diff --git a/src/interfaces.jl b/src/interfaces.jl index 5e9e7aa..7d2e3a8 100644 --- a/src/interfaces.jl +++ b/src/interfaces.jl @@ -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 \ No newline at end of file diff --git a/test/interfaces.jl b/test/interfaces.jl index 5d0dbc9..d79bd1f 100644 --- a/test/interfaces.jl +++ b/test/interfaces.jl @@ -1,4 +1,4 @@ -using GenericTensorNetworks +using GenericTensorNetworks, GenericTensorNetworks.ProblemReductions using Graphs, Test, Random @testset "independent set problem" begin @@ -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 \ No newline at end of file +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