Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemCharacteristicAnalysis/DoubleMatrixCharacteristicCalculator.cs @ 17514

Last change on this file since 17514 was 15718, checked in by abeham, 7 years ago

#1614:

  • Scale directed walk fitness values
  • Performance improvements to RLD view
  • Changed CPLEX to use a single thread only
  • Set the context to null when algorithm is stopped
File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Data;
26
27namespace HeuristicLab.Analysis.FitnessLandscape {
28  public static class DoubleMatrixCharacteristicCalculator {
29    public static double CoeffVariation(DoubleMatrix m) {
30      var avg = m.Average();
31      var stdDev = m.StandardDeviation();
32      return stdDev / avg;
33    }
34
35    public static double CoeffVariationNonZeroes(DoubleMatrix m) {
36      var nonzeroes = m.Where(x => !x.IsAlmost(0));
37      var avg = nonzeroes.Average();
38      var stdDev = nonzeroes.StandardDeviation();
39      return stdDev / avg;
40    }
41
42    public static double Sparsity(DoubleMatrix m) {
43      return m.Count(x => x == 0) / (double)(m.Rows * m.Columns);
44    }
45
46    public static double Disparity(DoubleMatrix m) {
47      if (m.Rows != m.Columns) throw new ArgumentException("not a quadratic matrix", "m");
48      var n = m.Rows;
49      var avg = m.Sum() / n;
50      var disparity = 0.0;
51      for (var i = 0; i < n; i++) {
52        var tmp = 0.0;
53        for (var j = 0; j < n; j++) {
54          tmp += m[i, j];
55        }
56        disparity += (tmp - avg) * (tmp - avg);
57      }
58      return Math.Sqrt(disparity / n) / avg;
59    }
60
61    public static double Skewness(DoubleMatrix m) {
62      double mean = 0, variance = 0, skewness = 0, kurtosis = 0;
63      alglib.basestat.samplemoments(m.ToArray(), m.Rows * m.Columns, ref mean, ref variance, ref skewness, ref kurtosis);
64      return skewness;
65    }
66
67    public static double Asymmetry(DoubleMatrix m) {
68      if (m.Rows != m.Columns) throw new ArgumentException("not a quadratic matrix", "m");
69      var n = m.Rows;
70      var count = 0;
71      for (var i = 0; i < n; i++) {
72        for (var j = 0; j < i; j++) {
73          var v = Math.Abs(m[i, j] - m[j, i]);
74          if (v > 0) {
75            count++;
76          }
77        }
78      }
79      return count / (n * (n - 1) / 2.0);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.