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