Example #1
0
        /// <summary>
        /// Generate an isolation list containing multiplexed windows, attempting to minimize the number
        /// and frequency of repeated window pairings within each scan.
        /// </summary>
        /// <param name="writer">writer to write results</param>
        /// <param name="windowsPerScan">how many windows are contained in each scan</param>
        /// <param name="progressMonitor">progress monitor</param>
        private void WriteMultiplexedWindows(TextWriter writer, int windowsPerScan, IProgressMonitor progressMonitor)
        {
            int    maxInstrumentWindows = Assume.Value(_maxInstrumentWindows);
            int    windowCount          = IsolationScheme.PrespecifiedIsolationWindows.Count;
            int    cycleCount           = maxInstrumentWindows / windowCount;
            double totalScore           = 0.0;

            // Prepare to generate the best isolation list possible within the given time limit.
            var            startTime       = DateTime.UtcNow; // Said to be 117x faster than Now and this is for a delta
            var            cycle           = new Cycle(windowCount, windowsPerScan);
            int            cyclesGenerated = 0;
            ProgressStatus status          = new ProgressStatus(Resources.AbstractDiaExporter_WriteMultiplexedWindows_Exporting_Isolation_List);

            progressMonitor.UpdateProgress(status);

            // Generate each cycle.
            for (int cycleNumber = 1; cycleNumber <= cycleCount; cycleNumber++)
            {
                // Update status.
                if (progressMonitor.IsCanceled)
                {
                    return;
                }
                progressMonitor.UpdateProgress(status.ChangePercentComplete(
                                                   (int)(DateTime.UtcNow - startTime).TotalSeconds * 100 / CalculationTime).ChangeMessage(
                                                   string.Format(Resources.AbstractDiaExporter_WriteMultiplexedWindows_Exporting_Isolation_List__0__cycles_out_of__1__,
                                                                 cycleNumber - 1, cycleCount)));

                double secondsRemaining = CalculationTime - (DateTime.UtcNow - startTime).TotalSeconds;
                double secondsPerCycle  = secondsRemaining / (cycleCount - cycleNumber + 1);
                var    endTime          = DateTime.UtcNow.AddSeconds(secondsPerCycle);

                Cycle bestCycle = null;
                do
                {
                    // Generate a bunch of cycles, looking for one with the lowest score.
                    const int attemptCount = 50;
                    for (int i = 0; i < attemptCount; i++)
                    {
                        cycle.Generate(cycleNumber);
                        if (bestCycle == null || bestCycle.CycleScore > cycle.CycleScore)
                        {
                            bestCycle = new Cycle(cycle);
                            if (bestCycle.CycleScore == 0.0)
                            {
                                cyclesGenerated += i + 1 - attemptCount;
                                endTime          = DateTime.UtcNow; // Break outer loop.
                                break;
                            }
                        }
                    }
                    cyclesGenerated += attemptCount;
                } while (DateTime.UtcNow < endTime);

                // ReSharper disable PossibleNullReferenceException
                totalScore += bestCycle.CycleScore;
                WriteCycle(writer, bestCycle, cycleNumber);
                WriteCycleInfo(bestCycle, cycleNumber, cyclesGenerated, startTime);
                // ReSharper restore PossibleNullReferenceException
            }

            WriteTotalScore(totalScore);

            // Show 100% in the wait dialog.
            progressMonitor.UpdateProgress(status.ChangePercentComplete(100).ChangeMessage(
                                               string.Format(Resources.AbstractDiaExporter_WriteMultiplexedWindows_Exporting_Isolation_List__0__cycles_out_of__0__,
                                                             cycleCount)));
        }