Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1331:

  • added problem specific improvement operators (KnapsackImprovementOperator, TravelingSalesmanImprovementOperator)
  • added custom interface (IScatterSearchTargetProcessor) for Scatter Search specific operators that use a target parameter
  • added custom operator (OffspringProcessor) to process multiple children that were created by crossovers
  • extracted diversity calculation and added problem/encoding specific operators (BinaryVectorDiversityCalculator, PermutationDiversityCalculator) for it
  • added parameters and adjusted types
  • adjusted event handling
  • minor code improvements
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 System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.ScatterSearch {
32  /// <summary>
33  /// An operator that updates the reference set.
34  /// </summary>
35  [Item("ReferenceSetUpdateMethod", "An operator that updates the reference set.")]
36  [StorableClass]
37  public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator {
38    #region Parameter properties
39    public ScopeParameter CurrentScopeParameter {
40      get { return (ScopeParameter)Parameters["CurrentScope"]; }
41    }
42    public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
44    }
45    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
47    }
48    public IValueLookupParameter<IItem> TargetParameter {
49      get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }
50    }
51    public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter {
52      get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; }
53    }
54    #endregion
55
56    #region Properties
57    private IScope CurrentScope {
58      get { return CurrentScopeParameter.ActualValue; }
59    }
60    private IntValue NumberOfHighQualitySolutions {
61      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
62    }
63    private IntValue ReferenceSetSize {
64      get { return ReferenceSetSizeParameter.ActualValue; }
65    }
66    private IItem Target {
67      get { return TargetParameter.ActualValue; }
68    }
69    private DiversityCalculator DiversityCalculator {
70      get { return DiversityCalculatorParameter.ActualValue; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    private ReferenceSetUpdateMethod(bool deserializing) : base(deserializing) { }
76    private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { }
77    public ReferenceSetUpdateMethod() : base() { Initialize(); }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      return new ReferenceSetUpdateMethod(this, cloner);
81    }
82
83    private void Initialize() {
84      #region Create parameters
85      Parameters.Add(new ScopeParameter("CurrentScope"));
86      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
87      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
88      Parameters.Add(new ValueLookupParameter<IItem>("Target"));
89      Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator"));
90      #endregion
91      TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem
92    }
93
94    public override IOperation Apply() {
95      var population = new Dictionary<IScope, double>();
96      foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) {
97        int diversity = 0;
98        var pSol = pScope.Variables[TargetParameter.ActualName].Value;
99        foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) {
100          var rSol = rScope.Variables[TargetParameter.ActualName].Value;
101          diversity += DiversityCalculator.ExecuteCalculation(pSol, rSol);
102        }
103        population[pScope] = diversity;
104      }
105      foreach (var entry in population.OrderByDescending(x => x.Value).Take(ReferenceSetSize.Value - NumberOfHighQualitySolutions.Value)) {
106        CurrentScope.SubScopes[1].SubScopes.Add(entry.Key);
107        CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key);
108      }
109      return base.Apply();
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.