1 | package main
|
---|
2 |
|
---|
3 | import "C"
|
---|
4 | import (
|
---|
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 |
|
---|
14 | func main() {
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 | var cps *pge.PgeSearch
|
---|
19 | var tp *probs.TreeParams
|
---|
20 | var epcomm *probs.ExprProblemComm
|
---|
21 | var trainDatas []*probs.PointSet
|
---|
22 | var testDatas []*probs.PointSet
|
---|
23 | var testDatasN int
|
---|
24 | var trainDatasN int
|
---|
25 | var ep *probs.ExprProblem
|
---|
26 | var per_eqns []*probs.ExprReportArray
|
---|
27 |
|
---|
28 | var trainData *probs.PointSet
|
---|
29 | var testData *probs.PointSet
|
---|
30 |
|
---|
31 | var stepResult []*pge.PeelResult
|
---|
32 | var resultNr int
|
---|
33 |
|
---|
34 | //export InitSearch
|
---|
35 | func 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
|
---|
53 | func 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
|
---|
64 | func 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
|
---|
78 | func 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
|
---|
91 | func 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
|
---|
121 | func GetMaxIterW() int {
|
---|
122 | return cps.GetMaxIter()
|
---|
123 | }
|
---|
124 |
|
---|
125 | //export SetMaxIterW
|
---|
126 | func SetMaxIterW(iter int) {
|
---|
127 | cps.SetMaxIter(iter)
|
---|
128 | }
|
---|
129 |
|
---|
130 | //export SetPeelCountW
|
---|
131 | func SetPeelCountW(cnt int) {
|
---|
132 | cps.SetPeelCount(cnt)
|
---|
133 | }
|
---|
134 |
|
---|
135 | //export SetInitMethodW
|
---|
136 | func SetInitMethodW(init string) {
|
---|
137 | cps.SetInitMethod(init)
|
---|
138 | }
|
---|
139 |
|
---|
140 | //export SetGrowMethodW
|
---|
141 | func SetGrowMethodW(grow string) {
|
---|
142 | cps.SetGrowMethod(grow)
|
---|
143 | }
|
---|
144 |
|
---|
145 | //export SetEvalrCountW
|
---|
146 | func SetEvalrCountW(cnt int) {
|
---|
147 | cps.SetEvalrCount(cnt)
|
---|
148 | }
|
---|
149 |
|
---|
150 | //export EvaluateW
|
---|
151 | func EvaluateW() {
|
---|
152 | cps.Evaluate()
|
---|
153 | }
|
---|
154 |
|
---|
155 | //export RunW
|
---|
156 | func RunW() {
|
---|
157 | cps.Run()
|
---|
158 | }
|
---|
159 |
|
---|
160 | //export LoopW
|
---|
161 | func LoopW() {
|
---|
162 | cps.Loop()
|
---|
163 | }
|
---|
164 |
|
---|
165 | //export GetStepResult
|
---|
166 | func 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
|
---|
197 | func StepW() {
|
---|
198 | resultNr = 0
|
---|
199 | stepResult = cps.SingleStep()
|
---|
200 | }
|
---|
201 |
|
---|
202 | //export CheckStop
|
---|
203 | func CheckStop() bool {
|
---|
204 | if cps.GetIter() > GetMaxIterW() {
|
---|
205 | return true
|
---|
206 | }
|
---|
207 | return false
|
---|
208 | }
|
---|
209 |
|
---|
210 | func 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
|
---|
221 | func 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
|
---|
230 | func CleanW() {
|
---|
231 | cps.Clean()
|
---|
232 | } |
---|