Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.AlgorithmBehavior.Analyzers/3.3/WeightedParentsQualityComparer.cs @ 10635

Last change on this file since 10635 was 9789, checked in by ascheibe, 11 years ago

#1886 fixed alglib reference and updated year and version information

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
27  /// <summary>
28  /// Compares the quality against that of its parents.
29  /// This is more or less the same code as in the WeightedParentsQualityComparator operator, but without the operator part
30  /// </summary>
31  public static class WeightedParentsQualityComparer {
32    public static bool Compare(double leftQuality, double rightQuality1, double rightQuality2, double compFact, bool maximization) {
33      return Compare(leftQuality, new double[] { rightQuality1, rightQuality2 }, compFact, maximization);
34    }
35
36    public static bool Compare(double leftQuality, IList<double> rightQualities, double compFact, bool maximization) {
37      if (rightQualities.Count < 1) throw new InvalidOperationException("No rightQualities found. ");
38
39      double threshold = 0;
40
41      if (rightQualities.Count == 2) { // this case will probably be used most often
42        double minQuality = Math.Min(rightQualities[0], rightQualities[1]);
43        double maxQuality = Math.Max(rightQualities[0], rightQualities[1]);
44        if (maximization)
45          threshold = minQuality + (maxQuality - minQuality) * compFact;
46        else
47          threshold = maxQuality - (maxQuality - minQuality) * compFact;
48      } else if (rightQualities.Count == 1) { // case for just one parent
49        threshold = rightQualities[0];
50      } else { // general case extended to 3 or more parents
51        List<double> sortedQualities = rightQualities.ToList();
52        sortedQualities.Sort();
53        double minimumQuality = sortedQualities.First();
54
55        double integral = 0;
56        for (int i = 0; i < sortedQualities.Count - 1; i++) {
57          integral += (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; // sum of the trapezoid
58        }
59        integral -= minimumQuality * sortedQualities.Count;
60        if (integral == 0) threshold = sortedQualities[0]; // all qualities are equal
61        else {
62          double selectedArea = integral * (maximization ? compFact : (1 - compFact));
63          integral = 0;
64          for (int i = 0; i < sortedQualities.Count - 1; i++) {
65            double currentSliceArea = (sortedQualities[i] + sortedQualities[i + 1]) / 2.0;
66            double windowedSliceArea = currentSliceArea - minimumQuality;
67            if (windowedSliceArea == 0) continue;
68            integral += windowedSliceArea;
69            if (integral >= selectedArea) {
70              double factor = 1 - ((integral - selectedArea) / (windowedSliceArea));
71              threshold = sortedQualities[i] + (sortedQualities[i + 1] - sortedQualities[i]) * factor;
72              break;
73            }
74          }
75        }
76      }
77
78      bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold;
79      return result;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.