/// <summary> /// Calculates the statistics of a single train journey, /// typically used for the simualtion statistics. /// </summary> /// <param name="trains">A single train object.</param> /// <returns>A statistics object containing statistical information about the train.</returns> public static TrainStatistics generateStats(Train train) { TrainStatistics stats = new TrainStatistics(); if (train.journey.Where(t => t.speed > 0).Count() == 0) { return(stats); } stats.Category = train.Category.ToString() + " " + train.trainDirection.ToString(); /* Extract the number of trains in the list */ stats.numberOfTrains = 1; /* Calculate the distance travelled for each train */ double distanceTravelled = 0; double averageSpeed = 0; if (train.journey.Where(t => t.speed > 0).Count() > 0) { distanceTravelled = (train.journey.Where(t => t.speed > 0).Max(t => t.kilometreage) - train.journey.Where(t => t.speed > 0).Min(t => t.kilometreage)); /* Calculate the average speed of the train journey. */ averageSpeed = train.journey.Where(t => t.speed > 0).Average(t => t.speed); } /* Populate the averages. */ stats.averageSpeed = averageSpeed; stats.averageDistanceTravelled = distanceTravelled; stats.averagePowerToWeightRatio = train.powerToWeight; /* Because there is only one train, the standard deviation is not relevant. */ stats.standardDeviationP2W = 0; return(stats); }
/// <summary> /// Calculates the statistics of the list of trains passed in. /// </summary> /// <param name="trains">A list of train objects.</param> /// <returns>A statistics object containing statistical information about the trains.</returns> public static TrainStatistics generateStats(List <Train> trains) { TrainStatistics stats = new TrainStatistics(); if (trains.Count() == 0) { return(stats); } else { stats.Category = trains[0].Category.ToString() + " " + trains[0].trainDirection.ToString(); /* Extract the number of trains in the list */ stats.numberOfTrains = trains.Count(); List <double> distance = new List <double>(); List <double> speed = new List <double>(); List <double> power2Weight = new List <double>(); /* Cycle through all the trains. */ foreach (Train train in trains) { /* Calculate the distance travelled for each train */ double distanceTravelled = 0; if (train.journey.Where(t => t.speed > 0).Count() > 0) { distanceTravelled = (train.journey.Where(t => t.speed > 0).Max(t => t.kilometreage) - train.journey.Where(t => t.speed > 0).Min(t => t.kilometreage)); /* Calculate the average speed of the train journey. */ speed.Add(train.journey.Where(t => t.speed > 0).Average(t => t.speed)); } distance.Add(distanceTravelled); /* Add the power to weight ratio to the list. */ power2Weight.Add(train.powerToWeight); } /* Calculate the averages. */ if (speed.Count() > 0) { stats.averageSpeed = speed.Average(); } else { stats.averageSpeed = 0; } if (distance.Count() > 0) { stats.averageDistanceTravelled = distance.Average(); } else { stats.averageDistanceTravelled = 0; } if (power2Weight.Count() > 1) { stats.averagePowerToWeightRatio = power2Weight.Average(); double sum = power2Weight.Sum(p => Math.Pow(p - stats.averagePowerToWeightRatio, 2)); /* Calculate the standard deviation of the power to weight ratios. */ stats.standardDeviationP2W = Math.Sqrt(sum / (power2Weight.Count() - 1)); } else { stats.averagePowerToWeightRatio = 0; stats.standardDeviationP2W = 0; } } return(stats); }