[12575] | 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 |
|
---|
| 13 | namespace 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 | }
|
---|