/// <summary>Adds to referenced array the indexes of similar words of given seed word.</summary> /// <param name="words">Words sequence.</param> /// <param name="wrdIndex">The index of the seed word in the word's sequence.</param> /// <param name="lockedLetters">Array of signs indicated wich letter's position is locked on this step.</param> /// <param name="startPos">Started letter's position in seed word at wich search begins</param> /// <param name="mismatchCnt">Number of mismathes.</param> /// <param name="smlIndexes">Array of indexes of similar words.</param> /// <param name="currSmlIndex">Current writing position in array of indexes of similar words.</param> static void FillUnder(ref byte[] words, int wrdIndex, ref bool[] lockedLetters, int startPos, int mismatchCnt, ref Similars smlIndexes, ref int currSmlIndex) { byte j, originLetter, currLetter; int ind, i; mismatchCnt--; // reduce count of mismatches because of processing current mismatch's level for (i = startPos; i < Words.WordLength; i++) { if (!lockedLetters[i]) // pass locked letter { ind = i + wrdIndex; originLetter = words[ind]; // save original letter wich would be changed // substitute other letters for (j = 0; j < Chars.Count; j++) { //currLetter = Chars.GetCodeByIndex(j); currLetter = j; if (currLetter != originLetter) { words[ind] = currLetter; // substitute other letter smlIndexes[currSmlIndex++] = Words.GetNumber(words, wrdIndex); // save words index if (mismatchCnt > 0) // lock letter and pass down { lockedLetters[i] = true; // lock letter FillUnder(ref words, wrdIndex, ref lockedLetters, i + 1, mismatchCnt, ref smlIndexes, ref currSmlIndex); lockedLetters[i] = false; // unlock letter } } } words[ind] = originLetter; // restore original letter } } mismatchCnt++; // restore count of mismatches }
/// <summary> /// Converts the array of strings represented ScanWindows to its instance. /// </summary> /// <param name="inputs">Array of strings represented ScanWindows to convert.</param> /// <returns></returns> static ScanWindows Parse(ArrayList inputs) { int i = 0; string[] images; byte[] name; // read the name of first pattern and inizialise session by given word length string tmp = inputs[0] as string; Words.Init((byte)tmp.Substring(tmp.LastIndexOf(Abbr.TAB) + 1).Length, 0, false); ScanWindows sWins = new ScanWindows(inputs.Count, BY_DEF); // Default sort mode was set by writing to a file // initialises sWins member foreach (string str in inputs) { // each string contains 6 number separated by TAB images = str.Split(Abbr.TAB); name = Sequence.Parse(images[4]); sWins[i++] = new ScanWindow( int.Parse(images[0]), int.Parse(images[1]), //new Pattern(Words.GetNumber(name) * Words.WordLength, 1), Words.GetNumber(name), float.Parse(images[2]), float.Parse(images[3])); } return(sWins); }
///// <summary>Gets a copy of sorted Similars without elements with undefined number.</summary> ///// <returns>The sorted Similars without elements with undefined number</returns> //public Similars GetShrink() //{ // int i; // Array.Sort(_numbers); // for (i = 0; i < Count; i++) // if (this[i] == UndefNumber) // break; // Similars res = new Similars(i); // Array.Copy(_numbers, res._numbers, i); // return res; //} /// <summary>Returns the Coefficient of Variation.</summary> /// <param name="startIndex">The index in the input sequence at which scanning begins.</param> /// <param name="length">The length of scanning range.</param> /// <param name="seedNumber">The serial number of seed word.</param> /// <returns>Coefficient of Variation</returns> /// <remarks>This instance should be shrinked.</remarks> public float GetCV(int startIndex, int length, int seedNumber) { if (Count == 0) { return(0); } int i, j = 0, number, lastFound = -1; int stopInd = startIndex + length - Words.WordLength; // set the array of count of neighbouring word's (clusters) with max possible capacity int[] grps = new int[length]; int k = 1; for (i = startIndex; i < stopInd; i++) { number = Words.GetNumber(Sequence.Original, i); // this collection is sorted by increasing! if (number != seedNumber && Array.BinarySearch(_numbers, number) < 0) // word is not belong to similars { continue; } if (lastFound >= 0) // count began { if (i - lastFound <= _DepthOfClastering) // founded word is in group { k++; // increase group counter } else // founded word is out of group { grps[j++] = k; // close current group k = 1; // start new group } } lastFound = i; // remember current position } // fill sum Counter Average.SumCounter counter = new Average.SumCounter(); // check i<cnt needs when groups is full, in other words, when each "group" has just one member for (i = 0; i < Count && (k = grps[i]) > 0; i++) { counter += k; } return(new Average(counter).CV); }
/// <summary>Adds to referenced array the numbers of similar words for given seed word.</summary> /// <param name="startPos">Started letter's position in seed word at wich search begins</param> /// <param name="mismatchCnt">Number of mismathes.</param> static void FillUnder_recurs(byte startPos, byte mismatchCnt) { byte j, originLetter; for (byte i = startPos; i < Words.WordLength; i++) { originLetter = _word[i]; // save original letter wich would be changed // substitute other letters for (j = 0; j < Chars.Count; j++) { if (j != originLetter) { _word[i] = j; // substitute other letter _smlNumbers[_currSmlIndex++] = Words.GetNumber(_word); // save words index if (mismatchCnt > 1) // lock letter and pass down { FillUnder_recurs((byte)(i + 1), (byte)(mismatchCnt - 1)); } } } _word[i] = originLetter; // restore original letter } }
/// <summary>Initializes an instance of the Patterns.</summary> /// <param name="worker">The BackgroundWorker in wich Processing is run.</param> /// <param name="sequence">The 'names' sequence.</param> /// <param name="startIndex">The index in the inputArray at which scanning begins.</param> /// <param name="strictLength">The precise length of range of inputArray for scanning.</param> /// <param name="strictFreqs">The external array of strict frequencies; needed to avoid memory overallotment.</param> /// <param name="loopsPerStep">The amount of loops per one progress bar step; -1 if without progress bar steps.</param> /// <param name="maxFreqNumber">Return number of Pattern with max Frequence: if init value = -1, then no return.</param> /// <param name="isFirstCall">True if method is called first time in cycle; otherwise, false.</param> /// <param name="isDrawPlot">True if plot is drawing; otherwise, false.</param> /// <returns>Maximum frequency value in the instance; if init maxFreqNumber param = -1, then 0.</returns> int Init( BackgroundWorker worker, byte[] sequence, int startIndex, int strictLength, int[] strictFreqs, int loopsPerStep, ref int maxFreqNumber, bool isFirstCall, bool isDrawPlot) { #region Comments /* * Patterns are generated from inputArray sequence on 2 steps. * * Step 1: Initialization strict frequency * We create a new collection with the maximum possible length and an temporary array of strict frequencies. * Index of each element for both collections is equal to absolute word number, * so we scan through the input sequence and increase strict frequency in array directly by indexing. * * Step 2: Initialization Frequency * From each pattern in collection we added the Frequency of all his Similars. * In ALL POSSIBLE mode we search all the patterns, because their Similars may have Frequency > 0; * in FROM INPUT mode we skip zero-frequency patterns. * * In TURBO mode all the Similars are keeping in memory after first generation, * so we should go through the given Similars collection and adding thier Frequency directly by word number, equls to index in this instance. * In ORDINARY mode we receive the sum of Frequencies of all Similars through single call of Similars.SumUp(). * * So we receive the frequencies with mismatches for all the Patterns in this instance. * * This is the maximum fastest method because of using only direct indexing. * We scan input sequence just once? and no searches through collection are required. */ #endregion //System.Windows.Forms.MessageBox.Show(loopsPerStep.ToString()); int i, maxFreq = 0; worker.ReportProgress(TreatSCANNING); OnPrgBarReseat(); this.Clear(); Array.Clear(strictFreqs, 0, strictFreqs.Length); //System.Windows.Forms.MessageBox.Show(startIndex.ToString("startIndex= 0 ") + strictLength.ToString("strictLength= 0 ")); //*** Step_1: Look over all the patterns in a range of sequence for (i = 0; i <= strictLength; i++) { strictFreqs[Words.GetNumber(sequence, startIndex + i)]++; } //*** Step_2: expanding to mismatches int k; Similars smls; //Pattern ptn; // compare each Pattern to all the Pattern's in the same collection with a glance of mismathes; // collection is still unsorted for (i = 0; i < Count; i++) { if (worker.CancellationPending) // Canceled by user. { throw new ApplicationException(string.Empty); } if (IsAllPossible || (strictFreqs[i] > 0)) { //ptn = this[i]; if (Similars.IsTurbo) { smls = Similars.GetSimilars(i); for (k = 0; k < smls.Count; k++) { //ptn.Freq += strictFreqs[smls[k]]; this[i] += strictFreqs[smls[k]]; } } else { //ptn.Freq += Similars.SumUp(i, strictFreqs); this[i] += Similars.SumUp(i, strictFreqs); } //ptn.WordNumber = i; // initialize number //ptn.Freq += strictFreqs[i]; // frequency itself this[i] += strictFreqs[i]; // frequency itself //this[i] = ptn; // keep index of maximum Freq without sorting: needs only in case of Max Average Choice or Global task. //if (maxFreqNumber >= 0 && ptn.Freq > maxFreq) if (maxFreqNumber >= 0 && this[i] > maxFreq) { //maxFreq = ptn.Freq; maxFreq = this[i]; maxFreqNumber = i; } } OnPrgBarIncreased(worker, loopsPerStep); } if (isDrawPlot) { // if it needs to keep sorting by name, we should clone collection for drawing, // because it will be sorted by Freq during drawing worker.ReportProgress(TreatSORTING); worker.ReportProgress(Convert.ToInt32(isFirstCall), Clone()); } return(maxFreq); }