Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs @ 18183

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

#2875: Removed years in copyrights

File size: 4.4 KB
RevLine 
[7789]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7789]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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
[8086]28using HeuristicLab.Optimization;
[7789]29using HeuristicLab.Parameters;
[16565]30using HEAL.Attic;
[7789]31
32namespace HeuristicLab.Algorithms.ScatterSearch {
33  /// <summary>
34  /// An operator that updates the reference set.
35  /// </summary>
36  [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
[16565]37  [StorableType("478625DC-87BD-4111-98D1-45A9939198C8")]
[12069]38  public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
[7789]39    #region Parameter properties
40    public ScopeParameter CurrentScopeParameter {
41      get { return (ScopeParameter)Parameters["CurrentScope"]; }
42    }
43    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
45    }
[12069]46    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
47      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
48    }
[7789]49    #endregion
50
51    #region Properties
52    private IScope CurrentScope {
53      get { return CurrentScopeParameter.ActualValue; }
54    }
55    private IntValue ReferenceSetSize {
56      get { return ReferenceSetSizeParameter.ActualValue; }
57    }
58    #endregion
59
60    [StorableConstructor]
[16565]61    private ReferenceSetUpdateMethod(StorableConstructorFlag _) : base(_) { }
[12069]62    private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
[7789]63    public ReferenceSetUpdateMethod()
64      : base() {
65      #region Create parameters
[8086]66      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the population and the reference set."));
67      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
[12069]68      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
[7789]69      #endregion
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ReferenceSetUpdateMethod(this, cloner);
74    }
75
[12069]76    [StorableHook(HookType.AfterDeserialization)]
77    private void AfterDeserialization() {
78      // BackwardsCompatibility3.3
79      #region Backwards compatible code, remove with 3.4
80      if (!Parameters.ContainsKey("SimilarityCalculator"))
81        Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
82      #endregion
83    }
84
[7789]85    public override IOperation Apply() {
[8319]86      var populationSimilarity = new Dictionary<IScope, double>();
87      var populationScope = CurrentScope.SubScopes[0];
88      var refSetScope = CurrentScope.SubScopes[1];
[12069]89      var similarityMatrix = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(populationScope, refSetScope);
[8319]90      for (int i = 0; i < populationScope.SubScopes.Count; i++) {
91        populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum();
[7789]92      }
93      int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count;
[8319]94      foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
[7789]95        CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
96        CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
97      }
98      return base.Apply();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.