#include #include #include #include #include "list.c" #ifdef __i386__ #include "libpge32.h" #else #include "libpge.h" #endif #define BESTRES #define MAX_CHAR 255 //#define DEBUG char* trim(char *str) { char *end; while(isspace((unsigned char)*str)) { str++; } if(*str == 0) { return "\0"; } end = str + strlen(str) - 1; while(end > str && isspace((unsigned char)*end)) { end--; } end[1] = '\0'; return str; } GoInt CountCols(char *str) { GoInt res = 0; trim(str); char *partial = strtok(str, " "); while(partial != NULL) { res++; partial = strtok(NULL, " "); } return res; } char* lowerCase(char *str) { int i = 0; while(str[i] && str[i] != '\0'){ char c = tolower(str[i]); str[i] = c; i++; } return str; } char *strndup(char *str, int chars) { char *buffer; int n; buffer = (char *) malloc(chars +1); if (buffer) { for (n = 0; ((n < chars) && (str[n] != 0)) ; n++) buffer[n] = str[n]; buffer[n] = 0; } return buffer; } //Read train / test data GoFloat64* ReadInputData(char *fileName, GoInt* nEntries, GoInt* nCol, char** depHead, char** indHead) { FILE *fp; fp = fopen(fileName, "r"); if (fp == NULL) { char msg[MAX_CHAR]; strcpy(msg, "Error in ReadInputData, failed to open file: "); perror(strcat(msg, fileName)); exit(EXIT_FAILURE); } int lineNr = 0; int currentC = 0; char line[MAX_CHAR]; fgets(line, MAX_CHAR, fp); GoInt nIndCol = CountCols(strdup(line)); *indHead = strdup(trim(line)); //, nIndCol * 2 - 1); #ifdef DEBUG printf("Independent Variables %d\n", nIndCol); #endif fgets(line, MAX_CHAR, fp); GoInt nDepCol = CountCols(strdup(line)); *depHead = strdup(trim(line)); //, nDepCol * 2 - 1); #ifdef DEBUG printf("Dependent Variables %d\n", nDepCol); #endif *nCol = nDepCol + nIndCol; if(nDepCol < 0 || nIndCol < 0) { perror("There must be at least one independent and one dependent colum!\n"); exit(EXIT_FAILURE); } GoFloat64 *values = NULL; while(fgets(line, MAX_CHAR, fp)) { trim(line); currentC = 0; values = (GoFloat64 *) realloc(values, (lineNr + 1) * (*nCol) * sizeof(GoFloat64)); if(values == NULL) { perror("Out of memory!\n"); } char *partial = strtok(line, " "); while(partial != NULL) { GoFloat64 value = 0; value = strtod(partial, NULL); int pos = lineNr * (*nCol) + currentC; values[pos] = (GoFloat64) value; #ifdef DEBUG printf("Value (%d, %d) %d: %f\n", lineNr, currentC, pos, values[pos]); #endif currentC++; partial = strtok(NULL, " "); } lineNr++; } *nEntries = lineNr; fclose(fp); return values; } //Print Test / Train data void PrintInputData(double *values, int nEntries, int nCols) { for(int i = 0; i < nEntries; i++) { //Rows for(int j = 0; j < nCols; j++) { //Cols int pos = i * nCols + j; printf("Value (%d, %d) %d: %f\n", i, j, pos, values[pos]); } } } GoInt* CreateUseableVars(long n) { GoInt *data = malloc(sizeof(GoInt) * n); for(GoInt i = 0; i < n; i++) { data[i] = i; } return data; } int isTreeSetting(char *key) { if(strcmp(key, "roots") == 0 || strcmp(key, "nodes") == 0 || strcmp(key, "nontrig") == 0 || strcmp(key, "leafs") == 0) { return 1; } return 0; } char* MergeArgs(char **values, int nValues) { char margs[MAX_CHAR] = "\0"; for(int i = 0; i < nValues; i++) { strcat(margs, lowerCase(values[i])); if(i + 1 < nValues) { strcat(margs, " "); } } return strdup(margs); } void SetConfValue(char *keySetting, char **values, int nValues) { #ifdef DEBUG for(int i = 0; i < nValues; i++) { printf("Key: %s Nr: %d Value: %s\n", keySetting, i, values[i]); } #endif void *data = NULL; if (strcmp(keySetting, "sorttype") == 0) { data = (GoInt *)malloc(sizeof(GoInt)); *((GoInt *)data) = 0; if(strcmp("paretotesterror", lowerCase(values[0])) != 0) { *((GoInt *)data) = 1; } } else if (strcmp(keySetting, "usablevars") == 0) { data = (GoInt *)malloc(sizeof(GoInt)); *((GoInt *)data) = nValues; } else if(strcmp(keySetting, "zeroepsilon") == 0 || strcmp(keySetting, "hitratio") == 0) { data = (GoFloat64 *)malloc(sizeof(GoFloat64)); *((GoFloat64 *)data) = strtod(values[0], NULL); } else if(isTreeSetting(keySetting) == 1) { data = MergeArgs(values, nValues); } else if(strcmp(keySetting, "problemtype") == 0 || strcmp(keySetting, "name") == 0) { data = strdup(lowerCase(values[0])); } else { //Integer data = (GoInt *)malloc(sizeof(GoInt)); *((GoInt *)data) = strtol(values[0], NULL, 10); } configPrepend(strdup(keySetting), (void *)data); } void ReadConfigFile(char *filePath) { FILE *fp; fp = fopen(filePath, "r"); if (fp == NULL) { perror("Error in ReadConfigFile, failed to open file\n"); exit(EXIT_FAILURE); } char line[MAX_CHAR]; fgets(line, MAX_CHAR, fp); while(fgets(line, MAX_CHAR, fp)) { trim(line); char *partial = strtok(line, " "); int currentPart = 0; char keySetting[MAX_CHAR] = "\0"; int valid = 0; char **values = NULL; int nValues = 0; while(partial != NULL) { trim(partial); if(strlen(partial) > 0 && partial[0] == '#') { break; } if(strlen(partial) > 0) { if(currentPart == 0) { //Name / Key sscanf(partial, "%s", keySetting); } else if(currentPart == 1) { //Eq char if(strlen(keySetting) > 0) { valid = 1; } } else { //Value values = (char **) realloc(values, (nValues + 1) * MAX_CHAR * sizeof(char)); values[nValues] = partial; nValues++; } currentPart++; } partial = strtok(NULL, " "); } if(valid == 1) { SetConfValue(lowerCase(keySetting), values, nValues); } } } void RunTest(GoFloat64 *TrainData, int nTrainEntries, int nTrainCols, GoFloat64 *TestData, int nTestEntries, int nTestCols, char *depHeadTrain, char *indepHeadTrain, char *depHeadTest, char *indepHeadTest) { putenv("GOGC=off"); putenv("GODEBUG=cgocheck=0"); putenv("CGO_ENABLED=1"); #ifdef DEBUG putenv("PGEDEBUG=1"); printf("%s %s %s %s", indepHeadTest, indepHeadTrain, depHeadTest, depHeadTrain); #endif fflush(stdout); AddTestData(indepHeadTest, depHeadTest, TestData, nTestEntries); AddTrainData(indepHeadTrain, depHeadTrain, TrainData, nTrainEntries); printf("Added Train- and Testdata\n"); GoInt MaxGen = *((GoInt *)configGet("maxgen")); GoInt PgeRptEpoch = *((GoInt *)configGet("pgerptepoch")); GoInt PgeRptCount = *((GoInt *)configGet("pgerptcount")); GoInt PgeArchiveCap = *((GoInt *)configGet("pgearchivecap")); GoInt PeelCnt = *((GoInt *)configGet("peelcnt")); GoInt EvalrCount = *((GoInt *)configGet("evalcnt")); GoInt SortType = *((GoInt *)configGet("sorttype")); GoFloat64 ZeroEpsilon = *((GoFloat64 *)configGet("zeroepsilon")); char *InitMethod = (char *)configGet("initmethod"); char *GrowMethod = (char *)configGet("growmethod"); #ifdef DEBUG printf("1: %d 2: %d 3: %d 4: %d 5: %d 6: %d 7: %d 8: %lf 9: %s 10: %s\n", MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, SortType, ZeroEpsilon, InitMethod, GrowMethod); #endif InitSearch(MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, ZeroEpsilon, InitMethod, GrowMethod, SortType); printf("Initialized Search\n"); fflush(stdout); GoInt MaxSize = *((GoInt *)configGet("maxsize")); GoInt MinSize = *((GoInt *)configGet("minsize")); GoInt MaxDepth = *((GoInt *)configGet("maxdepth")); GoInt MinDepth = *((GoInt *)configGet("mindepth")); char *Roots = (char *)configGet("roots"); char *Nodes = (char *)configGet("nodes"); char *NonTrig = (char *)configGet("nontrig"); char *Leafs = (char *)configGet("leafs"); GoInt nUsableVars = *((GoInt *)configGet("usablevars")); #ifdef DEBUG 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); #endif InitTreeParams(Roots, Nodes, NonTrig, Leafs, nUsableVars, MaxSize, MinSize, MaxDepth, MinDepth); printf("Initialized TreeParams\n"); fflush(stdout); GoInt MaxIter = *((GoInt *)configGet("maxiter")); GoInt SearchVar = *((GoInt *)configGet("searchvar")); GoFloat64 HitRatio = *((GoFloat64 *)configGet("hitratio")); GoInt NumProcs = *((GoInt *)configGet("numprocs")); char *Name = (char *)configGet("name"); char *ProblemType = (char *)configGet("problemtype"); #ifdef DEBUG printf("1: %d 2: %d 3: %lf 4: %d 5: %s 6: %s\n", MaxIter, SearchVar, HitRatio, NumProcs, Name, ProblemType); #endif InitProblem(Name, MaxIter, HitRatio, SearchVar, ProblemType, NumProcs); printf("Initialized Problem\n"); fflush(stdout); for (int fs1 = 1; fs1 <= MaxIter; fs1++) { printf("******************************** StepW: %d **********************************\n", fs1); GoInt nresults = StepW(); for (int ires = 0; ires < nresults; ires++) { GoInt testscore = 0; GoInt ncoeff = 0; char* stepRes = GetStepResult(&testscore, &ncoeff); printf("C: push/pop (%d,%d) %s\n", fs1, ires, stepRes); printf("C: TestScore %d\n", testscore); for(int icoeff = 0; icoeff < ncoeff; icoeff++){ GoFloat64 coeffVal = GetCoeffResult(); printf("C: coeff vals: %f\n", coeffVal); } } fflush(stdout); } #ifdef BESTRES printf("\n############################ Best results ############################\n"); GoInt nBest = GetBestResults(); GoInt more = 1; for (GoInt i = 0; i < nBest; i++) { GoInt ncoeff = 0; GoInt testscore = 0; char* stepRes = GetStepResult(&testscore, &ncoeff); printf("%ld. Best: %s\n", i + 1, stepRes); for(int icoeff = 0; icoeff < ncoeff; icoeff++){ GoFloat64 coeffVal = GetCoeffResult(); printf("\tcoeff vals: %f\n", coeffVal); } } #endif } //TODO: Dynamic overwrite void ParseArgs(int argc, char *argv[], char** problemName, char **initMethod, char **growMethod, GoInt *nPeel, GoInt *nEvals, GoInt *maxIter) { if(argc < 2 ){ printf("Usage: %s ProblemName [-evals=3] [-peel=3] [-iter=200] [-init=method1] [-grow=method1]\n\n", argv[0]); printf("Using default Problem: %s\n", *problemName); perror("Missing required argument 1: ProblemName, eg. Pagie_1, Korns_02\n"); } else { *problemName = strdup(argv[1]); for(int i = 2; i < argc; i++) { if(strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { printf("Usage: %s ProblemName [-evals=3] [-peel=3] [-iter=200] [-init=method1] [-grow=method1]\n\n" , argv[0]); exit(EXIT_SUCCESS); } else if(strncmp(argv[i], "-evals", 6) == 0) { sscanf(argv[i], "-evals=%ld", nEvals); } else if(strncmp(argv[i], "-peel", 5) == 0) { sscanf(argv[i], "-peel=%ld", nPeel); } else if(strncmp(argv[i], "-iter", 5) == 0) { sscanf(argv[i], "-iter=%ld", maxIter); } else if(strncmp(argv[i], "-init", 5) == 0) { sscanf(argv[i], "-init=%s", *initMethod); } else if(strncmp(argv[i], "-grow", 5) == 0) { sscanf(argv[i], "-grow=%s", *growMethod); } else { char msg[MAX_CHAR]; strcpy(msg, "Unkown argument: "); perror(strcat(msg, argv[i])); } } } } int main(int argc, char *argv[]) { char *problemName = strdup("Pagie_1"); GoInt *nEvals = malloc(sizeof(GoInt)); GoInt *nPeel = malloc(sizeof(GoInt)); GoInt *maxIter = malloc(sizeof(GoInt)); GoInt *numProcs = malloc(sizeof(GoInt)); *nEvals = 3; *nPeel = 3; *maxIter = 200; *numProcs = 12; char *initMethod = strdup("method1"); char *growMethod = strdup("method1"); ParseArgs(argc, argv, &problemName, &initMethod, &growMethod, nPeel, nEvals, maxIter); printf("MaxIter: %ld\n", *maxIter); printf("Evals: %ld\n", *nEvals); printf("InitMethod: %s\n", initMethod); //* Testdata char tstPath[MAX_CHAR] = "data/benchmark/"; strcat(tstPath, problemName); strcat(tstPath, ".tst"); printf("TestFile %s\n", tstPath); GoInt nTestEntries = 0; GoInt nTestCols = 0; char *TestDepHead, *TestIndHead; GoFloat64 *TestData = ReadInputData(tstPath, &nTestEntries, &nTestCols, &TestDepHead, &TestIndHead); printf("TestData: Entries %ld, Colums %ld\n", nTestEntries, nTestCols); #ifdef DEBUG PrintInputData(TestData, nTestEntries, nTestCols); #endif //* Traindata char trnPath[MAX_CHAR] = "data/benchmark/"; strcat(trnPath, problemName); strcat(trnPath, ".trn"); printf("TrainFile %s\n", trnPath); GoInt nTrainEntries = 0; GoInt nTrainCols = 0; char *TrainDepHead, *TrainIndHead; GoFloat64 *TrainData = ReadInputData(trnPath, &nTrainEntries, &nTrainCols, &TrainDepHead, &TrainIndHead); printf("TrainData: Entries %ld, Colums %ld\n", nTrainEntries, nTrainCols); #ifdef DEBUG PrintInputData(TrainData, nTrainEntries, nTrainCols); #endif //* Configfiles char defaultConfPath[] = "config/pge/pge_default.cfg"; printf("Default Config Path %s\n", defaultConfPath); ReadConfigFile(defaultConfPath); char confPath[MAX_CHAR] = "config/prob/bench/"; strcat(confPath, problemName); strcat(confPath, ".cfg"); printf("Config Path %s\n", confPath); ReadConfigFile(confPath); configPrint(); #ifdef DEBUG configPrint(); #endif //Overwrite with args configPrepend(strdup("growmethod"), growMethod); configPrepend(strdup("initmethod"), initMethod); configPrepend(strdup("evalcnt"), nEvals); configPrepend(strdup("peelcnt"), nPeel); configPrepend(strdup("maxiter"), maxIter); configPrepend(strdup("name"), problemName); configPrepend(strdup("numprocs"), numProcs); //* RunPGE RunTest(TrainData, nTrainEntries, nTrainCols, TestData, nTestEntries, nTestCols, TrainDepHead, TrainIndHead, TestDepHead, TestIndHead); //* Dispose free(TestData); TestData = NULL; free(TrainData); TrainData = NULL; configDispose(); return EXIT_SUCCESS; }