Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection.OffspringSelection/OffspringSelector.cs @ 23

Last change on this file since 23 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Selection.OffspringSelection {
29  public class OffspringSelector : OperatorBase {
30    public override string Description {
31      get { return @"TODO\r\nOperator description still missing ..."; }
32    }
33
34    public OffspringSelector() {
35      AddVariableInfo(new VariableInfo("SuccessfulChild", "True if the child was successful", typeof(BoolData), VariableKind.In));
36      AddVariableInfo(new VariableInfo("SelectionPressureLimit", "Maximum selection pressure", typeof(DoubleData), VariableKind.In));
37      AddVariableInfo(new VariableInfo("SuccessRatioLimit", "Maximum success ratio", typeof(DoubleData), VariableKind.In));
38      AddVariableInfo(new VariableInfo("SelectionPressure", "Current selection pressure", typeof(DoubleData), VariableKind.New | VariableKind.Out));
39      AddVariableInfo(new VariableInfo("SuccessRatio", "Current success ratio", typeof(DoubleData), VariableKind.New | VariableKind.Out));
40      AddVariableInfo(new VariableInfo("GoodChildren", "Temporarily store successful children", typeof(ItemList), VariableKind.New | VariableKind.Out | VariableKind.In | VariableKind.Deleted));
41      AddVariableInfo(new VariableInfo("BadChildren", "Temporarily store unsuccessful children", typeof(ItemList), VariableKind.New | VariableKind.Out | VariableKind.In | VariableKind.Deleted));
42    }
43
44    public override IOperation Apply(IScope scope) {
45      double selectionPressureLimit = GetVariableValue<DoubleData>("SelectionPressureLimit", scope, true).Data;
46      double successRatioLimit = GetVariableValue<DoubleData>("SuccessRatioLimit", scope, true).Data;
47
48      IScope parents = scope.SubScopes[0];
49      IScope children = scope.SubScopes[1];
50
51      // retrieve good and bad children
52      ItemList goodChildren = GetVariableValue<ItemList>("GoodChildren", scope, false, false);
53      if (goodChildren == null) {
54        goodChildren = new ItemList();
55        goodChildren.ItemType = typeof(IScope);
56        IVariableInfo goodChildrenInfo = GetVariableInfo("GoodChildren");
57        if (goodChildrenInfo.Local)
58          AddVariable(new Variable(goodChildrenInfo.ActualName, goodChildren));
59        else
60          scope.AddVariable(new Variable(goodChildrenInfo.ActualName, goodChildren));
61      }
62      ItemList badChildren = GetVariableValue<ItemList>("BadChildren", scope, false, false);
63      if (badChildren == null) {
64        badChildren = new ItemList();
65        badChildren.ItemType = typeof(IScope);
66        IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
67        if (badChildrenInfo.Local)
68          AddVariable(new Variable(badChildrenInfo.ActualName, badChildren));
69        else
70          scope.AddVariable(new Variable(badChildrenInfo.ActualName, badChildren));
71      }
72
73      // separate new children in good and bad children
74      IVariableInfo successfulInfo = GetVariableInfo("SuccessfulChild");
75      while (children.SubScopes.Count > 0) {
76        IScope child = children.SubScopes[0];
77        bool successful = child.GetVariableValue<BoolData>(successfulInfo.ActualName, false).Data;
78        if (successful) goodChildren.Add(child);
79        else badChildren.Add(child);
80        children.RemoveSubScope(child);
81      }
82
83      // calculate actual selection pressure and success ratio
84      DoubleData selectionPressure = GetVariableValue<DoubleData>("SelectionPressure", scope, false, false);
85      if (selectionPressure == null) {
86        IVariableInfo selectionPressureInfo = GetVariableInfo("SelectionPressure");
87        selectionPressure = new DoubleData(0);
88        if (selectionPressureInfo.Local)
89          AddVariable(new Variable(selectionPressureInfo.ActualName, selectionPressure));
90        else
91          scope.AddVariable(new Variable(selectionPressureInfo.ActualName, selectionPressure));
92      }
93      DoubleData successRatio = GetVariableValue<DoubleData>("SuccessRatio", scope, false, false);
94      if (successRatio == null) {
95        IVariableInfo successRatioInfo = GetVariableInfo("SuccessRatio");
96        successRatio = new DoubleData(0);
97        if (successRatioInfo.Local)
98          AddVariable(new Variable(successRatioInfo.ActualName, successRatio));
99        else
100          scope.AddVariable(new Variable(successRatioInfo.ActualName, successRatio));
101      }
102      int goodCount = goodChildren.Count;
103      int badCount = badChildren.Count;
104      selectionPressure.Data = (goodCount + badCount) / ((double)parents.SubScopes.Count);
105      successRatio.Data = goodCount / ((double)parents.SubScopes.Count);
106
107      // check if enough children have been generated
108      if (((selectionPressure.Data < selectionPressureLimit) && (successRatio.Data < successRatioLimit)) ||
109          ((goodCount + badCount) < parents.SubScopes.Count)) {
110        // more children required -> reduce left and start children generation again
111        scope.RemoveSubScope(parents);
112        scope.RemoveSubScope(children);
113        for (int i = 0; i < parents.SubScopes.Count; i++)
114          scope.AddSubScope(parents.SubScopes[i]);
115
116        return new AtomicOperation(SubOperators[0], scope);
117      } else {
118        // enough children generated
119        while (children.SubScopes.Count < parents.SubScopes.Count) {
120          if (goodChildren.Count > 0) {
121            children.AddSubScope((IScope)goodChildren[0]);
122            goodChildren.RemoveAt(0);
123          } else {
124            children.AddSubScope((IScope)badChildren[0]);
125            badChildren.RemoveAt(0);
126          }
127        }
128
129        // remove good and bad children again
130        IVariableInfo goodChildrenInfo = GetVariableInfo("GoodChildren");
131        if (goodChildrenInfo.Local)
132          RemoveVariable(goodChildrenInfo.ActualName);
133        else
134          scope.RemoveVariable(goodChildrenInfo.ActualName);
135        IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
136        if (badChildrenInfo.Local)
137          RemoveVariable(badChildrenInfo.ActualName);
138        else
139          scope.RemoveVariable(badChildrenInfo.ActualName);
140
141        return null;
142      }
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.