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