Changeset 16620 for branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/PGE.cs
- Timestamp:
- 02/26/19 00:20:12 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2929_PrioritizedGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.PGE/3.3/PGE.cs
r16614 r16620 25 25 26 26 [DllImport("go-pge.dll", EntryPoint = "addTestData", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 27 public static extern void AddTestData( IntPtr indepNames, IntPtr depndNames, IntPtrmatrix, int nEntries);27 public static extern void AddTestData([MarshalAs(UnmanagedType.AnsiBStr)] string indepNames, [MarshalAs(UnmanagedType.AnsiBStr)] string depndNames, double[] matrix, int nEntries); 28 28 29 29 [DllImport("go-pge.dll", EntryPoint = "addTrainData", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 30 public static extern void AddTrainData( IntPtr indepNames, IntPtr depndNames, IntPtrmatrix, int nEntries);30 public static extern void AddTrainData([MarshalAs(UnmanagedType.AnsiBStr)] string indepNames, [MarshalAs(UnmanagedType.AnsiBStr)] string depndNames, double[] matrix, int nEntries); 31 31 32 32 [DllImport("go-pge.dll", EntryPoint = "initSearch", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 33 public static extern void InitSearch(int maxGen, int pgeRptEpoch, int pgeRptCount, int pgeArchiveCap, int peelCnt, int evalrCount, double zeroEpsilon, IntPtr initMethod, IntPtrgrowMethod, int sortType);33 public static extern void InitSearch(int maxGen, int pgeRptEpoch, int pgeRptCount, int pgeArchiveCap, int peelCnt, int evalrCount, double zeroEpsilon, [MarshalAs(UnmanagedType.AnsiBStr)] string initMethod, [MarshalAs(UnmanagedType.AnsiBStr)] string growMethod, int sortType); 34 34 35 35 [DllImport("go-pge.dll", EntryPoint = "initTreeParams", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 36 public static extern void InitTreeParams( IntPtr roots, IntPtr nodes, IntPtr nonTrig, IntPtrleafs, int numUsableVars, int maxSize, int minSize, int maxDepth, int minDepth);36 public static extern void InitTreeParams([MarshalAs(UnmanagedType.AnsiBStr)] string roots, [MarshalAs(UnmanagedType.AnsiBStr)] string nodes, [MarshalAs(UnmanagedType.AnsiBStr)] string nonTrig, [MarshalAs(UnmanagedType.AnsiBStr)] string leafs, int numUsableVars, int maxSize, int minSize, int maxDepth, int minDepth); 37 37 38 38 [DllImport("go-pge.dll", EntryPoint = "initProblem", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 39 public static extern void InitProblem( IntPtr name, int maxIter, double hitRatio, int searchVar, IntPtrProblemTypeString, int numProcs);39 public static extern void InitProblem([MarshalAs(UnmanagedType.AnsiBStr)] string name, int maxIter, double hitRatio, int searchVar, [MarshalAs(UnmanagedType.AnsiBStr)] string ProblemTypeString, int numProcs); 40 40 41 41 [DllImport("go-pge.dll", EntryPoint = "stepW", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] … … 303 303 304 304 //Constants 305 int sortType = 0; // TODO what's sort type? 305 int sortType = 0; // TODO what's sort type? // 306 //1 = PESORT_PARETO_TRN_ERR 307 //0 = PESORT_PARETO_TST_ERR 308 306 309 string problemTypeString = "benchmark"; 307 310 int numProc = 12; … … 312 315 var variables = problemData.AllowedInputVariables.Concat(new string[] { problemData.TargetVariable }); 313 316 // no idea why the following are IntPtr, this should not be necessary for marshalling, it should be ok to just send the double[,] 314 int nTrainData; 315 int nTestData; 316 IntPtr trainData = GetData(problemData.Dataset, variables, problemData.TrainingIndices, out nTrainData); 317 IntPtr testData = GetData(problemData.Dataset, variables, problemData.TestIndices, out nTestData); 318 319 nTrainData = Problem.ProblemData.TrainingPartition.Size; 320 nTestData = Problem.ProblemData.TestPartition.Size; 317 318 double[] trainData = GetData(problemData.Dataset, variables, problemData.TrainingIndices); 319 double[] testData = GetData(problemData.Dataset, variables, problemData.TestIndices); 320 321 int nTrainData = Problem.ProblemData.TrainingPartition.Size; 322 int nTestData = Problem.ProblemData.TestPartition.Size; 321 323 322 324 if (problemData.AllowedInputVariables.Any(iv => iv.Contains(" "))) … … 325 327 var inputVariableNames = string.Join(" ", problemData.AllowedInputVariables); 326 328 327 IntPtr cIndepNames = Marshal.StringToHGlobalAnsi(inputVariableNames); 328 IntPtr cDependentNames = Marshal.StringToHGlobalAnsi(problemData.TargetVariable); 329 // Dependent- and Independentnames are the variables from the test/train data, e.g. from "Korns_02.trn" indep: x y z v w dep: f(xs) 330 331 IntPtr cInitMethod = Marshal.StringToHGlobalAnsi(InitMethod); 332 IntPtr cGrowMethod = Marshal.StringToHGlobalAnsi(GrowMethod); 333 334 IntPtr cRoots = Marshal.StringToHGlobalAnsi(Roots); 335 IntPtr cNodes = Marshal.StringToHGlobalAnsi(Nodes); 336 IntPtr cNonTrig = Marshal.StringToHGlobalAnsi(NonTrig); 337 IntPtr cLeafs = Marshal.StringToHGlobalAnsi(Leafs); 338 339 IntPtr cName = Marshal.StringToHGlobalAnsi(problemName); 340 IntPtr cProblemTypeString = Marshal.StringToHGlobalAnsi(problemTypeString); 341 342 343 AddTestData(cIndepNames, cDependentNames, testData, nTestData); 344 AddTrainData(cIndepNames, cDependentNames, trainData, nTrainData); 329 AddTestData(inputVariableNames, problemData.TargetVariable, testData, nTestData); 330 AddTrainData(inputVariableNames, problemData.TargetVariable, trainData, nTrainData); 345 331 346 332 int numberOfUseableVariables = problemData.AllowedInputVariables.Count(); 347 333 348 InitSearch(MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, ZeroEpsilon, cInitMethod, cGrowMethod, sortType);334 InitSearch(MaxGen, PgeRptEpoch, PgeRptCount, PgeArchiveCap, PeelCnt, EvalrCount, ZeroEpsilon, InitMethod, GrowMethod, sortType); 349 335 350 336 // cUsableVars: list of indices into independent variables 351 InitTreeParams( cRoots, cNodes, cNonTrig, cLeafs, numberOfUseableVariables, MaxSize, MinSize, MaxDepth, MinDepth);352 353 InitProblem( cName, MaxIterations, HitRatio,337 InitTreeParams(Roots, Nodes, NonTrig, Leafs, numberOfUseableVariables, MaxSize, MinSize, MaxDepth, MinDepth); 338 339 InitProblem(Name, MaxIterations, HitRatio, 354 340 searchVar: 0, // SearchVar: index of dependent variables (this is always zero because we only have one target variable) 355 ProblemTypeString: cProblemTypeString, numProcs: numProc);341 ProblemTypeString: problemTypeString, numProcs: numProc); 356 342 357 343 var bestTestScore = int.MaxValue; … … 387 373 if (cancellationToken.IsCancellationRequested) break; 388 374 } 389 390 Marshal.FreeHGlobal(trainData);391 Marshal.FreeHGlobal(testData);392 393 Marshal.FreeHGlobal(cIndepNames);394 Marshal.FreeHGlobal(cDependentNames);395 396 Marshal.FreeHGlobal(cInitMethod);397 Marshal.FreeHGlobal(cGrowMethod);398 399 Marshal.FreeHGlobal(cRoots);400 Marshal.FreeHGlobal(cNodes);401 Marshal.FreeHGlobal(cNonTrig);402 Marshal.FreeHGlobal(cLeafs);403 404 Marshal.FreeHGlobal(cName);405 Marshal.FreeHGlobal(cProblemTypeString);406 375 407 376 // Results.Add(new Result("Execution time", new TimeSpanValue(this.ExecutionTime))); … … 444 413 } 445 414 446 private static IntPtr GetData(IDataset ds, IEnumerable<string> variableNames, IEnumerable<int> rows, out int n) { 447 415 private static double[] GetData(IDataset ds, IEnumerable<string> variableNames, IEnumerable<int> rows) { 448 416 var dim = variableNames.Count(); 449 417 double[] val = new double[rows.Count() * dim]; … … 457 425 r++; 458 426 } 459 460 n = val.Length; 461 462 // TODO: seems strange to marshal this explicitly, we can just send the data over to PGE 463 IntPtr data = Marshal.AllocHGlobal(sizeof(double) * val.Length); 464 Marshal.Copy(val, 0, data, val.Length); 465 466 return data; 427 return val; 467 428 } 468 429 }
Note: See TracChangeset
for help on using the changeset viewer.