Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Problems.FacilityLocation/3.3/IntegerVectorHammingSimilarityOperator.cs @ 17317

Last change on this file since 17317 was 14587, checked in by abeham, 8 years ago

#2205: Added FLP (facility location problem) for use within a network that solves the location routing problem (LRP)

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Encodings.IntegerVectorEncoding;
26using HeuristicLab.Optimization.Operators;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Problems.FacilityLocation {
30  [Item("IntegerVector Hamming Similarity Calculator", "Calculates the hamming similarity of two integer vectors (relative amount of identical positions")]
31  [StorableClass]
32  public class IntegerVectorHammingSimilarityOperator : SingleObjectiveSolutionSimilarityCalculator {
33
34    protected override bool IsCommutative {
35      get { return true; }
36    }
37
38    [StorableConstructor]
39    protected IntegerVectorHammingSimilarityOperator(bool deserializing) : base(deserializing) { }
40    protected IntegerVectorHammingSimilarityOperator(IntegerVectorHammingSimilarityOperator original, Cloner cloner) : base(original, cloner) { }
41    public IntegerVectorHammingSimilarityOperator() { }
42
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new IntegerVectorHammingSimilarityOperator(this, cloner);
45    }
46
47    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
48      var a = leftSolution.Variables[SolutionVariableName] as IntegerVector;
49      var b = rightSolution.Variables[SolutionVariableName] as IntegerVector;
50      if (a == null || b == null) throw new ArgumentException("Scopes do not contain a solution variable.");
51      if (a.Length != b.Length) throw new ArgumentException("Solutions are of unequal length.");
52
53      var similarity = 0;
54      var len = a.Length;
55      for (var i = 0; i < len; i++)
56        if (a[i] == b[i]) similarity++;
57
58      return similarity / (double)len;
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.