Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.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: 7.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.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Algorithms.ScatterSearch {
34  /// <summary>
35  /// An operator that updates the solution pool.
36  /// </summary>
37  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
38  [StorableClass]
39  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, ISimilarityBasedOperator {
40    #region ISimilarityBasedOperator Members
41    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
42    #endregion
43
44    #region Parameter properties
45    public ScopeParameter CurrentScopeParameter {
46      get { return (ScopeParameter)Parameters["CurrentScope"]; }
47    }
48    public IValueLookupParameter<BoolValue> MaximizationParameter {
49      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
50    }
51    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
52      get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
53    }
54    public IValueLookupParameter<IItem> QualityParameter {
55      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
56    }
57    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
59    }
60    #endregion
61
62    #region Properties
63    private IScope CurrentScope {
64      get { return CurrentScopeParameter.ActualValue; }
65    }
66    private BoolValue Maximization {
67      get { return MaximizationParameter.ActualValue; }
68      set { MaximizationParameter.ActualValue = value; }
69    }
70    private BoolValue NewSolutions {
71      get { return NewSolutionsParameter.ActualValue; }
72    }
73    private IItem Quality {
74      get { return QualityParameter.ActualValue; }
75    }
76    private IntValue ReferenceSetSize {
77      get { return ReferenceSetSizeParameter.ActualValue; }
78      set { ReferenceSetSizeParameter.ActualValue = value; }
79    }
80    #endregion
81
82    [StorableConstructor]
83    private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
84    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
85    public SolutionPoolUpdateMethod() : base() { Initialize(); }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      return new SolutionPoolUpdateMethod(this, cloner);
89    }
90
91    private void Initialize() {
92      #region Create parameters
93      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
94      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
95      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
96      Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
97      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
98      #endregion
99    }
100
101    public override IOperation Apply() {
102      ScopeList parents = new ScopeList();
103      ScopeList offspring = new ScopeList();
104
105      // split parents and offspring
106      foreach (var scope in CurrentScope.SubScopes) {
107        parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
108        offspring.AddRange(scope.SubScopes.Last().SubScopes);
109      }
110
111      CurrentScope.SubScopes.Clear();
112
113      // attention: assumes that parents are distinct
114      // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
115
116      var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
117                                                parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
118      var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
119                                                  offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
120
121      CurrentScope.SubScopes.AddRange(orderedParents);
122
123      double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
124
125      var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
126                                                  (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
127
128      // is there any offspring better than the worst parent?
129      if (orderedOffspring.Any(hasBetterQuality)) {
130        // produce the set union
131        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.CalculateSolutionSimilarity));
132        if (union.Count() > orderedParents.Count()) {
133          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
134                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
135          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value));
136          NewSolutions.Value = true;
137        }
138      }
139
140      return base.Apply();
141    }
142
143    // derive SingleObjectiveSolutionSimilarityCalculator from EqualityComparer
144    // delete this ...
145    public class SolutionEqualityComparer<T> : EqualityComparer<T> {
146      private readonly Func<T, T, double> similarityCalculator;
147
148      public SolutionEqualityComparer(Func<T, T, double> similarityCalculator) {
149        this.similarityCalculator = similarityCalculator;
150      }
151
152      public override bool Equals(T x, T y) {
153        if (object.ReferenceEquals(x, y)) return true;
154        if (x == null || y == null) return false;
155        return similarityCalculator(x, y) == 1.0;
156      }
157
158      public override int GetHashCode(T obj) {
159        return 0; // return the same hash code for each object, otherwise Equals will not be called
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.