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 @ 16191

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

#2929: Added current DLL to PGE dir that needs to be placed in bin path

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