Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/DistanceCalculators/RealVectorDistanceCalculator.cs @ 15529

Last change on this file since 15529 was 12575, checked in by epitzer, 9 years ago

#2306 Add RealVectorDistanceCalculator (for neutral walks on real vector problems)

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