Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3043-Regression-Instances-For-Scaling/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs @ 18060

Last change on this file since 18060 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
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  [StorableType("F295B5C8-8551-4DFC-A38C-354E14CED2E3")]
38  public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, ISingleObjectiveOperator {
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<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
56      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
57    }
58    #endregion
59
60    #region Properties
61    private IScope CurrentScope {
62      get { return CurrentScopeParameter.ActualValue; }
63    }
64    private BoolValue Maximization {
65      get { return MaximizationParameter.ActualValue; }
66      set { MaximizationParameter.ActualValue = value; }
67    }
68    private BoolValue NewSolutions {
69      get { return NewSolutionsParameter.ActualValue; }
70    }
71    private IItem Quality {
72      get { return QualityParameter.ActualValue; }
73    }
74    private IntValue ReferenceSetSize {
75      get { return ReferenceSetSizeParameter.ActualValue; }
76      set { ReferenceSetSizeParameter.ActualValue = value; }
77    }
78    #endregion
79
80    [StorableConstructor]
81    private SolutionPoolUpdateMethod(StorableConstructorFlag _) : base(_) { }
82    private SolutionPoolUpdateMethod(SolutionPoolUpdateMethod original, Cloner cloner) : base(original, cloner) { }
83    public SolutionPoolUpdateMethod()
84      : base() {
85      #region Create parameters
86      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set."));
87      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
88      Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false."));
89      Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only."));
90      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
91      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
92      #endregion
93    }
94
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new SolutionPoolUpdateMethod(this, cloner);
97    }
98
99    [StorableHook(HookType.AfterDeserialization)]
100    private void AfterDeserialization() {
101      // BackwardsCompatibility3.3
102      #region Backwards compatible code, remove with 3.4
103      if (!Parameters.ContainsKey("SimilarityCalculator"))
104        Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
105      #endregion
106    }
107
108    public override IOperation Apply() {
109      ScopeList parents = new ScopeList();
110      ScopeList offspring = new ScopeList();
111
112      // split parents and offspring
113      foreach (var scope in CurrentScope.SubScopes) {
114        parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));
115        offspring.AddRange(scope.SubScopes.Last().SubScopes);
116      }
117
118      CurrentScope.SubScopes.Clear();
119
120      // attention: assumes that parents are distinct
121      // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2}
122
123      var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
124                                                parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
125      var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) :
126                                                  offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value);
127
128      CurrentScope.SubScopes.AddRange(orderedParents);
129
130      double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;
131
132      var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :
133                                                  (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });
134
135      // is there any offspring better than the worst parent?
136      if (orderedOffspring.Any(hasBetterQuality)) {
137        // produce the set union
138        var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), SimilarityCalculatorParameter.ActualValue);
139        if (union.Count() > orderedParents.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));
143          NewSolutions.Value = true;
144        }
145      }
146
147      return base.Apply();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.