Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions/3.3/PathRelinkers/SingleObjectiveTestFunctionPathRelinker.cs @ 12282

Last change on this file since 12282 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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  /// It is based on an implementation described in Duarte, A., Martí, R., and Gortazar, F. (2011). Path Relinking for Large Scale Global Optimization. Soft Computing, Vol. 15.<br />
39  /// The operator incrementally assimilates the initiating solution into the guiding solution by adapting the solution vector's elements.
40  /// </remarks>
41  [Item("SingleObjectiveTestFunctionPathRelinker", "An operator that relinks paths between test functions solutions. It is based on an implementation described in Duarte, A., Martí, R., and Gortazar, F. (2011). Path Relinking for Large Scale Global Optimization. Soft Computing, Vol. 15.")]
42  [StorableClass]
43  public sealed class SingleObjectiveTestFunctionPathRelinker : SingleObjectivePathRelinker {
44    #region Parameter properties
45    public IValueParameter<IntValue> RelinkingIntensityParameter {
46      get { return (IValueParameter<IntValue>)Parameters["RelinkingIntensity"]; }
47    }
48    #endregion
49
50    #region Properties
51    private IntValue RelinkingIntensity {
52      get { return RelinkingIntensityParameter.Value; }
53    }
54    #endregion
55
56    [StorableConstructor]
57    private SingleObjectiveTestFunctionPathRelinker(bool deserializing) : base(deserializing) { }
58    private SingleObjectiveTestFunctionPathRelinker(SingleObjectiveTestFunctionPathRelinker original, Cloner cloner) : base(original, cloner) { }
59    public SingleObjectiveTestFunctionPathRelinker()
60      : base() {
61      #region Create parameters
62      Parameters.Add(new ValueParameter<IntValue>("RelinkingIntensity", "Determines how strong path relinking should be applied.", new IntValue(10)));
63      #endregion
64    }
65
66    public override IDeepCloneable Clone(Cloner cloner) {
67      return new SingleObjectiveTestFunctionPathRelinker(this, cloner);
68    }
69
70    public static ItemArray<IItem> Apply(IItem initiator, IItem guide, IntValue k, PercentValue n) {
71      if (!(initiator is RealVector) || !(guide is RealVector))
72        throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type.");
73      if (n.Value <= 0.0)
74        throw new ArgumentException("RelinkingAccuracy must be greater than 0.");
75
76      RealVector v1 = initiator.Clone() as RealVector;
77      RealVector v2 = guide as RealVector;
78
79      if (v1.Length != v2.Length)
80        throw new ArgumentException("The solutions are of different length.");
81
82      IList<RealVector> solutions = new List<RealVector>();
83      for (int i = 0; i < k.Value; i++) {
84        RealVector solution = v1.Clone() as RealVector;
85        for (int j = 0; j < solution.Length; j++)
86          solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]);
87        solutions.Add(solution);
88      }
89
90      IList<IItem> selection = new List<IItem>();
91      if (solutions.Count > 0) {
92        int noSol = (int)(solutions.Count * n.Value);
93        if (noSol <= 0) noSol++;
94        double stepSize = (double)solutions.Count / (double)noSol;
95        for (int i = 0; i < noSol; i++)
96          selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5)));
97      }
98
99      return new ItemArray<IItem>(selection);
100    }
101
102    protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) {
103      if (parents.Length != 2)
104        throw new ArgumentException("The number of parents is not equal to 2.");
105      return Apply(parents[0], parents[1], RelinkingIntensity, n);
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.