public void NoiseTest() { const int colDimSize = 64; const int noiseStepPercent = 5; var parameters = GetDefaultParams(); parameters.Set(KEY.POTENTIAL_RADIUS, 32 * 32); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, true); parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5); parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * colDimSize * colDimSize); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000); parameters.Set(KEY.MAX_BOOST, 0.0); parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.01); parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001); parameters.Set(KEY.SEED, 42); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { colDimSize, colDimSize }); var sp = new SpatialPoolerMT(); var mem = new Connections(); //var rnd = new Random(); parameters.apply(mem); sp.init(mem); List <int[]> inputVectors = new List <int[]>(); inputVectors.Add(getInputVector1()); inputVectors.Add(getInputVector2()); int vectorIndex = 0; int[][] activeArrayWithZeroNoise = new int[inputVectors.Count][]; foreach (var inputVector in inputVectors) { var x = getNumBits(inputVector); Debug.WriteLine(""); Debug.WriteLine($"----- VECTOR {vectorIndex} ----------"); //int[] activeArray = new int[64 * 64]; activeArrayWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize]; int[] activeArray = null; for (int j = 0; j < 25; j += noiseStepPercent) { Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------"); int[] noisedInput; if (j > 0) { noisedInput = ArrayUtils.flipBit(inputVector, (double)((double)j / 100.00)); } else { noisedInput = inputVector; } var d = MathHelpers.GetHammingDistance(inputVector, noisedInput, true); Debug.WriteLine($"Input with noise {j} - HamDist: {d}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}"); for (int i = 0; i < 10; i++) { //sp.compute( noisedInput, activeArray, true); activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[]; if (j > 0) { Debug.WriteLine($"{ MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); } } if (j == 0) { Array.Copy(activeArray, activeArrayWithZeroNoise[vectorIndex], activeArrayWithZeroNoise[vectorIndex].Length); } var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var d2 = MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Output with noise {j} - Ham Dist: {d2}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArrayWithZeroNoise[vectorIndex], (el) => el == 1))}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); List <int[, ]> arrays = new List <int[, ]>(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, 64, 64); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(noisedInput, 32, 32))); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(activeArray, 64, 64))); // NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5); NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize); } vectorIndex++; } // // Prediction code. // This part of code takes a single sample of every input vector and add // some noise to it. Then it predicts it. // Calculated hamming distance (percent overlap) between predicted output and output // trained without noise is final result, which should be higher than 95% (realistic guess). vectorIndex = 0; foreach (var inputVector in inputVectors) { double noise = 7; var noisedInput = ArrayUtils.flipBit(inputVector, noise / 100.00); int[] activeArray = new int[64 * 64]; sp.compute(noisedInput, activeArray, false); var dist = MathHelpers.GetHammingDistance(activeArrayWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Result for vector {vectorIndex++} with noise {noise} - Ham Dist: {dist}"); Assert.IsTrue(dist >= 95); } }
//[DataRow("MnistTestImages\\digit7.png", 128, 30)] public void CalculateSpeedOfLearningTest(string mnistImage, int[] imageSize, int[] topologies) { int index1 = mnistImage.IndexOf("\\") + 1; int index2 = mnistImage.IndexOf("."); string sub1 = mnistImage.Substring(0, index2); string sub2 = mnistImage.Substring(0, index1); string name = mnistImage.Substring(index1, sub1.Length - sub2.Length); for (int imSizeIndx = 0; imSizeIndx < imageSize.Length; imSizeIndx++) { string testName = $"{name}_{imageSize[imSizeIndx]}"; string outputSpeedFile = $"Output\\{testName}_speed.txt"; string inputBinaryImageFile = BinarizeImage("Output\\" + mnistImage, imageSize[imSizeIndx], testName); for (int topologyIndx = 0; topologyIndx < topologies.Length; topologyIndx++) { string finalName = $"{testName}_{topologies[topologyIndx]}"; string outputHamDistFile = $"Output\\{finalName}_hamming.txt"; string outputActColFile = $"Output\\{finalName}_activeCol.txt"; string outputImage = $"Output\\{finalName}.png"; int numOfActCols = 0; var sw = new Stopwatch(); using (StreamWriter swHam = new StreamWriter(outputHamDistFile)) { using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true)) { using (StreamWriter swActCol = new StreamWriter(outputActColFile)) { numOfActCols = topologies[topologyIndx] * topologies[topologyIndx]; var parameters = GetDefaultParams(); parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] }); parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] }); parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols); var sp = new SpatialPooler(); var mem = new Connections(); parameters.apply(mem); sw.Start(); sp.Init(mem); sw.Stop(); swSpeed.WriteLine($"{topologies[topologyIndx]}|{(double)sw.ElapsedMilliseconds / (double)1000}"); int actiColLen = numOfActCols; int[] activeArray = new int[actiColLen]; //Read input csv file into array int[] inputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray(); sw.Restart(); int iterations = 2; int[] oldArray = new int[activeArray.Length]; for (int k = 0; k < iterations; k++) { sp.compute(inputVector, activeArray, true); var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var distance = MathHelpers.GetHammingDistance(oldArray, activeArray); swHam.WriteLine(distance + "\n"); var str = Helpers.StringifyVector(activeCols); oldArray = new int[actiColLen]; activeArray.CopyTo(oldArray, 0); } var activeStr = Helpers.StringifyVector(activeArray); swActCol.WriteLine("Active Array: " + activeStr); sw.Stop(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, topologies[topologyIndx], topologies[topologyIndx]); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, outputImage); } } } } } }
private void DrawImage(Image img, Image img2, Graphics g) { int width, height; if (RandomSize) { int size = MathHelpers.Random(Math.Min(RandomSizeMin, RandomSizeMax), Math.Max(RandomSizeMin, RandomSizeMax)); width = size; height = size; if (img2.Width > img2.Height) { height = (int)Math.Round(size * ((double)img2.Height / img2.Width)); } else if (img2.Width < img2.Height) { width = (int)Math.Round(size * ((double)img2.Width / img2.Height)); } } else { width = img2.Width; height = img2.Height; } if (width < 1 || height < 1) { return; } int xOffset = img.Width - width - 1; int yOffset = img.Height - height - 1; Rectangle rect, overlapRect; int attemptCount = 0; do { attemptCount++; if (attemptCount > 1000) { return; } rect = new Rectangle(MathHelpers.Random(Math.Min(0, xOffset), Math.Max(0, xOffset)), MathHelpers.Random(Math.Min(0, yOffset), Math.Max(0, yOffset)), width, height); overlapRect = rect.Offset(NoOverlapOffset); } while (NoOverlap && imageRectangles.Any(x => x.IntersectsWith(overlapRect))); imageRectangles.Add(rect); if (RandomAngle) { float moveX = rect.X + (rect.Width / 2f); float moveY = rect.Y + (rect.Height / 2f); int rotate = MathHelpers.Random(Math.Min(RandomAngleMin, RandomAngleMax), Math.Max(RandomAngleMin, RandomAngleMax)); g.TranslateTransform(moveX, moveY); g.RotateTransform(rotate); g.TranslateTransform(-moveX, -moveY); } if (RandomOpacity) { float opacity = MathHelpers.Random(Math.Min(RandomOpacityMin, RandomOpacityMax), Math.Max(RandomOpacityMin, RandomOpacityMax)).Clamp(0, 100) / 100f; ColorMatrix matrix = new ColorMatrix(); matrix.Matrix33 = opacity; using (ImageAttributes attributes = new ImageAttributes()) { attributes.SetColorMatrix(matrix); g.DrawImage(img2, rect, 0, 0, img2.Width, img2.Height, GraphicsUnit.Pixel, attributes); } } else { g.DrawImage(img2, rect); } if (RandomAngle) { g.ResetTransform(); } }
public static bool IsPalindrome(int x) { return(MathHelpers.IsPalindrome(x)); }
public float Union(float d1, float d2) { return(MathHelpers.Min(d1, d2)); }
public float Intersection(float d1, float d2) { return(MathHelpers.Max(d1, d2)); }
public override string ToString() { int r = MathHelpers.GCD(ResolutionSize.Width, ResolutionSize.Height); return($"{ResolutionSize.Width} x {ResolutionSize.Height} | {ResolutionSize.Width / r} : {ResolutionSize.Height / r}"); }
/// <summary> /// This function train the input image and write result to text files in folder @"/OutputDutyCycle" /// The result text files include speed comparison between global inhibition and local inhibition, /// the stable of the out put array (by comparing hamming distance arrays). /// Finally this method draw an image of active column as .png file. /// This training method is used for testing speed of training with different value of max boost and duty cycle /// </summary> /// <param name="inputBinarizedFile">input image after binarized</param> /// <param name="hammingFile">Path to hamming distance output file</param> /// <param name="outputSpeedFile">Path to speed comparison output file</param> /// <param name="outputImage">Path to active column after training output file (as .png image file)</param> /// <param name="parameters">Parameter setup</param> private static void Training(string inputBinarizedFile, string hammingFile, string outputSpeedFile, string outputImage, Parameters parameters) { int outputImageSize = 1024; int topology = parameters.Get <int[]>(KEY.COLUMN_DIMENSIONS)[0]; int activeColumn = topology * topology; var stopwatch = new Stopwatch(); using (StreamWriter swHamming = new StreamWriter(hammingFile)) { using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true)) { var sp = new SpatialPooler(); var mem = new Connections(); parameters.apply(mem); stopwatch.Start(); sp.Init(mem); stopwatch.Stop(); int actiColumnLength = activeColumn; int[] activeArray = new int[actiColumnLength]; // Read input csv file into array int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray(); stopwatch.Restart(); int iterations = 1000; int[] oldArray = new int[activeArray.Length]; for (int k = 0; k < iterations; k++) { sp.compute(inputVector, activeArray, true); var activeCols = activeArray.IndexWhere((el) => el == 1); var distance = MathHelpers.GetHammingDistance(oldArray, activeArray); var similarity = MathHelpers.CalcArraySimilarity(oldArray, activeArray); swHamming.WriteLine($"{distance} | {similarity}"); var str = Helpers.StringifyVector(activeCols); Debug.WriteLine(str); oldArray = new int[actiColumnLength]; activeArray.CopyTo(oldArray, 0); } var activeArrayString = Helpers.StringifyVector(activeArray); stopwatch.Stop(); Debug.WriteLine("Active Array: " + activeArrayString); int potentialRadius = parameters.Get <int>(KEY.POTENTIAL_RADIUS); bool isGlobalInhibition = parameters.Get <bool>(KEY.GLOBAL_INHIBITION); string inhibition = isGlobalInhibition ? "Global" : "Local"; double milliseconds = stopwatch.ElapsedMilliseconds; double seconds = milliseconds / 1000; swSpeed.WriteLine($"Column dimension: {topology.ToString().PadRight(5)} |Potential Radius: {potentialRadius}| Inhibition type: {inhibition.PadRight(7)} | Total time: {milliseconds:N0} milliseconds ({seconds:N2} seconds)."); int[,] twoDimenArray = ArrayUtils.Make2DArray(activeArray, topology, topology); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimenArray, outputImageSize, outputImageSize, outputImage); } } }
/// <summary> /// Calculates the larger width and height required when the original texture is rotated. /// </summary> /// <returns>A vector <newWidth, newHeight>.</returns> public override RotationPacket getRotationPacketWithoutSensors(bool includeRotatedPixels = false) { // Find the borders (unmaximized) of the creature's texture Vector2 NWcoord = new Vector2(this.Position.X, this.Position.Y); Vector2 NEcoord = new Vector2(this.Position.X + Texture.Width, this.Position.Y); Vector2 SWcoord = new Vector2(this.Position.X, this.Position.Y + Texture.Height); Vector2 SEcoord = new Vector2(this.Position.X + Texture.Width, this.Position.Y + Texture.Height); // Rotate the points to find the actual global coordinates of the creature's corners Vector2 center = new Vector2(Texture.Width / 2, Texture.Height / 2); Point rotatedNWcoord = MathHelpers.rotateAroundPoint(Convert.ToInt32(NWcoord.X), Convert.ToInt32(NWcoord.Y), Convert.ToInt32(NWcoord.X + center.X), Convert.ToInt32(NWcoord.Y + center.Y), Heading); Point rotatedNEcoord = MathHelpers.rotateAroundPoint(Convert.ToInt32(NEcoord.X), Convert.ToInt32(NEcoord.Y), Convert.ToInt32(NWcoord.X + center.X), Convert.ToInt32(NWcoord.Y + center.Y), Heading); Point rotatedSWcoord = MathHelpers.rotateAroundPoint(Convert.ToInt32(SWcoord.X), Convert.ToInt32(SWcoord.Y), Convert.ToInt32(NWcoord.X + center.X), Convert.ToInt32(NWcoord.Y + center.Y), Heading); Point rotatedSEcoord = MathHelpers.rotateAroundPoint(Convert.ToInt32(SEcoord.X), Convert.ToInt32(SEcoord.Y), Convert.ToInt32(NWcoord.X + center.X), Convert.ToInt32(NWcoord.Y + center.Y), Heading); // Use the rotated corners to find the min and max values along the x and y dimensions int minX = Math.Min(Math.Min(rotatedNWcoord.X, rotatedNEcoord.X), Math.Min(rotatedSWcoord.X, rotatedSEcoord.X)); int minY = Math.Min(Math.Min(rotatedNWcoord.Y, rotatedNEcoord.Y), Math.Min(rotatedSWcoord.Y, rotatedSEcoord.Y)); int maxX = Math.Max(Math.Max(rotatedNWcoord.X, rotatedNEcoord.X), Math.Max(rotatedSWcoord.X, rotatedSEcoord.X)); int maxY = Math.Max(Math.Max(rotatedNWcoord.Y, rotatedNEcoord.Y), Math.Max(rotatedSWcoord.Y, rotatedSEcoord.Y)); // Return the new width, height, NW coord, and SW coord of the bounding box now that the creature (including its sensor field) has been rotated if (includeRotatedPixels) { // Calculate the size of the larger texture that we'll need int newWidth = maxX - minX; int newHeight = maxY - minY; int halfChangeInWidth = Convert.ToInt32(Math.Floor((newWidth - Texture.Width) / 2.0f)); int halfChangeInHeight = Convert.ToInt32(Math.Floor((newHeight - Texture.Height) / 2.0f)); int maxDimension = Math.Max(newWidth, newHeight); // Create a new array of pixels, initialized to transparent Color[] rotatedPixels = new Color[maxDimension * maxDimension]; for (int x = 0; x < maxDimension; x++) { for (int y = 0; y < maxDimension; y++) { rotatedPixels[x + (y * x)] = Color.Transparent; } } // Loop through the new texture and find the corresponding pixels in the original texture Point unrotatedPoint; Point textureCenter = new Point(Texture.Width / 2, Texture.Height / 2); for (int rotatedX = 0; rotatedX < Texture.Width; rotatedX++) { for (int rotatedY = 0; rotatedY < Texture.Height; rotatedY++) { // Find the (local) coordinates of the pixel after rotation based on the original texture's size unrotatedPoint = MathHelpers.UnRotateAroundPoint(rotatedX, rotatedY, textureCenter.X, textureCenter.Y, XNAHeading); if (rotatedX == 25 && rotatedY == 75) { Console.Write("hi"); } // Convert the rotatedpoint into the corresponding index into the rotated pixel array (accounting for the size difference) if (unrotatedPoint.X > -1 && unrotatedPoint.X < Texture.Width && unrotatedPoint.Y > -1 && unrotatedPoint.Y < Texture.Height) { rotatedPixels[(rotatedX + halfChangeInWidth) + (newWidth * (rotatedY + halfChangeInHeight))] = TextureAsColorArray[unrotatedPoint.X + (Texture.Width * unrotatedPoint.Y)]; } } } // Return the rotated pixel array return(new RotationPacket(newWidth, newHeight, new Point(minX, minY), new Point(maxX, maxY), rotatedPixels)); } else { return(new RotationPacket(maxX - minX, maxY - minY, new Point(minX, minY), new Point(maxX, maxY))); } }
public MinMaxStatCollector(string memberName, Configuration configuration, out bool cancel) { MemberName = memberName; cancel = false; _math = MathHelpers.GetMath <T>(); }
//returns the number of months left for the contract public int getMonthsLeft() { return(MathHelpers.GetMonthsBetween(GameObject.GetInstance().GameTime, this.ContractDate.AddYears(this.Length))); }
private string GetRulerText(Rectangle area) { Point endPos = new Point(area.X + area.Width - 1, area.Y + area.Height - 1); return(string.Format("X: {0} / Y: {1} / X2: {2} / Y2: {3}\nWidth: {4} px / Height: {5} px\nDistance: {6:0.00} px / Angle: {7:0.00}°", area.X, area.Y, endPos.X, endPos.Y, area.Width, area.Height, MathHelpers.Distance(area.Location, endPos), MathHelpers.LookAtDegree(area.Location, endPos))); }
public override void Decorate(ChunkColumn column, Biome biome, float[] thresholdMap, int x, int y, int z, bool surface, bool isBelowMaxHeight) { if (surface || column.GetBlock(x, y, z) != 1 && isBelowMaxHeight) { return; } if (isBelowMaxHeight && !surface) { int rx = column.x * 16 + x; int rz = column.z * 16 + z; var noise = _simplex.GetValue(rx, y, rz); foreach (var ore in Ores.Where(o => o.MinY <y && o.MaxY> y)) { var weightOffsets = (ore.MaxY > 30) ? HighWeightOffset : LowWeightOffset; if (MathHelpers.Abs(noise) * 3f < ore.Rarity) { double weight = 0; for (int i = 0; i < 4; i++) { weight += _random.NextDouble(); } weight /= ore.Rarity; weight = weightOffsets[0] - MathHelpers.Abs((float)weight - weightOffsets[1]); if (noise > weight) { int xOffset = 0; int zOffset = 0; int yOffset = 0; for (int i = 0; i < _random.Next(0, ore.Abundance); i++) { int offset = _random.Next(0, 3); double offset2 = _random.NextDouble(); if (offset.Equals(0) && offset2 < 0.4) { xOffset += 1; } else if (offset.Equals(1) && offset2 >= 0.4 && offset2 < 0.65) { yOffset += 1; } else { zOffset += 1; } var mX = Math.Min(x + xOffset, x); var my = Math.Min(y + yOffset, ore.MaxY); var mz = Math.Min(z + zOffset, z); if (column.GetBlock(mX, my, mz) != 1) { return; } column.SetBlock(mX, my, mz, ore.ID); } } } } } }
private static double CalculateExpectedNumberOfFalseAlarmsLog10( int numberOfPixels, int numberOfAlignedPoints, double precision, double logNumberOfTests) { // Binomial parameters var n = numberOfPixels; var k = numberOfAlignedPoints; var p = precision; if (n < 0) { throw new ArgumentOutOfRangeException(nameof(numberOfPixels), "Must be positive"); } if (k < 0) { throw new ArgumentOutOfRangeException(nameof(numberOfAlignedPoints), "Must be positive"); } if (k > n) { throw new ArgumentException("The number of aligned points cannot be greater than the total number of points/pixels"); } if (p <= 0.0 || p >= 1.1) { throw new ArgumentOutOfRangeException(nameof(precision), "The precision should be in the range [0, 1]"); } if (n == 0 || k == 0) { return(-logNumberOfTests); } if (n == k) { return(-logNumberOfTests - n * Math.Log10(p)); } var probabilityTerm = p / (1.0 - p); /* * compute the first term of the series * * binomial_tail(n,k,p) = sum_{i=k}^n bincoef(n,i) * p^i * (1-p)^{n-i} * where bincoef(n,i) are the binomial coefficients. * But * bincoef(n,k) = gamma(n+1) / ( gamma(k+1) * gamma(n-k+1) ). * We use this to compute the log of the first term. * * Below k is just i in the formulae above. */ var logBinomialCoefficient = MathHelpers.LogAbsoluteGamma(n + 1.0) - MathHelpers.LogAbsoluteGamma(k + 1.0) - MathHelpers.LogAbsoluteGamma(n - k + 1.0); var logFirstTerm = logBinomialCoefficient + k * Math.Log(p) + (n - k) * Math.Log(1.0 - p); var firstTerm = Math.Exp(logFirstTerm); if (firstTerm.IsRoughlyEqualTo(0.0)) { if (k > n * p) { return(-logFirstTerm / MathHelpers.NaturalLog10 - logNumberOfTests); } return(-logNumberOfTests); } var term = firstTerm; var binomialTail = firstTerm; for (var i = k + 1; i <= n; i++) { /* * As * term_i = bincoef(n,i) * p^i * (1-p)^(n-i) * and * bincoef(n,i)/bincoef(n,i-1) = n-1+1 / i, * then, * term_i / term_i-1 = (n-i+1)/i * p/(1-p) * and * term_i = term_i-1 * (n-i+1)/i * p/(1-p). * p/(1-p) is computed only once and stored in 'p_term'. */ var binomialTerm = (n - i + 1) * (1.0 / i); var multipliedTerm = binomialTerm * probabilityTerm; term *= multipliedTerm; binomialTail += term; if (!(binomialTerm < 1.0)) { continue; } /* * When bin_term<1 then mult_term_j<mult_term_i for j>i * (remaining terms will be smaller). * * Then, the error on the binomial tail when truncated at * the i term can be bounded by a geometric series of form * term_i * sum mult_term_i^j. */ var maximumPossibleError = term * ((1.0 - Math.Pow(multipliedTerm, n - i + 1)) / (1.0 - multipliedTerm) - 1.0); /* * One wants an error at most of tolerance*final_result, or: * tolerance * abs(-log10(bin_tail)-logNT). * Now, the error that can be accepted on bin_tail is * given by tolerance*final_result divided by the derivative * of -log10(x) when x=bin_tail. that is: * tolerance * abs(-log10(bin_tail)-logNT) / (1/bin_tail) * Finally, we truncate the tail if the error is less than: * tolerance * abs(-log10(bin_tail)-logNT) * bin_tail */ if (maximumPossibleError < MaximumErrorAllowed * Math.Abs(-Math.Log10(binomialTail) - logNumberOfTests) * binomialTail) { break; } } return(-Math.Log10(binomialTail) - logNumberOfTests); }
public void SparseSingleMnistImageTest(string trainingFolder, string digit, int imageSize, int columnTopology) { //Thread.Sleep(3000); string TestOutputFolder = $"Output-{nameof(SparseSingleMnistImageTest)}"; var trainingImages = Directory.GetFiles(Path.Combine(trainingFolder, digit)); //if (Directory.Exists(TestOutputFolder)) // Directory.Delete(TestOutputFolder, true); Directory.CreateDirectory(TestOutputFolder); Directory.CreateDirectory($"{TestOutputFolder}\\{digit}"); int counter = 0; var numOfActCols = columnTopology * columnTopology; ThreadSafeRandom rnd = new ThreadSafeRandom(42); var parameters = Parameters.getAllDefaultParameters(); parameters.Set(KEY.POTENTIAL_RADIUS, imageSize * imageSize); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, true); parameters.Set(KEY.RANDOM, rnd); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * 64 * 64); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.POTENTIAL_RADIUS, imageSize * imageSize); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.STIMULUS_THRESHOLD, 50.0); //*** parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); //*** parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.05); //*** //parameters.Set(KEY.STIMULUS_THRESHOLD, 0.0); //*** //parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.0); //*** //parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.0); //*** parameters.Set(KEY.INHIBITION_RADIUS, (int)0.025 * imageSize * imageSize); parameters.Set(KEY.SYN_PERM_CONNECTED, 0.2); parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.001); parameters.Set(KEY.MIN_PCT_ACTIVE_DUTY_CYCLES, 0.001); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000); parameters.Set(KEY.MAX_BOOST, 100); parameters.Set(KEY.WRAP_AROUND, true); parameters.Set(KEY.SEED, -1); parameters.setInputDimensions(new int[] { imageSize, imageSize }); parameters.setColumnDimensions(new int[] { columnTopology, columnTopology }); var sp = new SpatialPoolerParallel(); var mem = new Connections(); parameters.apply(mem); Stopwatch sw = new Stopwatch(); sw.Start(); sp.init(mem, UnitTestHelpers.GetMemory(new HtmConfig())); sw.Stop(); Debug.WriteLine($"Init time: {sw.ElapsedMilliseconds}"); int actiColLen = numOfActCols; int[] activeArray = new int[actiColLen]; string outFolder = $"{TestOutputFolder}\\{digit}\\{columnTopology}x{columnTopology}"; Directory.CreateDirectory(outFolder); string outputHamDistFile = $"{outFolder}\\digit{digit}_{columnTopology}_hamming.txt"; string outputActColFile = $"{outFolder}\\digit{digit}_{columnTopology}_activeCol.txt"; using (StreamWriter swHam = new StreamWriter(outputHamDistFile)) { using (StreamWriter swActCol = new StreamWriter(outputActColFile)) { foreach (var mnistImage in trainingImages) { FileInfo fI = new FileInfo(mnistImage); string outputImage = $"{outFolder}\\digit_{digit}_cycle_{counter}_{columnTopology}_{fI.Name}"; string testName = $"{outFolder}\\digit_{digit}_{fI.Name}_{columnTopology}"; string inputBinaryImageFile = Helpers.BinarizeImage($"{mnistImage}", imageSize, testName); //Read input csv file into array int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinaryImageFile).ToArray(); int numIterationsPerImage = 5; int[] oldArray = new int[activeArray.Length]; List <double[, ]> overlapArrays = new List <double[, ]>(); List <double[, ]> bostArrays = new List <double[, ]>(); for (int k = 0; k < numIterationsPerImage; k++) { sw.Reset(); sw.Start(); sp.compute(inputVector, activeArray, true); sw.Stop(); Debug.WriteLine($"Compute time: {sw.ElapsedMilliseconds}"); var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var distance = MathHelpers.GetHammingDistance(oldArray, activeArray); swHam.WriteLine($"{counter++}|{distance} "); oldArray = new int[actiColLen]; activeArray.CopyTo(oldArray, 0); //var mem = sp.GetMemory(layer); overlapArrays.Add(ArrayUtils.Make2DArray <double>(ArrayUtils.toDoubleArray(mem.Overlaps), columnTopology, columnTopology)); bostArrays.Add(ArrayUtils.Make2DArray <double>(mem.BoostedOverlaps, columnTopology, columnTopology)); } var activeStr = Helpers.StringifyVector(activeArray); swActCol.WriteLine("Active Array: " + activeStr); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, columnTopology, columnTopology); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); List <int[, ]> arrays = new List <int[, ]>(); arrays.Add(twoDimenArray); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(inputVector, (int)Math.Sqrt(inputVector.Length), (int)Math.Sqrt(inputVector.Length)))); const int OutImgSize = 1024; NeoCortexUtils.DrawBitmaps(arrays, outputImage, Color.Yellow, Color.Gray, OutImgSize, OutImgSize); //NeoCortexUtils.DrawHeatmaps(overlapArrays, $"{outputImage}_overlap.png", OutImgSize, OutImgSize, 150, 50, 5); //NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", OutImgSize, OutImgSize, 150, 50, 5); } } } }
/// <summary> /// This function train the input image and write result to text files in folder @"/Output" /// The result text files include speed comparison between global inhibition and local inhibition, /// the stable of the out put array (by comparing hamming distance arrays). /// Finally this method draw an image of active column as .png file. /// </summary> /// <param name="imageSize">Size of the image (image has same width and height)</param> /// <param name="columnDimension">List of sparse space size.(with same width and height)</param> /// <param name="inputBinarizedFile">input image after binarized</param> /// <param name="hammingFile">Path to hamming distance output file </param> /// <param name="outputSpeedFile">Path to speed comparison output file</param> /// <param name="activeColumnFile">Path to active column after training output file (as array text)</param> /// <param name="outputImage">Path to active column after training output file (as .png image file)</param> /// <param name="isGlobalInhibition">is using Global inhibition algorithms or not (if false using local inhibition)</param> private static void Training(int imageSize, int columnDimension, string inputBinarizedFile, string hammingFile, string outputSpeedFile, string activeColumnFile, string outputImage, bool isGlobalInhibition) { int outputImageSize = 1024; int activeColumn = columnDimension * columnDimension; var stopwatch = new Stopwatch(); using (StreamWriter swHamming = new StreamWriter(hammingFile)) { using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true)) { using (StreamWriter swActiveColumn = new StreamWriter(activeColumnFile)) { var parameters = SetupParameters(imageSize, columnDimension, isGlobalInhibition); var sp = new SpatialPooler(); var mem = new Connections(); parameters.apply(mem); stopwatch.Start(); sp.Init(mem); stopwatch.Stop(); int actiColumnLength = activeColumn; int[] activeArray = new int[actiColumnLength]; // Read input csv file into array int[] inputVector = NeoCortexUtils.ReadCsvFileTest(inputBinarizedFile).ToArray(); stopwatch.Restart(); int iterations = 300; int[] oldArray = new int[activeArray.Length]; for (int k = 0; k < iterations; k++) { sp.compute(inputVector, activeArray, true); var activeCols = activeArray.IndexWhere((el) => el == 1); var distance = MathHelpers.GetHammingDistance(oldArray, activeArray); var similarity = MathHelpers.CalcArraySimilarity(oldArray, activeArray); swHamming.WriteLine($"{distance} | {similarity}"); var str = Helpers.StringifyVector(activeCols); Debug.WriteLine(str); oldArray = new int[actiColumnLength]; activeArray.CopyTo(oldArray, 0); } stopwatch.Stop(); var activeArrayString = Helpers.StringifyVector(activeArray); swActiveColumn.WriteLine("Active Array: " + activeArrayString); string inhibition = isGlobalInhibition ? "Global" : "Local"; double milliseconds = stopwatch.ElapsedMilliseconds; double seconds = milliseconds / 1000; swSpeed.WriteLine($"Topology: {columnDimension.ToString().PadRight(5)} | Inhibition type: {inhibition.PadRight(7)} | Total time: {milliseconds:N0} milliseconds ({seconds:N2} seconds)."); int[,] twoDimenArray = ArrayUtils.Make2DArray(activeArray, columnDimension, columnDimension); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimenArray, outputImageSize, outputImageSize, outputImage); } } } }
public override void StartMovement() { apostleController.CheckForVerticalInput(); apostleStatusVariables.canJump = CheckGroundForJump(); apostleStatusVariables.isOnAir = apostleStatusVariables.CheckIsOnAir(); if (apostleStatusVariables.canClimbLadder && apostleController.ClimbLadderPress) { apostleStatusVariables.isClimbingLadder = apostleStatusVariables.canClimbLadder && apostleController.ClimbLadderPress || apostleStatusVariables.isClimbingLadder; } if (apostleStatusVariables.canClimbObstacle && apostleController.ClimbObstaclePress) { apostleStatusVariables.isClimbingObstacle = apostleStatusVariables.canClimbObstacle && apostleController.ClimbObstaclePress || apostleStatusVariables.isClimbingObstacle; } //Para velocidades ridiculamente altas, vai bugar if (apostleStatusVariables.isClimbingLadder && apostleStatusVariables.canJump) { var coroutine = CoroutineManager.FindCoroutine("ClimbOntoLadderCoroutine", this); if (coroutine != null && !coroutine.IsRunning) { PhysicsHelpers.IgnoreCollision(capsuleCollider2D, GetLadderPosition().GetComponent <LadderController>().adjacentCollider, false); apostleStatusVariables.isClimbingLadder = false; apostleController.RevokeControl(0.3f, false, ControlTypeToRevoke.AllMovement, monoBehaviour); PhysicsHelpers.SwitchGravity(rigidbody2D, true, currentGravityScale); PhysicsHelpers.ResetVelocityY(rigidbody2D); rigidbody2D.isKinematic = false; CoroutineManager.DeleteCoroutine("ClimbOntoLadderCoroutine", this); } } if (apostleStatusVariables.isClimbingObstacle && apostleStatusVariables.canJump) { var coroutine = CoroutineManager.FindCoroutine("ClimbOntoObstacleCoroutine", this); if (coroutine != null && !coroutine.IsRunning) { apostleStatusVariables.isClimbingObstacle = false; apostleController.RevokeControl(0.3f, false, ControlTypeToRevoke.AllMovement, monoBehaviour); PhysicsHelpers.SwitchGravity(rigidbody2D, true, currentGravityScale); rigidbody2D.isKinematic = false; CoroutineManager.DeleteCoroutine("ClimbOntoObstacleCoroutine", this); } } CheckForClimbingStairs(); if (apostleStatusVariables.isOnAir) { VerticalMovementState = VerticalMovementState.OnAir; } else { if (apostleStatusVariables.canClimbLadder && apostleController.ClimbLadderPress) { VerticalPressMovementState = VerticalPressMovementState.ClimbLadder; } else if (apostleStatusVariables.canClimbObstacle && apostleController.ClimbObstaclePress) { VerticalPressMovementState = VerticalPressMovementState.ClimbObstacle; } if (apostleStatusVariables.isClimbingLadder && !MathHelpers.Approximately(apostleController.VerticalMovement, 0, float.Epsilon)) { VerticalMovementState = VerticalMovementState.ClimbingLadder; } else if (apostleStatusVariables.isClimbingObstacle) { VerticalMovementState = VerticalMovementState.ClimbingObstacle; } else { VerticalMovementState = VerticalMovementState.Grounded; } } }
public void NoiseExperimentTest() { const int colDimSize = 64; const int noiseStepPercent = 5; var parameters = GetDefaultParams(); parameters.Set(KEY.POTENTIAL_RADIUS, 32 * 32); parameters.Set(KEY.POTENTIAL_PCT, 1.0); parameters.Set(KEY.GLOBAL_INHIBITION, true); parameters.Set(KEY.STIMULUS_THRESHOLD, 0.5); parameters.Set(KEY.INHIBITION_RADIUS, (int)0.01 * colDimSize * colDimSize); parameters.Set(KEY.LOCAL_AREA_DENSITY, -1); parameters.Set(KEY.NUM_ACTIVE_COLUMNS_PER_INH_AREA, 0.02 * colDimSize * colDimSize); parameters.Set(KEY.DUTY_CYCLE_PERIOD, 1000); parameters.Set(KEY.MAX_BOOST, 0.0); parameters.Set(KEY.SYN_PERM_INACTIVE_DEC, 0.008); parameters.Set(KEY.SYN_PERM_ACTIVE_INC, 0.01); parameters.Set(KEY.MIN_PCT_OVERLAP_DUTY_CYCLES, 0.0); parameters.Set(KEY.SEED, 42); parameters.setInputDimensions(new int[] { 32, 32 }); parameters.setColumnDimensions(new int[] { colDimSize, colDimSize }); var sp = new SpatialPoolerMT(); var mem = new Connections(); parameters.apply(mem); sp.Init(mem); List <int[]> inputVectors = new List <int[]>(); inputVectors.Add(getInputVector1()); inputVectors.Add(getInputVector2()); int vectorIndex = 0; int[][] activeColumnsWithZeroNoise = new int[inputVectors.Count][]; foreach (var inputVector in inputVectors) { var x = getNumBits(inputVector); Debug.WriteLine(""); Debug.WriteLine($"----- VECTOR {vectorIndex} ----------"); // Array of active columns with zero noise. The reference (ideal) output. activeColumnsWithZeroNoise[vectorIndex] = new int[colDimSize * colDimSize]; int[] activeArray = null; for (int j = 0; j < 25; j += noiseStepPercent) { Debug.WriteLine($"--- Vector {0} - Noise Iteration {j} ----------"); int[] noisedInput; if (j > 0) { noisedInput = ArrayUtils.FlipBit(inputVector, (double)((double)j / 100.00)); } else { noisedInput = inputVector; } // TODO: Try CalcArraySimilarity var d = MathHelpers.GetHammingDistance(inputVector, noisedInput, true); Debug.WriteLine($"Input with noise {j} - HamDist: {d}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(inputVector)}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(noisedInput)}"); for (int i = 0; i < 10; i++) { activeArray = sp.Compute(noisedInput, true, returnActiveColIndiciesOnly: false) as int[]; if (j > 0) { Debug.WriteLine($"{ MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true)} -> {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); } } if (j == 0) { Array.Copy(activeArray, activeColumnsWithZeroNoise[vectorIndex], activeColumnsWithZeroNoise[vectorIndex].Length); } var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); var d2 = MathHelpers.GetHammingDistance(activeColumnsWithZeroNoise[vectorIndex], activeArray, true); Debug.WriteLine($"Output with noise {j} - Ham Dist: {d2}"); Debug.WriteLine($"Original: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeColumnsWithZeroNoise[vectorIndex], (el) => el == 1))}"); Debug.WriteLine($"Noised: {Helpers.StringifyVector(ArrayUtils.IndexWhere(activeArray, (el) => el == 1))}"); List <int[, ]> arrays = new List <int[, ]>(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArray, 64, 64); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(noisedInput, 32, 32))); arrays.Add(ArrayUtils.Transpose(ArrayUtils.Make2DArray <int>(activeArray, 64, 64))); // NeoCortexUtils.DrawHeatmaps(bostArrays, $"{outputImage}_boost.png", 1024, 1024, 150, 50, 5); NeoCortexUtils.DrawBitmaps(arrays, $"Vector_{vectorIndex}_Noise_{j * 10}.png", Color.Yellow, Color.Gray, OutImgSize, OutImgSize); } vectorIndex++; } vectorIndex = OutputPredictionResult(sp, inputVectors, activeColumnsWithZeroNoise); }
/// <summary> /// Implements the experiment. /// </summary> /// <param name="cfg"></param> /// <param name="encoder"></param> /// <param name="inputValues"></param> private static void RunExperiment(HtmConfig cfg, EncoderBase encoder, List <int[]> inputValues) { // Creates the htm memory. var mem = new Connections(cfg); bool isInStableState = false; // // HPC extends the default Spatial Pooler algorithm. // The purpose of HPC is to set the SP in the new-born stage at the begining of the learning process. // In this stage the boosting is very active, but the SP behaves instable. After this stage is over // (defined by the second argument) the HPC is controlling the learning process of the SP. // Once the SDR generated for every input gets stable, the HPC will fire event that notifies your code // that SP is stable now. HomeostaticPlasticityController hpa = new HomeostaticPlasticityController(mem, inputValues.Count * 40, (isStable, numPatterns, actColAvg, seenInputs) => { // Event should only be fired when entering the stable state. // Ideal SP should never enter unstable state after stable state. if (isStable == false) { Debug.WriteLine($"INSTABLE STATE"); // This should usually not happen. isInStableState = false; } else { Debug.WriteLine($"STABLE STATE"); // Here you can perform any action if required. isInStableState = true; } }, requiredSimilarityThreshold: 0.975); // It creates the instance of Spatial Pooler Multithreaded version. SpatialPooler sp = new SpatialPoolerMT(hpa); // Initializes the sp.Init(mem); // Holds the indicies of active columns of the SDR. Dictionary <string, int[]> prevActiveColIndicies = new Dictionary <string, int[]>(); // Holds the active column SDRs. Dictionary <string, int[]> prevActiveCols = new Dictionary <string, int[]>(); // Will hold the similarity of SDKk and SDRk-1 fro every input. Dictionary <string, double> prevSimilarity = new Dictionary <string, double>(); // // Initiaize start similarity to zero. for (int i = 0; i < inputValues.Count; i++) { string inputKey = GetInputGekFromIndex(i); prevSimilarity.Add(inputKey, 0.0); prevActiveColIndicies.Add(inputKey, new int[0]); } // Learning process will take 1000 iterations (cycles) int maxSPLearningCycles = 1000; for (int cycle = 0; cycle < maxSPLearningCycles; cycle++) { //Debug.WriteLine($"Cycle ** {cycle} ** Stability: {isInStableState}"); // // This trains the layer on input pattern. for (int inputIndx = 0; inputIndx < inputValues.Count; inputIndx++) { string inputKey = GetInputGekFromIndex(inputIndx); int[] input = inputValues[inputIndx]; double similarity; int[] activeColumns = new int[(int)cfg.NumColumns]; // Learn the input pattern. // Output lyrOut is the output of the last module in the layer. sp.compute(input, activeColumns, true); // DrawImages(cfg, inputKey, input, activeColumns); var actColsIndicies = ArrayUtils.IndexWhere(activeColumns, c => c == 1); similarity = MathHelpers.CalcArraySimilarity(actColsIndicies, prevActiveColIndicies[inputKey]); Debug.WriteLine($"[i={inputKey}, cols=:{actColsIndicies.Length} s={similarity}] SDR: {Helpers.StringifyVector(actColsIndicies)}"); prevActiveCols[inputKey] = activeColumns; prevActiveColIndicies[inputKey] = actColsIndicies; prevSimilarity[inputKey] = similarity; if (isInStableState) { GenerateResult(cfg, inputValues, prevActiveColIndicies, prevActiveCols); return; } } } }
public float Subtract(float d1, float d2) { return(MathHelpers.Max(-d1, d2)); }
private void OnAddTrendLine(object sender, RoutedEventArgs e) { if (this.series.Values.Count == 0) { return; } // only do the visible points we have zoomed into. List <Point> points = new List <Point>(); DataValue first = null; DataValue last = null; double w = this.ActualWidth; foreach (DataValue d in this.series.Values) { Point point = scaleTransform.Transform(new Point(d.X, d.Y)); point = zoomTransform.Transform(point); if (point.X >= 0 && point.X <= w) { // then it is a visible point. if (first == null) { first = d; } last = d; points.Add(new Point(d.X, d.Y)); } } if (first == null) { return; } double a, b; // y = a + b.x MathHelpers.LinearRegression(points, out a, out b); Point start = new Point(first.X, a + (b * first.X)); Point end = new Point(last.X, a + (b * last.X)); double height = this.ActualHeight - 1; double availableHeight = height; double offset = Canvas.GetLeft(Graph); // scale start point Point point1 = scaleTransform.Transform(start); point1 = zoomTransform.Transform(point1); double y1 = availableHeight - point1.Y; // scale end point Point point2 = scaleTransform.Transform(end); point2 = zoomTransform.Transform(point2); double y2 = availableHeight - point2.Y; Line line = new Line() { Stroke = Graph.Stroke, StrokeThickness = 1, X1 = point1.X, Y1 = y1, X2 = point2.X, Y2 = y2, StrokeDashArray = new DoubleCollection(new double[] { 2, 2 }) }; AdornerCanvas.Children.Add(line); TextBlock startLabel = new TextBlock() { Text = String.Format("{0:N3}", start.Y), Foreground = Brushes.White, Margin = new Thickness(point1.X, y1 + 2, 0, 0) }; AdornerCanvas.Children.Add(startLabel); TextBlock endlabel = new TextBlock() { Text = String.Format("{0:N3}", end.Y), Foreground = Brushes.White }; endlabel.SizeChanged += (s, args) => { endlabel.Margin = new Thickness(point2.X - args.NewSize.Width - 10, y2 + 2, 0, 0); }; AdornerCanvas.Children.Add(endlabel); }
private void Update() { if (target == null) { return; } bool hasVerticalObject = false; bool azimuthalLock = false; bool azimuthalPerfectLock = false; bool verticalLock = false; bool verticalPerfectLock = false; Vector3 targetPosition = target.position; if (azimuthalTrackingObject != null) { Vector3 targetLocalPosition = MathHelpers.Azimuthal(azimuthalTrackingObject.transform.InverseTransformPoint(targetPosition)); float sinAngle = MathHelpers.CrossY(Vector3.forward, targetLocalPosition.normalized); // only the y component will be valid and we want it signed float absSinAngle = Mathf.Abs(sinAngle); if (absSinAngle > m_sinMaxErrorDegrees) { float direction = Mathf.Sign(sinAngle); azimuthalTrackingObject.Rotate(0, direction * Time.deltaTime * azimuthalSpeed, 0); azimuthalLock = absSinAngle <= m_sinLockDegrees; } else { azimuthalLock = true; azimuthalPerfectLock = true; } } if (verticalTrackingObject != null && azimuthalTrackingObject != null) { hasVerticalObject = true; // Transform both the target and the vertical rotating object into the // local coordinate system of the azimuthal object, whose xz-plane will // be our ground plane from which to measure vertical angle. Vector3 targetLocalVector = azimuthalTrackingObject.transform.InverseTransformPoint(targetPosition); Vector3 objectLocalVector = azimuthalTrackingObject.transform.InverseTransformVector(verticalTrackingObject.forward); // Compute the angles from the ground (or rather, the sine of the angles) float sinTargetAngle = targetLocalVector.y / targetLocalVector.magnitude; float sinObjectAngle = objectLocalVector.y / objectLocalVector.magnitude; // Clamp the target angle to allowable range sinTargetAngle = Mathf.Clamp(sinTargetAngle, m_sinMinVerticalAngle, m_sinMaxVerticalAngle); // Rotate appropriately to minimize error float deltaSinAngle = sinObjectAngle - sinTargetAngle; float absDeltaSinAngle = Mathf.Abs(deltaSinAngle); if (absDeltaSinAngle > m_deltaSinMaxErrorDegrees) { float direction = Mathf.Sign(deltaSinAngle); verticalTrackingObject.Rotate(direction * Time.deltaTime * verticalSpeed, 0, 0); verticalLock = absDeltaSinAngle <= m_sinLockDegrees; } else { verticalLock = true; verticalPerfectLock = true; } /* * // This code is a reference implementation that computes everything in * // angles but requires the use of slow arcsin functions * Vector3 targetLocalPosition = azimuthalTrackingObject.transform.InverseTransformPoint(targetPosition); * Vector3 objectLocalPosition = azimuthalTrackingObject.transform.InverseTransformVector(verticalTrackingObject.forward); * float deltaAngle = Mathf.Rad2Deg * (Mathf.Asin(targetLocalPosition.y / targetLocalPosition.magnitude) - Mathf.Asin(objectLocalPosition.y / objectLocalPosition.magnitude)); * if (Mathf.Abs(deltaAngle ) > maxErrorDegrees) * { * float direction = -Mathf.Sign(deltaAngle); * verticalTrackingObject.Rotate(direction * Time.deltaTime * verticalSpeed, 0, 0); * } */ } // Callbacks bool oldLockState = lockedOn; lockedOn = false; if (!hasVerticalObject && azimuthalLock) { lockedOn = true; perfectLock = azimuthalPerfectLock; if (OnLockObtained != null && oldLockState == false) { OnLockObtained(); } } else if (verticalLock && azimuthalLock) { lockedOn = true; perfectLock = azimuthalPerfectLock && verticalPerfectLock; if (OnLockObtained != null && oldLockState == false) { OnLockObtained(); } } if (OnLockLost != null && oldLockState == true && lockedOn == false) { OnLockLost(); } }
protected override long CalculateAnswer() { // must be at least 2 digits or more // allowable digits // left : 2, 3, 5, 7 (single digit primes) // middle : 1, 3, 7, 9 (0, 2, 4, 5, 6, 8 would not work because after right truncation to that digit it would be divisible by 2 or 5) // right : 3, 7 (single digit primes except 2 and 5 since it would not be prime for two or more digits) var answer = 0L; var countMiddleDigitsBound = 4; var isCompositeBySieve = MathHelpers.IndexCompositeBySieve(UpperBoundSieve(countMiddleDigitsBound)); WriteLineDetail("Incremental Results:"); for (var countMiddleDigits = 0; countMiddleDigits <= countMiddleDigitsBound; ++countMiddleDigits) { var maxIndex = CountDigitsBase4ToMaxIndex10(countMiddleDigits); for (var m = 0L; m <= maxIndex; ++m) { // enumerate all possibilities by converting m to base 4 and replacing digits var middleDigits = 0L; var factor10Middle = 1L; var mTemp = m; for (var digit = 0; digit < countMiddleDigits; ++digit) { middleDigits += factor10Middle * DigitMappingMiddle(mTemp % 4L); mTemp /= 4L; factor10Middle *= 10L; } for (var l = 0L; l < 4; ++l) { var leftAndMiddleDigits = factor10Middle * DigitMappingLeft(l) + middleDigits; var factor10LeftAndMiddle = factor10Middle * 10L; for (var r = 0L; r < 2; ++r) { var number = 10 * leftAndMiddleDigits + DigitMappingRight(r); var primeTruncatableLeft = true; var primeTruncatableRight = true; var numberTruncatedRight = number; while (numberTruncatedRight > 0) { if (isCompositeBySieve[numberTruncatedRight]) { primeTruncatableRight = false; break; } numberTruncatedRight /= 10L; } if (primeTruncatableRight) { var numberTruncatedLeft = number; var factorMod = factor10LeftAndMiddle; while (numberTruncatedLeft > 0) { if (isCompositeBySieve[numberTruncatedLeft]) { primeTruncatableLeft = false; break; } numberTruncatedLeft %= factorMod; factorMod /= 10L; } if (primeTruncatableLeft) { WriteLineDetail(number); answer += number; } } } } } } return(answer); }
private string GetAreaText(Rectangle area) { if (Mode == RegionCaptureMode.Ruler) { Point endPos = new Point(area.Right - 1, area.Bottom - 1); return(string.Format(Resources.RectangleRegion_GetRulerText_Ruler_info, area.X, area.Y, endPos.X, endPos.Y, area.Width, area.Height, MathHelpers.Distance(area.Location, endPos), MathHelpers.LookAtDegree(area.Location, endPos))); } return(string.Format(Resources.RectangleRegion_GetAreaText_Area, area.X, area.Y, area.Width, area.Height)); }
//creates the line between two airports private void createRouteLine(Airport a1, Airport a2, Panel panelMap, int zoom, Point margin, Airline airline, Boolean isStopoverRoute = false) { DoubleCollection dottedDash = new DoubleCollection(); dottedDash.Add(2); dottedDash.Add(2); int d = 50; double distance = MathHelpers.GetDistance(a1, a2); GeoCoordinate c1 = a1.Profile.Coordinates; int i = 0; System.Windows.Shapes.Path flightPath = new System.Windows.Shapes.Path(); flightPath.Stroke = new AirlineBrushConverter().Convert(airline) as SolidColorBrush; flightPath.StrokeThickness = 1; flightPath.Fill = new AirlineBrushConverter().Convert(airline) as SolidColorBrush; GeometryGroup flightGeometryGroup = new GeometryGroup(); while (i < distance) { GeoCoordinate c3 = MathHelpers.GetRoutePoint(c1, a2.Profile.Coordinates, d); Point pos1 = UIHelpers.WorldToTilePos(c1, zoom); Point pos2 = UIHelpers.WorldToTilePos(c3, zoom); /* * Line line = new Line(); * * if (isStopoverRoute) * line.StrokeDashArray = dottedDash; * /* * line.Stroke = new AirlineBrushConverter().Convert(airline) as SolidColorBrush; * line.X1 = Math.Min(panelMap.Width, pos1.X * ImageSize - margin.X * ImageSize); * line.X2 = Math.Min(panelMap.Width, pos2.X * ImageSize - margin.X * ImageSize); * line.Y1 = pos1.Y * ImageSize - margin.Y * ImageSize; * line.Y2 = pos2.Y * ImageSize - margin.Y * ImageSize; * * if (Math.Abs(line.X1 - line.X2) > ImageSize) * line.X2 = (line.X1 - line.X2) < 0 ? 0 : ImageSize * Math.Pow(2,zoom); */ double x1 = Math.Min(panelMap.Width, pos1.X * ImageSize - margin.X * ImageSize); double x2 = Math.Min(panelMap.Width, pos2.X * ImageSize - margin.X * ImageSize); double y1 = pos1.Y * ImageSize - margin.Y * ImageSize; double y2 = pos2.Y * ImageSize - margin.Y * ImageSize; if (Math.Abs(x1 - x2) > ImageSize) { x2 = (x1 - x2) < 0 ? 0 : ImageSize *Math.Pow(2, zoom); } LineGeometry flightGeometry = new LineGeometry(); flightGeometry.StartPoint = new Point(x1, y1); flightGeometry.EndPoint = new Point(x2, y2); flightGeometryGroup.Children.Add(flightGeometry); //panelMap.Children.Add(line); i += Math.Min(d, (int)(distance - i) + 1); c1 = c3; } flightPath.Data = flightGeometryGroup; panelMap.Children.Add(flightPath); }
public override void OnNodePositionUpdate() { Manager.ResizeNodes[(int)NodePosition.TopLeft].Position = StartPosition; Manager.ResizeNodes[(int)NodePosition.BottomRight].Position = EndPosition; if (!CenterNodeActive) { CenterPosition = new Point((int)MathHelpers.Lerp(StartPosition.X, EndPosition.X, 0.5f), (int)MathHelpers.Lerp(StartPosition.Y, EndPosition.Y, 0.5f)); } Manager.ResizeNodes[(int)NodePosition.Extra].Position = CenterPosition; }
protected void Update() { AttackDirection = MathHelpers.DegreeToVector2(transform.rotation.eulerAngles.z); Attack(AttackDirection); }
private bool Match(DynamicBuffer <Pattern> pattern, ref PatternInfo patternInfo, int2 gridPos, NativeArray <int> matchedBlocks) { var level = levelBufferLookup[levelInfo.entity]; var blockToMatch = level[MathHelpers.To1D(gridPos, levelInfo.size.x)]; var height = patternInfo.size.y; var width = patternInfo.size.x; var matchAll = true; var blockMatched = 0; for (int patternY = 0; patternY < height; ++patternY) { for (int patternX = 0; patternX < width; ++patternX) { // First : loop on the pattern to get the position in the level // from which to start the pattern match matchAll = true; blockMatched = 0; var patternOffset = new int2(patternX, patternY); var levelStart = gridPos - patternOffset; for (int levelY = 0; levelY < height; ++levelY) { for (int levelX = 0; levelX < width; ++levelX) { // Second : loop on the pattern and level at the same time to check if block match var localPos = new int2(levelX, levelY); var blockPos = levelStart + localPos; if (blockPos.x < 0 || blockPos.x >= levelInfo.size.x || blockPos.y < 0 || blockPos.y >= levelInfo.size.y ) { matchAll = false; break; } var levelBufferId = MathHelpers.To1D(blockPos, levelInfo.size.x); var block = level[levelBufferId]; var shouldMatch = pattern[MathHelpers.To1D(localPos, width)].match; var matchThis = !shouldMatch || block.blockId == blockToMatch.blockId; matchAll = matchAll && matchThis; if (matchThis && shouldMatch) { matchedBlocks[blockMatched] = levelBufferId + 1; blockMatched++; } // Debug.Log($"Pos : {blockPos} | Local : {localPos} | Should match ? {shouldMatch} | Match {blockToMatch.blockId} with {block.blockId} | Match this ? {matchThis} | All ? {matchAll}"); // TODO break early if no match } } if (matchAll) { break; } } if (matchAll) { break; } } // Debug.Log($"Match ? {matchAll}"); return(matchAll); }
private void OnAddTrendLine(object sender, RoutedEventArgs e) { if (this.series.Values.Count == 0) { return; } List <Point> points = new List <Point>(from d in this.series.Values select new Point(d.X, d.Y)); double a, b; // y = a + b.x MathHelpers.LinearRegression(points, out a, out b); DataValue first = this.series.Values.First(); DataValue last = this.series.Values.Last(); // now scale this line to fit the scaled graph Point start = new Point(first.X, a + (b * first.X)); Point end = new Point(last.X, a + (b * last.X)); double height = this.ActualHeight - 1; double availableHeight = height; double offset = Canvas.GetLeft(Graph); // scale start point Point point1 = scaleTransform.Transform(start); point1 = zoomTransform.Transform(point1); double y1 = availableHeight - point1.Y; // scale end point Point point2 = scaleTransform.Transform(end); point2 = zoomTransform.Transform(point2); double y2 = availableHeight - point2.Y; Line line = new Line() { Stroke = Graph.Stroke, StrokeThickness = 1, X1 = point1.X, Y1 = y1, X2 = point2.X, Y2 = y2, StrokeDashArray = new DoubleCollection(new double[] { 2, 2 }) }; AdornerCanvas.Children.Add(line); TextBlock startLabel = new TextBlock() { Text = String.Format("{0:N3}", start.Y), Foreground = Graph.Stroke, Margin = new Thickness(point1.X, y1 + 2, 0, 0) }; AdornerCanvas.Children.Add(startLabel); TextBlock endlabel = new TextBlock() { Text = String.Format("{0:N3}", end.Y), Foreground = Graph.Stroke }; endlabel.SizeChanged += (s, args) => { endlabel.Margin = new Thickness(point2.X - args.NewSize.Width - 10, y2 + 2, 0, 0); }; AdornerCanvas.Children.Add(endlabel); }
// [DataRow("MnistTestImages\\digit7.png", 128, 30)] public void LearningimageDoubleShiftStble(string mnistImage, string shiftedImage, int[] imageSize, int[] topologies) { var path = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, mnistImage); var pathShifted = Path.Combine(Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName, shiftedImage); Console.WriteLine("Test started"); Console.WriteLine(mnistImage); Console.WriteLine(shiftedImage); const int OutImgSize = 1024; int index1 = mnistImage.IndexOf("\\") + 1; int index2 = mnistImage.IndexOf("."); string sub1 = mnistImage.Substring(0, index2); string sub2 = mnistImage.Substring(0, index1); string name = mnistImage.Substring(index1, sub1.Length - sub2.Length); int index1Shift = shiftedImage.IndexOf("\\") + 1; int index2Shift = shiftedImage.IndexOf("."); string sub1Shift = shiftedImage.Substring(0, index2Shift); string sub2Shift = shiftedImage.Substring(0, index1Shift); string nameShift = shiftedImage.Substring(index1Shift, sub1Shift.Length - sub2Shift.Length); for (int imSizeIndx = 0; imSizeIndx < imageSize.Length; imSizeIndx++) { Console.WriteLine(String.Format("Image Size: \t{0}", imageSize[imSizeIndx])); string testName = $"{name}_{imageSize[imSizeIndx]}"; string outputSpeedFile = $"Output\\{testName}_speed.txt"; string testNameShifted = $"{nameShift}_{imageSize[imSizeIndx]}"; string outputSpeedFileShifted = $"Output\\{testNameShifted}_speed.txt"; string inputBinaryImageFile = BinarizeImage(path, imageSize[imSizeIndx], testName); string inputBinaryImageShiftedFile = BinarizeImage(pathShifted, imageSize[imSizeIndx], testNameShifted); //string inputBinaryImageFile = BinarizeImage("Output\\" + mnistImage, imageSize[imSizeIndx], testName); for (int topologyIndx = 0; topologyIndx < topologies.Length; topologyIndx++) { Console.WriteLine(String.Format("Topology: \t{0}", topologies[topologyIndx])); string finalName = $"{testName}_{topologies[topologyIndx]}"; string outputHamDistFile = $"Output\\{finalName}_hamming.txt"; string outputActColFile = $"Output\\{finalName}_activeCol.txt"; string outputImage = $"Output\\{finalName}.png"; string finalNameShifted = $"{testNameShifted}_{topologies[topologyIndx]}"; string outputHamDistFileShifted = $"Output\\{finalNameShifted}_hamming.txt"; string outputActColFileShifted = $"Output\\{finalNameShifted}_activeCol.txt"; string outputImageShifted = $"Output\\{finalNameShifted}.png"; //File.Create(finalName); //Directory.CreateDirectory("Output"); //File.Create(outputHamDistFile); //File.Create(outputActColFile); int numOfActCols = 0; var sw = new Stopwatch(); using (StreamWriter swHam = new StreamWriter(outputHamDistFile)) { using (StreamWriter swSpeed = new StreamWriter(outputSpeedFile, true)) { using (StreamWriter swActCol = new StreamWriter(outputActColFile)) { numOfActCols = topologies[topologyIndx] * topologies[topologyIndx]; var parameters = GetDefaultParams(); parameters.setInputDimensions(new int[] { imageSize[imSizeIndx], imageSize[imSizeIndx] }); parameters.setColumnDimensions(new int[] { topologies[topologyIndx], topologies[topologyIndx] }); parameters.setNumActiveColumnsPerInhArea(0.02 * numOfActCols); var sp = new SpatialPooler(); var mem = new Connections(); parameters.apply(mem); sw.Start(); sp.Init(mem); sw.Stop(); swSpeed.WriteLine($"{topologies[topologyIndx]}|{(double)sw.ElapsedMilliseconds / (double)1000}"); int actiColLen = numOfActCols; int[] activeArray = new int[actiColLen]; //Read input csv file into array int[] inputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray(); var inputOverlap = new List <double>(); var outputOverlap = new List <double>(); int[] newActiveArray = new int[topologies[topologyIndx] * topologies[topologyIndx]]; double[][] newActiveArrayDouble = new double[1][]; newActiveArrayDouble[0] = new double[newActiveArray.Length]; //Training sp.compute(inputVector, activeArray, true); var activeCols = ArrayUtils.IndexWhere(activeArray, (el) => el == 1); double[][] oldActiveArray = new double[1][]; oldActiveArray[0] = new double[activeArray.Length]; for (int a = 0; a < activeArray.Length; a++) { oldActiveArray[0][a] = activeArray[a]; } int isTrained = 0; while (isTrained == 0) { sp.compute(inputVector, newActiveArray, true); activeCols = ArrayUtils.IndexWhere(newActiveArray, (el) => el == 1); for (int a = 0; a < newActiveArray.Length; a++) { newActiveArrayDouble[0][a] = newActiveArray[a]; } if (MathHelpers.GetHammingDistance(oldActiveArray, newActiveArrayDouble, true)[0] == 100) { isTrained = 1; } else { isTrained = 0; oldActiveArray = newActiveArrayDouble; } } var str = Helpers.StringifyVector(activeCols); int[] oldInputVector = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageFile).ToArray(); int[] inputVectorShifted = NeoCortexUtils.ReadCsvIntegers(inputBinaryImageShiftedFile).ToArray(); int[] activeArrayShifted = new int[topologies[topologyIndx] * topologies[topologyIndx]]; double[][] activeArrayShiftedDouble = new double[1][]; activeArrayShiftedDouble[0] = new double[activeArrayShifted.Length]; sw.Restart(); //Prediction sp.compute(inputVectorShifted, activeArrayShifted, false); var resActiveColsPercent = ArrayUtils.IndexWhere(activeArrayShifted, (el) => el == 1); var resStrPercent = Helpers.StringifyVector(activeArrayShifted); for (int a = 0; a < activeArrayShifted.Length; a++) { activeArrayShiftedDouble[0][a] = activeArrayShifted[a]; } var distance = MathHelpers.GetHammingDistance(newActiveArrayDouble, activeArrayShiftedDouble, false)[0]; var distPercent = ((100 - distance) * 100) / (topologies[topologyIndx] * topologies[topologyIndx]); swHam.WriteLine(distance + "\t" + (100 - distance) + "\t" + distPercent + "\t" + (1 - (distPercent / 100.0)) + "\n"); outputOverlap.Add(1 - (distPercent / 100.0)); swActCol.WriteLine(String.Format(@"Active Cols: {0}", Helpers.StringifyVector(resActiveColsPercent))); Console.WriteLine(resStrPercent); swActCol.WriteLine("Active Array: " + resStrPercent); sw.Stop(); int[,] twoDimenArray = ArrayUtils.Make2DArray <int>(activeArrayShifted, topologies[topologyIndx], topologies[topologyIndx]); twoDimenArray = ArrayUtils.Transpose(twoDimenArray); NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, outputImage); //NeoCortexUtils.DrawBitmap(twoDimenArray, OutImgSize, OutImgSize, "D:\\Project_Latest\\FromGit\\se-dystsys-2018-2019-softwareengineering\\outputs\\eight"); swActCol.WriteLine("inputOverlaps: " + Helpers.StringifyVector(inputOverlap.ToArray())); swActCol.WriteLine("outputOverlaps: " + Helpers.StringifyVector(outputOverlap.ToArray())); } } } } } }
// Use this for initialization void Start() { m_startled = false; m_state = CrowState.Resting; m_startedTakeoff = false; m_animation = this.transform.FindChild("CrowAnims2").GetComponent<Animation>(); m_takeOffAnimation = GetComponent<Animation>(); MathHelp = GetComponent<MathHelpers>(); m_lastPos = this.transform.position; m_audio = GetComponent<AudioSource>(); }