Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11544 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
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, ISimilarityBasedOperator {
39    #region ISimilarityBasedOperator Members
40    [Storable]
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)
85      : base(original, cloner) {
86      this.SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
87    }
88    public SolutionPoolUpdateMethod() : base() { Initialize(); }
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      return new SolutionPoolUpdateMethod(this, cloner);
92    }
93
94    private void Initialize() {
95      #region Create parameters
96      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
97      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
98      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
99      Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
100      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
101      #endregion
102    }
103
104    public override IOperation Apply() {
105      ScopeList parents = new ScopeList();
106      ScopeList offspring = new ScopeList();
107
108      // split parents and offspring
109      foreach (var scope in CurrentScope.SubScopes) {
110        parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
111        offspring.AddRange(scope.SubScopes.Last().SubScopes);
112      }
113
114      CurrentScope.SubScopes.Clear();
115
116      // attention: assumes that parents are distinct
117      // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
118
119      var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
120                                                parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
121      var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
122                                                  offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
123
124      CurrentScope.SubScopes.AddRange(orderedParents);
125
126      double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
127
128      var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
129                                                  (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
130
131      // is there any offspring better than the worst parent?
132      if (orderedOffspring.Any(hasBetterQuality)) {
133        // produce the set union
134        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), SimilarityCalculator);
135        if (union.Count() > orderedParents.Count()) {
136          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
137                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
138          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value));
139          NewSolutions.Value = true;
140        }
141      }
142
143      return base.Apply();
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.