[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 |
|
---|
| 28 | namespace 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] | 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));
|
---|
[2] | 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
|
---|
[40] | 52 | ItemList<IScope> goodChildren = GetVariableValue<ItemList<IScope>>("GoodChildren", scope, false, false);
|
---|
[2] | 53 | if (goodChildren == null) {
|
---|
[40] | 54 | goodChildren = new ItemList<IScope>();
|
---|
[2] | 55 | IVariableInfo goodChildrenInfo = GetVariableInfo("GoodChildren");
|
---|
| 56 | if (goodChildrenInfo.Local)
|
---|
| 57 | AddVariable(new Variable(goodChildrenInfo.ActualName, goodChildren));
|
---|
| 58 | else
|
---|
[77] | 59 | scope.AddVariable(new Variable(scope.TranslateName(goodChildrenInfo.FormalName), goodChildren));
|
---|
[2] | 60 | }
|
---|
[40] | 61 | ItemList<IScope> badChildren = GetVariableValue<ItemList<IScope>>("BadChildren", scope, false, false);
|
---|
[2] | 62 | if (badChildren == null) {
|
---|
[40] | 63 | badChildren = new ItemList<IScope>();
|
---|
[2] | 64 | IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
|
---|
| 65 | if (badChildrenInfo.Local)
|
---|
| 66 | AddVariable(new Variable(badChildrenInfo.ActualName, badChildren));
|
---|
| 67 | else
|
---|
[77] | 68 | scope.AddVariable(new Variable(scope.TranslateName(badChildrenInfo.FormalName), badChildren));
|
---|
[2] | 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];
|
---|
[77] | 75 | bool successful = child.GetVariableValue<BoolData>(successfulInfo.FormalName, false).Data;
|
---|
[2] | 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
|
---|
[77] | 89 | scope.AddVariable(new Variable(scope.TranslateName(selectionPressureInfo.FormalName), selectionPressure));
|
---|
[2] | 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
|
---|
[77] | 98 | scope.AddVariable(new Variable(scope.TranslateName(successRatioInfo.FormalName), successRatio));
|
---|
[2] | 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
|
---|
[77] | 132 | scope.RemoveVariable(scope.TranslateName(goodChildrenInfo.FormalName));
|
---|
[2] | 133 | IVariableInfo badChildrenInfo = GetVariableInfo("BadChildren");
|
---|
| 134 | if (badChildrenInfo.Local)
|
---|
| 135 | RemoveVariable(badChildrenInfo.ActualName);
|
---|
| 136 | else
|
---|
[77] | 137 | scope.RemoveVariable(scope.TranslateName(badChildrenInfo.FormalName));
|
---|
[2] | 138 |
|
---|
| 139 | return null;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|