Skip to content

Commit

Permalink
First test-ready version of LogK
Browse files Browse the repository at this point in the history
  • Loading branch information
cem-okulmus committed Nov 5, 2020
1 parent 5ee77cd commit 22eb62a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 185 deletions.
208 changes: 28 additions & 180 deletions algorithms/logKDecomp.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (l LogKDecomp) baseCaseCheck(lenE int, lenSp int, lenAE int) bool {
}

func (l LogKDecomp) baseCase(H Graph, Sp []Special, lenAE int) Decomp {
log.Printf("Base case reached. Number of Special Edges %d\n", len(Sp))
// log.Printf("Base case reached. Number of Special Edges %d\n", len(Sp))
var output Decomp

// cover faiure cases
Expand Down Expand Up @@ -130,8 +130,8 @@ func (l LogKDecomp) baseCase(H Graph, Sp []Special, lenAE int) Decomp {

//attach the two subtrees to form one
func (l LogKDecomp) attachingSubtrees(subtreeAbove Node, subtreeBelow Node, connecting Special) Node {
log.Println("Two Nodes enter: ", subtreeAbove, subtreeBelow)
log.Println("Connecting: ", PrintVertices(connecting.Vertices))
// log.Println("Two Nodes enter: ", subtreeAbove, subtreeBelow)
// log.Println("Connecting: ", PrintVertices(connecting.Vertices))
// up = Inter(up, parent.Vertices())
//finding connecting leaf in parent
leaf := subtreeAbove.CombineNodes(subtreeBelow, connecting)
Expand All @@ -146,10 +146,11 @@ func (l LogKDecomp) attachingSubtrees(subtreeAbove Node, subtreeBelow Node, conn
// //attaching subtree to parent
// leaf.Children = []Node{subtree}
// log.Println("Leaf ", leaf)
log.Println("One Node leaves: ", leaf)
// log.Println("One Node leaves: ", leaf)
return *leaf
}

// sequential version, for developement purposes only
func (l LogKDecomp) findHD(H Graph, Sp []Special, Conn []int, allowedFull Edges) Decomp {

log.Printf("\n\nCurrent SubGraph: %v\n", H)
Expand Down Expand Up @@ -248,36 +249,6 @@ CHILD:
return Decomp{Graph: H, Root: root}
}

// //Connectivity Check Parent
// vertCompLow := append(comp_low.Vertices(), VerticesSpecial(compSp_low)...)
// vertCompLowProper := Diff(vertCompLow, parentλ.Vertices())

// if !Subset(Inter(vertCompLow, Conn), parentλ.Vertices()) {
// log.Println("Parent breaks connectivity wrt. comp_low", len(Inter(parentλ.Vertices(), Conn)))
// continue PARENT
// }

// log.Printf("Parent cover chosen: %v\n", Graph{Edges: parentλ})

// log.Printf("Comps of Parent: %v\n", comps_p)

// log.Println("Lower comp: \n", comp_low)

// Conn_child := Inter(vertCompLow, parentλ.Vertices())
// bound := FilterVertices(allowed, Conn_child)

// genChildCover := NewCover(l.K, Inter(vertCompLow, parentλ.Vertices()), bound, vertCompLow)

// allowedInsideComplow := FilterVertices(allowed, vertCompLowProper)

//Parent loops checks all possible parents that fit the chosen child node

// Set up iterator for parent

// parentMap := CreateOrderingMap(allowed, append(childλ.Vertices(), Conn...))

// allowedParent := FilterVertices(allowed, append(childλ.Vertices(), Conn...))

genParent := GetCombin(allowed.Len(), l.K)

PARENT:
Expand All @@ -288,71 +259,6 @@ CHILD:
// parentλ := GetSubset(allowedParent, genParent.Combination)
genParent.Confirm()

// var tryPartAlone bool

// out := genChildCover.NextSubset()

// if out == -1 {
// if genChildCover.HasNext {
// log.Panicln(" -1 but hasNext not false!")
// }
// continue CHILD_OUTER
// }

// childPart := GetSubset(bound, genChildCover.Subset)
// if len(Inter(childPart.Vertices(), vertCompLowProper)) > 0 {
// tryPartAlone = true
// }

// remainingAllowed := allowedInsideComplow.Diff(childPart)

// genChild := GetCombin(remainingAllowed.Len(), l.K-childPart.Len())
// CHILD_INNER:
// for genChild.HasNext() || tryPartAlone {
// if !genChild.HasNext() { // run once on just childPart if it extends into comp
// tryPartAlone = false
// }

// var childλ Edges
// if !genChild.HasNext() {
// childλ = childPart
// } else {
// childAdded := GetSubset(remainingAllowed, genChild.Combination)
// childλ = NewEdges(append(childAdded.Slice(), childPart.Slice()...))
// }
// genChild.Confirm()

// childχ := Inter(childλ.Vertices(), vertCompLow)

// // Connectivity check
// if !Subset(Inter(vertCompLow, parentλ.Vertices()), childχ) {
// // log.Println("Child ", childλ, " breaks connectivity!")
// log.Println("Parent lambda: ", PrintVertices(parentλ.Vertices()))
// log.Println("Child lambda: ", PrintVertices(childλ.Vertices()))

// log.Println("Child_part ", childPart)

// log.Println("Child Full ", childλ)

// log.Panicln("new loop not working, connectivity broken")
// continue CHILD_INNER
// }

// // Check if progress made (TODO: implement this properly with minimal covers + extensions !)
// if Subset(childχ, parentλ.Vertices()) {
// // log.Println("No progress made!")

// log.Println("Parent lambda: ", PrintVertices(parentλ.Vertices()))
// log.Println("Child lambda: ", PrintVertices(childλ.Vertices()))

// log.Println("Child_part ", childPart)

// log.Println("Child Full ", childλ)

// log.Panicln("New loop not working, no progress made")
// continue CHILD_INNER
// }

comps_p, compsSp_p, _, isolatedEdges := H.GetComponents(parentλ, Sp)

foundLow := false
Expand Down Expand Up @@ -424,16 +330,6 @@ CHILD:
}
}

// log.Println("Size of H' : ", H.Edges.Len()+len(Sp))

// // Check childχ is balanced separator
// for i := range comps_c {
// if comps_c[i].Edges.Len()+len(compsSp_c[i]) > (H.Edges.Len()+len(Sp))/2 {
// log.Println("Child is not a bal. sep. !")
// continue CHILD_INNER
// }
// }

log.Printf("Parent Found: %v (%s) \n", Graph{Edges: parentλ}, PrintVertices(parentλ.Vertices()))

log.Printf("Child chosen: %v (%s) \n", Graph{Edges: childλ}, PrintVertices(childχ))
Expand Down Expand Up @@ -539,10 +435,10 @@ CHILD:
// a parallel version of logK, using goroutines and channels to run various parts concurrently
func (l LogKDecomp) findHDParallel(H Graph, Sp []Special, Conn []int, allowedFull Edges) Decomp {

log.Printf("\n\nCurrent SubGraph: %v\n", H)
log.Printf("Current Allowed Edges: %v\n", allowedFull)
log.Printf("Current Special Edges: %v\n", Sp)
log.Println("Conn: ", PrintVertices(Conn), "\n\n")
// log.Printf("\n\nCurrent SubGraph: %v\n", H)
// log.Printf("Current Allowed Edges: %v\n", allowedFull)
// log.Printf("Current Special Edges: %v\n", Sp)
// log.Println("Conn: ", PrintVertices(Conn), "\n\n")

// Base Case
if l.baseCaseCheck(H.Edges.Len(), len(Sp), allowedFull.Len()) {
Expand All @@ -563,45 +459,21 @@ func (l LogKDecomp) findHDParallel(H Graph, Sp []Special, Conn []int, allowedFul

parallelSearch.FindNext(pred) // initial Search

// genChild := GetCombin(allowed.Len(), l.K)

// checks all possibles nodes in H, together with PARENT loops, it covers all parent-child pairings
CHILD:
for ; !parallelSearch.ExhaustedSearch; parallelSearch.FindNext(pred) {

// var found []int

// parallelSearch(H, Sp, allowed, &found, genChild, l.BalFactor)

// if len(found) == 0 { // meaning that the search above never found anything
// log.Printf("REJECT: Couldn't find balsep for H %v SP %v\n", H, Sp)
// genChild_hasNext = false
// continue
// }

childλ := GetSubset(allowed, parallelSearch.Result)

comps_c, compsSp_c, _, _ := H.GetComponents(childλ, Sp)

// // Check if child is balanced
// for i := range comps_c {
// if comps_c[i].Edges.Len()+len(compsSp_c[i]) > (H.Edges.Len()+len(Sp))/2 {
// continue CHILD
// // lowFound = true
// // comp_low_index = i //keep track of the index for composing comp_up later
// // comp_low = comps_p[i]
// // compSp_low = compsSp_p[i]
// }

// }

log.Println("Balanced Child found, ", childλ)
// log.Println("Balanced Child found, ", childλ)

// Check if child is possible root
if Subset(Conn, childλ.Vertices()) {

log.Printf("Child-Root cover chosen: %v\n", Graph{Edges: childλ})
log.Printf("Comps of Child-Root: %v\n", comps_c)
// log.Printf("Child-Root cover chosen: %v\n", Graph{Edges: childλ})
// log.Printf("Comps of Child-Root: %v\n", comps_c)

childχ := Inter(childλ.Vertices(), Vertices_H)

Expand All @@ -618,7 +490,7 @@ CHILD:
} else {
for j := range comps_c {
if l.checkNegative(childχ, comps_c[j], Sp) { //TODO: Add positive check and cutNodes
log.Println("Skipping a child sep", childχ)
// log.Println("Skipping a child sep", childχ)
continue CHILD
}

Expand All @@ -632,11 +504,11 @@ CHILD:

decomp := l.findHDParallel(comps_c[y], compsSp_c[y], Conn_y, allowedFull)
if reflect.DeepEqual(decomp, Decomp{}) {
log.Println("Rejecting child-root")
log.Printf("\nCurrent SubGraph: %v\n", H)
log.Printf("Current Allowed Edges: %v\n", allowed)
log.Printf("Current Special Edges: %v\n", Sp)
log.Println("Conn: ", PrintVertices(Conn), "\n\n")
// log.Println("Rejecting child-root")
// log.Printf("\nCurrent SubGraph: %v\n", H)
// log.Printf("Current Allowed Edges: %v\n", allowed)
// log.Printf("Current Special Edges: %v\n", Sp)
// log.Println("Conn: ", PrintVertices(Conn), "\n\n")
l.addNegative(childχ, comps_c[y], compsSp_c[y])
continue CHILD
}
Expand All @@ -649,8 +521,6 @@ CHILD:
return Decomp{Graph: H, Root: root}
}

// genParent := GetCombin(allowed.Len(), l.K)

genParent := SplitCombin(allowed.Len(), l.K, runtime.GOMAXPROCS(-1), false)

parentalSearch := Search{H: &H, Sp: Sp, Edges: &allowed, BalFactor: l.BalFactor, Generators: genParent}
Expand All @@ -662,21 +532,9 @@ CHILD:
PARENT:
for ; !parentalSearch.ExhaustedSearch; parentalSearch.FindNext(predPar) {

// var foundParent []int

// parallelSearch(H, Sp, allowed, &foundParent, genParent, l.BalFactor)

// if len(foundParent) == 0 { // meaning that the search above never found anything
// log.Printf("REJECT: Couldn't find balsep for H %v SP %v\n", H, Sp)
// genChild_hasNext = false
// continue
// }

// parentλ := GetSubsetMap(allowed, genParent.Combination, parentMap)
parentλ := GetSubset(allowed, parentalSearch.Result)
// parentλ := GetSubset(allowedParent, genParent.Combination)

log.Println("Looking at parent ", parentλ)
// log.Println("Looking at parent ", parentλ)

comps_p, compsSp_p, _, isolatedEdges := H.GetComponents(parentλ, Sp)

Expand Down Expand Up @@ -727,20 +585,10 @@ CHILD:
}
}

// log.Println("Size of H' : ", H.Edges.Len()+len(Sp))

// // Check childχ is balanced separator
// for i := range comps_c {
// if comps_c[i].Edges.Len()+len(compsSp_c[i]) > (H.Edges.Len()+len(Sp))/2 {
// log.Println("Child is not a bal. sep. !")
// continue CHILD_INNER
// }
// }

log.Printf("Parent Found: %v (%s) \n", Graph{Edges: parentλ}, PrintVertices(parentλ.Vertices()))
// log.Printf("Parent Found: %v (%s) \n", Graph{Edges: parentλ}, PrintVertices(parentλ.Vertices()))

log.Printf("Child chosen: %v (%s) \n", Graph{Edges: childλ}, PrintVertices(childχ))
log.Printf("Comps of Child: %v\n", comps_c)
// log.Printf("Child chosen: %v (%s) \n", Graph{Edges: childλ}, PrintVertices(childχ))
// log.Printf("Comps of Child: %v\n", comps_c)

//Computing subcomponents of Child

Expand All @@ -751,11 +599,11 @@ CHILD:
decomp := l.findHDParallel(comps_c[x], compsSp_c[x], Conn_x, allowedFull)
if reflect.DeepEqual(decomp, Decomp{}) {
l.addNegative(childχ, comps_c[x], compsSp_c[x])
log.Println("Rejecting child")
// log.Println("Rejecting child")
continue PARENT
}

log.Printf("Produced Decomp: %+v\n", decomp)
// log.Printf("Produced Decomp: %+v\n", decomp)
subtrees = append(subtrees, decomp.Root)

}
Expand Down Expand Up @@ -797,7 +645,7 @@ CHILD:

comp_up.Edges = NewEdges(tempEdgeSlice)

log.Println("Upper component:", comp_up)
// log.Println("Upper component:", comp_up)

//Reducing the allowed edges
allowedReduced := allowedFull.Diff(comp_low.Edges)
Expand All @@ -810,7 +658,7 @@ CHILD:
if reflect.DeepEqual(decompUp, Decomp{}) {

// l.addNegative(childχ, comp_up, Sp)
log.Println("Rejecting comp_up")
// log.Println("Rejecting comp_up")

continue PARENT
}
Expand All @@ -827,11 +675,11 @@ CHILD:
finalRoot = rootChild
}

log.Printf("Produced Decomp: %v\n", finalRoot)
// log.Printf("Produced Decomp: %v\n", finalRoot)
return Decomp{Graph: H, Root: finalRoot}

}
log.Println("ran out of parents", parentalSearch.ExhaustedSearch, "\n\n")
// log.Println("ran out of parents", parentalSearch.ExhaustedSearch, "\n\n")
}

// exhausted search space
Expand Down
9 changes: 4 additions & 5 deletions lib/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package lib

import (
"log"
"runtime"
"sync"
)
Expand All @@ -29,7 +28,7 @@ func (s *Search) FindNext(pred Predicate) {
}
}()

log.Println("starting search")
// log.Println("starting search")

s.Result = []int{} // reset result

Expand Down Expand Up @@ -63,7 +62,7 @@ func (s *Search) FindNext(pred Predicate) {
func (s Search) Worker(workernum int, found chan []int, wg *sync.WaitGroup, finished *bool, pred Predicate) {
defer func() {
if r := recover(); r != nil {
log.Printf("Worker %d 'forced' to quit, reason: %v", workernum, r)
// log.Printf("Worker %d 'forced' to quit, reason: %v", workernum, r)
return
}
}()
Expand All @@ -73,7 +72,7 @@ func (s Search) Worker(workernum int, found chan []int, wg *sync.WaitGroup, fini

for gen.HasNext() {
if *finished {
log.Printf("Worker %d told to quit", workernum)
// log.Printf("Worker %d told to quit", workernum)
return
}
j := gen.Combination
Expand All @@ -82,7 +81,7 @@ func (s Search) Worker(workernum int, found chan []int, wg *sync.WaitGroup, fini
if gen.BalSep || pred.Check(s.H, s.Sp, &sep, s.BalFactor) {
gen.BalSep = true // cache result
found <- j
log.Printf("Worker %d \" won \"", workernum)
// log.Printf("Worker %d \" won \"", workernum)
gen.Confirm()
*finished = true
return
Expand Down

0 comments on commit 22eb62a

Please sign in to comment.