private List <Tree> ScoreTreesAndReturnKept(IEnumerable <Tree> trees, int generation) { List <Tree> keeps = new List <Tree>(); Logger.WriteLine("point count: " + dataPointMgr._pointsToTest.Count); foreach (var tree in trees) { GeneticAlgorithmRunResults results = null; //TODO figure out why a NullRefExc came through here if (tree == null) { continue; } //quick pass through for trees already processed if (!tree._isDirty) { tree._source = "pass through"; keeps.Add(tree); continue; } results = new GeneticAlgorithmRunResults(this); tree.ProcessDataThroughTree(dataPointMgr, results, dataPointMgr._pointsToTest); tree.RemoveZeroCountNodes(); GeneticOperations.PruneTreeOfUselessNodes(tree); //will add kepeers if there was no previous result or if the score improved or randomly if (tree._prevResults == null || results.MetricResult > tree._prevResults.MetricResult) { //only add the new tree to the results if the score improved keeps.Add(tree); } tree._isDirty = false; tree._prevResults = results; //now run a set of values through for the cross validation //TODO determine when to do the CV step, how many points to use, and what to do with the results /*TODO uncomment to get CV back, consider impact of node level matrices changing) * var cv_results = new GeneticAlgorithmRunResults(this); * tree.ProcessDataThroughTree(dataPointMgr, cv_results, dataPointMgr._pointsNotUsedToTest.TakeEvery(5)); * double loss_ratio = results.AverageLoss / cv_results.AverageLoss; * Logger.WriteLine(loss_ratio); */ } return(keeps); }
private List <Tree> CreateRandomPoolOfTrees(int size) { var trees = new List <Tree>(); Parallel.For(0, size, index => { var tree = GeneticOperations.CreateRandomTree(this); lock (trees) { trees.Add(tree); } }); return(trees); }