Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlReaderWriterBranch/HeuristicLab.Selection.OffspringSelection/OffspringSelector.cs @ 125

Last change on this file since 125 was 77, checked in by swagner, 17 years ago

Fixed ticket #67

  • adapted accessing of variables in operators due to changes of variable lookup and the new name aliasing mechanism (actual/formal name translations should not be done directly anymore; instead the new method Scope.TranslateName should be used)
File size: 7.0 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<IScope>), VariableKind.New | VariableKind.Out | VariableKind.In | VariableKind.Deleted));
41      AddVariableInfo(new VariableInfo("BadChildren", "Temporarily store unsuccessful children", typeof(ItemList<IScope>), 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<IScope> goodChildren = GetVariableValue<ItemList<IScope>>("GoodChildren", scope, false, false);
53      if (goodChildren == null) {
54        goodChildren = new ItemList<IScope>();
55        IVariableInfo goodChildrenInfo = GetVariableInfo("GoodChildren");
56        if (goodChildrenInfo.Local)
57          AddVariable(new Variable(goodChildrenInfo.ActualName, goodChildren));
58        else
59          scope.AddVariable(new Variable(scope.TranslateName(goodChildrenInfo.FormalName), goodChildren));
60      }
61      ItemList<IScope> badChildren = GetVariableValue<ItemList<IScope>>("BadChildren", scope, false, false);
62      if (badChildren == null) {
63        badChildren = new ItemList<IScope>();
64        IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
65        if (badChildrenInfo.Local)
66          AddVariable(new Variable(badChildrenInfo.ActualName, badChildren));
67        else
68          scope.AddVariable(new Variable(scope.TranslateName(badChildrenInfo.FormalName), badChildren));
69      }
70
71      // separate new children in good and bad children
72      IVariableInfo successfulInfo = GetVariableInfo("SuccessfulChild");
73      while (children.SubScopes.Count > 0) {
74        IScope child = children.SubScopes[0];
75        bool successful = child.GetVariableValue<BoolData>(successfulInfo.FormalName, false).Data;
76        if (successful) goodChildren.Add(child);
77        else badChildren.Add(child);
78        children.RemoveSubScope(child);
79      }
80
81      // calculate actual selection pressure and success ratio
82      DoubleData selectionPressure = GetVariableValue<DoubleData>("SelectionPressure", scope, false, false);
83      if (selectionPressure == null) {
84        IVariableInfo selectionPressureInfo = GetVariableInfo("SelectionPressure");
85        selectionPressure = new DoubleData(0);
86        if (selectionPressureInfo.Local)
87          AddVariable(new Variable(selectionPressureInfo.ActualName, selectionPressure));
88        else
89          scope.AddVariable(new Variable(scope.TranslateName(selectionPressureInfo.FormalName), selectionPressure));
90      }
91      DoubleData successRatio = GetVariableValue<DoubleData>("SuccessRatio", scope, false, false);
92      if (successRatio == null) {
93        IVariableInfo successRatioInfo = GetVariableInfo("SuccessRatio");
94        successRatio = new DoubleData(0);
95        if (successRatioInfo.Local)
96          AddVariable(new Variable(successRatioInfo.ActualName, successRatio));
97        else
98          scope.AddVariable(new Variable(scope.TranslateName(successRatioInfo.FormalName), successRatio));
99      }
100      int goodCount = goodChildren.Count;
101      int badCount = badChildren.Count;
102      selectionPressure.Data = (goodCount + badCount) / ((double)parents.SubScopes.Count);
103      successRatio.Data = goodCount / ((double)parents.SubScopes.Count);
104
105      // check if enough children have been generated
106      if (((selectionPressure.Data < selectionPressureLimit) && (successRatio.Data < successRatioLimit)) ||
107          ((goodCount + badCount) < parents.SubScopes.Count)) {
108        // more children required -> reduce left and start children generation again
109        scope.RemoveSubScope(parents);
110        scope.RemoveSubScope(children);
111        for (int i = 0; i < parents.SubScopes.Count; i++)
112          scope.AddSubScope(parents.SubScopes[i]);
113
114        return new AtomicOperation(SubOperators[0], scope);
115      } else {
116        // enough children generated
117        while (children.SubScopes.Count < parents.SubScopes.Count) {
118          if (goodChildren.Count > 0) {
119            children.AddSubScope((IScope)goodChildren[0]);
120            goodChildren.RemoveAt(0);
121          } else {
122            children.AddSubScope((IScope)badChildren[0]);
123            badChildren.RemoveAt(0);
124          }
125        }
126
127        // remove good and bad children again
128        IVariableInfo goodChildrenInfo = GetVariableInfo("GoodChildren");
129        if (goodChildrenInfo.Local)
130          RemoveVariable(goodChildrenInfo.ActualName);
131        else
132          scope.RemoveVariable(scope.TranslateName(goodChildrenInfo.FormalName));
133        IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
134        if (badChildrenInfo.Local)
135          RemoveVariable(badChildrenInfo.ActualName);
136        else
137          scope.RemoveVariable(scope.TranslateName(badChildrenInfo.FormalName));
138
139        return null;
140      }
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.