source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TestFunctions/3.3/SimilarityCalculators/SingleObjectiveTestFunctionSimilarityCalculator.cs @ 7793

Last change on this file since 7793 was 7793, checked in by jkarder, 8 years ago

#1331:

  • added operators for the VehicleRouting problem
  • minor code improvements
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.TestFunctions {
32  /// <summary>
33  /// An operator that performs similarity calculation between two test functions solutions.
34  /// </summary>
35  [Item("SingleObjectiveTestFunctionSimilarityCalculator", "An operator that performs similarity calculation between two test functions solutions.")]
36  [StorableClass]
37  public sealed class SingleObjectiveTestFunctionSimilarityCalculator : SimilarityCalculator {
38    #region Parameter properties
39    public ILookupParameter<DoubleMatrix> BoundsParameter {
40      get { return (ILookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
41    }
42    #endregion
43
44    #region Properties
45    private DoubleMatrix Bounds {
46      get { return BoundsParameter.ActualValue; }
47      set { BoundsParameter.ActualValue = value; }
48    }
49    #endregion
50
51    [StorableConstructor]
52    private SingleObjectiveTestFunctionSimilarityCalculator(bool deserializing) : base(deserializing) { }
53    private SingleObjectiveTestFunctionSimilarityCalculator(SingleObjectiveTestFunctionSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
54    public SingleObjectiveTestFunctionSimilarityCalculator()
55      : base() {
56      #region Create parameters
57      Parameters.Add(new LookupParameter<DoubleMatrix>("Bounds"));
58      #endregion
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SingleObjectiveTestFunctionSimilarityCalculator(this, cloner);
63    }
64
65    protected override double CalculateSimilarity(IScope left, IScope right) {
66      RealVector sol1 = left.Variables["Point"].Value as RealVector;
67      RealVector sol2 = right.Variables["Point"].Value as RealVector;
68
69      double maxSum = 0.0;
70      for (int i = 0; i < sol1.Length; i++)
71        maxSum += Math.Pow(Bounds[0, 0] - Bounds[0, 1], 2);
72      double maxDistance = Math.Sqrt(maxSum) / sol1.Length;
73
74      double sum = 0.0;
75      for (int i = 0; i < sol1.Length; i++)
76        sum += Math.Pow(sol1[i] - sol2[i], 2);
77      double distance = Math.Sqrt(sum) / sol1.Length;
78
79      return 1.0 - distance / maxDistance;
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.