Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/15/16 17:19:34 (8 years ago)
Author:
bwerth
Message:

#1087 regorganized testfunctions, added view for qualities

Location:
branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/Crowding.cs

    r13562 r13620  
    11using System;
    22using System.Collections.Generic;
     3using System.Linq;
     4using HeuristicLab.Data;
    35using HeuristicLab.Encodings.RealVectorEncoding;
     6using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    47
    58namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
    69
    710  /// <summary>
    8   /// Crowding distance d(x,A) is usually defined between a point x and a set of points  A
     11  /// Crowding distance d(x,A) is usually defined between a point x and a set of points A
    912  /// d(x,A) is then a weighted sum over all dimensions where for each dimension the next larger and the next smaller Point to x are subtracted
    1013  /// I extended the concept and defined the Crowding distance of a front A as the mean of the crowding distances of every point x in A
    11   /// C(A) = mean(x,A) where x in A
     14  /// C(A) = mean(d(x,A)) where x in A  and d(x,A) is not infinite
    1215  /// </summary>
    1316  public class Crowding : IMultiObjectiveDistance {
    14     private double[][] bounds;
    15     public Crowding(double[][] bounds) {
     17    private double[,] bounds;
     18
     19    public Crowding(double[,] bounds) {
    1620      this.bounds = bounds;
     21
    1722    }
    1823
    19 
    20     public static double GetDistance(RealVector[] front, RealVector[] optimalFront, double[][] bounds) {
     24    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double[,] bounds) {
    2125      return new Crowding(bounds).Compare(front, optimalFront);
    2226    }
    2327
    24 
    25     public static double GetCrowding(RealVector[] front, double[][] bounds) {
     28    public static double GetCrowding(IEnumerable<double[]> front, double[,] bounds) {
    2629      return new Crowding(bounds).Get(front);
    2730    }
    2831
    29 
    30     public double Get(RealVector[] front) {
     32    public double Get(IEnumerable<double[]> front) {
    3133      double sum = 0;
    32       foreach(RealVector point in front) {
    33         sum += CalcCrowding(point, front);
     34      int c = 0;
     35      foreach (double[] point in front) {
     36        double d = CalcCrowding(point, front);
     37        if (!Double.IsInfinity(d)) {
     38          sum += d;
     39          c++;
     40        }
    3441      }
    35       return sum / front.Length;
     42      return c==0?Double.PositiveInfinity:sum / c;
    3643    }
    3744
    38     public double Compare(RealVector[] front, RealVector[] optimalFront) {
     45    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
    3946      return Get(optimalFront) - Get(front);
    4047    }
    4148
    42     private double CalcCrowding(RealVector point, RealVector[] list) {
     49    private double CalcCrowding(double[] point, IEnumerable<double[]> list) {
    4350      double sum = 0;
    4451      for (int i = 0; i < point.Length; i++) {
     
    4855    }
    4956
    50     private double CalcCrowding(RealVector point, RealVector[] list, int dim) {
    51       double fmax = bounds[dim % bounds.Length][1];
    52       double fmin = bounds[dim % bounds.Length][0];
    53 
    54       Array.Sort<RealVector>(list, new DimensionComparer(dim, false));
    55       if (list[0][dim] == point[0] || list[list.Length - 1][dim] == point[0]) return Double.PositiveInfinity;
    56       int pos = binarySearch(point[dim], list, dim);
    57       return (list[pos + 1][dim] - list[pos - 1][dim] )/ (fmax - fmin);
    58     }
    59 
    60     private int binarySearch(double x, RealVector[] list, int dim) {
    61       int low = 0;
    62       int high = list.Length - 1;
    63       while (high >= low) {
    64         int middle = (low + high) / 2;
    65         if (list[middle][dim] == x) return middle;
    66         if (list[middle][dim] < x)  low = middle + 1;
    67         if (list[middle][dim] > x)  high = middle - 1;
    68       }
    69       return -1; //This should never happen
    70     }
    71 
    72     private class DimensionComparer : IComparer<RealVector> {
    73       private int dim;
    74       private int descending;
    75 
    76       public DimensionComparer(int dimension, bool descending) {
    77         this.dim = dimension;
    78         this.descending = descending ? -1 : 1;
    79       }
    80 
    81       #region IComparer<DoubleArray> Members
    82 
    83       public int Compare(RealVector x, RealVector y) {
    84         if (x[dim] < y[dim]) return -descending;
    85         else if (x[dim] > y[dim]) return descending;
    86         else return 0;
    87       }
    88 
    89       #endregion
     57    private double CalcCrowding(double[] point, IEnumerable<double[]> list, int dim) {
     58      double fmax = bounds[dim % bounds.GetLength(0), 1];
     59      double fmin = bounds[dim % bounds.GetLength(0), 0];
     60      double[][] arr = list.ToArray();  //TODO Shady
     61      Array.Sort<double[]>(arr, Utilities.getDimensionComparer(dim, false));
     62      if (arr[0][dim] == point[0] || arr[arr.Length - 1][dim] == point[0]) return Double.PositiveInfinity;
     63      int pos = Utilities.binarySearch(point[dim], arr, dim);
     64      if (pos == 0 || pos == list.Count()-1) return Double.PositiveInfinity;
     65      return (arr[pos + 1][dim] - arr[pos - 1][dim]) / (fmax - fmin);
    9066    }
    9167
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/GenerationalDistance.cs

    r13562 r13620  
    11using System;
     2using System.Collections.Generic;
    23using HeuristicLab.Encodings.RealVectorEncoding;
     4using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    35
    46namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
     
    1517    }
    1618
    17 
    18     public static double GetDistance(RealVector[] front, RealVector[] optimalFront, double p) {
     19    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double p) {
    1920     return new GenerationalDistance(p).Compare(front,optimalFront);
    2021    }
    2122
    22     public double Compare(RealVector[] front, RealVector[] optimalFront) {
    23      //TODO build a kd-tree, sort the array, do someting intelligent here
    24      double sum = 0;
    25       if (front.Length == 0 || optimalFront.Length == 0) throw new Exception("Both Fronts need to contain at least one point");
    26      foreach(RealVector r in front) {
    27         sum += minDistance(r, optimalFront);
     23    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
     24      //TODO build a kd-tree, sort the array, do someting intelligent here
     25      if (front == null || optimalFront == null) throw new ArgumentException("Fronts must not be null");
     26      double sum = 0;
     27      int c = 0;
     28     foreach(double[] r in front) {
     29        sum += Utilities.minDistance(r, optimalFront,true);
     30        c++;
    2831      }
    29       return Math.Pow(sum, p) /front.Length;
     32      if (c == 0) throw new ArgumentException("Fronts must not be empty");
     33      return Math.Pow(sum, p) /c;
    3034    }
    3135
    32     private double minDistance(RealVector point, RealVector[] list) {
    33       //TODO inefficient
    34       double min = Double.MaxValue;
    35       foreach (RealVector r in list) {
    36         if (r == point) continue;
    37         double d = 0;
    38         for (int i = 0; i < r.Length; i++) {
    39           d += (point[i] - r[i]) * (point[i] - r[i]);
    40         }
    41         min = Math.Min(d, min);
    42       }
    43       return Math.Sqrt(min);
    44     }
     36   
    4537
    4638  }
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/HyperVolume.cs

    r13562 r13620  
    11using System;
    22using System.Collections.Generic;
    3 using HeuristicLab.Encodings.RealVectorEncoding;
     3using System.Linq;
     4using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    45
    56namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
     
    2930  public class Hypervolume : IMultiObjectiveDistance {
    3031
    31     private RealVector reference;
     32    private double[] reference;
    3233    private bool[] maximization;
    33     public Hypervolume(RealVector reference, bool[] maximization) {
     34    public Hypervolume(double[] reference, bool[] maximization) {
    3435      if (reference.Length != 2) throw new Exception("Only 2-dimensional cases are supported yet");
    3536      this.reference = reference;
     
    3738    }
    3839
    39     public double Compare(RealVector[] front, RealVector[] optimalFront) {
     40    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
    4041      return GetHypervolume(optimalFront) - GetHypervolume(front);
    4142
    4243    }
    4344
    44     public double GetHypervolume(RealVector[] front) {
     45    public double GetHypervolume(IEnumerable<double[]> front) {
     46      if (front == null) throw new ArgumentException("Fronts must not be null");
    4547      //TODO what to do if set contains dominated points
    46       front = (RealVector[])front.Clone(); //TODO this seems shady
    47       Array.Sort<RealVector>(front, new DimensionComparer(0, maximization[0]));
    48       RealVector last = front[front.Length - 1];
     48      double[][] set = front.ToArray();   //Still no Good
     49      if (set.Length == 0) throw new ArgumentException("Fronts must not be empty");
     50      Array.Sort<double[]>(set, Utilities.getDimensionComparer(0, maximization[0]));
     51      double[] last = set[set.Length - 1];
    4952      CheckConsistency(last, 0);
    5053      CheckConsistency(last, 1);
    5154      double sum = 0;
    52       for (int i = 0; i < front.Length - 1; i++) {
    53         CheckConsistency(front[i], 1);
    54         sum += Math.Abs((front[i][0] - front[i + 1][0])) * Math.Abs((front[i][1] - reference[1]));
     55      for (int i = 0; i < set.Length - 1; i++) {
     56        CheckConsistency(set[i], 1);
     57        sum += Math.Abs((set[i][0] - set[i + 1][0])) * Math.Abs((set[i][1] - reference[1]));
    5558      }
    5659
     
    5962    }
    6063
    61     public static double GetHypervolume(RealVector[] front, RealVector reference, bool[] maximization){
     64    public static double GetHypervolume(IEnumerable<double[]> front, double[] reference, bool[] maximization){
    6265      Hypervolume comp = new Hypervolume(reference, maximization);
    6366      return comp.GetHypervolume(front);
    6467    }
    6568
    66     public static double GetDistance(RealVector[] front, RealVector[] optimalFront, RealVector reference, bool[] maximization) {
     69    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double[] reference, bool[] maximization) {
    6770      return GetHypervolume(optimalFront, reference, maximization) - GetHypervolume(front, reference, maximization);
    6871    }
    6972
    70     private void CheckConsistency(RealVector point, int dim) {
    71       if (!maximization[dim] && point[dim] > reference[dim]) throw new Exception("Reference Point must be dominated by all points of the front");
    72       if (maximization[dim] && point[dim] < reference[dim]) throw new Exception("Reference Point must be dominated by all points of the front");
    73       if (point.Length != 2) throw new Exception("Only 2-dimensional cases are supported yet");
     73    private void CheckConsistency(double[] point, int dim) {
     74      if (!maximization[dim] && point[dim] > reference[dim]) throw new ArgumentException("Reference Point must be dominated by all points of the front");
     75      if (maximization[dim] && point[dim] < reference[dim]) throw new ArgumentException("Reference Point must be dominated by all points of the front");
     76      if (point.Length != 2) throw new ArgumentException("Only 2-dimensional cases are supported yet");
    7477    }
    75 
    76     private class DimensionComparer : IComparer<RealVector> {
    77       private int dim;
    78       private int descending;
    79 
    80       public DimensionComparer(int dimension, bool descending) {
    81         this.dim = dimension;
    82         this.descending = descending ? -1 : 1;
    83       }
    84 
    85       #region IComparer<DoubleArray> Members
    86 
    87       public int Compare(RealVector x, RealVector y) {
    88         if (x[dim] < y[dim]) return -descending;
    89         else if (x[dim] > y[dim]) return descending;
    90         else return 0;
    91       }
    92 
    93       #endregion
    94     }
    95 
    9678  }
    9779}
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/HyperVolumeFast.cs

    r13562 r13620  
    11using System;
    22using System.Collections.Generic;
    3 using HeuristicLab.Encodings.RealVectorEncoding;
     3using HeuristicLab.Common;
     4using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    45
    56namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
     
    2930  public class FastHypervolume {
    3031
    31     private RealVector reference;
     32    private double[] reference;
    3233    private bool[] maximization;
    33     public FastHypervolume(RealVector reference, bool[] maximization) {
    34       if (reference.Length != 2) throw new Exception("Only 2-dimensional cases are supported yet");
     34    public FastHypervolume(double[] reference, bool[] maximization) {
     35      if (reference.Length != 2) throw new NotSupportedException("Only 2-dimensional cases are supported yet");
    3536      this.reference = reference;
    3637      this.maximization = maximization;
    3738    }
    3839
    39     public double Compare(RealVector[] front, RealVector[] optimalFront, double[][] bounds) {
     40    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double[,] bounds) {
    4041      return GetHypervolume(optimalFront, bounds) - GetHypervolume(front, bounds);
    4142
     
    4950    /// <param name="bounds">also called region</param>
    5051    /// <returns></returns>
    51     public double GetHypervolume(RealVector[] front, double[][] bounds) {
     52    public double GetHypervolume(IEnumerable<double[]> front, double[,] bounds) {
    5253      //TODO what to do if set contains dominated points
    53       var list = new List<RealVector>();
     54      if (front == null) throw new ArgumentException("Fronts must not be null");
     55      var list = new List<double[]>();
    5456      list.AddRange(front);
    55       list.Sort(new DimensionComparer(reference.Length - 1, maximization[reference.Length - 1]));
     57      list.Sort(Utilities.getDimensionComparer(reference.Length - 1, maximization[reference.Length - 1]));
    5658      var results = new ExPrivates(this);
    57       GetHV(DeepClone(bounds), list, 1, reference[reference.Length - 1], results);
     59      GetHV(SpanRegion(), list, 0, reference[reference.Length - 1], results);
    5860      return results.Volume;
    5961
    6062    }
     63
     64    private double[][] SpanRegion(double[,] bounds, int length) {
     65      if (bounds.GetLength(1) != 2) throw new ArgumentException();
     66      double[][] copy = new double[length][];
     67      for (int i = 0; i < copy.Length; i++) {
     68        copy[i] = new double[2];
     69        for (int j = 0; j < 2; j++) {
     70          copy[i][j] = bounds[i%bounds.GetLength(0),j];
     71        }
     72      }
     73      return copy;
     74    }
     75
     76    private double[][] SpanRegion() {
     77      double[][] copy = new double[reference.Length][];
     78      for (int i = 0; i < copy.Length; i++) {
     79        copy[i] = new double[2];
     80        copy[i][0] = 0;
     81        copy[i][1] = reference[i];
     82      }
     83      return copy;
     84
     85    }
     86
    6187
    6288    private double[][] DeepClone(double[][] array) {
     
    6490      for (int i = 0; i < copy.Length; i++) {
    6591        copy[i] = new double[array[i].Length];
    66         for (int j = 0; i < array[i].Length; j++) {
     92        for (int j = 0; j < array[i].Length; j++) {
    6793          copy[i][j] = array[i][j];
    6894        }
     
    80106
    81107      public int D {
    82         get { return D; }
     108        get { return d; }
    83109      }
    84110      public ExPrivates(FastHypervolume hv) {
     
    91117    }
    92118
    93     private void GetHV(double[][] region, List<RealVector> points, int split, double cover, ExPrivates results) {
     119    private void GetHV(double[][] region, List<double[]> points, int split, double cover, ExPrivates results) {
    94120      double coverNew = cover;
    95       int coverIndex = 1;
     121      int coverIndex = 0;
    96122      bool allPiles = true;
    97       int bound = -1;
     123      double bound = -1;
    98124
    99125      /* is the region completely covered? */
     
    101127        if (covers(points[coverIndex], region, results)) {
    102128          coverNew = points[coverIndex][results.D];
    103           results.Volume += getMeasure(region) * (cover - coverNew);
    104         } else { }
    105         coverIndex++;
    106       }
    107       if (coverIndex == 1) return;
    108 
    109       for (int i = 1; i < coverIndex; i++) { if (checkPile(points[i], region) == -1) allPiles = false; }
     129          results.Volume += GetMeasure(region,results) * (cover - coverNew);
     130        } else coverIndex++;
     131      }
     132      if (coverIndex == 0) return;
     133
     134      for (int i = 0; i < coverIndex; i++) { if (checkPile(points[i], region, results) == -1) allPiles = false; }
    110135
    111136      if (allPiles) {
    112137        /* calculate volume by sweeping along dimension d */
    113138        var trellis = new double[reference.Length];
    114         int i = 1;
    115         for (int j = 1; j < results.D; j++) trellis[j] = reference[j];
     139        int i = 0;
     140        for (int j = 0; j < results.D-1; j++) trellis[j] = reference[j];
    116141
    117142        double next;//bernhard
    118143        do {
    119           double current = points[i][results.D];
     144          double current = points[i][results.D-1];
    120145          do {
    121             int pile = getPile(points[i], region);
     146            int pile = checkPile(points[i], region,results);
    122147            if (points[i][pile] < trellis[pile]) trellis[pile] = points[i][pile];
    123148            i++;
    124             if (i < coverIndex - 1) next = points[i][results.D]; else next = coverNew;
     149            if (i < coverIndex - 1) next = points[i][results.D-1]; else next = coverNew;
    125150          } while (current == next);
    126           results.Volume += measure(trellis, region) * (next - current);
     151          results.Volume += measure(trellis, region, results) * (next - current);
    127152        } while (next != coverNew);
    128153
     
    131156        do {
    132157          var intersect = new List<Double>(); var nonIntersect = new List<Double>();
    133           for (int i = 1; i < coverIndex; i++) {
     158          for (int i = 0; i < coverIndex; i++) {
    134159            var intersection = intersects(points[i], region, split);
    135160            if (intersection == 1) intersect.Add(points[i][split]);
    136161            if (intersection == 0) nonIntersect.Add(points[i][split]);
    137162          }
    138           if (intersect.Count != 0) bound = median(intersect);
    139           else if (nonIntersect.Count > Math.Sqrt(n)) bound = median(nonIntersect);
     163          if (intersect.Count != 0) bound = intersect.Median();
     164          else if (nonIntersect.Count > Math.Sqrt(points.Count)) bound = nonIntersect.Median();
    140165          else split++;
    141166        } while (bound == -1);
     
    145170      var regionC = DeepClone(region);
    146171      regionC[split][1] = bound;
    147       var pointsC = new List<RealVector>();
    148       for (int i = 1; i < coverIndex; i++) {
    149         if (partCovers(points[i], regionC, results)) move(points[i], pointsC);
     172      var pointsC = new List<double[]>();
     173      for (int i = 0; i < coverIndex; i++) {
     174        if (partCovers(points[i], regionC, results)) move(points,i, pointsC);
    150175      }
    151176      if (pointsC.Count != 0) GetHV(regionC, pointsC, split, coverNew, results);
     
    153178      regionC = region;
    154179      regionC[split][0] = bound;
    155       pointsC = new List<RealVector>();
     180      pointsC = new List<double[]>();
    156181      for (int i = 1; i < coverIndex; i++) {
    157         if (partCovers(points[i], regionC, results)) move(points[i], pointsC);
     182        if (partCovers(points[i], regionC, results)) move(points,i, pointsC);
    158183      }
    159184      if (pointsC.Count != 0) GetHV(regionC, pointsC, split, coverNew, results);
     
    161186    }
    162187
    163     private int checkPile(RealVector point, double[][] region, ExPrivates results) {
     188    private void reinsert(List<double[]> pointsC, List<double[]> points) {
     189      points.AddRange(pointsC);
     190      pointsC.Clear();
     191    }
     192
     193    private double GetMeasure(double[][] region,ExPrivates result) {
     194      double volume = 1.0;
     195      // for ( std::size_t i = 0; i < regionLow.size(); i++ ) {
     196      for (int i = 1; i < result.D; i++) {
     197      volume *= (region[1][i] - region[0][i]);
     198      }
     199      return (volume);
     200    }
     201
     202    private void move(List<double[]> points, int i, List<double[]> pointsC) {
     203      double[] v = points[i];
     204      points.Remove(v);
     205      pointsC.Add(v);
     206    }
     207
     208    private double measure(double[] trellis, double[][] region, ExPrivates results) {
     209      double volume=0;
     210      bool[] indicator = new bool[results.D];
     211      for (int i = 0; i < results.D-1; i++) indicator[i] = true;
     212      int numberSummands = integerValue(indicator);
     213
     214      for (int i = 0; i <= numberSummands; i++) {
     215        indicator = binaryValue(i);
     216        int oneCounter = 0;
     217        double summand = 0;
     218        for (int j = 1; j < results.D; j++) {
     219          if (indicator[i] == true) {
     220            summand += region[1][j] - trellis[j];
     221            oneCounter++;
     222          } else {
     223            summand += region[1][j] - region[0][j];
     224          }
     225        }
     226        if(oneCounter%2 == 0) {
     227          volume -= summand;
     228        } else {
     229          volume += summand;
     230        }
     231      }
     232      return volume;
     233    }
     234
     235    private int integerValue(bool[] binary) {
     236      int sum=0;
     237      foreach (bool b in binary) {
     238        sum = (sum << 1) + (b ? 1 : 0);
     239      }
     240      return sum;
     241    }
     242
     243    private bool[] binaryValue(int integer) {
     244      bool[] res = new bool[32];
     245      int i = 0;
     246      while (integer != 0) {
     247        res[i++]= (integer & 1)==1;
     248        integer >>= 1;
     249      }
     250      return res;
     251
     252    }
     253
     254    private int checkPile(double[] point, double[][] region, ExPrivates results) {
    164255      int pile = -1;
    165       for (int j = 1; j < results.D; j++) {
     256      for (int j = 0; j < results.D-1; j++) {
    166257        if (point[j] > region[j][0]) {
    167258          if (pile != -1) return -1;
     
    173264    }
    174265
    175     private int intersects(RealVector point, double[][] region, int split) {
     266    private int intersects(double[] point, double[][] region, int split) {
    176267      if (region[split][0] >= point[split]) return -1;
    177268      for (int j = 1; j < split; j++) {
     
    182273    }
    183274
    184     private bool covers(RealVector point, double[][] region, ExPrivates results) {
     275    private bool covers(double[] point, double[][] region, ExPrivates results) {
    185276      for (int j = 1; j < results.D; j++) {
    186277        if (point[j] > region[j][0]) return false;
     
    190281    }
    191282
    192     private bool partCovers(RealVector point, double[][] region, ExPrivates results) {
     283    private bool partCovers(double[] point, double[][] region, ExPrivates results) {
    193284      for (int j = 1; j < results.D; j++) {
    194285        if (point[j] >= region[j][1]) return false;
     
    197288    }
    198289
    199     public static double GetHypervolume(RealVector[] front, RealVector reference, bool[] maximization) {
    200       Hypervolume comp = new Hypervolume(reference, maximization);
    201       return comp.GetHypervolume(front);
    202     }
    203 
    204     public static double GetDistance(RealVector[] front, RealVector[] optimalFront, RealVector reference, bool[] maximization) {
    205       return GetHypervolume(optimalFront, reference, maximization) - GetHypervolume(front, reference, maximization);
    206     }
    207 
    208     private void CheckConsistency(RealVector point, int dim) {
     290    public static double GetHypervolume(IEnumerable<double[]> front, double[] reference, bool[] maximization, double[,] bounds) {
     291      FastHypervolume comp = new FastHypervolume(reference, maximization);
     292      return comp.GetHypervolume(front,bounds);
     293    }
     294
     295    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double[] reference, bool[] maximization, double[,] bounds) {
     296      return GetHypervolume(optimalFront, reference, maximization, bounds) - GetHypervolume(front, reference, maximization,bounds);
     297    }
     298
     299    private void CheckConsistency(double[] point, int dim) {
    209300      if (!maximization[dim] && point[dim] > reference[dim]) throw new Exception("Reference Point must be dominated by all points of the front");
    210301      if (maximization[dim] && point[dim] < reference[dim]) throw new Exception("Reference Point must be dominated by all points of the front");
    211302    }
    212303
    213     private class DimensionComparer : IComparer<RealVector> {
    214       private int dim;
    215       private int descending;
    216 
    217       public DimensionComparer(int dimension, bool descending) {
    218         this.dim = dimension;
    219         this.descending = descending ? -1 : 1;
    220       }
    221 
    222       #region IComparer<DoubleArray> Members
    223 
    224       public int Compare(RealVector x, RealVector y) {
    225         if (x[dim] < y[dim]) return -descending;
    226         else if (x[dim] > y[dim]) return descending;
    227         else return 0;
    228       }
    229 
    230       #endregion
    231     }
     304
     305
     306
     307
     308
     309
     310   
     311
    232312
    233313  }
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/InvertedGenerationalDistance.cs

    r13562 r13620  
    11using System;
     2using System.Collections.Generic;
    23using HeuristicLab.Encodings.RealVectorEncoding;
     4using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    35
    46namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
     
    1113
    1214    public InvertedGenerationalDistance(double p) {
     15      if (p <= 0) throw new ArgumentOutOfRangeException("weighting factor p has to be greater than 0");
    1316      this.p = 1 / p;
    1417    }
    15 
    16 
    1718
    1819    /// <summary>
     
    2324    /// <param name="p"></param>
    2425    /// <returns></returns>
    25     public static double GetDistance(RealVector[] front, RealVector[] optimalFront, double p) {
     26    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront, double p) {
    2627      return new InvertedGenerationalDistance(p).Compare(front, optimalFront);
    2728    }
    2829
    29     public double Compare(RealVector[] front, RealVector[] optimalFront) {
    30       //TODO build a kd-tree, sort the array, do someting intelligent here
    31       double sum = 0;
    32       if (front.Length == 0 || optimalFront.Length == 0) throw new Exception("Both Fronts need to contain at least one point");
    33       foreach (RealVector r in optimalFront) {
    34         sum += minDistance(r, front);
    35       }
    36       return Math.Pow(sum, p) / optimalFront.Length;
     30    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
     31      return new GenerationalDistance(p).Compare(optimalFront, front);
    3732    }
    38 
    39     private double minDistance(RealVector point, RealVector[] list) {
    40       //TODO inefficient
    41       double min = Double.MaxValue;
    42       foreach (RealVector r in list) {
    43         if (r == point) continue;
    44         double d = 0;
    45         for (int i = 0; i < r.Length; i++) {
    46           d += (point[i] - r[i]) * (point[i] - r[i]);
    47         }
    48         min = Math.Min(d, min);
    49       }
    50       return Math.Sqrt(min);
    51     }
    52 
    5333  }
    5434}
  • branches/HeuristicLab.Problems.MultiObjectiveTestFunctions/HeuristicLab.Problems.MultiObjectiveTestFunctions/3.3/Comparators/Spacing.cs

    r13562 r13620  
    11using System;
     2using System.Collections.Generic;
     3using HeuristicLab.Common;
    24using HeuristicLab.Encodings.RealVectorEncoding;
     5using HeuristicLab.Problems.MultiObjectiveTestFunctions.Comparators;
    36
    47namespace HeuristicLab.Problems.MultiObjectiveTestFunctions {
     
    1518
    1619
    17     public static double GetSpacing(RealVector[] front) {
     20    public static double GetSpacing(IEnumerable<double[]> front) {
    1821      return new Spacing().Get(front);
    1922    }
    20     public static double GetDistance(RealVector[] front, RealVector[] optimalFront) {
     23    public static double GetDistance(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
    2124      return new Spacing().Get(front);
    2225    }
    2326
    24     public double Compare(RealVector[] front, RealVector[] optimalFront) {
     27    public double Compare(IEnumerable<double[]> front, IEnumerable<double[]> optimalFront) {
    2528      return GetSpacing(front) - GetSpacing(optimalFront);
    2629    }
    2730
    28     public double Get(RealVector[] front) {
     31    public double Get(IEnumerable<double[]> front) {
    2932      //TODO build a kd-tree, sort the array, do someting intelligent here
    30       double sum = 0;
    31       if (front.Length == 0) throw new Exception("Front does not contain any points");
    32       double[] d = new double[front.Length];
    33       int i = 0;
    34       foreach (RealVector r in front) {
    35         d[i] = minDistance(r, front);
    36         sum += d[i++];
     33      if (front == null) throw new ArgumentException("Fronts must not be null");
     34      List<double> d = new List<double>();
     35      foreach (double[] r in front) {
     36        double dist = Utilities.minDistance(r, front, false);
     37        d.Add(dist>=0?dist:0);
     38      }
     39      int n = d.Count;
     40      if (n == 0) throw new ArgumentException("Fronts must not be empty");
     41      return Math.Sqrt(d.Variance()*(n-1)/n);
    3742
    38       }
    39       double mean = sum / front.Length;
    40       sum = 0;
    41       foreach (double e in d) {
    42         sum += (e - mean) * (e - mean);
    43       }
    44       sum /= front.Length;
    45       return Math.Sqrt(sum);
    46 
    47     }
    48 
    49     private double minDistance(RealVector point, RealVector[] list) {
    50       //TODO inefficient
    51       double min = Double.MaxValue;
    52       foreach (RealVector r in list) {
    53         if (r == point) continue;
    54         double d = 0;
    55         for (int i = 0; i < r.Length; i++) {
    56           d += (point[i] - r[i]) * (point[i] - r[i]);
    57         }
    58         min = Math.Min(d, min);
    59       }
    60       return Math.Sqrt(min);
    6143    }
    6244
Note: See TracChangeset for help on using the changeset viewer.