Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Selection.OffspringSelection/OffspringAnalyzer.cs @ 13636

Last change on this file since 13636 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: 5.7 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 OffspringAnalyzer : OperatorBase {
30    public override string Description {
31      get { return @"TODO\r\nOperator description still missing ..."; }
32    }
33
34    public OffspringAnalyzer() {
35      AddVariableInfo(new VariableInfo("Maximization", "Problem is a maximization problem", typeof(BoolData), VariableKind.In));
36      AddVariableInfo(new VariableInfo("Quality", "Quality value", typeof(DoubleData), VariableKind.In));
37      AddVariableInfo(new VariableInfo("ParentQualities", "Temporarily store parent qualities", typeof(DoubleArrayData), VariableKind.New | VariableKind.Out | VariableKind.In | VariableKind.Deleted));
38      AddVariableInfo(new VariableInfo("SuccessfulChild", "True if the child was successful", typeof(BoolData), VariableKind.New));
39
40      VariableInfo compFactorInfo = new VariableInfo("ComparisonFactor", "Factor for comparing the quality of a child with the qualities of its parents (0 = better than worst parent, 1 = better than best parent)", typeof(DoubleData), VariableKind.In);
41      compFactorInfo.Local = true;
42      AddVariableInfo(compFactorInfo);
43      AddVariable(new Variable("ComparisonFactor", new DoubleData(0.5)));
44      VariableInfo parentCount = new VariableInfo("ParentsCount", "How many parents created the offspring", typeof(IntData), VariableKind.In);
45      parentCount.Local = true;
46      AddVariableInfo(parentCount);
47      AddVariable(new Variable("ParentsCount", new IntData(2)));
48    }
49
50    public override IOperation Apply(IScope scope) {
51      bool maximize = GetVariableValue<BoolData>("Maximization", scope, true).Data;
52      double compFact = GetVariableValue<DoubleData>("ComparisonFactor", scope, true).Data;
53      int parentsCount = GetVariableValue<IntData>("ParentsCount", scope, true).Data;
54      if (parentsCount < 1) throw new InvalidOperationException("OffspringAnalyzer: ParentsCount must be >= 1");
55
56      DoubleArrayData qualities = GetVariableValue<DoubleArrayData>("ParentQualities", scope, false, false);
57      if (qualities == null) {
58        // fetch and store parent qualities
59        IVariableInfo qualityInfo = GetVariableInfo("Quality");
60        double[] qualitiesArray = new double[scope.SubScopes.Count];
61        for (int i = 0; i < qualitiesArray.Length; i++)
62          qualitiesArray[i] = scope.SubScopes[i].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
63        qualities = new DoubleArrayData(qualitiesArray);
64        IVariableInfo parentQualitiesInfo = GetVariableInfo("ParentQualities");
65        if (parentQualitiesInfo.Local)
66          AddVariable(new Variable(parentQualitiesInfo.ActualName, qualities));
67        else
68          scope.AddVariable(new Variable(scope.TranslateName(parentQualitiesInfo.FormalName), qualities));
69
70        CompositeOperation next = new CompositeOperation();
71        next.AddOperation(new AtomicOperation(SubOperators[0], scope));
72        next.AddOperation(new AtomicOperation(this, scope));
73        return next;
74      } else {
75        // analyze offspring
76        for (int i = 0; i < scope.SubScopes.Count; i++) {
77          double parent1 = double.MaxValue; // lowest quality parent
78          double parent2 = double.MinValue; // highest quality parent
79          for (int y = 0 ; y < parentsCount ; y++) {
80            if (qualities.Data[parentsCount * i + y] < parent1) parent1 = qualities.Data[parentsCount * i + y];
81            if (qualities.Data[parentsCount * i + y] > parent2) parent2 = qualities.Data[parentsCount * i + y];
82          }
83          IVariableInfo qualityInfo = GetVariableInfo("Quality");
84          double child = scope.SubScopes[i].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
85          double threshold;
86
87          if (!maximize)
88            threshold = parent2 + (parent1 - parent2) * compFact;
89          else
90            threshold = parent1 + (parent2 - parent1) * compFact;
91
92          IVariableInfo successfulInfo = GetVariableInfo("SuccessfulChild");
93          BoolData successful;
94          if (((!maximize) && (child < threshold)) ||
95              ((maximize) && (child > threshold)))
96            successful = new BoolData(true);
97          else
98            successful = new BoolData(false);
99          scope.SubScopes[i].AddVariable(new Variable(scope.TranslateName(successfulInfo.FormalName), successful));
100        }
101
102        // remove parent qualities again
103        IVariableInfo parentQualitiesInfo = GetVariableInfo("ParentQualities");
104        if (parentQualitiesInfo.Local)
105          RemoveVariable(parentQualitiesInfo.ActualName);
106        else
107          scope.RemoveVariable(scope.TranslateName(parentQualitiesInfo.FormalName));
108
109        return null;
110      }
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.