Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Indicators/MinimalDistanceIndicator.cs @ 17226

Last change on this file since 17226 was 17226, checked in by mkommend, 5 years ago

#2521: Merged trunk changes into problem refactoring branch.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
25using HEAL.Attic;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Algorithms.MOCMAEvolutionStrategy {
32  [Item("MinimalDistanceIndicator", "Selection of Offspring based on distance to nearest neighbour")]
33  [StorableType("FBBD4517-164C-4DEE-B87D-49B99172EDF4")]
34  internal class MinimalDistanceIndicator : Item, IIndicator {
35    #region Constructor and Cloning
36    [StorableConstructor]
37    protected MinimalDistanceIndicator(StorableConstructorFlag _) : base(_) { }
38    protected MinimalDistanceIndicator(MinimalDistanceIndicator original, Cloner cloner) : base(original, cloner) { }
39    public override IDeepCloneable Clone(Cloner cloner) {
40      return new MinimalDistanceIndicator(this, cloner);
41    }
42    public MinimalDistanceIndicator() { }
43    #endregion
44
45    public int LeastContributer(IReadOnlyList<Individual> front, IMultiObjectiveProblemDefinition problem) {
46      var extracted = front.Select(x => x.PenalizedFitness).ToArray();
47      if (extracted.Length <= 2) return 0;
48      var distances = CalcDistances(extracted);
49      var mindexI = 0;
50      var mindexJ = 0;
51      var min = double.MaxValue;
52      for (var i = 0; i < extracted.Length; i++) {
53        var d = double.MaxValue;
54        var minj = 0;
55        for (var j = 0; j < extracted.Length; j++) {
56          if (i == j) continue;
57          var d1 = distances[i, j];
58          if (!(d1 < d)) continue;
59          minj = j;
60          d = d1;
61        }
62        if (!(d < min)) continue;
63        min = d;
64        mindexI = i;
65        mindexJ = minj;
66      }
67
68      //break tie with distance to second nearest
69      var minI = double.MaxValue;
70      var minJ = double.MaxValue;
71
72      for (var i = 0; i < extracted.Length; i++) {
73        double d;
74        if (mindexI != i) {
75          d = distances[mindexI, i];
76          if (!d.IsAlmost(min)) minI = Math.Min(minI, d);
77        }
78        if (mindexJ == i) continue;
79        d = distances[mindexJ, i];
80        if (!d.IsAlmost(min)) minJ = Math.Min(minJ, d);
81      }
82
83      //find min
84      return minI < minJ ? mindexI : mindexJ;
85    }
86
87    #region Helpers
88    private static double[,] CalcDistances(IReadOnlyList<double[]> extracted) {
89      var res = new double[extracted.Count, extracted.Count];
90      for (var i = 0; i < extracted.Count; i++)
91      for (var j = 0; j < i; j++)
92        res[i, j] = res[j, i] = Dist(extracted[i], extracted[j]);
93      return res;
94    }
95
96    private static double Dist(IEnumerable<double> a, IEnumerable<double> b) {
97      return Math.Sqrt(a.Zip(b, (x, y) => (x - y) * (x - y)).Sum());
98    }
99    #endregion
100  }
101}
Note: See TracBrowser for help on using the repository browser.