Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16653 was 16573, checked in by gkronber, 6 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

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