public List <ISpatialmHGResult> SpatialmHGWrapper3D(List <Tuple <double, double, double, bool> > input, bool runViaAzure = false) { var coordinates = input.Select(c => (ICoordinate) new Coordinate3D(c.Item1, c.Item2, c.Item3)).ToList(); Normalizer nrm = new Normalizer(coordinates); var normcoords = nrm.Normalize(coordinates).Select(c => (Coordinate3D)c).ToList(); var labels = input.Select(c => c.Item4).ToList(); InitializeMHG(labels); int idx = -1; var solutions = new ConcurrentPriorityQueue <double, SpatialmHGResult3D>(); var planeList = new ConcurrentPriorityQueue <double, Plane>(); //minheap based, smaller is better //Foreach perpendicular bisector plane for (var i = 0; i < coordinates.Count; i++) { for (var j = 0; j < coordinates.Count; j++) { if (labels[i] != labels[j]) { //Reduce to 2D problem var plane = Plane.Bisector(normcoords[i], normcoords[j]); planeList.Enqueue(1.0, plane); } } } var numPlanes = planeList.Count(); if ((Config.ActionList & Actions.Search_EmpricalSampling) != 0) { var problem = normcoords.Zip(labels, (a, b) => new Tuple <ICoordinate, bool>(a, b)).ToList(); var gr = new Gridding(); var problemSize = MathExtensions.Binomial(numPlanes, 3) + MathExtensions.Binomial(numPlanes, 2) + numPlanes + 1; gr.GenerateEmpricialDensityGrid((long)Math.Min(problemSize, 100000), problem); var results = new ConcurrentPriorityQueue <double, ISpatialmHGResult>(); Parallel.ForEach(gr.GetPivots(), pivot => { var binvec = problem.OrderBy(c => c.Item1.EuclideanDistance(pivot)).Select(c => c.Item2).ToArray(); var res = mHGJumper.minimumHypergeometric(binvec); results.Enqueue(res.Item1, new SpatialmHGResult3D(res.Item1, res.Item2, (Coordinate3D)pivot)); while (results.Count > Config.GetTopKResults) { results.TryDequeue(out var junk); } }); return(results.Select(v => v.Value).ToList()); } if ((Config.ActionList & Actions.Search_CellSkipping) != 0) { if (runViaAzure) { var fileList = new List <string>(); foreach (var file in Directory.EnumerateFiles(@"3D\Planes\")) { File.Delete(file); } foreach (var file in Directory.EnumerateFiles(@"3D\2dProblems\")) { File.Delete(file); } var asList = planeList.ToList(); Parallel.ForEach(asList, currPlane => { var currIdx = Interlocked.Increment(ref idx); Console.Write($"\r\r\r\r\r\rGenerating 2D projection {currIdx}/{numPlanes}."); var plane = currPlane.Value; if (StaticConfigParams.WriteToCSV) { Config.Log.WriteLine("Selected plane {0}/{1} at distance {2}", currIdx, numPlanes, currPlane.Key); } var subProblemIn2D = plane.ProjectOntoAndRotate(normcoords, out PrincipalComponentAnalysis pca); pca.NumberOfOutputs = 3; //project back to 3D pca.Save($@"3D\PCA\pca{currIdx}.bin"); Generics.SaveToCSV(plane, $@"3D\Planes\plane{currIdx}.csv", true); Generics.SaveToCSV(subProblemIn2D.Zip(labels, (c, l) => c.ToString() + "," + l), $@"3D\2dProblems\coords{currIdx}.csv", true); fileList.Add($@"3D\2dProblems\coords{currIdx}.csv"); }); Console.WriteLine(@"Done. Initializing Batch pool."); AzureBatchExecution.MainAsync(fileList).Wait(); } else { while (planeList.TryDequeue(out var currPlane)) { var plane = currPlane.Value; idx++; if (StaticConfigParams.WriteToCSV) { Generics.SaveToCSV(plane, $@"Planes\plane{idx}.csv", true); } Config.Log.WriteLine("Selected plane {0}/{1} at distance {2}", idx, numPlanes, currPlane.Key); var subProblemIn2D = plane.ProjectOntoAndRotate(normcoords, out PrincipalComponentAnalysis pca); pca.NumberOfOutputs = 3; //project back to 3D //Solve 2D problem StaticConfigParams.filenamesuffix = idx.ToString(); var res = Solve2DProblem(subProblemIn2D, labels, normcoords, pca); foreach (var mHGresult2D in res) { var projectedResult = new SpatialmHGResult3D(mHGresult2D, pca, idx); solutions.Enqueue(projectedResult.pvalue, projectedResult); } solutions.TryPeek(out var bestCell); var bestCellCenter = bestCell.Value.GetCenter(); var remainingPlanes = planeList.Select(t => t.Value).ToList(); planeList.Clear(); foreach (var p in remainingPlanes) { planeList.Enqueue(bestCellCenter.DistanceToPlane(p), p); } } } //Combine 2D solutions var combinedResultsNaive = new List <SpatialmHGResult3D>(); for (var i = 0; i < Config.GetTopKResults; i++) { KeyValuePair <double, SpatialmHGResult3D> bestCell; solutions.TryDequeue(out bestCell); if (bestCell.Key <= Config.SIGNIFICANCE_THRESHOLD) { bestCell.Value.Denormalize(nrm); combinedResultsNaive.Add(bestCell.Value); } else { break; } } Config.Log.updater?.Wait(); return(combinedResultsNaive.Cast <ISpatialmHGResult>().ToList()); } return(null); }
private List <SpatialmHGResult> Solve2DProblem(List <Coordinate> coords, List <bool> labels, List <Coordinate3D> projectedFrom = null, PrincipalComponentAnalysis pca = null) { var T = new Tesselation(coords, labels, new List <string>(), Config) { pca = pca }; if (projectedFrom != null) { T.ProjectedFrom = projectedFrom.Cast <ICoordinate>().ToList(); } IEnumerable <Cell> topResults = null; if ((Config.ActionList & Actions.Search_CellSkipping) != 0) { topResults = T.GradientSkippingSweep(numStartCoords: 20, numThreads: Environment.ProcessorCount - 1); Tesselation.Reset(); return(topResults.Select(t => new SpatialmHGResult(t)).ToList()); } if ((Config.ActionList & Actions.Search_Exhaustive) != 0) { T.GenerateFromCoordinates(); } if ((Config.ActionList & Actions.Search_Originals) != 0) { //mHGOnOriginalPoints(args, coordinates, labels, numcoords); } if ((Config.ActionList & Actions.Search_FixedSet) != 0) { /* * var avgX = coordinates.Select(c => c.GetDimension(0)).Average(); * var avgY = coordinates.Select(c => c.GetDimension(1)).Average(); * var cord = new Coordinate(avgX, avgY); * mHGOnOriginalPoints(args, coordinates, labels, numcoords, new List<ICoordinate>() { cord }); */ } if ((Config.ActionList & Actions.Search_LineSweep) != 0) { T.LineSweep(); } if ((Config.ActionList & Actions.Search_EmpricalSampling) != 0) { var problem = coords.Zip(labels, (a, b) => new Tuple <ICoordinate, bool>(a, b)).ToList(); var gr = new Gridding(); var problemSize = MathExtensions.Binomial(Line.Count, 2) + Line.Count + 1; gr.GenerateEmpricialDensityGrid((long)Math.Min(problemSize, 100000), problem); var results = new ConcurrentPriorityQueue <double, SpatialmHGResult>(); Parallel.ForEach(gr.GetPivots(), pivot => { var binvec = problem.OrderBy(c => c.Item1.EuclideanDistance(pivot)).Select(c => c.Item2).ToArray(); var res = mHGJumper.minimumHypergeometric(binvec); results.Enqueue(res.Item1, new SpatialmHGResult(res.Item1, res.Item2, (Coordinate)pivot)); while (results.Count > Config.GetTopKResults) { results.TryDequeue(out var junk); } }); return(results.Select(v => v.Value).ToList()); } return(null); }