1 | using System.Collections.Generic;
|
---|
2 | using System.Globalization;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Optimization;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Analysis {
|
---|
7 | public static class ExpectedRuntimeHelper {
|
---|
8 | public static ErtCalculationResult CalculateErt(List<IRun> runs, string indexedDataTableName, double target, bool maximization) {
|
---|
9 | var successful = new List<double>();
|
---|
10 | var unsuccessful = new List<double>();
|
---|
11 | foreach (var r in runs) {
|
---|
12 | var targetAchieved = false;
|
---|
13 | var row = ((IndexedDataTable<double>)r.Results[indexedDataTableName]).Rows.First();
|
---|
14 | foreach (var v in row.Values) {
|
---|
15 | if (maximization && v.Item2 >= target || !maximization && v.Item2 <= target) {
|
---|
16 | successful.Add(v.Item1);
|
---|
17 | targetAchieved = true;
|
---|
18 | break;
|
---|
19 | }
|
---|
20 | }
|
---|
21 | if (!targetAchieved) unsuccessful.Add(row.Values.Last().Item1);
|
---|
22 | }
|
---|
23 |
|
---|
24 | var ert = double.NaN;
|
---|
25 |
|
---|
26 | if (successful.Count > 0) {
|
---|
27 | if (unsuccessful.Count == 0) ert = successful.Average();
|
---|
28 | else {
|
---|
29 | var ps = successful.Count / (double)(successful.Count + unsuccessful.Count);
|
---|
30 | ert = successful.Average() + ((1.0 - ps) / ps) * unsuccessful.Average();
|
---|
31 | }
|
---|
32 | }
|
---|
33 | return new ErtCalculationResult(successful.Count, (successful.Count + unsuccessful.Count), ert);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public struct ErtCalculationResult {
|
---|
38 | public int SuccessfulRuns;
|
---|
39 | public int TotalRuns;
|
---|
40 | public double ExpectedRuntime;
|
---|
41 |
|
---|
42 | public ErtCalculationResult(int successful, int total, double ert) {
|
---|
43 | SuccessfulRuns = successful;
|
---|
44 | TotalRuns = total;
|
---|
45 | ExpectedRuntime = ert;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override string ToString() {
|
---|
49 | return SuccessfulRuns == 0 ? "\u221e"
|
---|
50 | : ExpectedRuntime.ToString("##,0.0", CultureInfo.CurrentCulture.NumberFormat);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | }
|
---|