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 HEAL.Attic;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Analysis.FitnessLandscape.DistanceCalculators {
|
---|
14 |
|
---|
15 | [Item("BinaryVectorDistanceCalculator", "Calculates the hamming distance of two binary vectors")]
|
---|
16 | [StorableType("BB6A8FD8-22B1-41CC-B38D-C8C9A2B79C83")]
|
---|
17 | public class BinaryVectorDistanceCalculator : 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 BinaryVectorDistanceCalculator(StorableConstructorFlag _) : base(_) { }
|
---|
31 |
|
---|
32 | protected BinaryVectorDistanceCalculator(BinaryVectorDistanceCalculator original, Cloner cloner)
|
---|
33 | : base(original, cloner) {
|
---|
34 | }
|
---|
35 |
|
---|
36 | public BinaryVectorDistanceCalculator() {
|
---|
37 | name = ItemName;
|
---|
38 | description = ItemDescription;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
42 | return new BinaryVectorDistanceCalculator(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 | BinaryVector a = (BinaryVector)x;
|
---|
55 | BinaryVector b = (BinaryVector)y;
|
---|
56 | if (a.Length != b.Length)
|
---|
57 | throw new InvalidOperationException("Cannot compare vectors of different lengths");
|
---|
58 | double nEqualBits = 0;
|
---|
59 | for (int i = 0; i < a.Length; i++) {
|
---|
60 | if (a[i] == b[i])
|
---|
61 | nEqualBits++;
|
---|
62 | }
|
---|
63 | return Math.Max(a.Length, b.Length) - nEqualBits;
|
---|
64 | }
|
---|
65 |
|
---|
66 | #endregion
|
---|
67 | }
|
---|
68 | }
|
---|