source: branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/PopulationRebuildMethod.cs @ 7740

Last change on this file since 7740 was 7740, checked in by jkarder, 9 years ago

#1331:

  • added custom crossover operator (NChildCrossover)
  • added parameters and adjusted types
  • replaced SolutionCombinationMethod with a placeholder
  • adjusted event handling
  • changed access levels
  • minor code improvements
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.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<IntValue> NumberOfHighQualitySolutionsParameter {
42      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }
43    }
44    public IValueLookupParameter<IntValue> ReferenceSetSizeParameter {
45      get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; }
46    }
47    public IValueLookupParameter<IItem> QualityParameter {
48      get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }
49    }
50    #endregion
51
52    #region Properties
53    private IScope CurrentScope {
54      get { return CurrentScopeParameter.ActualValue; }
55    }
56    private IntValue NumberOfHighQualitySolutions {
57      get { return NumberOfHighQualitySolutionsParameter.ActualValue; }
58      set { NumberOfHighQualitySolutionsParameter.ActualValue = value; }
59    }
60    private IntValue ReferenceSetSize {
61      get { return ReferenceSetSizeParameter.ActualValue; }
62      set { ReferenceSetSizeParameter.ActualValue = value; }
63    }
64    private IItem Quality {
65      get { return QualityParameter.ActualValue; }
66    }
67    #endregion
68
69    [StorableConstructor]
70    private PopulationRebuildMethod(bool deserializing) : base(deserializing) { }
71    private PopulationRebuildMethod(PopulationRebuildMethod original, Cloner cloner) : base(original, cloner) { }
72    public PopulationRebuildMethod() : base() { Initialize(); }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new PopulationRebuildMethod(this, cloner);
76    }
77
78    private void Initialize() {
79      #region Create parameters
80      Parameters.Add(new ScopeParameter("CurrentScope"));
81      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));
82      Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));
83      Parameters.Add(new ValueLookupParameter<IItem>("Quality"));
84      #endregion
85    }
86
87    public override IOperation Apply() {
88      var population = CurrentScope.SubScopes[0];
89      var refSet = CurrentScope.SubScopes[1];
90      refSet.SubScopes.Replace(refSet.SubScopes.OrderByDescending(r => r.Variables[QualityParameter.ActualName].Value)
91                                               .Take(NumberOfHighQualitySolutions.Value).ToList());
92      population.SubScopes.Clear();
93      return base.Apply();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.