Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Problems.TestFunctions/3.3/PathRelinkers/SingleObjectiveTestFunctionPathRelinker.cs @ 8319

Last change on this file since 8319 was 8319, checked in by jkarder, 12 years ago

#1331:

  • applied some of the changes suggested by ascheibe in comment:32:ticket:1331
  • restructured path relinking and improvement operators and similarity calculators
  • fixed bug in TSPMultipleGuidesPathRelinker
File size: 4.3 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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization.Operators;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.TestFunctions {
34  /// <summary>
35  /// An operator that relinks paths between test functions solutions.
36  /// </summary>
37  /// <remarks>
38  /// TODO: add reference and remarks
39  /// </remarks>
40  [Item("SingleObjectiveTestFunctionPathRelinker", "An operator that relinks paths between test functions solutions.")]
41  [StorableClass]
42  public sealed class SingleObjectiveTestFunctionPathRelinker : SingleObjectivePathRelinker {
43    #region Parameter properties
44    public IValueParameter<IntValue> RelinkingIntensityParameter {
45      get { return (IValueParameter<IntValue>)Parameters["RelinkingIntensity"]; }
46    }
47    #endregion
48
49    #region Properties
50    private IntValue RelinkingIntensity {
51      get { return RelinkingIntensityParameter.Value; }
52    }
53    #endregion
54
55    [StorableConstructor]
56    private SingleObjectiveTestFunctionPathRelinker(bool deserializing) : base(deserializing) { }
57    private SingleObjectiveTestFunctionPathRelinker(SingleObjectiveTestFunctionPathRelinker original, Cloner cloner) : base(original, cloner) { }
58    public SingleObjectiveTestFunctionPathRelinker()
59      : base() {
60      #region Create parameters
61      Parameters.Add(new ValueParameter<IntValue>("RelinkingIntensity", "Determines how strong path relinking should be applied.", new IntValue(10)));
62      #endregion
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new SingleObjectiveTestFunctionPathRelinker(this, cloner);
67    }
68
69    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, IntValue k, PercentValue n) {
70      if (!(initiator is RealVector) || !(guide is RealVector))
71        throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
72      if (n.Value <= 0.0)
73        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
74
75      RealVector v1 = initiator.Clone() as RealVector;
76      RealVector v2 = guide as RealVector;
77
78      if (v1.Length != v2.Length)
79        throw new ArgumentException("The solutions are of different length.");
80
81      IList<RealVector> solutions = new List<RealVector>();
82      for (int i = 0; i < k.Value; i++) {
83        RealVector solution = v1.Clone() as RealVector;
84        for (int j = 0; j < solution.Length; j++)
85          solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]);
86        solutions.Add(solution);
87      }
88
89      IList<IItem> selection = new List<IItem>();
90      if (solutions.Count > 0) {
91        int noSol = (int)(solutions.Count * n.Value);
92        if (noSol <= 0) noSol++;
93        double stepSize = (double)solutions.Count / (double)noSol;
94        for (int i = 0; i < noSol; i++)
95          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
96      }
97
98      return new ItemArray<IItem>(selection);
99    }
100
101    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
102      if (parents.Length != 2)
103        throw new ArgumentException("The number of parents is not equal to 2.");
104      return Apply(parents[0], parents[1], RelinkingIntensity, n);
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.