Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs @ 7724

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

#1331:

  • added analyzer
  • added parameters and adjusted parameter types
  • corrected ReferenceSetUpdateMethod
  • changed access levels
File size: 4.7 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.BinaryVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Algorithms.ScatterSearch {
31  /// <summary>
32  /// An operator that updates the reference set.
33  /// </summary>
34  [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
35  [StorableClass]
36  public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
37    #region Parameter properties
38    private ScopeParameter CurrentScopeParameter {
39      get { return (ScopeParameter)Parameters["CurrentScope"]; }
40    }
41    public ValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
42      get { return (ValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
43    }
44    public ValueLookupParameter<IntValue> ReferenceSetSizeParameter {
45      get { return (ValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
46    }
47    #endregion
48
49    #region Properties
50    public IScope CurrentScope {
51      get { return CurrentScopeParameter.ActualValue; }
52    }
53    public IntValue NumberOfHighQualitySolutions {
54      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
55      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
56    }
57    public IntValue ReferenceSetSize {
58      get { return ReferenceSetSizeParameter.ActualValue; }
59      set { ReferenceSetSizeParameter.ActualValue = value; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    private ReferenceSetUpdateMethod(bool deserializing) : base(deserializing) { }
65    private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
66    public ReferenceSetUpdateMethod() : base() { Initialize(); }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new ReferenceSetUpdateMethod(this, cloner);
70    }
71
72    private void Initialize() {
73      #region Create parameters
74      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope to which the new solutions are added as sub-scopes."));
75      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions that should be added to the reference set."));
76      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set."));
77      #endregion
78
79      #region Create operators
80      #endregion
81
82      #region Create operator graph
83      #endregion
84    }
85
86    public override IOperation Apply() {
87      for (int i = 0; i < ReferenceSetSize.Value - NumberOfHighQualitySolutions.Value; i++) {
88        IScope mostDiverseSolution = null;
89        int maxDiversity = 0;
90        foreach (var p in CurrentScope.SubScopes[0].SubScopes) {
91          int diversity = 0;
92          BinaryVector pSol = p.Variables["KnapsackSolution"].Value as BinaryVector;
93          foreach (var r in CurrentScope.SubScopes[1].SubScopes) {
94            BinaryVector rSol = r.Variables["KnapsackSolution"].Value as BinaryVector;
95            if (pSol != null && rSol != null) diversity += CalculateDiversity(pSol, rSol);
96          }
97          if (mostDiverseSolution == null || diversity > maxDiversity) {
98            mostDiverseSolution = p;
99            maxDiversity = diversity;
100          }
101        }
102        CurrentScope.SubScopes[1].SubScopes.Add(mostDiverseSolution); // Update parent?
103        CurrentScope.SubScopes[0].SubScopes.Remove(mostDiverseSolution); // Update parent?
104      }
105      return base.Apply();
106    }
107
108    private int CalculateDiversity(BinaryVector pSol, BinaryVector rSol) {
109      int diversity = 0;
110      for (int i = 0; i < pSol.Length; i++)
111        if (pSol[i] != rSol[i]) diversity++;
112      return diversity;
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.