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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Analysis {
|
---|
32 | [StorableType("d84ac1f1-a6d4-425f-bcff-28472ef7529a")]
|
---|
33 | [Item("SpacingAnalyzer", "The spacing of the current front (see Multi-Objective Performance Metrics - Shodhganga for more information)")]
|
---|
34 | public class SpacingAnalyzer : MultiObjectiveSuccessAnalyzer {
|
---|
35 | public override string ResultName {
|
---|
36 | get { return "Spacing"; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [StorableConstructor]
|
---|
40 | protected SpacingAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
41 |
|
---|
42 |
|
---|
43 | protected SpacingAnalyzer(SpacingAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
44 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
45 | return new SpacingAnalyzer(this, cloner);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public SpacingAnalyzer() {
|
---|
49 | Parameters.Add(new ResultParameter<DoubleValue>("Spacing", "The spacing of the current front", "Results", new DoubleValue(double.NaN)));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override IOperation Apply() {
|
---|
53 | var qualities = QualitiesParameter.ActualValue;
|
---|
54 | ResultParameter.ActualValue.Value = CalculateSpacing(qualities.Select(x => x.ToArray()));
|
---|
55 | return base.Apply();
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static double CalculateSpacing(IEnumerable<double[]> qualities) {
|
---|
59 | if (qualities == null) throw new ArgumentNullException("qualities");
|
---|
60 | var l = qualities.ToList();
|
---|
61 | if (l.Count == 0) throw new ArgumentException("Front must not be empty.");
|
---|
62 | if (l.Count == 1) return 0;
|
---|
63 |
|
---|
64 | var mat = l.ToMatrix();
|
---|
65 | alglib.kdtree tree;
|
---|
66 | alglib.kdtreebuild(mat, mat.GetLength(0), mat.GetLength(1), 0, 2, out tree);
|
---|
67 | var summand = new double[2];
|
---|
68 | var dists = new List<double>();
|
---|
69 | foreach (var point in l) {
|
---|
70 | alglib.kdtreequeryknn(tree, point.ToArray(), 2, true);
|
---|
71 | alglib.kdtreequeryresultsdistances(tree, ref summand);
|
---|
72 | dists.Add(summand[1]);
|
---|
73 | }
|
---|
74 | return dists.StandardDeviationPop();
|
---|
75 | }
|
---|
76 | }
|
---|
77 | } |
---|