[16103] | 1 | using System;
|
---|
[13757] | 2 | using System.Collections.Generic;
|
---|
[12956] | 3 | using System.Globalization;
|
---|
| 4 | using System.Linq;
|
---|
[16103] | 5 | using HeuristicLab.Optimization;
|
---|
[12956] | 6 |
|
---|
| 7 | namespace HeuristicLab.Analysis {
|
---|
| 8 | public static class ExpectedRuntimeHelper {
|
---|
[13757] | 9 | public static ErtCalculationResult CalculateErt(IEnumerable<IEnumerable<Tuple<double, double>>> convGraphs, double target, bool maximization) {
|
---|
[12956] | 10 | var successful = new List<double>();
|
---|
| 11 | var unsuccessful = new List<double>();
|
---|
[16103] | 12 |
|
---|
[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;
|
---|
[16103] | 30 | if (successful.Count > 0)
|
---|
| 31 | ert = (successful.Average() * nRuns) / successful.Count;
|
---|
[13803] | 32 | return new ErtCalculationResult(successful.Count, nRuns, ert);
|
---|
[12956] | 33 | }
|
---|
[13757] | 34 |
|
---|
| 35 | public static ErtCalculationResult CalculateErt(List<IRun> runs, string indexedDataTableName, double target, bool maximization) {
|
---|
| 36 | return CalculateErt(runs.Select(r => ((IndexedDataTable<double>)r.Results[indexedDataTableName]).Rows.First().Values), target, maximization);
|
---|
| 37 | }
|
---|
[12956] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public struct ErtCalculationResult {
|
---|
[15048] | 41 | public readonly int SuccessfulRuns;
|
---|
| 42 | public readonly int TotalRuns;
|
---|
| 43 | public readonly double ExpectedRuntime;
|
---|
[12956] | 44 |
|
---|
| 45 | public ErtCalculationResult(int successful, int total, double ert) {
|
---|
| 46 | SuccessfulRuns = successful;
|
---|
| 47 | TotalRuns = total;
|
---|
| 48 | ExpectedRuntime = ert;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override string ToString() {
|
---|
[15048] | 52 | return ExpectedRuntime.ToString("##,0.0", CultureInfo.CurrentCulture.NumberFormat);
|
---|
[12956] | 53 | }
|
---|
| 54 | }
|
---|
| 55 | }
|
---|