Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Utilities.cs @ 14728

Last change on this file since 14728 was 14577, checked in by bwerth, 8 years ago

#2592 made MOCMAES compatible with MultiObjectiveBasicProblem instead of MultiObjectiveTestfunction, fixed Bug in CrowdingIndicator

File size: 1.6 KB
Line 
1using System.Collections.Generic;
2using HeuristicLab.Data;
3
4namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
5  internal static class Utilities {
6    internal static double[][] ToArray(DoubleMatrix m) {
7      if (m == null) return null;
8      var i = m.Rows - 1;
9      var a = new double[i][];
10      for (i--; i >= 0; i--) {
11        var j = m.Columns;
12        a[i] = new double[j];
13        for (j--; j >= 0; j--) a[i][j] = m[i, j];
14      }
15      return a;
16    }
17
18    public static int ArgMin<T>(IEnumerable<T> values, IComparer<T> comparer) {
19      var mindex = -1;
20      var min = default(T);
21      var i = 0;
22      foreach (var v in values) {
23        if (mindex < 0 || comparer.Compare(v, min) < 0) {
24          min = v;
25          mindex = i;
26        }
27        i++;
28      }
29      return mindex;
30
31    }
32    public static int ArgMax<T>(IEnumerable<T> values, IComparer<T> comparer) {
33      var mindex = -1;
34      var min = default(T);
35      var i = 0;
36      foreach (var v in values) {
37        if (mindex < 0 || comparer.Compare(v, min) > 0) {
38          min = v;
39          mindex = i;
40        }
41        i++;
42      }
43      return mindex;
44    }
45    private class DoubleComparer : IComparer<double> {
46      public int Compare(double x, double y) {
47        return x.CompareTo(y);
48      }
49    }
50    public static int ArgMax(this IEnumerable<double> values) {
51      return ArgMax(values, new DoubleComparer());
52    }
53    public static int ArgMin(this IEnumerable<double> values) {
54      return ArgMin(values, new DoubleComparer());
55    }
56  }
57}
Note: See TracBrowser for help on using the repository browser.