Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/go-code/go-pge/plug/pge.go @ 16230

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

#2929: Adapted pge plugin to check for null value

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