Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection.OffspringSelection/OffspringAnalyzer.cs @ 972

Last change on this file since 972 was 806, checked in by gkronber, 16 years ago

implemented #386 (OffspringAnalyser should support m children for n parents)

File size: 6.3 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        int crossoverEvents = qualities.Data.Length / parentsCount;
76        int childrenPerCrossoverEvent = scope.SubScopes.Count / crossoverEvents;
77        if (scope.SubScopes.Count % crossoverEvents != 0 ||
78          childrenPerCrossoverEvent < 1) throw new InvalidOperationException("OffspringAnalyzer: The number of children per crossover event has to be constant and >= 1");
79        // analyze offspring of all crossoverEvents
80        for (int i = 0; i < crossoverEvents; i++) {
81          double worstParent = double.MaxValue; // lowest quality parent
82          double bestParent = double.MinValue; // highest quality parent
83          for (int y = 0; y < parentsCount; y++) {
84            if (qualities.Data[i * parentsCount + y] < worstParent) worstParent = qualities.Data[i * parentsCount + y];
85            if (qualities.Data[i * parentsCount + y] > bestParent) bestParent = qualities.Data[i * parentsCount + y];
86          }
87          for (int j = 0; j < childrenPerCrossoverEvent; j++) {
88            IVariableInfo qualityInfo = GetVariableInfo("Quality");
89            double child = scope.SubScopes[i * childrenPerCrossoverEvent + j].GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
90            double threshold;
91
92            if (!maximize)
93              threshold = bestParent + (worstParent - bestParent) * compFact;
94            else
95              threshold = worstParent + (bestParent - worstParent) * compFact;
96
97            IVariableInfo successfulInfo = GetVariableInfo("SuccessfulChild");
98            BoolData successful;
99            if (((!maximize) && (child < threshold)) ||
100                ((maximize) && (child > threshold)))
101              successful = new BoolData(true);
102            else
103              successful = new BoolData(false);
104            scope.SubScopes[i * childrenPerCrossoverEvent + j].AddVariable(new Variable(scope.TranslateName(successfulInfo.FormalName), successful));
105          }
106        }
107
108        // remove parent qualities again
109        IVariableInfo parentQualitiesInfo = GetVariableInfo("ParentQualities");
110        if (parentQualitiesInfo.Local)
111          RemoveVariable(parentQualitiesInfo.ActualName);
112        else
113          scope.RemoveVariable(scope.TranslateName(parentQualitiesInfo.FormalName));
114
115        return null;
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.