1 | package main |
---|
2 | |
---|
3 | import "C" |
---|
4 | import ( |
---|
5 | pge "go-pge/pge" |
---|
6 | "unsafe" |
---|
7 | rt "runtime" |
---|
8 | rand "math/rand" |
---|
9 | "strings" |
---|
10 | probs "go-pge/problems" |
---|
11 | ) |
---|
12 | |
---|
13 | func main() { |
---|
14 | |
---|
15 | } |
---|
16 | |
---|
17 | var cps *pge.PgeSearch |
---|
18 | var tp *probs.TreeParams |
---|
19 | var epcomm *probs.ExprProblemComm |
---|
20 | var trainDatas []*probs.PointSet |
---|
21 | var testDatas []*probs.PointSet |
---|
22 | var testDatasN int |
---|
23 | var trainDatasN int |
---|
24 | var ep *probs.ExprProblem |
---|
25 | var per_eqns []*probs.ExprReportArray |
---|
26 | |
---|
27 | var trainData *probs.PointSet |
---|
28 | var testData *probs.PointSet |
---|
29 | |
---|
30 | var probIndepNames []string |
---|
31 | var probDepndNames []string |
---|
32 | |
---|
33 | var stepResult probs.ExprReportArray |
---|
34 | var lastStepRes *probs.ExprReport |
---|
35 | var resultNr int |
---|
36 | |
---|
37 | var nCurrentCoeff int |
---|
38 | var nMaxCoeff int |
---|
39 | var sortTypeNr int |
---|
40 | var nLastResults int |
---|
41 | |
---|
42 | //export InitSearch |
---|
43 | func 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 |
---|
63 | func 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 |
---|
74 | func 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 |
---|
91 | func 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 |
---|
105 | func 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 |
---|
142 | func GetMaxIterW() int { |
---|
143 | return cps.GetMaxIter() |
---|
144 | } |
---|
145 | |
---|
146 | //export SetMaxIterW |
---|
147 | func SetMaxIterW(iter int) { |
---|
148 | cps.SetMaxIter(iter) |
---|
149 | } |
---|
150 | |
---|
151 | //export SetPeelCountW |
---|
152 | func SetPeelCountW(cnt int) { |
---|
153 | cps.SetPeelCount(cnt) |
---|
154 | } |
---|
155 | |
---|
156 | //export SetInitMethodW |
---|
157 | func SetInitMethodW(init string) { |
---|
158 | cps.SetInitMethod(init) |
---|
159 | } |
---|
160 | |
---|
161 | //export SetGrowMethodW |
---|
162 | func SetGrowMethodW(grow string) { |
---|
163 | cps.SetGrowMethod(grow) |
---|
164 | } |
---|
165 | |
---|
166 | //export SetEvalrCountW |
---|
167 | func SetEvalrCountW(cnt int) { |
---|
168 | cps.SetEvalrCount(cnt) |
---|
169 | } |
---|
170 | |
---|
171 | //export EvaluateW |
---|
172 | func EvaluateW() { |
---|
173 | cps.Evaluate() |
---|
174 | } |
---|
175 | |
---|
176 | //export RunW |
---|
177 | func RunW() { |
---|
178 | cps.Run() |
---|
179 | } |
---|
180 | |
---|
181 | //export LoopW |
---|
182 | func LoopW() { |
---|
183 | cps.Loop() |
---|
184 | } |
---|
185 | |
---|
186 | //export GetStepResult |
---|
187 | func 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 |
---|
206 | func 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 |
---|
217 | func 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 |
---|
236 | func Clear() { |
---|
237 | cps.Stop() |
---|
238 | } |
---|
239 | |
---|
240 | //export CheckStop |
---|
241 | func CheckStop() bool { |
---|
242 | if cps.GetIter() > GetMaxIterW() { |
---|
243 | return true |
---|
244 | } |
---|
245 | return false |
---|
246 | } |
---|
247 | |
---|
248 | func 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 | |
---|
256 | func 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 |
---|
267 | func CleanW() { |
---|
268 | cps.Clean() |
---|
269 | } |
---|