[15455] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15455] | 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;
|
---|
[15479] | 23 | using System.Collections;
|
---|
[15455] | 24 | using System.Collections.Generic;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[17097] | 30 | using HEAL.Attic;
|
---|
[15531] | 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[15455] | 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[17097] | 34 | [StorableType("A660B39D-1B11-459D-98C2-65942E6D375C")]
|
---|
[15479] | 35 | [Item("WeightedEuclideanDistance", "A weighted norm function that uses Euclidean distance √(Σ(w[i]²*(p1[i]-p2[i])²))")]
|
---|
| 36 | public class WeightedEuclideanDistance : ParameterizedNamedItem, IDistance<IEnumerable<double>> {
|
---|
[15531] | 37 | [Storable]
|
---|
| 38 | private double[] weights;
|
---|
[15455] | 39 | public const string WeightsParameterName = "Weights";
|
---|
[15531] | 40 | public IValueParameter<DoubleArray> WeightsParameter {
|
---|
[15479] | 41 | get { return (IValueParameter<DoubleArray>) Parameters[WeightsParameterName]; }
|
---|
[15455] | 42 | }
|
---|
| 43 |
|
---|
| 44 | public DoubleArray Weights {
|
---|
[15531] | 45 | get { return WeightsParameter.Value; }
|
---|
| 46 | set { WeightsParameter.Value = value; }
|
---|
[15455] | 47 | }
|
---|
| 48 |
|
---|
| 49 | #region HLConstructors & Cloning
|
---|
| 50 | [StorableConstructor]
|
---|
[17097] | 51 | protected WeightedEuclideanDistance(StorableConstructorFlag _) : base(_) { }
|
---|
[15571] | 52 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[15479] | 53 | private void AfterDeserialization() {
|
---|
| 54 | RegisterParameterEvents();
|
---|
| 55 | }
|
---|
| 56 | protected WeightedEuclideanDistance(WeightedEuclideanDistance original, Cloner cloner) : base(original, cloner) {
|
---|
| 57 | RegisterParameterEvents();
|
---|
[15531] | 58 | weights = original.weights != null ? original.weights.ToArray() : null;
|
---|
[15479] | 59 | }
|
---|
[15455] | 60 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 61 | return new WeightedEuclideanDistance(this, cloner);
|
---|
| 62 | }
|
---|
| 63 | public WeightedEuclideanDistance() {
|
---|
[15531] | 64 | Parameters.Add(new ValueParameter<DoubleArray>(WeightsParameterName, "The weights used to modify the euclidean distance.", new DoubleArray(new[] {1.0})));
|
---|
[15479] | 65 | RegisterParameterEvents();
|
---|
[15455] | 66 | }
|
---|
| 67 | #endregion
|
---|
| 68 |
|
---|
[15479] | 69 | public static double GetDistance(IEnumerable<double> point1, IEnumerable<double> point2, IEnumerable<double> weights) {
|
---|
| 70 | using (IEnumerator<double> p1Enum = point1.GetEnumerator(), p2Enum = point2.GetEnumerator(), weEnum = weights.GetEnumerator()) {
|
---|
| 71 | var sum = 0.0;
|
---|
[15487] | 72 | while (p1Enum.MoveNext() & p2Enum.MoveNext() & weEnum.MoveNext()) {
|
---|
[15479] | 73 | var d = p1Enum.Current - p2Enum.Current;
|
---|
| 74 | var w = weEnum.Current;
|
---|
| 75 | sum += d * d * w * w;
|
---|
| 76 | }
|
---|
[15487] | 77 | if (weEnum.MoveNext() || p1Enum.MoveNext() || p2Enum.MoveNext()) throw new ArgumentException("Weighted Euclidean distance not defined on vectors of different length");
|
---|
[15479] | 78 | return Math.Sqrt(sum);
|
---|
[15455] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[15479] | 82 | public double Get(IEnumerable<double> a, IEnumerable<double> b) {
|
---|
[15531] | 83 | return GetDistance(a, b, weights);
|
---|
[15455] | 84 | }
|
---|
[15479] | 85 | public IComparer<IEnumerable<double>> GetDistanceComparer(IEnumerable<double> item) {
|
---|
| 86 | return new DistanceBase<IEnumerable<double>>.DistanceComparer(item, this);
|
---|
| 87 | }
|
---|
| 88 | public double Get(object x, object y) {
|
---|
| 89 | return Get((IEnumerable<double>) x, (IEnumerable<double>) y);
|
---|
| 90 | }
|
---|
| 91 | public IComparer GetDistanceComparer(object item) {
|
---|
| 92 | return new DistanceBase<IEnumerable<double>>.DistanceComparer((IEnumerable<double>) item, this);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[15531] | 95 | public void AdaptToProblemData(IDataAnalysisProblemData problemData) {
|
---|
| 96 | Weights = new DoubleArray(problemData.AllowedInputVariables.Select(v => Weights.ElementNames.Contains(v) ? GetWeight(v) : 1).ToArray())
|
---|
| 97 | {ElementNames = problemData.AllowedInputVariables};
|
---|
| 98 | }
|
---|
| 99 | public void Initialize(IDataAnalysisProblemData problemData) {
|
---|
| 100 | if (Weights.Length != problemData.AllowedInputVariables.Count()) throw new ArgumentException("Number of Weights does not match the number of input variables");
|
---|
| 101 | weights = Weights.ElementNames.All(v => v == null || v.Equals(string.Empty)) ?
|
---|
| 102 | Weights.ToArray() :
|
---|
| 103 | problemData.AllowedInputVariables.Select(GetWeight).ToArray();
|
---|
| 104 | }
|
---|
| 105 | private double GetWeight(string v) {
|
---|
| 106 | var w = Weights;
|
---|
| 107 | var names = w.ElementNames.ToArray();
|
---|
| 108 | for (var i = 0; i < w.Length; i++) if (names[i].Equals(v)) return w[i];
|
---|
| 109 | throw new ArgumentException("weigth for " + v + " was requested but not specified.");
|
---|
| 110 | }
|
---|
[15479] | 111 | private void RegisterParameterEvents() {
|
---|
[15531] | 112 | WeightsParameter.ValueChanged += OnWeightsArrayChanged;
|
---|
| 113 | WeightsParameter.Value.ItemChanged += OnWeightChanged;
|
---|
[15479] | 114 | }
|
---|
| 115 | private void OnWeightChanged(object sender, EventArgs<int> e) {
|
---|
[15531] | 116 | WeightsParameter.Value.ItemChanged -= OnWeightChanged;
|
---|
[15479] | 117 | Weights[e.Value] = Math.Max(0, Weights[e.Value]);
|
---|
[15531] | 118 | WeightsParameter.Value.ItemChanged -= OnWeightChanged;
|
---|
[15479] | 119 | }
|
---|
| 120 | private void OnWeightsArrayChanged(object sender, EventArgs e) {
|
---|
[15531] | 121 | for (var i = 0; i < Weights.Length; i++)
|
---|
[15479] | 122 | Weights[i] = Math.Max(0, Weights[i]);
|
---|
[15531] | 123 | WeightsParameter.Value.ItemChanged += OnWeightChanged;
|
---|
[15479] | 124 | }
|
---|
[15455] | 125 | }
|
---|
| 126 | } |
---|