Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/OffspringSelector.cs @ 3484

Last change on this file since 3484 was 3479, checked in by abeham, 14 years ago

added first draft of SASEGASA #839

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Selection {
32  [Item("OffspringSelector", "Selects among the offspring population those that are designated successful and discards the unsuccessful offspring, except for some lucky losers. It expects the parent scopes to be below the first sub-scope, and offspring scopes to be below the second sub-scope separated again in two sub-scopes, the first with the failed offspring and the second with successful offspring.")]
33  [StorableClass]
34  public class OffspringSelector : SingleSuccessorOperator {
35
36    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
37      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
38    }
39    public ValueLookupParameter<DoubleValue> SuccessRatioParameter {
40      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
41    }
42    public LookupParameter<DoubleValue> SelectionPressureParameter {
43      get { return (ValueLookupParameter<DoubleValue>)Parameters["SelectionPressure"]; }
44    }
45    public LookupParameter<DoubleValue> CurrentSuccessRatioParameter {
46      get { return (LookupParameter<DoubleValue>)Parameters["CurrentSuccessRatio"]; }
47    }
48    public LookupParameter<ItemList<IScope>> WinnersParameter {
49      get { return (LookupParameter<ItemList<IScope>>)Parameters["Winners"]; }
50    }
51    public LookupParameter<ItemList<IScope>> LuckyLosersParameter {
52      get { return (LookupParameter<ItemList<IScope>>)Parameters["LuckyLosers"]; }
53    }
54    public OperatorParameter OffspringCreatorParameter {
55      get { return (OperatorParameter)Parameters["OffspringCreator"]; }
56    }
57
58    public IOperator OffspringCreator {
59      get { return OffspringCreatorParameter.Value; }
60      set { OffspringCreatorParameter.Value = value; }
61    }
62
63    public OffspringSelector()
64      : base() {
65      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step."));
66      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful offspring that has to be produced."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>("SelectionPressure", "The amount of selection pressure currently necessary to fulfill the success ratio."));
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio indicates how much of the successful offspring have already been generated."));
69      Parameters.Add(new LookupParameter<ItemList<IScope>>("Winners", "Temporary store of the successful offspring."));
70      Parameters.Add(new LookupParameter<ItemList<IScope>>("LuckyLosers", "Temporary store of the lucky losers."));
71      Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring."));
72    }
73
74    public override IOperation Apply() {
75      double maxSelPress = MaximumSelectionPressureParameter.ActualValue.Value;
76      double successRatio = SuccessRatioParameter.ActualValue.Value;
77      IScope scope = ExecutionContext.Scope;
78      IScope parents = scope.SubScopes[0];
79      IScope children = scope.SubScopes[1];
80      int populationSize = parents.SubScopes.Count;
81
82      // retrieve actual selection pressure and success ratio
83      DoubleValue selectionPressure = SelectionPressureParameter.ActualValue;
84      if (selectionPressure == null) {
85        selectionPressure = new DoubleValue(0);
86        SelectionPressureParameter.ActualValue = selectionPressure;
87      }
88      DoubleValue currentSuccessRatio = CurrentSuccessRatioParameter.ActualValue;
89      if (currentSuccessRatio == null) {
90        currentSuccessRatio = new DoubleValue(0);
91        CurrentSuccessRatioParameter.ActualValue = currentSuccessRatio;
92      }
93
94      // retrieve winners and lucky losers
95      ItemList<IScope> winners = WinnersParameter.ActualValue;
96      if (winners == null) {
97        winners = new ItemList<IScope>();
98        WinnersParameter.ActualValue = winners;
99        selectionPressure.Value = 0; // initialize selection pressure for this round
100        currentSuccessRatio.Value = 0; // initialize current success ratio for this round
101      }
102      ItemList<IScope> luckyLosers = LuckyLosersParameter.ActualValue;
103      if (luckyLosers == null) {
104        luckyLosers = new ItemList<IScope>();
105        LuckyLosersParameter.ActualValue = luckyLosers;
106      }
107
108      // separate new offspring in winners and lucky losers, the unlucky losers are discarded, sorry guys
109      int winnersCount = 0;
110      int losersCount = 0;
111      ScopeList offspring = children.SubScopes[1].SubScopes; // the winners
112      winnersCount += offspring.Count;
113      winners.AddRange(offspring);
114      offspring = children.SubScopes[0].SubScopes; // the losers
115      while (offspring.Count > 0 && ((1 - successRatio) * populationSize > luckyLosers.Count ||
116            selectionPressure.Value >= maxSelPress)) {
117        luckyLosers.Add(offspring[0]);
118        losersCount++;
119        offspring.RemoveAt(0);
120      }
121      losersCount += offspring.Count;
122      children.SubScopes.Clear();
123
124      // calculate actual selection pressure and success ratio
125      selectionPressure.Value += (winnersCount + losersCount) / ((double)populationSize);
126      currentSuccessRatio.Value = winners.Count / ((double)populationSize);
127
128      // check if enough children have been generated
129      if (((selectionPressure.Value < maxSelPress) && (currentSuccessRatio.Value < successRatio)) ||
130          ((winners.Count + luckyLosers.Count) < populationSize)) {
131        // more children required -> reduce left and start children generation again
132        scope.SubScopes.Remove(parents);
133        scope.SubScopes.Remove(children);
134        while(parents.SubScopes.Count > 0)
135          scope.SubScopes.Add(parents.SubScopes[0]);
136
137        IOperator moreOffspring = OffspringCreatorParameter.ActualValue as IOperator;
138        if (moreOffspring == null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them.");
139        return ExecutionContext.CreateOperation(moreOffspring);
140      } else {
141        // enough children generated
142        while (children.SubScopes.Count < populationSize) {
143          if (winners.Count > 0) {
144            children.SubScopes.Add((IScope)winners[0]);
145            winners.RemoveAt(0);
146          } else {
147            children.SubScopes.Add((IScope)luckyLosers[0]);
148            luckyLosers.RemoveAt(0);
149          }
150        }
151
152        scope.Variables.Remove(WinnersParameter.ActualName);
153        scope.Variables.Remove(LuckyLosersParameter.ActualName);
154        return base.Apply();
155      }
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.