Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331:

  • added Scatter Search algorithm
  • added problem specific operators for improvement, path relinking and similarity calculation
  • adjusted event handling
File size: 4.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 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  [Item("SingleObjectiveTestFunctionPathRelinker", "An operator that relinks paths between test functions solutions.")]
38  [StorableClass]
39  public sealed class SingleObjectiveTestFunctionPathRelinker : PathRelinker {
40    #region Parameter properties
41    public IValueParameter<IntValue> RelinkingIntensityParameter {
42      get { return (IValueParameter<IntValue>)Parameters["RelinkingIntensity"]; }
43    }
44    #endregion
45
46    #region Properties
47    private IntValue RelinkingIntensity {
48      get { return RelinkingIntensityParameter.Value; }
49      set { RelinkingIntensityParameter.Value = value; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    private SingleObjectiveTestFunctionPathRelinker(bool deserializing) : base(deserializing) { }
55    private SingleObjectiveTestFunctionPathRelinker(SingleObjectiveTestFunctionPathRelinker original, Cloner cloner) : base(original, cloner) { }
56    public SingleObjectiveTestFunctionPathRelinker()
57      : base() {
58      #region Create parameters
59      Parameters.Add(new ValueParameter<IntValue>("RelinkingIntensity", new IntValue(10)));
60      #endregion
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new SingleObjectiveTestFunctionPathRelinker(this, cloner);
65    }
66
67    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, IntValue k, PercentValue n) {
68      if (!(initiator is RealVector) || !(guide is RealVector))
69        throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
70      if (n.Value <= 0.0)
71        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
72
73      RealVector v1 = initiator.Clone() as RealVector;
74      RealVector v2 = guide as RealVector;
75
76      if (v1.Length != v2.Length)
77        throw new ArgumentException("The solutions are of different length.");
78
79      IList<RealVector> solutions = new List<RealVector>();
80      for (int i = 0; i < k.Value; i++) {
81        RealVector solution = v1.Clone() as RealVector;
82        for (int j = 0; j < solution.Length; j++)
83          solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]);
84        solutions.Add(solution);
85      }
86
87      IList<IItem> selection = new List<IItem>();
88      if (solutions.Count > 0) {
89        int noSol = (int)(solutions.Count * n.Value);
90        if (noSol <= 0) noSol++;
91        double stepSize = (double)solutions.Count / (double)noSol;
92        for (int i = 0; i < noSol; i++)
93          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
94      }
95
96      return new ItemArray<IItem>(selection);
97    }
98
99    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
100      if (parents.Length != 2)
101        throw new ArgumentException("The number of parents is not equal to 2.");
102      return Apply(parents[0], parents[1], RelinkingIntensity, n);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.