Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs @ 8086

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