Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2943_MOBasicProblem_MOCMAES/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/Indicators/MinimalDistanceIndicator.cs @ 16310

Last change on this file since 16310 was 16310, checked in by bwerth, 5 years ago

#2943 worked on MOBasicProblem and MOAnalyzers

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