Skip to content

Commit 673c71a

Browse files
authored
open pit mining (#44)
* new mining * fix negative pow for truncated poly * update * polish open pit mining, add branching method
1 parent 85b2c09 commit 673c71a

20 files changed

+449
-43
lines changed

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
{
2-
"julia.environmentPath": "/home/leo/.julia/dev/GenericTensorNetworks"
32
}

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ OMEinsum = "ebe7aa44-baf0-506c-a96f-8464559b3922"
1818
OMEinsumContractionOrders = "6f22d1fd-8eed-4bb7-9776-e7d684900715"
1919
Polynomials = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
2020
Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae"
21+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
2122
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2223
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
2324
SIMDTypes = "94e857df-77ce-4151-89e5-788b33177be4"

docs/src/ref.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PaintShop
1313
Satisfiability
1414
SetCovering
1515
SetPacking
16+
OpenPitMining
1617
```
1718

1819
#### Graph Problem Interfaces
@@ -54,6 +55,9 @@ satisfiable
5455
5556
¬
5657
58+
59+
is_valid_mining
60+
print_mining
5761
```
5862

5963
## Properties

src/GenericTensorNetworks.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using TropicalNumbers
66
using OMEinsum
77
using OMEinsum: timespace_complexity, getixsv
88
using Graphs, Random
9-
using DelimitedFiles, Serialization
9+
using DelimitedFiles, Serialization, Printf
1010

1111
# OMEinsum
1212
export timespace_complexity, timespacereadwrite_complexity, @ein_str, getixsv, getiyv
@@ -41,6 +41,7 @@ export DominatingSet, is_dominating_set
4141
export Matching, is_matching
4242
export SetPacking, is_set_packing
4343
export SetCovering, is_set_covering
44+
export OpenPitMining, is_valid_mining, print_mining
4445

4546
# Interfaces
4647
export solve, SizeMax, SizeMin, CountingAll, CountingMax, CountingMin, GraphPolynomial, SingleConfigMax, SingleConfigMin, ConfigsAll, ConfigsMax, ConfigsMin, Single

src/arithematics.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ function Base.show(io::IO, ::MIME"text/plain", x::TruncatedPoly{K}) where K
169169
end
170170
end
171171

172+
Base.literal_pow(::typeof(^), a::TruncatedPoly, ::Val{b}) where b = a ^ b
173+
function Base.:^(x::TruncatedPoly{K,T,TO}, b::Integer) where {K,T,TO}
174+
if b >= 0
175+
return Base.power_by_squaring(x, b)
176+
else
177+
if count(!iszero, x.coeffs) <= 1
178+
return TruncatedPoly{K,T,TO}(ntuple(i->iszero(x.coeffs[i]) ? x.coeffs[i] : x.coeffs[i] ^ b, K), x.maxorder*b)
179+
else
180+
error("can not apply negative power over truncated polynomial: $x")
181+
end
182+
end
183+
end
184+
172185
############################ ExtendedTropical #####################
173186
"""
174187
ExtendedTropical{K,TO} <: Number
@@ -342,12 +355,15 @@ function Base.:+(a::ExtendedTropical{K,TO}, b::ExtendedTropical{K,TO}) where {K,
342355
return ExtendedTropical{K,TO}(res)
343356
end
344357

358+
Base.literal_pow(::typeof(^), a::ExtendedTropical, ::Val{b}) where b = a ^ b
345359
Base.:^(a::ExtendedTropical, b::Integer) = Base.invoke(^, Tuple{ExtendedTropical, Real}, a, b)
346360
function Base.:^(a::ExtendedTropical{K,TO}, b::Real) where {K,TO}
347361
if iszero(b) # to avoid NaN
348362
return one(ExtendedTropical{K,TO})
349363
else
350-
return ExtendedTropical{K,TO}(a.orders .^ b)
364+
# sort for negative order
365+
# the -Inf cannot become possitive after powering
366+
return ExtendedTropical{K,TO}(sort!(map(x->iszero(x) ? x : x^b, a.orders)))
351367
end
352368
end
353369

@@ -812,6 +828,7 @@ for TYPE in [:AbstractSetNumber, :TruncatedPoly, :ExtendedTropical]
812828
end
813829

814830
# to handle power of polynomials
831+
Base.literal_pow(::typeof(^), x::SumProductTree, ::Val{y}) where y = Base.:^(x, y)
815832
function Base.:^(x::SumProductTree, y::Real)
816833
if y == 0
817834
return one(x)
@@ -821,6 +838,7 @@ function Base.:^(x::SumProductTree, y::Real)
821838
error("pow of non-leaf nodes is forbidden!")
822839
end
823840
end
841+
Base.literal_pow(::typeof(^), x::ConfigEnumerator, ::Val{y}) where y = Base.:^(x, y)
824842
function Base.:^(x::ConfigEnumerator, y::Real)
825843
if y <= 0
826844
return one(x)
@@ -830,6 +848,7 @@ function Base.:^(x::ConfigEnumerator, y::Real)
830848
error("pow of configuration enumerator of `size > 1` is forbidden!")
831849
end
832850
end
851+
Base.literal_pow(::typeof(^), x::ConfigSampler, ::Val{y}) where y = Base.:^(x, y)
833852
function Base.:^(x::ConfigSampler, y::Real)
834853
if y <= 0
835854
return one(x)

src/networks/Coloring.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
"""
22
Coloring{K,CT<:AbstractEinsum, WT<:Union{NoWeight, Vector}} <: GraphProblem
3-
Coloring{K}(graph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
3+
Coloring{K}(graph; weights=NoWeight(), openvertices=(),
4+
optimizer=GreedyMethod(), simplifier=nothing,
5+
fixedvertices=Dict()
6+
)
47
58
The [Vertex Coloring](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/Coloring.html) problem.
6-
`weights` has one to one correspondence with `edges(graph)`.
7-
`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
9+
10+
Positional arguments
11+
-------------------------------
12+
* `graph` is the problem graph.
13+
14+
Keyword arguments
15+
-------------------------------
16+
* `weights` are associated with the edges of the `graph`.
17+
* `optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
18+
* `fixedvertices` is a dict to specify the values of degree of freedoms on vertices, where a value can be in ``0,...,K-1`` (different colors).
19+
* `openvertices` is a tuple of labels to specify the output tensor. Theses degree of freedoms will not be contracted.
820
"""
921
struct Coloring{K,CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1022
code::CT

src/networks/DominatingSet.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""
22
DominatingSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
33
DominatingSet(graph; weights=NoWeight(), openvertices=(),
4-
optimizer=GreedyMethod(), simplifier=nothing)
4+
optimizer=GreedyMethod(), simplifier=nothing,
5+
fixedvertices=Dict()
6+
)
57
68
The [dominating set](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/DominatingSet.html) problem.
7-
In the constructor, `weights` are associated with vertices.
8-
`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
9+
10+
Positional arguments
11+
-------------------------------
12+
* `graph` is the problem graph.
13+
14+
Keyword arguments
15+
-------------------------------
16+
* `weights` are associated with the vertices of the `graph`.
17+
* `optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
18+
* `fixedvertices` is a dict to specify the values of degree of freedoms on vertices, where a value can be `0` (absent in the set) or `1` (present in the set).
19+
* `openvertices` is a tuple of labels to specify the output tensor. Theses degree of freedoms will not be contracted.
920
"""
1021
struct DominatingSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1122
code::CT

src/networks/IndependentSet.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
"""
22
IndependentSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
33
IndependentSet(graph; weights=NoWeight(), openvertices=(),
4-
optimizer=GreedyMethod(), simplifier=nothing)
4+
optimizer=GreedyMethod(), simplifier=nothing,
5+
fixedvertices=Dict()
6+
)
57
68
The [independent set problem](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/IndependentSet.html) in graph theory.
7-
In the constructor, `weights` are the weights of vertices.
8-
`openvertices` specifies labels for the output tensor.
9-
`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
9+
10+
Positional arguments
11+
-------------------------------
12+
* `graph` is the problem graph.
13+
14+
Keyword arguments
15+
-------------------------------
16+
* `weights` are associated with the vertices of the `graph`.
17+
* `optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
18+
* `fixedvertices` is a dict to specify the values of degree of freedoms on vertices, where a value can be `0` (absent in the set) or `1` (present in the set).
19+
* `openvertices` is a tuple of labels to specify the output tensor. Theses degree of freedoms will not be contracted.
20+
21+
Examples
22+
-------------------------------
23+
```jldoctest; setup=:(using Random; Random.seed!(2))
24+
julia> using GenericTensorNetworks, Graphs
25+
26+
julia> problem = IndependentSet(smallgraph(:petersen));
27+
28+
julia> solve(problem, ConfigsMax())
29+
0-dimensional Array{CountingTropical{Float64, ConfigEnumerator{10, 1, 1}}, 0}:
30+
(4.0, {0101010001, 1010000011, 0100100110, 0010111000, 1001001100})ₜ
31+
```
1032
"""
1133
struct IndependentSet{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1234
code::CT

src/networks/Matching.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
"""
22
Matching{CT<:AbstractEinsum, WT<:Union{NoWeight,Vector}} <: GraphProblem
3-
Matching(graph; weights=NoWeight(), openvertices=(), optimizer=GreedyMethod(), simplifier=nothing)
3+
Matching(graph; weights=NoWeight(), openvertices=(),
4+
optimizer=GreedyMethod(), simplifier=nothing,
5+
fixedvertices=Dict()
6+
)
47
58
The [Vertex matching](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/Matching.html) problem.
6-
`weights` has one to one correspondence with `edges(graph)`.
7-
`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
9+
10+
Positional arguments
11+
-------------------------------
12+
* `graph` is the problem graph.
13+
14+
Keyword arguments
15+
-------------------------------
16+
* `weights` are associated with the edges of the `graph`.
17+
* `optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
18+
* `fixedvertices` is a dict to specify the values of degree of freedoms on edges, where a value can be `0` (not matched) or `1` (mathced).
19+
* `openvertices` is a tuple of labels to specify the output tensor. Theses degree of freedoms will not be contracted.
820
"""
921
struct Matching{CT<:AbstractEinsum, WT<:Union{NoWeight,Vector}} <: GraphProblem
1022
code::CT

src/networks/MaxCut.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
"""
22
MaxCut{CT<:AbstractEinsum,WT<:Union{NoWieght, Vector}} <: GraphProblem
33
MaxCut(graph; weights=NoWeight(), openvertices=(),
4-
optimizer=GreedyMethod(), simplifier=nothing)
4+
optimizer=GreedyMethod(), simplifier=nothing,
5+
fixedvertices=Dict()
6+
)
57
68
The [cutting](https://psychic-meme-f4d866f8.pages.github.io/dev/tutorials/MaxCut.html) problem (or spin glass problem).
7-
In the constructor, `weights` are the weights of edges.
8-
`optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
9+
10+
Positional arguments
11+
-------------------------------
12+
* `graph` is the problem graph.
13+
14+
Keyword arguments
15+
-------------------------------
16+
* `weights` are associated with the edges of the `graph`.
17+
* `optimizer` and `simplifier` are for tensor network optimization, check [`optimize_code`](@ref) for details.
18+
* `fixedvertices` is a dict to specify the values of degree of freedoms, where a value can be `0` (in one side of the cut) or `1` (in the other side of the cut).
19+
* `openvertices` is a tuple of labels to specify the output tensor. Theses degree of freedoms will not be contracted.
920
"""
1021
struct MaxCut{CT<:AbstractEinsum,WT<:Union{NoWeight, Vector}} <: GraphProblem
1122
code::CT

0 commit comments

Comments
 (0)