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

Last change on this file since 8322 was 8322, checked in by jkarder, 8 years ago

#1331:

File size: 6.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.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    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
41    #endregion
42
43    #region Parameter properties
44    public ScopeParameter CurrentScopeParameter {
45      get { return (ScopeParameter)Parameters["CurrentScope"]; }
46    }
47    public IValueLookupParameter<BoolValue> MaximizationParameter {
48      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
49    }
50    public IValueLookupParameter<BoolValue> NewSolutionsParameter {
51      get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; }
52    }
53    public IValueLookupParameter<IItem> QualityParameter {
54      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
55    }
56    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
58    }
59    #endregion
60
61    #region Properties
62    private IScope CurrentScope {
63      get { return CurrentScopeParameter.ActualValue; }
64    }
65    private BoolValue Maximization {
66      get { return MaximizationParameter.ActualValue; }
67      set { MaximizationParameter.ActualValue = value; }
68    }
69    private BoolValue NewSolutions {
70      get { return NewSolutionsParameter.ActualValue; }
71    }
72    private IItem Quality {
73      get { return QualityParameter.ActualValue; }
74    }
75    private IntValue ReferenceSetSize {
76      get { return ReferenceSetSizeParameter.ActualValue; }
77      set { ReferenceSetSizeParameter.ActualValue = value; }
78    }
79    #endregion
80
81    [StorableConstructor]
82    private SolutionPoolUpdateMethod(bool deserializing) : base(deserializing) { }
83    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
84    public SolutionPoolUpdateMethod() : base() { Initialize(); }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new SolutionPoolUpdateMethod(this, cloner);
88    }
89
90    private void Initialize() {
91      #region Create parameters
92      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
93      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
94      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
95      Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
96      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
97      #endregion
98    }
99
100    public override IOperation Apply() {
101      ScopeList parents = new ScopeList();
102      ScopeList offspring = new ScopeList();
103
104      // split parents and offspring
105      foreach (var scope in CurrentScope.SubScopes) {
106        parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
107        offspring.AddRange(scope.SubScopes.Last().SubScopes);
108      }
109
110      CurrentScope.SubScopes.Clear();
111
112      // attention: assumes that parents are distinct
113      // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
114
115      var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
116                                                parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
117      var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
118                                                  offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
119
120      CurrentScope.SubScopes.AddRange(orderedParents);
121
122      double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
123
124      var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
125                                                  (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
126
127      // is there any offspring better than the worst parent?
128      if (orderedOffspring.Any(hasBetterQuality)) {
129        // produce the set union
130        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), SimilarityCalculator);
131        if (union.Count() > orderedParents.Count()) {
132          var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
133                                                  union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
134          CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value));
135          NewSolutions.Value = true;
136        }
137      }
138
139      return base.Apply();
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.