@@ -5,15 +5,15 @@ abstract type GraphProblem end
5
5
6
6
"""
7
7
Independence{CT<:EinTypes} <: GraphProblem
8
- Independence(graph; openvertices=(), optmethod=:tree , kwargs...)
8
+ Independence(graph; openvertices=(), optmethod=:greedy , kwargs...)
9
9
10
10
Independent set problem. `kwargs` is forwarded to `optimize_code`.
11
11
"""
12
12
struct Independence{CT<: EinTypes } <: GraphProblem
13
13
code:: CT
14
14
end
15
15
16
- function Independence (g:: SimpleGraph ; openvertices= (), optmethod= :tree , kwargs... )
16
+ function Independence (g:: SimpleGraph ; openvertices= (), optmethod= :greedy , kwargs... )
17
17
rawcode = EinCode (([(i,) for i in LightGraphs. vertices (g)]. .. , # labels for vertex tensors
18
18
[minmax (e. src,e. dst) for e in LightGraphs. edges (g)]. .. ), openvertices) # labels for edge tensors
19
19
code = optimize_code (rawcode, Val (optmethod); kwargs... )
22
22
23
23
"""
24
24
MaxCut{CT<:EinTypes} <: GraphProblem
25
- MaxCut(graph; openvertices=(), optmethod=:tree , kwargs...)
25
+ MaxCut(graph; openvertices=(), optmethod=:greedy , kwargs...)
26
26
27
27
Max cut problem (or spin glass problem). `kwargs` is forwarded to `optimize_code`.
28
28
"""
29
29
struct MaxCut{CT<: EinTypes } <: GraphProblem
30
30
code:: CT
31
31
end
32
- function MaxCut (g:: SimpleGraph ; openvertices= (), optmethod= :tree , kwargs... )
32
+ function MaxCut (g:: SimpleGraph ; openvertices= (), optmethod= :greedy , kwargs... )
33
33
rawcode = EinCode (([minmax (e. src,e. dst) for e in LightGraphs. edges (g)]. .. ,), openvertices) # labels for edge tensors
34
34
MaxCut (optimize_code (rawcode, Val (optmethod); kwargs... ))
35
35
end
36
36
37
37
"""
38
38
MaximalIndependence{CT<:EinTypes} <: GraphProblem
39
- MaximalIndependence(graph; openvertices=(), optmethod=:tree , kwargs...)
39
+ MaximalIndependence(graph; openvertices=(), optmethod=:greedy , kwargs...)
40
40
41
41
Maximal independent set problem. `kwargs` is forwarded to `optimize_code`.
42
42
"""
43
43
struct MaximalIndependence{CT<: EinTypes } <: GraphProblem
44
44
code:: CT
45
45
end
46
46
47
- function MaximalIndependence (g:: SimpleGraph ; openvertices= (), optmethod= :tree , kwargs... )
47
+ function MaximalIndependence (g:: SimpleGraph ; openvertices= (), optmethod= :greedy , kwargs... )
48
48
rawcode = EinCode (([(LightGraphs. neighbors (g, v)... , v) for v in LightGraphs. vertices (g)]. .. ,), openvertices)
49
49
MaximalIndependence (optimize_code (rawcode, Val (optmethod); kwargs... ))
50
50
end
51
51
52
52
"""
53
53
Matching{CT<:EinTypes} <: GraphProblem
54
- Matching(graph; openvertices=(), optmethod=:tree , kwargs...)
54
+ Matching(graph; openvertices=(), optmethod=:greedy , kwargs...)
55
55
56
56
Vertex matching problem. `kwargs` is forwarded to `optimize_code`.
57
57
The matching polynomial adopts the first definition in wiki page: https://en.wikipedia.org/wiki/Matching_polynomial
@@ -64,15 +64,15 @@ struct Matching{CT<:EinTypes} <: GraphProblem
64
64
code:: CT
65
65
end
66
66
67
- function Matching (g:: SimpleGraph ; openvertices= (), optmethod= :tree , kwargs... )
67
+ function Matching (g:: SimpleGraph ; openvertices= (), optmethod= :greedy , kwargs... )
68
68
rawcode = EinCode (([(minmax (e. src,e. dst),) for e in LightGraphs. edges (g)]. .. , # labels for edge tensors
69
69
[([minmax (i,j) for j in neighbors (g, i)]. .. ,) for i in LightGraphs. vertices (g)]. .. ,), openvertices) # labels for vertex tensors
70
70
Matching (optimize_code (rawcode, Val (optmethod); kwargs... ))
71
71
end
72
72
73
73
"""
74
74
Coloring{K,CT<:EinTypes} <: GraphProblem
75
- Coloring{K}(graph; openvertices=(), optmethod=:tree , kwargs...)
75
+ Coloring{K}(graph; openvertices=(), optmethod=:greedy , kwargs...)
76
76
77
77
K-Coloring problem. `kwargs` is forwarded to `optimize_code`.
78
78
"""
@@ -81,7 +81,7 @@ struct Coloring{K,CT<:EinTypes} <: GraphProblem
81
81
end
82
82
Coloring {K} (code:: ET ) where {K,ET<: EinTypes } = Coloring {K,ET} (code)
83
83
# same network layout as independent set.
84
- Coloring {K} (g:: SimpleGraph ; openvertices= (), optmethod= :tree , kwargs... ) where K = Coloring {K} (Independence (g; openvertices= openvertices, kwargs... ). code)
84
+ Coloring {K} (g:: SimpleGraph ; openvertices= (), optmethod= :greedy , kwargs... ) where K = Coloring {K} (Independence (g; openvertices= openvertices, optmethod = optmethod, D = K , kwargs... ). code)
85
85
86
86
"""
87
87
labels(code)
102
102
103
103
collect_ixs (ne:: EinCode ) = [collect (ix) for ix in getixs (ne)]
104
104
function collect_ixs (ne:: NestedEinsum )
105
- d = collect_ixs! (ne, Dict {Int,Vector{OMEinsum.labeltype(ne.eins)}} ())
106
- return [d[i] for i= 1 : length (d)]
105
+ d = OMEinsum. collect_ixs! (ne, Dict {Int,Vector{OMEinsum.labeltype(ne.eins)}} ())
106
+ ks = sort! (collect (keys (d)))
107
+ return [d[i] for i in ks]
107
108
end
108
109
109
110
function collect_ixs! (ne:: NestedEinsum , d:: Dict )
@@ -131,23 +132,26 @@ Optimize the contraction order.
131
132
* `:sa`, the simulated annealing approach, takes kwargs [`rw_weight`, `βs`, `ntrials`, `niters`]. Check `optimize_sa` in package `OMEinsumContractionOrders`.
132
133
* `:raw`, do nothing and return the raw EinCode.
133
134
"""
134
- function optimize_code (@nospecialize (code:: EinTypes ), :: Val{optmethod} ; sc_target= 17 , max_group_size= 40 , nrepeat= 10 , imbalances= 0.0 : 0.001 : 0.8 , initializer= :random , βs= 0.01 : 0.05 : 10.0 , ntrials= 50 , niters= 1000 , sc_weight= 2.0 , rw_weight= 1.0 ) where optmethod
135
- size_dict = Dict ([s=> 2 for s in labels (code)])
136
- optcode = if optmethod == :kahypar
135
+ function optimize_code (@nospecialize (code:: EinTypes ), :: Val{optmethod} ; sc_target= 17 , max_group_size= 40 , nrepeat= 10 , imbalances= 0.0 : 0.001 : 0.8 , initializer= :random , βs= 0.01 : 0.05 : 10.0 , ntrials= 50 , niters= 1000 , sc_weight= 2.0 , rw_weight= 1.0 , D= 2 ) where optmethod
136
+ if optmethod === :raw
137
+ return code
138
+ end
139
+ size_dict = Dict ([s=> D for s in labels (code)])
140
+ simplifier, code = merge_vectors (code)
141
+ optcode = if optmethod === :kahypar
137
142
optimize_kahypar (code, size_dict; sc_target= sc_target, max_group_size= max_group_size, imbalances= imbalances, greedy_nrepeat= nrepeat)
138
- elseif optmethod == :sa
143
+ elseif optmethod === :sa
139
144
optimize_sa (code, size_dict; sc_target= sc_target, max_group_size= max_group_size, βs= βs, ntrials= ntrials, niters= niters, initializer= initializer, greedy_nrepeat= nrepeat)
140
- elseif optmethod == :greedy
145
+ elseif optmethod === :greedy
141
146
optimize_greedy (code, size_dict; nrepeat= nrepeat)
142
- elseif optmethod == :tree
147
+ elseif optmethod === :tree
143
148
optimize_tree (code, size_dict; sc_target= sc_target, βs= βs, niters= niters, ntrials= ntrials, sc_weight= sc_weight, initializer= initializer, rw_weight= rw_weight)
144
- elseif optmethod == :auto
149
+ elseif optmethod === :auto
145
150
optimize_kahypar_auto (code, size_dict; max_group_size= max_group_size, effort= 500 , greedy_nrepeat= nrepeat)
146
- elseif optmethod == :raw
147
- code
148
151
else
149
152
ArgumentError (" optimizer `$optmethod ` not defined." )
150
153
end
154
+ optcode = embed_simplifier (optcode, simplifier)
151
155
@info " time/space complexity is $(OMEinsum. timespace_complexity (optcode, size_dict)) "
152
156
return optcode
153
157
end
160
164
bondsize (gp:: Coloring{K} ) where K = K
161
165
162
166
"""
163
- set_packing(sets; openvertices=(), optmethod=:tree , kwargs...)
167
+ set_packing(sets; openvertices=(), optmethod=:greedy , kwargs...)
164
168
165
169
Set packing is a generalization of independent set problem to hypergraphs.
166
170
Calling this function will return you an `Independence` instance.
@@ -177,8 +181,8 @@ julia> res = best_solutions(gp; all=true)[]
177
181
(2, {10010, 00110, 01100})ₜ
178
182
```
179
183
"""
180
- function set_packing (sets; openvertices= (), optmethod= :tree , kwargs... )
184
+ function set_packing (sets; openvertices= (), optmethod= :greedy , kwargs... )
181
185
n = length (sets)
182
- code = EinCode (([(i,) for i= 1 : n]. .. , [(i,j) for i= 1 : n,j= 1 : n if j> i && ! isempty (sets[i] ∩ sets[j])]. .. ), () )
186
+ code = EinCode (([(i,) for i= 1 : n]. .. , [(i,j) for i= 1 : n,j= 1 : n if j> i && ! isempty (sets[i] ∩ sets[j])]. .. ), openvertices )
183
187
Independence (optimize_code (code, Val (optmethod); kwargs... ))
184
188
end
0 commit comments