-
Notifications
You must be signed in to change notification settings - Fork 0
/
Callidus.go
301 lines (261 loc) · 7.57 KB
/
Callidus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
. "../Callidus/computation"
. "../Callidus/constraint"
. "../Callidus/hyperTree"
. "../Callidus/pre-processing"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
)
var contSols int
func main() {
contSols = 0
if len(os.Args) == 1 {
panic("The first parameter must be an xml file or lzma file")
}
args := os.Args[1:]
filePath := args[0]
if !strings.HasSuffix(filePath, ".xml") && !strings.HasSuffix(filePath, ".lzma") {
panic("The first parameter must be an xml file or lzma file")
}
SystemSettings.InitSettings(args, filePath)
fmt.Println("Start Callidus")
start := time.Now()
fmt.Println("creating hypergraph")
startTranslation := time.Now()
HypergraphTranslation(filePath)
fmt.Println("hypergraph created in ", time.Since(startTranslation))
hyperTreeRaw := ""
if SystemSettings.HypertreeFile == "output"+SystemSettings.FolderName+"hypertree" {
fmt.Println("decomposing hypertree")
startDecomposition := time.Now()
hyperTreeRaw = HypertreeDecomposition(filePath, "output"+SystemSettings.FolderName, SystemSettings.InMemory)
fmt.Println("hypertree decomposed in ", time.Since(startDecomposition))
}
var wg sync.WaitGroup
wg.Add(3)
fmt.Println("parsing hypertree, domain and constraints")
startPrep := time.Now()
var nodes []*Node
var root *Node
go func() {
if SystemSettings.InMemory {
root, nodes = GetHyperTreeInMemory(&hyperTreeRaw)
} else {
root, nodes = GetHyperTree()
}
wg.Done()
}()
var domains map[string][]int
go func() {
domains = GetDomains(filePath)
wg.Done()
}()
var constraints []*Constraint
go func() {
constraints = GetConstraints(filePath, "output"+SystemSettings.FolderName)
wg.Done()
}()
wg.Wait()
fmt.Println("hypertree, domain and constraints parsed in ", time.Since(startPrep))
if !SystemSettings.Debug {
err := os.RemoveAll("output" + SystemSettings.FolderName)
if err != nil {
panic(err)
}
}
fmt.Println("starting sub csp computation")
//go func() {
// for {
// PrintMemUsage()
// time.Sleep(5 * time.Second)
// }
//}()
startSubComputation := time.Now()
satisfiable := SubCSP_Computation(domains, constraints, nodes)
fmt.Println("sub csp computed in ", time.Since(startSubComputation).Seconds())
if !satisfiable {
fmt.Println("NO SOLUTIONS")
return
}
if !SystemSettings.Debug {
err := os.RemoveAll("subCSP-" + SystemSettings.FolderName)
if err != nil {
panic(err)
}
}
fmt.Println("starting yannakaki")
startYannakaki := time.Now()
Yannakaki(root)
fmt.Println("yannakaki finished in ", time.Since(startYannakaki))
fmt.Println("ended in ", time.Since(start))
finalResult := make([]map[string]int, 0)
startSearchResult := time.Now()
searchResults(root, &finalResult)
fmt.Println("search results ended in ", time.Since(startSearchResult))
if SystemSettings.PrintSol {
printSolution(&finalResult)
}
if !SystemSettings.Debug {
err := os.RemoveAll("tables-" + SystemSettings.FolderName)
if err != nil {
panic(err)
}
}
}
func printSolution(result *[]map[string]int) {
if len(*result) > 0 {
if SystemSettings.Output == "" {
for indexResult, res := range *result {
fmt.Print("Sol " + strconv.Itoa(indexResult+1) + "\n")
for key, value := range res {
fmt.Print(key + " -> " + strconv.Itoa(value) + "\n")
}
}
fmt.Print("Solutions found: " + strconv.Itoa(len(*result)) + "\n")
} else {
err := os.RemoveAll(SystemSettings.Output)
if err != nil {
panic(err)
}
file, err := os.OpenFile(SystemSettings.Output, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0777)
if err != nil {
panic(err)
}
for indexResult, res := range *result {
_, err = file.WriteString("Sol " + strconv.Itoa(indexResult+1) + "\n")
if err != nil {
panic(err)
}
for key, value := range res {
_, err = file.WriteString(key + " -> " + strconv.Itoa(value) + "\n")
if err != nil {
panic(err)
}
}
}
_, err = file.WriteString("Solutions found: " + strconv.Itoa(len(*result)))
if err != nil {
panic(err)
}
}
} else {
fmt.Println("NO SOLUTIONS")
}
}
func searchResults(actual *Node, finalResults *[]map[string]int) {
joinVariables := make(map[string]int, 0)
if actual.Father != nil {
for _, varFather := range actual.Father.Variables {
for index, varActual := range actual.Variables {
if varActual == varFather {
joinVariables[varActual] = index
break
}
}
}
}
addNewResults := false
newResults := make([]map[string]int, 0)
if SystemSettings.InMemory {
addNewResults, newResults = searchNewResultsInMemory(actual, &joinVariables, finalResults)
} else {
addNewResults, newResults = searchNewResultsOnFile(actual, &joinVariables, finalResults)
if newResults == nil {
*finalResults = nil
return
}
}
if addNewResults {
for _, singleNewResult := range newResults {
*finalResults = append(*finalResults, singleNewResult)
}
}
//fmt.Println(len(*finalResults))
for _, son := range actual.Sons {
searchResults(son, finalResults)
}
}
func searchNewResultsInMemory(actual *Node, joinVariables *map[string]int, finalResults *[]map[string]int) (bool, []map[string]int) {
joinDoneCount := make(map[string]int)
newResults := make([]map[string]int, 0)
addNewResults := false
for _, singleNodeSolution := range actual.PossibleValues {
computationNewResults(actual, singleNodeSolution, joinVariables, &joinDoneCount, finalResults, &addNewResults, &newResults)
}
return addNewResults, newResults
}
func searchNewResultsOnFile(actual *Node, joinVariables *map[string]int, finalResults *[]map[string]int) (bool, []map[string]int) {
joinDoneCount := make(map[string]int)
newResults := make([]map[string]int, 0)
addNewResults := false
fileActual, rActual := OpenNodeFile(actual.Id)
for rActual.Scan() {
singleNodeSolution := GetValues(rActual.Text(), len(actual.Variables))
if singleNodeSolution == nil {
break
}
if singleNodeSolution[0] == -1 {
return false, nil
}
computationNewResults(actual, singleNodeSolution, joinVariables, &joinDoneCount, finalResults, &addNewResults, &newResults)
}
fileActual.Close()
return addNewResults, newResults
}
func computationNewResults(actual *Node, singleNodeSolution []int, joinVariables *map[string]int, joinDoneCount *map[string]int, finalResults *[]map[string]int, addNewResults *bool, newResults *[]map[string]int) {
if singleNodeSolution == nil {
return
}
keyJoin := ""
for index, value := range singleNodeSolution {
_, isVariableJoin := (*joinVariables)[actual.Variables[index]]
if isVariableJoin {
keyJoin += strconv.Itoa(value)
}
}
_, alreadyInMap := (*joinDoneCount)[keyJoin]
if alreadyInMap {
(*joinDoneCount)[keyJoin]++
} else {
(*joinDoneCount)[keyJoin] = 1
}
if len(*joinVariables) >= 1 {
for _, singleFinalResult := range *finalResults {
joinOk := true
for joinKey, joinIndex := range *joinVariables {
if singleFinalResult[joinKey] != singleNodeSolution[joinIndex] {
joinOk = false
break
}
}
if joinOk {
if (*joinDoneCount)[keyJoin] == 1 {
for index, value := range singleNodeSolution {
singleFinalResult[actual.Variables[index]] = value
}
} else {
*addNewResults = true
copyRes := make(map[string]int, 0)
for key, val := range singleFinalResult {
copyRes[key] = val
}
for index, value := range singleNodeSolution {
copyRes[actual.Variables[index]] = value
}
*newResults = append(*newResults, copyRes)
}
}
}
} else {
resTemp := make(map[string]int)
for index, value := range singleNodeSolution {
resTemp[actual.Variables[index]] = value
}
*finalResults = append(*finalResults, resTemp)
}
}