1 | using System;
|
---|
2 | using HeuristicLab.Core;
|
---|
3 | using HeuristicLab.Common.Resources;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
6 | using System.Drawing;
|
---|
7 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
8 | using HEAL.Attic;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|