Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9376 was 8345, checked in by jkarder, 12 years ago

#1331: made similarity based operators storable and cloneable

File size: 3.9 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.Collections.Generic;
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 reference set.
35  /// </summary>
36  [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
37  [StorableClass]
38  public sealed class ReferenceSetUpdateMethod : 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<IntValue> ReferenceSetSizeParameter {
49      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
50    }
51    #endregion
52
53    #region Properties
54    private IScope CurrentScope {
55      get { return CurrentScopeParameter.ActualValue; }
56    }
57    private IntValue ReferenceSetSize {
58      get { return ReferenceSetSizeParameter.ActualValue; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    private ReferenceSetUpdateMethod(bool deserializing) : base(deserializing) { }
64    private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner)
65      : base(original, cloner) {
66      this.SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
67    }
68    public ReferenceSetUpdateMethod()
69      : base() {
70      #region Create parameters
71      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the population and the reference set."));
72      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
73      #endregion
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new ReferenceSetUpdateMethod(this, cloner);
78    }
79
80    public override IOperation Apply() {
81      var populationSimilarity = new Dictionary<IScope, double>();
82      var populationScope = CurrentScope.SubScopes[0];
83      var refSetScope = CurrentScope.SubScopes[1];
84      var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(populationScope, refSetScope);
85      for (int i = 0; i < populationScope.SubScopes.Count; i++) {
86        populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum();
87      }
88      int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count;
89      foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {
90        CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
91        CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
92      }
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.