Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/PopulationRebuildMethod.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.5 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
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 and rebuilds the population.
33  /// </summary>
34  [Item("PopulationRebuildMethod", "An operator that updates the reference set and rebuilds the population.")]
35  [StorableClass]
36  public sealed class PopulationRebuildMethod : SingleSuccessorOperator {
37    #region Parameter properties
38    public ScopeParameter CurrentScopeParameter {
39      get { return (ScopeParameter)Parameters["CurrentScope"]; }
40    }
41    public IValueLookupParameter<BoolValue> MaximizationParameter {
42      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
43    }
44    public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
46    }
47    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
48      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
49    }
50    public IValueLookupParameter<IItem> QualityParameter {
51      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
52    }
53    #endregion
54
55    #region Properties
56    private IScope CurrentScope {
57      get { return CurrentScopeParameter.ActualValue; }
58    }
59    private BoolValue Maximization {
60      get { return MaximizationParameter.ActualValue; }
61      set { MaximizationParameter.ActualValue = value; }
62    }
63    private IntValue NumberOfHighQualitySolutions {
64      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
65      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
66    }
67    private IntValue ReferenceSetSize {
68      get { return ReferenceSetSizeParameter.ActualValue; }
69      set { ReferenceSetSizeParameter.ActualValue = value; }
70    }
71    private IItem Quality {
72      get { return QualityParameter.ActualValue; }
73    }
74    #endregion
75
76    [StorableConstructor]
77    private PopulationRebuildMethod(bool deserializing) : base(deserializing) { }
78    private PopulationRebuildMethod(PopulationRebuildMethod original, Cloner cloner) : base(original, cloner) { }
79    public PopulationRebuildMethod() : base() { Initialize(); }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new PopulationRebuildMethod(this, cloner);
83    }
84
85    private void Initialize() {
86      #region Create parameters
87      Parameters.Add(new ScopeParameter("CurrentScope"));
88      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));
89      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
90      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
91      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
92      #endregion
93    }
94
95    public override IOperation Apply() {
96      var populationScope = CurrentScope.SubScopes[0];
97      var referenceSetScope = CurrentScope.SubScopes[1];
98      var orderedReferenceSet = Maximization.Value ? referenceSetScope.SubScopes.OrderByDescending(r => r.Variables[QualityParameter.ActualName].Value) :
99                                                     referenceSetScope.SubScopes.OrderBy(r => r.Variables[QualityParameter.ActualName].Value);
100      referenceSetScope.SubScopes.Replace(orderedReferenceSet.Take(NumberOfHighQualitySolutions.Value).ToList());
101      populationScope.SubScopes.Clear();
102      return base.Apply();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.