Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8303 was 8086, checked in by jkarder, 13 years ago

#1331:

  • synced branch with trunk
  • added custom interface (ISimilarityBasedOperator) to mark operators that conduct similarity calculation
  • similarity calculators are now parameterized by the algorithm
  • deleted SolutionPool2TierUpdateMethod
  • deleted KnapsackMultipleGuidesPathRelinker
  • moved IImprovementOperator, IPathRelinker and ISimilarityCalculator to HeuristicLab.Optimization
  • added parameter descriptions
  • fixed plugin references
  • fixed count of EvaluatedSolutions
  • fixed check for duplicate solutions
  • minor code improvements
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    }
50    #endregion
51
52    [StorableConstructor]
53    private SingleObjectiveTestFunctionPathRelinker(bool deserializing) : base(deserializing) { }
54    private SingleObjectiveTestFunctionPathRelinker(SingleObjectiveTestFunctionPathRelinker original, Cloner cloner) : base(original, cloner) { }
55    public SingleObjectiveTestFunctionPathRelinker()
56      : base() {
57      #region Create parameters
58      Parameters.Add(new ValueParameter<IntValue>("RelinkingIntensity", "Determines how strong path relinking should be applied.", new IntValue(10)));
59      #endregion
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new SingleObjectiveTestFunctionPathRelinker(this, cloner);
64    }
65
66    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, IntValue k, PercentValue n) {
67      if (!(initiator is RealVector) || !(guide is RealVector))
68        throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
69      if (n.Value <= 0.0)
70        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
71
72      RealVector v1 = initiator.Clone() as RealVector;
73      RealVector v2 = guide as RealVector;
74
75      if (v1.Length != v2.Length)
76        throw new ArgumentException("The solutions are of different length.");
77
78      IList<RealVector> solutions = new List<RealVector>();
79      for (int i = 0; i < k.Value; i++) {
80        RealVector solution = v1.Clone() as RealVector;
81        for (int j = 0; j < solution.Length; j++)
82          solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]);
83        solutions.Add(solution);
84      }
85
86      IList<IItem> selection = new List<IItem>();
87      if (solutions.Count > 0) {
88        int noSol = (int)(solutions.Count * n.Value);
89        if (noSol <= 0) noSol++;
90        double stepSize = (double)solutions.Count / (double)noSol;
91        for (int i = 0; i < noSol; i++)
92          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
93      }
94
95      return new ItemArray<IItem>(selection);
96    }
97
98    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
99      if (parents.Length != 2)
100        throw new ArgumentException("The number of parents is not equal to 2.");
101      return Apply(parents[0], parents[1], RelinkingIntensity, n);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.