Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16620 was 16620, checked in by hmaislin, 5 years ago

#2929: Reorganized folder structure for make script, removed explicit marshalling, erased go-side logging

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