Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs @ 17246

Last change on this file since 17246 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 4.4 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.Collections.Generic;
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 reference set.
35  /// </summary>
36  [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
37  [StorableType("478625DC-87BD-4111-98D1-45A9939198C8")]
38  public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
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    }
46    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
47      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
48    }
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]
61    private ReferenceSetUpdateMethod(StorableConstructorFlag _) : base(_) { }
62    private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
63    public ReferenceSetUpdateMethod()
64      : base() {
65      #region Create parameters
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."));
68      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
69      #endregion
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new ReferenceSetUpdateMethod(this, cloner);
74    }
75
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
85    public override IOperation Apply() {
86      var populationSimilarity = new Dictionary<IScope, double>();
87      var populationScope = CurrentScope.SubScopes[0];
88      var refSetScope = CurrentScope.SubScopes[1];
89      var similarityMatrix = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(populationScope, refSetScope);
90      for (int i = 0; i < populationScope.SubScopes.Count; i++) {
91        populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum();
92      }
93      int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count;
94      foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
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.