Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/RealVectorDistanceCalculator.cs @ 16995

Last change on this file since 16995 was 16995, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.FLA for new persistence

File size: 2.0 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Common.Resources;
4using HeuristicLab.Common;
5using HeuristicLab.Encodings.BinaryVectorEncoding;
6using System.Drawing;
7using HeuristicLab.Encodings.RealVectorEncoding;
8using HEAL.Attic;
9
10namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
11
12  [Item("RealVectorDistanceCalculator", "Calculates the Euclidian distance of two real vectors")]
13  [StorableType("26271DB0-0868-4363-84AE-46CF583276F0")]
14  public class RealVectorDistanceCalculator: NamedItem, IItemDistanceCalculator {
15
16    #region Properties
17
18    public override bool CanChangeName { get { return false; } }
19    public override bool CanChangeDescription { get { return false; } }
20    public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
21
22    #endregion
23
24    #region Construction & Cloning
25
26    [StorableConstructor]
27    protected RealVectorDistanceCalculator(StorableConstructorFlag _) : base(_) { }
28
29    protected RealVectorDistanceCalculator(RealVectorDistanceCalculator original, Cloner cloner)
30      : base(original, cloner) {
31    }
32
33    public RealVectorDistanceCalculator() {
34      name = ItemName;
35      description = ItemDescription;
36    }
37
38    public override IDeepCloneable Clone(Cloner cloner) {
39      return new RealVectorDistanceCalculator(this, cloner);
40    }
41
42    #endregion
43
44    #region IItemDistanceCalculator Members
45
46    public Type ItemType {
47      get { return typeof(BinaryVector); }
48    }
49
50    public double Distance(IItem x, IItem y) {
51      RealVector a = (RealVector )x;
52      RealVector b = (RealVector )y;
53      if (a.Length != b.Length)
54        throw new InvalidOperationException("Cannot compare vectors of different lengths");
55      double sqrSum = 0;
56      for (int i = 0; i < a.Length; i++) {
57        sqrSum = Sqr(a[i] - b[i]);
58      }
59      return Math.Sqrt(sqrSum);
60    }
61
62    private static double Sqr(double x) { return x*x; }
63
64    #endregion
65  }
66}
Note: See TracBrowser for help on using the repository browser.