using HeuristicLab.Optimization; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; 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.NaN; if (successful.Count > 0) { if (unsuccessful.Count == 0) ert = successful.Average(); else { var ps = successful.Count / (double)(successful.Count + unsuccessful.Count); ert = successful.Average() + ((1.0 - ps) / ps) * unsuccessful.Average(); } } return new ErtCalculationResult(successful.Count, (successful.Count + unsuccessful.Count), 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 int SuccessfulRuns; public int TotalRuns; public double ExpectedRuntime; public ErtCalculationResult(int successful, int total, double ert) { SuccessfulRuns = successful; TotalRuns = total; ExpectedRuntime = ert; } public override string ToString() { return SuccessfulRuns == 0 ? "\u221e" : ExpectedRuntime.ToString("##,0.0", CultureInfo.CurrentCulture.NumberFormat); } } }