using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using HeuristicLab.Optimization; namespace HeuristicLab.Analysis { public static class ExpectedRuntimeHelper { public static ErtCalculationResult CalculateErt(IEnumerable>> convGraphs, double target, bool maximization) { var successful = new List(); var unsuccessful = new List(); foreach (var graph in convGraphs) { var targetAchieved = false; var lastEffort = double.MaxValue; foreach (var v in graph) { if (maximization && v.Item2 >= target || !maximization && v.Item2 <= target) { successful.Add(v.Item1); targetAchieved = true; break; } lastEffort = v.Item1; } if (!targetAchieved) unsuccessful.Add(lastEffort); } var ert = double.PositiveInfinity; var nRuns = successful.Count + unsuccessful.Count; if (successful.Count > 0) ert = (successful.Average() * nRuns) / successful.Count; return new ErtCalculationResult(successful.Count, nRuns, ert); } public static ErtCalculationResult CalculateErt(List runs, string indexedDataTableName, double target, bool maximization) { return CalculateErt(runs.Select(r => ((IndexedDataTable)r.Results[indexedDataTableName]).Rows.First().Values), target, maximization); } } public struct ErtCalculationResult { public readonly int SuccessfulRuns; public readonly int TotalRuns; public readonly double ExpectedRuntime; public ErtCalculationResult(int successful, int total, double ert) { SuccessfulRuns = successful; TotalRuns = total; ExpectedRuntime = ert; } public override string ToString() { return ExpectedRuntime.ToString("##,0.0", CultureInfo.CurrentCulture.NumberFormat); } } }