-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctlearn.go
143 lines (127 loc) · 3.83 KB
/
ctlearn.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"time"
"github.com/britojr/lkbn/data"
"github.com/britojr/lkbn/emlearner"
"github.com/britojr/lkbn/learner"
"github.com/britojr/lkbn/model"
"github.com/britojr/lkbn/scores"
"github.com/britojr/utl/ioutl"
)
func runCTLearnComm() {
// Required Flags
if dataFile == "" {
fmt.Printf("\n error: missing dataset file\n\n")
ctLearnComm.PrintDefaults()
os.Exit(1)
}
if !verbose {
log.SetOutput(ioutil.Discard)
}
runCTLearner()
}
func runCTParamLearnComm() {
// Required Flags
if dataFile == "" {
fmt.Printf("\n error: missing dataset file\n\n")
ctParamLearnComm.PrintDefaults()
os.Exit(1)
}
if modelFIn == "" {
fmt.Printf("\n error: missing model structure file\n\n")
ctParamLearnComm.PrintDefaults()
os.Exit(1)
}
if !verbose {
log.SetOutput(ioutil.Discard)
}
runCTParamLearner()
}
func runCTLearner() {
log.Printf("=========== BEGIN MODEL LEARNING =================\n")
log.Printf("Dataset file: '%v'\n", dataFile)
log.Printf("Learning algorithm: '%v'\n", learnerAlg)
log.Printf("Max. iterations: %v\n", numSolutions)
log.Printf("Max. time available (sec): %v\n", timeAvailable)
log.Printf("Parameters file: '%v'\n", parmFile)
log.Printf("Save solution in: '%v'\n", modelFOut)
log.Printf("--------------------------------------------------\n")
props := make(map[string]string)
if len(parmFile) > 0 {
log.Println("Reading parameters file")
props = ioutl.ReadYaml(parmFile)
}
if len(scoreFile) > 0 {
props[learner.ParmParentScores] = scoreFile
}
if len(modelFIn) > 0 {
props[learner.ParmInputNet] = modelFIn
}
log.Println("Reading dataset file")
dataSet := data.NewDataset(dataFile, "")
log.Println("Initializing learning algorithm")
alg := learner.Create(learnerAlg)
alg.SetDataset(dataSet)
alg.SetFileParameters(props)
alg.ValidateParameters()
alg.PrintParameters()
log.Println("Searching structure")
start := time.Now()
m, it := learner.Search(alg, numSolutions, timeAvailable)
elapsed := time.Since(start)
log.Printf("========== SOLUTION ==============================\n")
if m == nil {
log.Printf("Couldn't find any solution in the given time!\n")
os.Exit(0)
}
log.Printf("Time: %v\n", elapsed)
log.Printf("Iterations: %v\n", it)
log.Printf("LogLikelihood: %.6f\n", m.Score())
log.Printf("--------------------------------------------------\n")
if len(modelFOut) > 0 {
writeSolution(modelFOut, m.(model.Model), alg)
}
}
func runCTParamLearner() {
log.Printf("=========== BEGIN MODEL LEARNING =================\n")
log.Printf("Dataset file: '%v'\n", dataFile)
log.Printf("Parameters file: '%v'\n", parmFile)
log.Printf("Read structure from: '%v'\n", modelFIn)
log.Printf("Save solution in: '%v'\n", modelFOut)
log.Printf("--------------------------------------------------\n")
var props map[string]string
if len(parmFile) > 0 {
log.Println("Reading parameters file")
props = ioutl.ReadYaml(parmFile)
}
log.Println("Reading dataset file")
dataSet := data.NewDataset(dataFile, "")
log.Println("Reading model structure")
ct := model.ReadCTreeXML(modelFIn)
log.Println("Initializong parameter learner")
eml := emlearner.New()
eml.SetProperties(props)
eml.PrintProperties()
log.Println("Learning parameters")
start := time.Now()
m, ll, it := eml.Run(ct, dataSet.IntMaps())
elapsed := time.Since(start)
m.SetBIC(scores.ComputeBIC(m, dataSet.IntMaps()))
log.Printf("========== SOLUTION ==============================\n")
log.Printf("Time: %v\n", elapsed)
log.Printf("Iterations: %v\n", it)
log.Printf("LogLikelihood: %.6f\n", ll)
log.Printf("BIC: %.6f\n", m.BIC())
log.Printf("--------------------------------------------------\n")
if len(modelFOut) > 0 {
writeSolution(modelFOut, m, nil)
}
}
func writeSolution(fname string, m model.Model, alg learner.Learner) {
log.Printf("Printing solution: '%v'\n", fname)
m.Write(fname)
}