[15045] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15583] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15045] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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 |
|
---|
[16171] | 43 | public int LeastContributer(IReadOnlyList<Individual> front, IMultiObjectiveBasicProblem problem) {
|
---|
[15045] | 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 |
|
---|
[15176] | 85 | #region Helpers
|
---|
[15045] | 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 | }
|
---|