Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/src/go-pge/pge.go @ 17740

Last change on this file since 17740 was 16666, checked in by hmaislin, 6 years ago

#2929: Modified make / folder structure to build original app, added x86 dll

File size: 6.6 KB
Line 
1package main
2
3import "C"
4import (
5  pge "go-pge/pge"
6  probs "go-pge/problems"
7  rand "math/rand"
8  rt "runtime"
9  "sort"
10  "strings"
11  "unsafe"
12)
13
14func main() {
15
16}
17
18var cps *pge.PgeSearch
19var tp *probs.TreeParams
20var epcomm *probs.ExprProblemComm
21var trainDatas []*probs.PointSet
22var testDatas []*probs.PointSet
23var testDatasN int
24var trainDatasN int
25var ep *probs.ExprProblem
26var per_eqns []*probs.ExprReportArray
27
28var trainData *probs.PointSet
29var testData *probs.PointSet
30
31var probIndepNames []string
32var probDepndNames []string
33
34var stepResult probs.ExprReportArray
35var lastStepRes *probs.ExprReport
36var resultNr int
37
38var nCurrentCoeff int
39var nMaxCoeff int
40var sortTypeNr int
41var nLastResults int
42
43//export InitSearch
44func InitSearch(maxGen int, pgeRptEpoch int, pgeRptCount int, pgeArchiveCap int, peelCnt int, evalrCount int, zeroEpsilon float64, initMethod *C.char, growMethod *C.char, sortType int) {
45  cps = new(pge.PgeSearch)
46
47  goInitMethod := C.GoString(initMethod)
48  goGrowMethod := C.GoString(growMethod)
49
50  testDatasN = 0
51  trainDatasN = 0
52  nLastResults = 0
53  sortTypeNr = sortType
54
55  cps.CreateDS(maxGen, pgeRptEpoch, pgeRptCount, pgeArchiveCap, peelCnt, evalrCount, zeroEpsilon, goInitMethod, goGrowMethod, sortType)
56
57  cps.SetPeelCount(peelCnt)
58  cps.SetGrowMethod(goGrowMethod)
59  cps.SetInitMethod(goInitMethod)
60  cps.SetEvalrCount(evalrCount)
61}
62
63//export InitTreeParams
64func InitTreeParams(Roots *C.char, Nodes *C.char, NonTrig *C.char, Leafs *C.char, nUseableVars int, MaxSize int, MinSize int, MaxDepth int, MinDepth int) {
65  tp = new(probs.TreeParams)
66  goRoots := C.GoString(Roots)
67  goNodes := C.GoString(Nodes)
68  goNonTrig := C.GoString(NonTrig)
69  goLeafs := C.GoString(Leafs)
70  goUsableVars := CreateUsableVars(nUseableVars)
71  tp.CreateTreeParams(goRoots, goNodes, goNonTrig, goLeafs, goUsableVars, MaxSize, MinSize, MaxDepth, MinDepth)
72}
73
74//export AddTrainData
75func AddTrainData(indepNames *C.char, depndNames *C.char, matrix unsafe.Pointer, nEntries int) {
76  trainDatas = make([]*probs.PointSet, 1)
77  trainData = new(probs.PointSet)
78
79  goIndepNames := strings.Split(C.GoString(indepNames), " ")
80  goDepndNames := strings.Split(C.GoString(depndNames), " ")
81
82  probIndepNames = goIndepNames
83  probDepndNames = goDepndNames
84
85  trainData.InitTrainData(goIndepNames, goDepndNames, matrix, nEntries)
86
87  trainDatas[trainDatasN] = trainData
88  trainDatasN++
89}
90
91//export AddTestData
92func AddTestData(indepNames *C.char, depndNames *C.char, matrix unsafe.Pointer, nEntries int) {
93  testDatas = make([]*probs.PointSet, 1)
94  testData = new(probs.PointSet)
95
96  goIndepNames := strings.Split(C.GoString(indepNames), " ")
97  goDepndNames := strings.Split(C.GoString(depndNames), " ")
98
99  testData.InitTrainData(goIndepNames, goDepndNames, matrix, nEntries)
100
101  testDatas[testDatasN] = testData
102  testDatasN++
103}
104
105//export InitProblem
106func InitProblem(Name *C.char, MaxIter int, HitRatio float64, SearchVar int, ProblemTypeString *C.char, numProcs int) {
107  ep = new(probs.ExprProblem)
108
109  ep.IndepNames = probIndepNames
110  ep.DepndNames = probDepndNames
111
112  goName := C.GoString(Name)
113  goProblemTypeString := C.GoString(ProblemTypeString)
114
115  ep.CreatePS(goName, MaxIter, HitRatio, SearchVar, goProblemTypeString)
116  ep.TreeCfg = tp
117  ep.UsableVars = tp.UsableVars
118
119  cps.SetMaxIter(MaxIter)
120  per_eqns = make([]*probs.ExprReportArray, 1)
121
122  ep.Train = trainDatas
123  ep.Test = testDatas
124
125  initDone := make(chan int)
126
127  rt.GOMAXPROCS(numProcs)
128  rand.Seed(rand.Int63())
129
130  epcomm = new(probs.ExprProblemComm)
131
132  epcomm.Cmds = make(chan int)
133  epcomm.Rpts = make(chan *probs.ExprReportArray, 64)
134  epcomm.Res = make(chan *probs.ExprReportArray, 64) //Stops at 64 if nothing gets retrieved
135  epcomm.Gen = make(chan [2]int, 64)
136
137  cps.SetProb(ep)
138  cps.Init(initDone, ep, "", epcomm)
139  //cps.SetSort(sortTypeNr) really? sorttype is hardcoded in PGE!
140}
141
142//export GetMaxIterW
143func GetMaxIterW() int {
144  return cps.GetMaxIter()
145}
146
147//export SetMaxIterW
148func SetMaxIterW(iter int) {
149  cps.SetMaxIter(iter)
150}
151
152//export SetPeelCountW
153func SetPeelCountW(cnt int) {
154  cps.SetPeelCount(cnt)
155}
156
157//export SetInitMethodW
158func SetInitMethodW(init string) {
159  cps.SetInitMethod(init)
160}
161
162//export SetGrowMethodW
163func SetGrowMethodW(grow string) {
164  cps.SetGrowMethod(grow)
165}
166
167//export SetEvalrCountW
168func SetEvalrCountW(cnt int) {
169  cps.SetEvalrCount(cnt)
170}
171
172//export EvaluateW
173func EvaluateW() {
174  cps.Evaluate()
175}
176
177//export RunW
178func RunW() {
179  cps.Run()
180}
181
182//export LoopW
183func LoopW() {
184  cps.Loop()
185}
186
187//export GetStepResult
188func GetStepResult(testscore *int, ncoeff *int) *C.char {
189  *testscore = 0
190
191  if resultNr < len(stepResult) && stepResult != nil {
192    elem := stepResult[resultNr]
193    resultNr++
194    lastStepRes = elem
195    if elem != nil {
196      *testscore = elem.TestScore()
197      *ncoeff = len(elem.Coeff())
198      nCurrentCoeff = 0
199      nMaxCoeff = len(elem.Coeff())
200      return C.CString(elem.Expr().String())
201    }
202  }
203  return C.CString("")
204}
205
206//export GetCoeffResult
207func GetCoeffResult() float64 {
208  var res float64 = 0
209  if nCurrentCoeff < nMaxCoeff {
210    coeffs := lastStepRes.Coeff()
211    res = coeffs[nCurrentCoeff]
212  }
213  nCurrentCoeff++
214  return res
215}
216
217func accumExprs(eqns *probs.ExprReportArray) probs.ExprReportArray {
218  union := *eqns //make(probs.ExprReportArray, 0)
219  //union = append(union, (*eqns)[:]...)
220
221  // remove duplicates
222  sort.Sort(union)
223  last := 0
224  for last < len(union) && union[last] == nil {
225    last++
226  }
227  for i := last + 1; i < len(union); i++ {
228    if union[i] == nil {
229      continue
230    }
231    if union[i].Expr().AmIAlmostSame(union[last].Expr()) {
232      union[i] = nil
233    } else {
234      last = i
235    }
236  }
237
238  queue := probs.NewQueueFromArray(union)
239  queue.SetSort(probs.GPSORT_PARETO_TST_ERR)
240  queue.Sort()
241
242  ds_eqns := make(probs.ExprReportArray, 20)
243  copy(ds_eqns, union[:20])
244
245  return ds_eqns
246}
247
248//export GetBestResults
249func GetBestResults() int {
250  cps.ReportExpr(true)
251
252  rpt := <-epcomm.Rpts
253  stepResult = accumExprs(rpt)
254  resultNr = 0
255
256  return len(stepResult)
257}
258
259//export StepW
260func StepW() int {
261  resultNr = 0
262  stepResult = nil
263  nRes := cps.Step()
264
265  resu, ok := <-epcomm.Res
266  resultNr = nLastResults
267
268  nNew := 0
269  if ok && resu != nil {
270    stepResult = *resu
271    nNew = nRes
272    nLastResults = 0
273  }
274
275  return nNew
276}
277
278//export Clear
279func Clear() {
280  cps.Stop()
281}
282
283//export CheckStop
284func CheckStop() bool {
285  if cps.GetIter() > GetMaxIterW() {
286    return true
287  }
288  return false
289}
290
291func CreateUsableVars(nEntries int) []int {
292  res := make([]int, nEntries)
293  for i := 0; i < nEntries; i++ {
294    res[i] = i
295  }
296  return res
297}
298
299func CIntArrayToSlice(matrix unsafe.Pointer, nEntries int) []int {
300  res := make([]int, nEntries)
301  for i := 0; i < nEntries; i++ {
302    item := (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(matrix)) + unsafe.Sizeof(int(0))*uintptr(i)))
303    res[i] = *item
304
305  }
306  return res
307}
308
309//export CleanW
310func CleanW() {
311  cps.Clean()
312}
Note: See TracBrowser for help on using the repository browser.