/// <summary> /// This is meant to be a generic prune method. It returns the top weights, and normalizes them so the sum of the smaller /// set is the same as the sum of the larger set /// </summary> private static Tuple<int, double>[] Prune(double[] weights, int count) { // Convert the weights into a list with the original index Tuple<int, double>[] weightsIndexed = new Tuple<int, double>[weights.Length]; for (int cntr = 0; cntr < weights.Length; cntr++) { weightsIndexed[cntr] = new Tuple<int, double>(cntr, weights[cntr]); } if (count > weights.Length) { // This method shouldn't have been called, there is nothing to do return weightsIndexed; } Tuple<int, double>[] topWeights = weightsIndexed.OrderByDescending(o => Math.Abs(o.Item2)).Take(count).ToArray(); double sumWeights = weights.Sum(o => Math.Abs(o)); double sumTop = topWeights.Sum(o => Math.Abs(o.Item2)); double ratio = sumWeights / sumTop; if (double.IsNaN(ratio)) { ratio = 1d; // probably divide by zero } // Normalize the top weights return topWeights.Select(o => new Tuple<int, double>(o.Item1, o.Item2 * ratio)).ToArray(); }
private Tuple<Point, List<Point>>[] unique(Tuple<Point, List<Point>>[] killPoints) { var uniqueEffects = new List<Tuple<Point, List<Point>>>(); foreach (var killPoint in killPoints.OrderByDescending(t=>t.Item2.Count)) { var isCoveredByExistingPoint = uniqueEffects.Any(existingKillPoint => killPoint.Item2.All(p=> existingKillPoint.Item2.Contains(p)) ); if (!isCoveredByExistingPoint) { //Console.Error.WriteLine(" + Adding test of {0}", killPoint.Item1); uniqueEffects.Add(killPoint); } else { //Console.Error.WriteLine(" - Skipping test of {0}", killPoint.Item1); } } return uniqueEffects.ToArray(); }
public static string sanitizeVars(this string inputCode) { replacementVars = memory.getVariableNames().Select((m, i) => new Tuple<string, string>(m, createHash(i))).ToArray(); replacementVars = replacementVars.OrderByDescending(n => n.Item1.Length).ToArray(); for (int i = 0; i < replacementVars.Length; i++) { inputCode = inputCode.Replace(replacementVars[i].Item1, "(" + replacementVars[i].Item2 + ")"); } Array.Reverse(replacementVars); return inputCode; }
private static void Main() { var img = new Bitmap(Image.FromFile(".\\Fingerprints\\102_5.tif")); var imgBytes = new int[img.Size.Width, img.Size.Height]; for (var x = 0; x < img.Size.Width; x++) for (var y = 0; y < img.Size.Height; y++) { var color = img.GetPixel(x, y); imgBytes[x, y] = (int)(.299 * color.R + .587 * color.G + .114 * color.B); } var orfield = OrientationFieldGenerator.GenerateOrientationField(imgBytes); //VSCOMEdetector(orfield); int nFilters = 7; int nBands = 3; int bandRadius = 24; int holeRadius = 14; int nSectors = 9; var filterbank = new List<List<Tuple<int, int, double>>>(); int[,] filtersInt = new int[32,nFilters*32]; for (int i = 0; i < nFilters; i++) { filterbank.Add(CreateGaborFilter((1.0/10), Math.PI/nFilters*i)); } // this code was used to test the similarity of the fingerprints //var min = filterbank.SelectMany(x=>x).Select(x=>x.Item3).Min(); //var max = filterbank.SelectMany(x=>x).Select(x=>x.Item3).Max(); //for (int i = 0; i < nFilters; i++) //{ // var filter = filterbank[i]; // foreach (var tuple in filter) // { // filtersInt[tuple.Item1, tuple.Item2 + i*32] = (int) ((tuple.Item3 - min)/(max - min)*255); // } //} //Common.SaveAndShowImage(filtersInt); int size = nBands * nSectors*nFilters; int num = 60000; List<double>[] dbase = new List<double>[num]; var rand = new Random(); for (int w = 0; w < num; w++) { var lst = new List<double>(); for(int s=0;s<size;s++) { lst.Add(rand.NextDouble() * 128.0f); } dbase[w] = lst; } using (FileStream fs = new FileStream("C:\\temp\\fusrodah_cpu_results.csv", FileMode.Create)) { using (StreamWriter streamWriter = new StreamWriter(fs)) { streamWriter.WriteLine("Find;Create;Match;Top"); for (int n = 0; n < 20; n++) { int radius = nBands*bandRadius + holeRadius + 16; //mask size var sw = new Stopwatch(); sw.Start(); PerformNewDetectionAlgorithmTestfire(imgBytes); streamWriter.Write(sw.ElapsedMilliseconds + ";"); sw.Stop(); sw.Restart(); var normalizedImage = NormalizeImage(imgBytes, nBands, holeRadius, bandRadius, 265, 292); //Common.SaveAndShowImage(normalizedImage); var filteredImages = new List<int[,]>(); for (int i = 0; i < nFilters; i++) filteredImages.Add(null); Parallel.ForEach(filterbank, new ParallelOptions() {MaxDegreeOfParallelism = 4}, filter => { var filtered = FilterWithGaborFilter(normalizedImage, filter, nBands, holeRadius, bandRadius, 265, 292); //Common.SaveAndShowImage(filtered); filteredImages[filterbank.IndexOf(filter)] = filtered; } ); var fingercode = new List<double>(); for (int i = 0; i < nFilters*nBands*nSectors; i++) { fingercode.Add(0); } Parallel.ForEach(filteredImages, new ParallelOptions {MaxDegreeOfParallelism = 4}, filteredImgBytes => { var fingercodePart = FormFingerCode(filteredImgBytes, nSectors, nBands, holeRadius, bandRadius); int startIndex = filteredImages.IndexOf(filteredImgBytes)* nBands*nSectors; foreach (var d in fingercodePart) { fingercode[startIndex++] = d; } }); sw.Stop(); streamWriter.Write(sw.ElapsedMilliseconds + ";"); //Common.SaveFingerCode(fingercode, nBands, nSectors, nFilters, bandRadius, holeRadius); sw.Restart(); Tuple<int, double>[] result = new Tuple<int, double>[num]; Parallel.ForEach(new[] {0, 1, 2, 3}, new ParallelOptions() {MaxDegreeOfParallelism = 4}, (offset) => { for (int i = 0; i < num/4; i++) { var index = i*4 + offset; result[index] = Tuple.Create(index, CalculateDistance(fingercode, dbase[index])); } }); sw.Stop(); streamWriter.Write(sw.ElapsedMilliseconds + ";"); sw.Restart(); result = result.OrderByDescending(x => x.Item2).ToArray(); sw.Stop(); streamWriter.WriteLine(sw.ElapsedMilliseconds); } } } }
public static Tuple<int[], int, int[], int, int> BeamSerach(int[] board) { var results = new Tuple<int[], int, int[], int, int>[30];//board, score, route, startRow, startColumn; Parallel.For(0, 6 * 5, idx => { var route = new int[50]; int tmpScore = 0; fixed (int* b = board, r = route) { tmpScore = BeamSearch(b, 1, 42, 400, idx / 6, idx % 6, r); } results[idx] = Tuple.Create(route, tmpScore, route, idx / 6, idx % 6); }); return results.OrderByDescending(x => x.Item2).First(); }
private static Tuple<Point3D, Point3D, double>[] Prune_Pre(Tuple<Point3D, Point3D, double>[] existing, int count) { // Sort it so the smaller links have a higher chance of being removed var retVal = existing. OrderByDescending(o => Math.Abs(o.Item3)). // the weights could be negative, so sort on distance from zero ToList(); Random rand = StaticRandom.GetRandomForThread(); while (retVal.Count > count) { // Choose a random item to remove - favor the end of the list (where the smallest links are) double percent = rand.NextPow(10); int index = UtilityCore.GetIndexIntoList(percent, retVal.Count); retVal.RemoveAt(index); } return retVal.ToArray(); }