Skip to content

Commit

Permalink
Added new algorithm test, various bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cem-okulmus committed Feb 19, 2021
1 parent ff0a1de commit 99a3c28
Show file tree
Hide file tree
Showing 19 changed files with 368 additions and 54 deletions.
3 changes: 1 addition & 2 deletions algorithms/algorithms.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This package implements various algorithms to compute Generalized Hypertree Decompositions as well as
// the more restricted set of Hypertree Deocmpositions.

// the more restricted set of Hypertree Decompositions.
package algorithms

import "github.com/cem-okulmus/BalancedGo/lib"
Expand Down
3 changes: 1 addition & 2 deletions algorithms/balsepGlobal.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package algorithms

import (
"log"
"reflect"
"runtime"

Expand Down Expand Up @@ -93,7 +92,7 @@ func rerooting(H lib.Graph, balsep lib.Edges, subtrees []lib.Decomp) lib.Decomp
for _, s := range subtrees {
// fmt.Println("H ", H, "balsep ", balsep, "comp ", s.Graph)
s.Root = s.Root.Reroot(rerootNode)
log.Printf("Rerooted Decomp: %v\n", s)
// log.Printf("Rerooted Decomp: %v\n", s)
output.Children = append(output.Children, s.Root.Children...)
}
// log.Println("H: ", H, "output: ", output)
Expand Down
12 changes: 7 additions & 5 deletions algorithms/balsepHybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ func (b BalSepHybrid) findDecomp(currentDepth int, H lib.Graph) lib.Decomp {

// Base case handling
//stop if there are at most two special edges left
if comps[i].Len() <= 2 {
if comps[i].Len() <= 1 {
comps[i].Special = append(comps[i].Special, SepSpecial)
ch <- baseCaseSmart(b.Graph, comps[i])
return
}

//Early termination
if comps[i].Edges.Len() <= b.K && len(comps[i].Special) == 1 {
if comps[i].Edges.Len() <= b.K && len(comps[i].Special) == 0 {
comps[i].Special = append(comps[i].Special, SepSpecial)
ch <- earlyTermination(comps[i])
return
}
Expand All @@ -126,12 +128,12 @@ func (b BalSepHybrid) findDecomp(currentDepth int, H lib.Graph) lib.Decomp {
det.cache.Init()

result := det.findDecomp(comps[i], balsep.Vertices())
if !reflect.DeepEqual(result, lib.Decomp{}) && currentDepth == 0 {
if !reflect.DeepEqual(result, lib.Decomp{}) {
result.SkipRerooting = true
} else {
// comps[i].Special = append(comps[i].Special, SepSpecial)
// res2 := b.findDecompBalSep(1000, comps[i])
// if !reflect.DeepEqual(res2, Decomp{}) {
// res2 := b.findDecomp(1000, comps[i])
// if !reflect.DeepEqual(res2, lib.Decomp{}) {
// fmt.Println("Result, ", res2)
// fmt.Println("H: ", comps[i], "balsep ", balsep)
// log.Panicln("Something is rotten in the state of this program")
Expand Down
12 changes: 7 additions & 5 deletions algorithms/detKDecomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type DetKDecomp struct {

// SetWidth sets the current width parameter of the algorithm
func (d *DetKDecomp) SetWidth(K int) {
d.cache.Reset() // reset the cache as the new width might invalidate any old results

d.K = K
}

Expand Down Expand Up @@ -181,9 +183,9 @@ OUTER:

d.cache.AddNegative(sepActual, comps[i])
// log.Printf("detK REJECTING %v: couldn't decompose %v \n",
// Graph{Edges: sepActual}, comps[i])
// log.Printf("\n\nCurrent oldSep: %v\n", PrintVertices(oldSep))
// log.Printf("Current SubGraph: %v ( %v edges)\n", H, H.Edges.Len(), H.Edges.Hash())
// lib.Graph{Edges: sepActual}, comps[i])
// log.Printf("\n\nCurrent oldSep: %v\n", lib.PrintVertices(oldSep))
// log.Printf("Current SubGraph: %v ( %v edges) %v\n", H, H.Edges.Len(), H.Edges.Hash())

if d.SubEdge {
if sepSub == nil {
Expand All @@ -209,8 +211,8 @@ OUTER:
}
}
}
// log.Printf("Sub Sep chosen: %vof %v \n", Graph{Edges: sepActual},
// Graph{Edges: sepActualOrigin})
// log.Printf("Sub Sep chosen: %vof %v \n", lib.Graph{Edges: sepActual},
// lib.Graph{Edges: sepActualOrigin})
continue subEdges
}

Expand Down
2 changes: 2 additions & 0 deletions algorithms/logKDecomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type decompInt struct {

// SetWidth sets the current width parameter of the algorithm
func (l *LogKDecomp) SetWidth(K int) {
l.cache.Reset() // reset the cache as the new width might invalidate any old results

l.K = K
}

Expand Down
2 changes: 2 additions & 0 deletions algorithms/logkHybrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ func (l *LogKHybrid) ETimesKDivAvgEdgePred(H lib.Graph, K int) bool {

// SetWidth sets the current width parameter of the algorithm
func (l *LogKHybrid) SetWidth(K int) {
l.cache.Reset() // reset the cache as the new width might invalidate any old results

l.K = K
}

Expand Down
4 changes: 2 additions & 2 deletions balanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// In addition to this, there is also a tool subdirectory in the repository which is intended to support functionality
// not directly related to the computation of decompositions, such as changing the formatting of hypergraphs, or fixing
// a faulty decomposition.

package main

import (
Expand Down Expand Up @@ -412,7 +411,8 @@ func main() {

if *globalBal {
global := &algo.BalSepGlobal{
K: *width, Graph: parsedGraph,
K: *width,
Graph: parsedGraph,
BalFactor: BalFactor,
}
solver = global
Expand Down
3 changes: 1 addition & 2 deletions lib/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This package provides various functions, data structures and methods to aid in the design of algorithms to
// compute structural decomposition methodds.

// compute structural decomposition methods.
package lib

import (
Expand Down
16 changes: 14 additions & 2 deletions lib/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func (c *Cache) CopyRef(other *Cache) {
other.once.Do(func() {}) // if cache is copied, it's assumed to already be initialised, so once is pre-fired here
}

// Reset will throw out all saved cache entries
func (c *Cache) Reset() {
if c.cacheMux == nil {
return // don't do anything if cache wasn't initialised yet
}
c.cacheMux.Lock()
defer c.cacheMux.Unlock()

c.cache = make(map[uint64]*compCache)

}

// Init needs to be called to initialise the cache
func (c *Cache) Init() {
c.once.Do(c.initFunction)
Expand All @@ -45,8 +57,8 @@ func (c *Cache) initFunction() {

// Len returns the number of bindings in the cache
func (c *Cache) Len() int {
c.cacheMux.Lock()
defer c.cacheMux.Unlock()
c.cacheMux.RLock()
defer c.cacheMux.RUnlock()

return len(c.cache)
}
Expand Down
22 changes: 22 additions & 0 deletions lib/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"reflect"
"sort"
"strconv"
"sync"
)

Expand Down Expand Up @@ -295,3 +296,24 @@ func (e *Edges) Diff(other Edges) Edges {

return NewEdges(output)
}

// FullString always prints the list of vertices of an edge, even if the edge is named
func (e Edge) FullStringInt() string {
var buffer bytes.Buffer
mutex.RLock()
defer mutex.RUnlock()
if e.Name > 0 {
buffer.WriteString("E" + strconv.Itoa(e.Name))
}
buffer.WriteString(" (")
for i, n := range e.Vertices {
var s string
s = fmt.Sprintf("V%v", n)
buffer.WriteString(s)
if i != len(e.Vertices)-1 {
buffer.WriteString(", ")
}
}
buffer.WriteString(")")
return buffer.String()
}
15 changes: 15 additions & 0 deletions lib/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,18 @@ func (g Graph) GetBIP() int {

return output
}

//ToHyperBench format transforms the graph structure to a string in HyperBench Format. This is only relevant for generated instances with no existing string representation. Using this with a parsed graph is not the target use case, only used for internal testing
func (g Graph) ToHyberBenchFormat() string {
var buffer bytes.Buffer

for i, e := range g.Edges.Slice() {
buffer.WriteString(e.FullStringInt())
if i != g.Edges.Len()-1 {
buffer.WriteString(",\n")
}
}

buffer.WriteString(".")
return buffer.String()
}
4 changes: 2 additions & 2 deletions lib/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type parseEdgePACE struct {
}

type parseGraphPACEInfo struct {
Vertices int `"p htd":Begin @(Number) `
Vertices int `"p htd" @(Number) `
Edges int `@(Number) "\n"`
}

Expand All @@ -149,7 +149,7 @@ func GetGraphPACE(s string) Graph {
Comment = ("c" | "//") { "\u0000"…"\uffff"-"\n" } Newline.
Begin = "p htd" .
Number = ("." | digit | "_"){"." | digit | stuff } .
Whitespace = " " | "\t" | "\n" | "\r" .
Whitespace = " " | "\t" | "\r" .
stuff = ":" | "@" | ";" | "-" | "_" .
Punct = "!"…"/" .
Newline = "\n" .
Expand Down
Loading

0 comments on commit 99a3c28

Please sign in to comment.