Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2929: Modified make / folder structure to build original app, added x86 dll

File size: 15.0 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <ctype.h>
5
6#include "list.c"
7
8#ifdef __i386__
9#include "libpge32.h"
10#else
11#include "libpge.h"
12#endif
13
14#define BESTRES
15#define MAX_CHAR 255
16//#define DEBUG
17
18char* trim(char *str)
19{
20  char *end;
21  while(isspace((unsigned char)*str)) {
22    str++;
23  }
24  if(*str == 0) {
25    return "\0";
26  }
27  end = str + strlen(str) - 1;
28  while(end > str && isspace((unsigned char)*end)) {
29    end--;
30  }
31  end[1] = '\0';
32  return str;
33}
34
35GoInt CountCols(char *str) {
36    GoInt res = 0;
37    trim(str);
38    char *partial = strtok(str, " ");
39    while(partial != NULL) {
40        res++;
41        partial = strtok(NULL, " ");
42    }
43    return res;
44}
45
46char* lowerCase(char *str) {
47    int i = 0;
48    while(str[i] && str[i] != '\0'){
49        char c = tolower(str[i]);
50        str[i] = c;
51        i++;
52    }
53    return str;
54}
55
56char *strndup(char *str, int chars)
57{
58    char *buffer;
59    int n;
60
61    buffer = (char *) malloc(chars +1);
62    if (buffer)
63    {
64        for (n = 0; ((n < chars) && (str[n] != 0)) ; n++) buffer[n] = str[n];
65        buffer[n] = 0;
66    }
67
68    return buffer;
69}
70
71//Read train / test data
72GoFloat64* ReadInputData(char *fileName, GoInt* nEntries, GoInt* nCol, char** depHead, char** indHead) {
73    FILE *fp;
74    fp = fopen(fileName, "r");
75    if (fp == NULL) {
76        char msg[MAX_CHAR];
77        strcpy(msg, "Error in ReadInputData, failed to open file: ");
78        perror(strcat(msg, fileName));
79        exit(EXIT_FAILURE);
80    }
81
82    int lineNr = 0;
83    int currentC = 0;
84    char line[MAX_CHAR];
85
86    fgets(line, MAX_CHAR, fp);
87    GoInt nIndCol = CountCols(strdup(line));
88    *indHead = strdup(trim(line)); //, nIndCol * 2 - 1);
89    #ifdef DEBUG
90        printf("Independent Variables %d\n", nIndCol);
91    #endif
92
93    fgets(line, MAX_CHAR, fp);
94    GoInt nDepCol = CountCols(strdup(line));
95    *depHead = strdup(trim(line)); //, nDepCol * 2 - 1);
96    #ifdef DEBUG
97        printf("Dependent Variables %d\n", nDepCol);
98    #endif
99
100    *nCol = nDepCol + nIndCol;
101    if(nDepCol < 0 || nIndCol < 0) {
102        perror("There must be at least one independent and one dependent colum!\n");
103        exit(EXIT_FAILURE);
104    }
105
106    GoFloat64 *values = NULL;
107    while(fgets(line, MAX_CHAR, fp)) {
108        trim(line);
109        currentC = 0;
110        values = (GoFloat64 *) realloc(values, (lineNr + 1) * (*nCol) * sizeof(GoFloat64));       
111        if(values == NULL) {
112            perror("Out of memory!\n");
113        }
114        char *partial = strtok(line, " ");
115        while(partial != NULL) {
116            GoFloat64 value = 0;
117            value = strtod(partial, NULL);
118            int pos = lineNr * (*nCol) + currentC;
119            values[pos] = (GoFloat64) value;
120            #ifdef DEBUG
121                printf("Value (%d, %d) %d: %f\n", lineNr, currentC, pos, values[pos]);
122            #endif       
123            currentC++;
124            partial = strtok(NULL, " ");
125        }
126        lineNr++;
127    }
128    *nEntries = lineNr;
129    fclose(fp);
130    return values;
131}
132
133//Print Test / Train data
134void PrintInputData(double *values, int nEntries, int nCols) {
135    for(int i = 0; i < nEntries; i++) {     //Rows
136        for(int j = 0; j < nCols; j++) {    //Cols
137            int pos = i * nCols + j;
138            printf("Value (%d, %d) %d: %f\n", i, j, pos, values[pos]);
139        }
140    }
141}
142
143GoInt* CreateUseableVars(long n) {
144  GoInt *data = malloc(sizeof(GoInt) * n);
145    for(GoInt i = 0; i < n; i++) {
146        data[i] = i;
147    }
148  return data;
149}
150
151int isTreeSetting(char *key) {
152    if(strcmp(key, "roots") == 0 || strcmp(key, "nodes") == 0 || strcmp(key, "nontrig") == 0 || strcmp(key, "leafs") == 0) {
153        return 1;
154    }
155    return 0;
156}
157
158char* MergeArgs(char **values, int nValues) {
159    char margs[MAX_CHAR] = "\0";
160    for(int i = 0; i < nValues; i++) {
161        strcat(margs, lowerCase(values[i]));
162        if(i + 1 < nValues) {
163            strcat(margs, " ");
164        }
165    }
166    return strdup(margs);
167}
168
169void SetConfValue(char *keySetting, char **values, int nValues) {
170    #ifdef DEBUG
171        for(int i = 0; i < nValues; i++) {
172            printf("Key: %s Nr: %d Value: %s\n", keySetting, i, values[i]);
173        }
174    #endif
175    void *data = NULL;
176    if (strcmp(keySetting, "sorttype") == 0) {
177        data = (GoInt *)malloc(sizeof(GoInt));
178        *((GoInt *)data) = 0;
179        if(strcmp("paretotesterror", lowerCase(values[0])) != 0) {
180            *((GoInt *)data) = 1;
181        }
182    } else if (strcmp(keySetting, "usablevars") == 0) {
183        data = (GoInt *)malloc(sizeof(GoInt));
184        *((GoInt *)data) = nValues;
185    } else if(strcmp(keySetting, "zeroepsilon") == 0 || strcmp(keySetting, "hitratio") == 0) {
186        data = (GoFloat64 *)malloc(sizeof(GoFloat64));
187        *((GoFloat64 *)data) = strtod(values[0], NULL);
188    } else if(isTreeSetting(keySetting) == 1) {
189        data = MergeArgs(values, nValues);
190    } else if(strcmp(keySetting, "problemtype") == 0 || strcmp(keySetting, "name") == 0) {
191        data = strdup(lowerCase(values[0]));
192    } else {    //Integer
193        data = (GoInt *)malloc(sizeof(GoInt));
194        *((GoInt *)data) = strtol(values[0], NULL, 10);
195    }
196    configPrepend(strdup(keySetting), (void *)data);
197}
198
199void ReadConfigFile(char *filePath) {
200    FILE *fp;
201    fp = fopen(filePath, "r");
202    if (fp == NULL) {
203        perror("Error in ReadConfigFile, failed to open file\n");
204        exit(EXIT_FAILURE);
205    }
206    char line[MAX_CHAR];
207    fgets(line, MAX_CHAR, fp);
208
209    while(fgets(line, MAX_CHAR, fp)) {
210        trim(line);
211        char *partial = strtok(line, " ");
212        int currentPart = 0;
213        char keySetting[MAX_CHAR] = "\0";
214        int valid = 0;
215        char **values = NULL;
216        int nValues = 0;
217        while(partial != NULL) {
218            trim(partial);
219            if(strlen(partial) > 0 && partial[0] == '#') {
220                break;
221            }
222            if(strlen(partial) > 0) {
223                if(currentPart == 0) {                  //Name / Key
224                    sscanf(partial, "%s", keySetting);
225                } else if(currentPart == 1) {           //Eq char
226                    if(strlen(keySetting) > 0) {
227                        valid = 1;
228                    }
229                } else {                                //Value
230                    values = (char **) realloc(values, (nValues + 1) * MAX_CHAR * sizeof(char));
231                    values[nValues] = partial;
232                    nValues++;
233                }
234                currentPart++;
235            }
236            partial = strtok(NULL, " ");
237        }
238        if(valid == 1) {
239            SetConfValue(lowerCase(keySetting), values, nValues);
240        }
241    }
242}
243
244void RunTest(GoFloat64 *TrainData, int nTrainEntries, int nTrainCols, GoFloat64 *TestData, int nTestEntries, int nTestCols, char *depHeadTrain, char *indepHeadTrain, char *depHeadTest, char *indepHeadTest) {
245    putenv("GOGC=off");
246  putenv("GODEBUG=cgocheck=0");
247  putenv("CGO_ENABLED=1");
248    #ifdef DEBUG
249        putenv("PGEDEBUG=1");
250        printf("%s %s %s %s", indepHeadTest, indepHeadTrain, depHeadTest, depHeadTrain);
251    #endif
252    fflush(stdout);
253
254    AddTestData(indepHeadTest, depHeadTest, TestData, nTestEntries);
255  AddTrainData(indepHeadTrain, depHeadTrain, TrainData, nTrainEntries);
256    printf("Added Train- and Testdata\n");
257
258    GoInt MaxGen = *((GoInt *)configGet("maxgen"));
259    GoInt PgeRptEpoch = *((GoInt *)configGet("pgerptepoch"));
260    GoInt PgeRptCount = *((GoInt *)configGet("pgerptcount"));
261    GoInt PgeArchiveCap = *((GoInt *)configGet("pgearchivecap"));
262    GoInt PeelCnt = *((GoInt *)configGet("peelcnt"));
263    GoInt EvalrCount = *((GoInt *)configGet("evalcnt"));
264    GoInt SortType = *((GoInt *)configGet("sorttype"));
265    GoFloat64 ZeroEpsilon = *((GoFloat64 *)configGet("zeroepsilon"));
266    char *InitMethod = (char *)configGet("initmethod");
267    char *GrowMethod = (char *)configGet("growmethod");
268   
269    #ifdef DEBUG
270        printf("1: %d 2: %d 3: %d 4: %d 5: %d 6: %d 7: %d 8: %lf 9: %s 10: %s\n",
271        MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, SortType, ZeroEpsilon, InitMethod, GrowMethod);
272    #endif
273
274    InitSearch(MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, ZeroEpsilon, InitMethod, GrowMethod, SortType);
275    printf("Initialized Search\n");
276    fflush(stdout);
277
278    GoInt MaxSize = *((GoInt *)configGet("maxsize"));
279    GoInt MinSize = *((GoInt *)configGet("minsize"));
280    GoInt MaxDepth = *((GoInt *)configGet("maxdepth"));
281    GoInt MinDepth = *((GoInt *)configGet("mindepth"));
282    char *Roots = (char *)configGet("roots");
283    char *Nodes = (char *)configGet("nodes");
284    char *NonTrig = (char *)configGet("nontrig");
285    char *Leafs = (char *)configGet("leafs");
286    GoInt nUsableVars = *((GoInt *)configGet("usablevars"));
287
288    #ifdef DEBUG
289        printf("1: %d 2: %d 3: %d 4: %d 5: %s 6: %s 7: %s 8: %s 9: %d\n", MaxSize, MinSize, MaxDepth, MinDepth, Roots, Nodes, NonTrig, Leafs, nUsableVars);
290    #endif
291
292    InitTreeParams(Roots, Nodes, NonTrig, Leafs, nUsableVars, MaxSize, MinSize, MaxDepth, MinDepth);
293  printf("Initialized TreeParams\n");
294    fflush(stdout);
295
296    GoInt MaxIter = *((GoInt *)configGet("maxiter"));
297    GoInt SearchVar = *((GoInt *)configGet("searchvar"));
298    GoFloat64 HitRatio = *((GoFloat64 *)configGet("hitratio"));
299    GoInt NumProcs = *((GoInt *)configGet("numprocs"));
300    char *Name = (char *)configGet("name");
301  char *ProblemType = (char *)configGet("problemtype");
302 
303    #ifdef DEBUG
304        printf("1: %d 2: %d 3: %lf 4: %d 5: %s 6: %s\n", MaxIter, SearchVar, HitRatio, NumProcs, Name, ProblemType);
305    #endif
306
307    InitProblem(Name, MaxIter, HitRatio, SearchVar, ProblemType, NumProcs);
308    printf("Initialized Problem\n");
309    fflush(stdout);
310
311    for (int fs1 = 1; fs1 <= MaxIter; fs1++) {   
312    printf("******************************** StepW: %d **********************************\n", fs1);
313    GoInt nresults = StepW();
314
315    for (int ires = 0; ires < nresults; ires++) {
316      GoInt testscore = 0;
317      GoInt ncoeff = 0;
318     
319      char* stepRes = GetStepResult(&testscore, &ncoeff);
320      printf("C: push/pop (%d,%d) %s\n", fs1, ires, stepRes);
321            printf("C: TestScore %d\n", testscore);
322      for(int icoeff = 0; icoeff < ncoeff; icoeff++){
323        GoFloat64 coeffVal = GetCoeffResult();
324        printf("C: coeff vals: %f\n", coeffVal);
325      }
326    }
327    fflush(stdout);
328  }
329
330    #ifdef BESTRES
331        printf("\n############################ Best results ############################\n");
332        GoInt nBest = GetBestResults();
333        GoInt more = 1;
334        for (GoInt i = 0; i < nBest; i++) {
335            GoInt ncoeff = 0;
336            GoInt testscore = 0;
337            char* stepRes = GetStepResult(&testscore, &ncoeff);
338            printf("%ld. Best: %s\n", i + 1, stepRes);
339            for(int icoeff = 0; icoeff < ncoeff; icoeff++){
340                GoFloat64 coeffVal = GetCoeffResult();
341                printf("\tcoeff vals: %f\n", coeffVal);
342            }
343        }
344    #endif
345}
346
347//TODO: Dynamic overwrite
348void ParseArgs(int argc, char *argv[], char** problemName, char **initMethod, char **growMethod, GoInt *nPeel, GoInt *nEvals, GoInt *maxIter) {
349    if(argc < 2 ){
350        printf("Usage: %s ProblemName [-evals=3] [-peel=3] [-iter=200] [-init=method1] [-grow=method1]\n\n", argv[0]);
351        printf("Using default Problem: %s\n", *problemName);
352        perror("Missing required argument 1: ProblemName, eg. Pagie_1, Korns_02\n");
353    } else {
354        *problemName = strdup(argv[1]);
355        for(int i = 2; i < argc; i++) {
356            if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
357                printf("Usage: %s ProblemName [-evals=3] [-peel=3] [-iter=200] [-init=method1] [-grow=method1]\n\n" , argv[0]);
358                exit(EXIT_SUCCESS);
359            } else if(strncmp(argv[i], "-evals", 6) == 0) {
360                sscanf(argv[i], "-evals=%ld", nEvals);
361            } else if(strncmp(argv[i], "-peel", 5) == 0) {
362                sscanf(argv[i], "-peel=%ld", nPeel);
363            } else if(strncmp(argv[i], "-iter", 5) == 0) {
364                sscanf(argv[i], "-iter=%ld", maxIter);
365            } else if(strncmp(argv[i], "-init", 5) == 0) {
366                sscanf(argv[i], "-init=%s", *initMethod);
367            } else if(strncmp(argv[i], "-grow", 5) == 0) {
368                sscanf(argv[i], "-grow=%s", *growMethod);
369            } else {
370                char msg[MAX_CHAR];
371                strcpy(msg, "Unkown argument: ");
372                perror(strcat(msg, argv[i]));
373            }
374        }
375    }
376}
377
378int main(int argc, char *argv[]) {
379    char *problemName = strdup("Pagie_1");
380    GoInt *nEvals = malloc(sizeof(GoInt));
381    GoInt *nPeel = malloc(sizeof(GoInt));
382    GoInt *maxIter = malloc(sizeof(GoInt));
383    GoInt *numProcs = malloc(sizeof(GoInt));
384    *nEvals = 3;
385    *nPeel = 3;
386    *maxIter = 200;
387    *numProcs = 12;
388    char *initMethod = strdup("method1");
389    char *growMethod = strdup("method1");
390    ParseArgs(argc, argv, &problemName, &initMethod, &growMethod, nPeel, nEvals, maxIter);
391
392    printf("MaxIter: %ld\n", *maxIter);
393    printf("Evals: %ld\n", *nEvals);
394    printf("InitMethod: %s\n", initMethod);
395
396    //* Testdata
397    char tstPath[MAX_CHAR] = "data/benchmark/";
398    strcat(tstPath, problemName);
399    strcat(tstPath, ".tst");
400
401    printf("TestFile %s\n", tstPath);
402    GoInt nTestEntries = 0;
403    GoInt nTestCols = 0;
404    char *TestDepHead, *TestIndHead;
405    GoFloat64 *TestData = ReadInputData(tstPath, &nTestEntries, &nTestCols, &TestDepHead, &TestIndHead);
406    printf("TestData: Entries %ld, Colums %ld\n", nTestEntries, nTestCols);
407    #ifdef DEBUG
408        PrintInputData(TestData, nTestEntries, nTestCols);
409    #endif
410
411    //* Traindata
412    char trnPath[MAX_CHAR] = "data/benchmark/";
413    strcat(trnPath, problemName);
414    strcat(trnPath, ".trn");
415
416    printf("TrainFile %s\n", trnPath);
417    GoInt nTrainEntries = 0;
418    GoInt nTrainCols = 0;
419    char *TrainDepHead, *TrainIndHead;
420    GoFloat64 *TrainData = ReadInputData(trnPath, &nTrainEntries, &nTrainCols, &TrainDepHead, &TrainIndHead);
421    printf("TrainData: Entries %ld, Colums %ld\n", nTrainEntries, nTrainCols);
422    #ifdef DEBUG
423        PrintInputData(TrainData, nTrainEntries, nTrainCols);
424    #endif
425
426    //* Configfiles
427    char defaultConfPath[] = "config/pge/pge_default.cfg";
428    printf("Default Config Path %s\n", defaultConfPath);
429    ReadConfigFile(defaultConfPath);
430
431    char confPath[MAX_CHAR] = "config/prob/bench/";
432    strcat(confPath, problemName);
433    strcat(confPath, ".cfg");
434    printf("Config Path %s\n", confPath);
435    ReadConfigFile(confPath);
436
437    configPrint();
438    #ifdef DEBUG
439        configPrint();
440    #endif
441
442    //Overwrite with args
443    configPrepend(strdup("growmethod"), growMethod);
444    configPrepend(strdup("initmethod"), initMethod);
445    configPrepend(strdup("evalcnt"), nEvals);
446    configPrepend(strdup("peelcnt"), nPeel);
447    configPrepend(strdup("maxiter"), maxIter);
448    configPrepend(strdup("name"), problemName);
449    configPrepend(strdup("numprocs"), numProcs);
450
451    //* RunPGE
452    RunTest(TrainData, nTrainEntries, nTrainCols, TestData, nTestEntries, nTestCols, TrainDepHead, TrainIndHead, TestDepHead, TestIndHead);
453
454    //* Dispose
455    free(TestData);
456    TestData = NULL;
457
458    free(TrainData);
459    TrainData = NULL;
460
461    configDispose();
462
463    return EXIT_SUCCESS;
464}
Note: See TracBrowser for help on using the repository browser.