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: Reduction between simple kinds of SpinGlass and MaxCut problems #44

Merged
merged 3 commits into from
Jul 23, 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 src/ProblemReductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export MaxCut
# rules
export target_problem, AbstractProblem, AbstractReductionResult, reduceto, extract_solution, reduction_complexity
export LogicGadget, truth_table
export spinglass_circuit, ReductionCircuitToSpinGlass
export ReductionCircuitToSpinGlass, ReductionMaxCutToSpinGlass, ReductionSpinGlassToMaxCut
export findbest, BruteForce
export CNF

Expand Down
24 changes: 12 additions & 12 deletions src/models/MaxCut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ weights of the edges that are cut.
Positional arguments
-------------------------------
* `graph` is the problem graph.
* `edge_weights` are associated with the edges of the `graph`.
* `weights` are associated with the edges of the `graph`. We have ensure that the `weights` are in the same order as the edges in `edges(graph)`.
"""
struct MaxCut{WT1<:AbstractVector} <: AbstractProblem
struct MaxCut{WT<:AbstractVector} <: AbstractProblem
graph::SimpleGraph{Int}
edge_weights::WT1
function MaxCut(g::SimpleGraph,edge_weights::AbstractVector=UnitWeight(ne(g)))
@assert length(edge_weights) == ne(g)
new{typeof(edge_weights)}(g, edge_weights)
weights::WT
function MaxCut(g::SimpleGraph,weights::AbstractVector=UnitWeight(ne(g)))
@assert length(weights) == ne(g)
new{typeof(weights)}(g, weights)
end
end
Base.:(==)(a::MaxCut, b::MaxCut) = a.graph == b.graph && a.edge_weights == b.edge_weights
Base.:(==)(a::MaxCut, b::MaxCut) = a.graph == b.graph && a.weights == b.weights

# varibles interface
variables(gp::MaxCut) = [1:nv(gp.graph)...]
num_variables(gp::MaxCut) = nv(gp.graph)
flavors(::Type{<:MaxCut}) = [0, 1] #choose it or not

# weights interface
parameters(c::MaxCut) = [[c.edge_weights[i] for i=1:ne(c.graph)]...]
parameters(c::MaxCut) = [[c.weights[i] for i=1:ne(c.graph)]...]
set_parameters(c::MaxCut, weights) = MaxCut(c.graph, weights[1:ne(c.graph)])


Expand All @@ -38,12 +38,12 @@ sum of the weights of the edges that are cut.
"""
function evaluate(c::MaxCut, config)
@assert length(config) == nv(c.graph)
-cut_size(vedges(c.graph), config; edge_weights=c.edge_weights)
-cut_size(vedges(c.graph), config; weights=c.weights)
end

function cut_size(terms, config; edge_weights=UnitWeight(length(terms)))
size = zero(promote_type(eltype(edge_weights)))
for (i,j)in zip(terms, edge_weights) # we have ensure that the edge_weights are in the same order as the edges in terms, so we could use zip()
function cut_size(terms, config; weights=UnitWeight(length(terms)))
size = zero(promote_type(eltype(weights)))
for (i,j)in zip(terms, weights)
size += (config[i[1]] != config[i[2]]) * j # terms are the edges,and terms[1],terms[2] are the two vertices of the edge.
end
return size
Expand Down
3 changes: 2 additions & 1 deletion src/rules/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ Extract the solution `solution` of the target problem to the original problem.
"""
function extract_solution end

include("spinglass_sat.jl")
include("spinglass_sat.jl")
include("spinglass_maxcut.jl")
67 changes: 67 additions & 0 deletions src/rules/spinglass_maxcut.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""
$TYPEDEF

The reduction result of a maxcut to a spin glass problem.

### Fields
- `spinglass::SpinGlass{GT, T}`: the spin glass problem.

We only consider a simple reduction from MaxCut to SpinGlass(the graph must be `SimpleGraph`).
"""
struct ReductionMaxCutToSpinGlass{GT, T}
spinglass::SpinGlass{GT, T}
end

target_problem(res::ReductionMaxCutToSpinGlass) = res.spinglass

function reduceto(::Type{<:SpinGlass}, maxcut::MaxCut)
sg = maxcut2spinglass(maxcut)
return ReductionMaxCutToSpinGlass(sg)
end

function maxcut2spinglass(maxcut::MaxCut)
@assert maxcut.graph isa SimpleGraph "the graph must be `SimpleGraph`"
return SpinGlass(maxcut.graph, maxcut.weights)
end

function extract_solution(res::ReductionMaxCutToSpinGlass, sol)
out = zeros(eltype(sol), num_variables(res.spinglass))
for (k, v) in enumerate(variables(res.spinglass))
out[v] = sol[k]
end
return out
end

"""
$TYPEDEF

The reduction result of a spin glass to a maxcut problem.

### Fields
- `maxcut::MaxCut{WT}`: the MaxCut problem.

We only consider a simple reduction from SpinGlass to MaxCut(the graph must be `SimpleGraph`).
"""
struct ReductionSpinGlassToMaxCut{WT}
maxcut::MaxCut{WT}
end

target_problem(res::ReductionSpinGlassToMaxCut) = res.maxcut

function reduceto(::Type{<:MaxCut}, sg::SpinGlass)
mc = spinglass2maxcut(sg)
return ReductionSpinGlassToMaxCut(mc)
end

function spinglass2maxcut(sg::SpinGlass)
@assert sg.graph isa SimpleGraph "the graph must be `SimpleGraph`"
return MaxCut(sg.graph, sg.weights)
end

function extract_solution(res::ReductionSpinGlassToMaxCut, sol)
out = zeros(eltype(sol), num_variables(res.maxcut))
for (k, v) in enumerate(variables(res.maxcut))
out[v] = sol[k]
end
return out
end
15 changes: 12 additions & 3 deletions test/rules/rules.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
using Test
using Test, ProblemReductions, Graphs

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

@testset "spinglass_sat" begin
include("spinglass_maxcut.jl")
end

@testset "rules" begin
circuit = CircuitSAT(@circuit begin
x = a ∨ ¬b
y = ¬c ∨ b
z = x ∧ y ∧ a
end)
graph = smallgraph(:petersen)
maxcut = MaxCut(graph)
spinglass = SpinGlass(graph, [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1])

for (source, target_type) in [
# please add more tests here
circuit => SpinGlass
circuit => SpinGlass,
maxcut => SpinGlass,
spinglass => MaxCut,
]
# directly solve
best_source = findbest(source, BruteForce())
Expand All @@ -29,4 +38,4 @@ end
# check if the solutions are the same
@test sort(best_source) == sort(best_source_extracted)
end
end
end
19 changes: 19 additions & 0 deletions test/rules/spinglass_maxcut.jl
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include this file into runtests.jl

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Test, ProblemReductions, Graphs
using ProblemReductions: maxcut2spinglass

# add a simple test to check the reduction process
@testset "spinglass_maxcut" begin
# construct a graph
g = SimpleGraph(4)
add_edge!(g, 1, 2)
add_edge!(g, 1, 3)
add_edge!(g, 3, 4)
add_edge!(g, 2, 3)

mc = MaxCut(g, [1, 3, 1, 4])
Base.:(==)(a::ReductionMaxCutToSpinGlass, b::ReductionMaxCutToSpinGlass) = a.spinglass == b.spinglass
@test reduceto(SpinGlass, mc) == ReductionMaxCutToSpinGlass(maxcut2spinglass(mc))
@test maxcut2spinglass(mc) == SpinGlass(g, [1, 3, 1, 4])
@test findbest(mc, BruteForce()) == [[0, 0, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1]] # in lexicographic order
@test findbest(maxcut2spinglass(mc), BruteForce()) == [[0, 0, 1, 0], [0, 1, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1]] # in lexicographic order
end