1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Common.Resources;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Common;
|
---|
9 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
10 | using System.Drawing;
|
---|
11 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
12 | using HEAL.Attic;
|
---|
13 |
|
---|
14 | namespace 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 | }
|
---|