Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.RAPGA/3.3/ProgressiveOffspringPreserver.cs @ 14731

Last change on this file since 14731 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Algorithms.RAPGA {
32  /// <summary>
33  /// An operator that progressively selects offspring by adding it to a scope list.
34  /// </summary>
35  /// <remarks>
36  /// The operator also performs duplication control.
37  /// </remarks>
38  [Item("ProgressiveOffspringPreserver", "An operator that progressively selects offspring by adding it to a scope list. The operator also performs duplication control.")]
39  [StorableClass]
40  public sealed class ProgressiveOffspringPreserver : SingleSuccessorOperator {
41    #region Parameter Properties
42    public ScopeParameter CurrentScopeParameter {
43      get { return (ScopeParameter)Parameters["CurrentScope"]; }
44    }
45    public ILookupParameter<ScopeList> OffspringListParameter {
46      get { return (ILookupParameter<ScopeList>)Parameters["OffspringList"]; }
47    }
48    public ILookupParameter<IntValue> ElitesParameter {
49      get { return (ILookupParameter<IntValue>)Parameters["Elites"]; }
50    }
51    public ILookupParameter<IntValue> MaximumPopulationSizeParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
53    }
54    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
55      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
56    }
57    #endregion
58
59    #region Properties
60    private IScope CurrentScope {
61      get { return CurrentScopeParameter.ActualValue; }
62    }
63    private ScopeList OffspringList {
64      get { return OffspringListParameter.ActualValue; }
65    }
66    private IntValue Elites {
67      get { return ElitesParameter.ActualValue; }
68    }
69    private IntValue MaximumPopulationSize {
70      get { return MaximumPopulationSizeParameter.ActualValue; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    private ProgressiveOffspringPreserver(bool deserializing) : base(deserializing) { }
76    private ProgressiveOffspringPreserver(ProgressiveOffspringPreserver original, Cloner cloner) : base(original, cloner) { }
77    public ProgressiveOffspringPreserver()
78      : base() {
79      #region Create parameters
80      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring."));
81      Parameters.Add(new LookupParameter<ScopeList>("OffspringList", "The list that contains the offspring."));
82      Parameters.Add(new LookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
83      Parameters.Add(new LookupParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions."));
84      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
85      #endregion
86    }
87
88    public override IDeepCloneable Clone(Cloner cloner) {
89      return new ProgressiveOffspringPreserver(this, cloner);
90    }
91
92    [StorableHook(HookType.AfterDeserialization)]
93    private void AfterDeserialization() {
94      // BackwardsCompatibility3.3
95      #region Backwards compatible code, remove with 3.4
96      if (!Parameters.ContainsKey("SimilarityCalculator"))
97        Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
98      #endregion
99    }
100
101    public override IOperation Apply() {
102      if (CurrentScope.SubScopes.Any()) { // offspring created
103        if (!OffspringList.Any()) OffspringList.AddRange(CurrentScope.SubScopes);
104        else { // stored offspring exists
105          var storedOffspringScope = new Scope();
106          storedOffspringScope.SubScopes.AddRange(OffspringList);
107          var similarityMatrix = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(CurrentScope, storedOffspringScope);
108
109          var createdOffspring = CurrentScope.SubScopes.ToArray();
110
111          int i = 0;
112          // as long as offspring is available and not enough offspring has been preserved
113          while (i < createdOffspring.Length && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value) {
114            if (similarityMatrix[i].Any(x => x.IsAlmost(1.0))) createdOffspring[i] = null; // discard duplicates
115            else OffspringList.Add(createdOffspring[i]);
116            i++;
117          }
118
119          // discard remaining offspring
120          while (i < createdOffspring.Length) createdOffspring[i++] = null;
121
122          // clean current scope
123          CurrentScope.SubScopes.Replace(createdOffspring.Where(x => x != null));
124        }
125      }
126      return base.Apply();
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.