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

Last change on this file since 3769 was 3769, checked in by abeham, 11 years ago

#976

  • fixed a bug in the OffspringSelector when counting the selection pressure
File size: 8.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>> OffspringPopulationParameter {
49      get { return (LookupParameter<ItemList<IScope>>)Parameters["OffspringPopulation"]; }
50    }
51    public LookupParameter<IntValue> OffspringPopulationWinnersParameter {
52      get { return (LookupParameter<IntValue>)Parameters["OffspringPopulationWinners"]; }
53    }
54    public ScopeTreeLookupParameter<BoolValue> SuccessfulOffspringParameter {
55      get { return (ScopeTreeLookupParameter<BoolValue>)Parameters["SuccessfulOffspring"]; }
56    }
57    public OperatorParameter OffspringCreatorParameter {
58      get { return (OperatorParameter)Parameters["OffspringCreator"]; }
59    }
60
61    public IOperator OffspringCreator {
62      get { return OffspringCreatorParameter.Value; }
63      set { OffspringCreatorParameter.Value = value; }
64    }
65
66    public OffspringSelector()
67      : base() {
68      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step."));
69      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful offspring that has to be produced."));
70      Parameters.Add(new ValueLookupParameter<DoubleValue>("SelectionPressure", "The amount of selection pressure currently necessary to fulfill the success ratio."));
71      Parameters.Add(new ValueLookupParameter<DoubleValue>("CurrentSuccessRatio", "The current success ratio indicates how much of the successful offspring have already been generated."));
72      Parameters.Add(new LookupParameter<ItemList<IScope>>("OffspringPopulation", "Temporary store of the offspring population."));
73      Parameters.Add(new LookupParameter<IntValue>("OffspringPopulationWinners", "Temporary store the number of successful offspring in the offspring population."));
74      Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("SuccessfulOffspring", "True if the offspring was more successful than its parents.", 2));
75      Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring."));
76    }
77
78    public override IOperation Apply() {
79      double maxSelPress = MaximumSelectionPressureParameter.ActualValue.Value;
80      double successRatio = SuccessRatioParameter.ActualValue.Value;
81      IScope scope = ExecutionContext.Scope;
82      IScope parents = scope.SubScopes[0];
83      IScope offspring = scope.SubScopes[1];
84      int populationSize = parents.SubScopes.Count;
85      int offspringSize = offspring.SubScopes.Count;
86
87      // retrieve actual selection pressure and success ratio
88      DoubleValue selectionPressure = SelectionPressureParameter.ActualValue;
89      if (selectionPressure == null) {
90        selectionPressure = new DoubleValue(0);
91        SelectionPressureParameter.ActualValue = selectionPressure;
92      }
93      DoubleValue currentSuccessRatio = CurrentSuccessRatioParameter.ActualValue;
94      if (currentSuccessRatio == null) {
95        currentSuccessRatio = new DoubleValue(0);
96        CurrentSuccessRatioParameter.ActualValue = currentSuccessRatio;
97      }
98
99      // retrieve next population
100      ItemList<IScope> population = OffspringPopulationParameter.ActualValue;
101      IntValue successfulOffspring;
102      if (population == null) {
103        population = new ItemList<IScope>();
104        OffspringPopulationParameter.ActualValue = population;
105        selectionPressure.Value = 0; // initialize selection pressure for this round
106        currentSuccessRatio.Value = 0; // initialize current success ratio for this round
107        successfulOffspring = new IntValue(0);
108        OffspringPopulationWinnersParameter.ActualValue = successfulOffspring;
109      } else successfulOffspring = OffspringPopulationWinnersParameter.ActualValue;
110     
111      int worseOffspringNeeded = (int)((1 - successRatio) * populationSize) - (population.Count - successfulOffspring.Value);
112      int successfulOffspringAdded = 0;
113
114      // implement the ActualValue fetch here - otherwise the parent scope would also be included, given that there may be 1000 or more parents, this is quite unnecessary
115      string tname = SuccessfulOffspringParameter.TranslatedName;
116      double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize;
117      for (int i = 0; i < offspringSize; i++) {
118        // fetch value
119        IVariable tmpVar;
120        if (!offspring.SubScopes[i].Variables.TryGetValue(tname, out tmpVar)) throw new InvalidOperationException(Name + ": Could not determine if an offspring was successful or not.");
121        BoolValue tmp = (tmpVar.Value as BoolValue);
122        if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue.");
123
124        // add to population
125        if (tmp.Value) {
126          population.Add(offspring.SubScopes[i]);
127          successfulOffspringAdded++;
128        } else if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) {
129          population.Add(offspring.SubScopes[i]);
130          worseOffspringNeeded--;
131        }
132        tmpSelPress += tmpSelPressInc;
133        if (population.Count == populationSize) break;
134      }
135      successfulOffspring.Value += successfulOffspringAdded;
136
137      // calculate actual selection pressure and success ratio
138      selectionPressure.Value = tmpSelPress;
139      currentSuccessRatio.Value = successfulOffspring.Value / ((double)populationSize);
140
141      // check if enough children have been generated
142      if (((selectionPressure.Value < maxSelPress) && (currentSuccessRatio.Value < successRatio)) ||
143          (population.Count < populationSize)) {
144        // more children required -> reduce left and start children generation again
145        scope.SubScopes.Remove(parents);
146        scope.SubScopes.Remove(offspring);
147        while(parents.SubScopes.Count > 0)
148          scope.SubScopes.Add(parents.SubScopes[0]);
149
150        IOperator moreOffspring = OffspringCreatorParameter.ActualValue as IOperator;
151        if (moreOffspring == null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them.");
152        return ExecutionContext.CreateOperation(moreOffspring);
153      } else {
154        // enough children generated
155        offspring.SubScopes.Clear();
156        offspring.SubScopes.AddRange(population);
157
158        scope.Variables.Remove(OffspringPopulationParameter.TranslatedName);
159        scope.Variables.Remove(OffspringPopulationWinnersParameter.TranslatedName);
160        return base.Apply();
161      }
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.