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 |
|
---|
12 | namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
|
---|
13 |
|
---|
14 | [Item("BinaryVectorDistanceCalculator", "Calculates the hamming distance of two binary vectors")]
|
---|
15 | [StorableClass]
|
---|
16 | public class BinaryVectorDistanceCalculator : NamedItem, IItemDistanceCalculator {
|
---|
17 |
|
---|
18 | #region Properties
|
---|
19 |
|
---|
20 | public override bool CanChangeName { get { return false; } }
|
---|
21 | public override bool CanChangeDescription { get { return false; } }
|
---|
22 | public static new Image StaticItemImage { get { return VSImageLibrary.Function; } }
|
---|
23 |
|
---|
24 | #endregion
|
---|
25 |
|
---|
26 | #region Construction & Cloning
|
---|
27 |
|
---|
28 | [StorableConstructor]
|
---|
29 | protected BinaryVectorDistanceCalculator(bool deserializing) : base(deserializing) { }
|
---|
30 |
|
---|
31 | protected BinaryVectorDistanceCalculator(BinaryVectorDistanceCalculator original, Cloner cloner)
|
---|
32 | : base(original, cloner) {
|
---|
33 | }
|
---|
34 |
|
---|
35 | public BinaryVectorDistanceCalculator() {
|
---|
36 | name = ItemName;
|
---|
37 | description = ItemDescription;
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
41 | return new BinaryVectorDistanceCalculator(this, cloner);
|
---|
42 | }
|
---|
43 |
|
---|
44 | #endregion
|
---|
45 |
|
---|
46 | #region IItemDistanceCalculator Members
|
---|
47 |
|
---|
48 | public Type ItemType {
|
---|
49 | get { return typeof(BinaryVector); }
|
---|
50 | }
|
---|
51 |
|
---|
52 | public double Distance(IItem x, IItem y) {
|
---|
53 | BinaryVector a = (BinaryVector)x;
|
---|
54 | BinaryVector b = (BinaryVector)y;
|
---|
55 | if (a.Length != b.Length)
|
---|
56 | throw new InvalidOperationException("Cannot compare vectors of different lengths");
|
---|
57 | double nEqualBits = 0;
|
---|
58 | for (int i = 0; i < a.Length; i++) {
|
---|
59 | if (a[i] == b[i])
|
---|
60 | nEqualBits++;
|
---|
61 | }
|
---|
62 | return Math.Max(a.Length, b.Length) - nEqualBits;
|
---|
63 | }
|
---|
64 |
|
---|
65 | #endregion
|
---|
66 | }
|
---|
67 | }
|
---|