Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs @ 8344

Last change on this file since 8344 was 7786, checked in by jkarder, 13 years ago

#1331:

  • fixed bug in path relinking selection
  • fixed bug in ScatterSearch.Prepare()
  • added custom interface (IImprovementOperator) for Scatter Search specific improvement operators
  • switched from diversity calculation to similarity calculation
  • separated IPathRelinker from ICrossover
  • changed TestFunctionsImprovementOperator to use reflection for evaluating functions
  • changed SolutionPoolUpdateMethod to use similarity calculation for solution comparison
  • improved TravelingSalesmanImprovementOperator
  • improved operator graph
  • removed specific operators used to evaluate TestFunctions problems
  • removed custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • adjusted event handling
  • changed access levels
  • minor code improvements
File size: 7.5 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.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.ScatterSearch {
33  /// <summary>
34  /// An operator that updates the solution pool.
35  /// </summary>
36  [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")]
37  [StorableClass]
38  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator {
39    #region Parameter properties
40    public ScopeParameter CurrentScopeParameter {
41      get { return (ScopeParameter)Parameters["CurrentScope"]; }
42    }
43    public IValueLookupParameter<BoolValue> MaximizationParameter {
44      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
47      get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
48    }
49    public IValueLookupParameter<IItem> QualityParameter {
50      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
51    }
52    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
53      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
54    }
55    public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter {
56      get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }
57    }
58    public IValueLookupParameter<IItem> TargetParameter {
59      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
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    private SimilarityCalculator SimilarityCalculator {
82      get { return SimilarityCalculatorParameter.ActualValue; }
83    }
84    private IItem Target {
85      get { return TargetParameter.ActualValue; }
86    }
87    #endregion
88
89    [StorableConstructor]
90    private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
91    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
92    public SolutionPoolUpdateMethod() : base() { Initialize(); }
93
94    public override IDeepCloneable Clone(Cloner cloner) {
95      return new SolutionPoolUpdateMethod(this, cloner);
96    }
97
98    private void Initialize() {
99      #region Create parameters
100      Parameters.Add(new ScopeParameter("CurrentScope"));
101      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
102      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions"));
103      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
104      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
105      Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator"));
106      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
107      #endregion
108    }
109
110    public override IOperation Apply() {
111      IScope parentsScope = new Scope("Parents");
112      IScope offspringScope = new Scope("Offspring");
113
114      // split parents and offspring
115      foreach (var scope in CurrentScope.SubScopes) {
116        parentsScope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
117        offspringScope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);
118      }
119
120      CurrentScope.SubScopes.Clear();
121
122      var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
123                                                parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
124      var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
125                                                  offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
126
127      CurrentScope.SubScopes.AddRange(orderedParents);
128
129      double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
130
131      var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
132                                                  (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
133
134      // is there any offspring better than the worst parent?
135      if (orderedOffspring.Any(hasBetterQuality)) {
136        // produce the set union
137        // attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2}
138        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.ExecuteCalculation));
139        if (union.Count() > orderedParents/*.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()))*/.Count()) {
140          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
141                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
142          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value).ToList());
143          NewSolutions.Value = true;
144        }
145      }
146
147      return base.Apply();
148    }
149
150    private class SolutionEqualityComparer<T> : EqualityComparer<T> {
151      private readonly Func<T, T, double> diversityCalculator;
152
153      public SolutionEqualityComparer(Func<T, T, double> diversityCalculator) {
154        this.diversityCalculator = diversityCalculator;
155      }
156
157      public override bool Equals(T x, T y) {
158        return diversityCalculator(x, y) == 0.0;
159      }
160
161      public override int GetHashCode(T obj) {
162        return obj.GetHashCode();
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.