/// <summary> /// Called by Simulator after parse. /// Initializes here time-dependant but not state dependent variables. /// </summary> /// <param name="simulationDates"> /// The dates at which the process realizations will be requested. /// </param> public void Setup(double[] simulationDates) { try { if (File.Exists(this.FilePath)) { // Store the lines of the file. string[] fileLines = File.ReadAllLines(this.FilePath) .Where(s => s != string.Empty) .ToArray(); List <string> tmpList = new List <string>(fileLines); tmpList.Sort((s1, s2) => { string d1String = s1.Split(HistoricalSimulator.elementSeparators)[0]; DateTime d1 = DateTime.Parse(d1String); string d2String = s2.Split(HistoricalSimulator.elementSeparators)[0]; DateTime d2 = DateTime.Parse(d2String); return(d1.CompareTo(d2)); }); fileLines = tmpList.ToArray(); // Use the file lines in order to get the file content. this.fileContent = new List <Tuple <DateTime, Vector> >(); for (int i = 0; i < fileLines.Length; i++) { string[] lineTokens = fileLines[i].Split(HistoricalSimulator.elementSeparators); DateTime date = DateTime.Parse(lineTokens[0]); double[] data = new double[lineTokens.Length - 1]; for (int j = 0; j < data.Length; j++) { data[j] = DoubleHelper.FromString(lineTokens[j + 1]); } this.fileContent.Add(new Tuple <DateTime, Vector>(date, new Vector(data))); } // Calculate the entry point in the file (the index of the starting line). DateTime nearestDate = DateTime.Now.Date; for (int i = 0; i < this.fileContent.Count; i++) { DateTime date = this.fileContent[i].Item1; if (i == 0) { nearestDate = date; } else { nearestDate = NearestDate(StartDate, nearestDate, date); } if (nearestDate == date) { this.startIndex = i; } } // Calculate the list of dates in the file. List <DateTime> dateList = new List <DateTime>(); for (int i = 0; i < this.fileContent.Count; i++) { DateTime date = this.fileContent[i].Item1; dateList.Add(date); } // Calculate for each date the line of the file to use in order the get the // values. Gets the nearest date between each simulation date and the dates // present in the file (if the date is before the entry point use the entry // point instead). this.simulationDateIndexes = new Dictionary <double, int>(); foreach (double doubleDate in simulationDates) { DateTime date = Document.ActiveDocument.DefaultProject.GetDate(doubleDate); TimeSpan dateDifference = date - Document.ActiveDocument.SimulationStartDate; int dateIndex = NearestDateIndex(StartDate + dateDifference, dateList.ToArray()); this.simulationDateIndexes.Add(doubleDate, Math.Max(dateIndex, this.startIndex)); } } else { this.fileContent = null; } } catch { this.fileContent = null; } // After reading the input, do the other initializations, if needed. if (OperatingMode == OperatingMode.Bootstrap) { this.bootstrap = new Bootstrap(this.fileContent); } }