Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Selection.Uncertainty/3.2/UncertainTournamentSelector.cs @ 4398

Last change on this file since 4398 was 1743, checked in by abeham, 16 years ago

Adding log messages to evaluate if this works at all (#611)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Selection;
29using HeuristicLab.StatisticalAnalysis;
30using HeuristicLab.Tracing;
31
32namespace HeuristicLab.Selection.Uncertainty {
33  public class UncertainTournamentSelector : StochasticSelectorBase {
34    public override string Description {
35      get { return @"Selects an individual from a tournament group, based on tests of statistical significance of quality arrays."; }
36    }
37
38    public UncertainTournamentSelector()
39      : base() {
40      AddVariableInfo(new VariableInfo("QualitySamples", "The array of quality samples resulting from several evaluations", typeof(DoubleArrayData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("GroupSize", "Size of the tournament group", typeof(IntData), VariableKind.In));
43      GetVariableInfo("GroupSize").Local = true;
44      AddVariable(new Variable("GroupSize", new IntData(2)));
45      GetVariable("CopySelected").GetValue<BoolData>().Data = true;
46      AddVariableInfo(new VariableInfo("SignificanceLevel", "The significance level for the mann whitney wilcoxon rank sum test", typeof(DoubleData), VariableKind.In));
47      GetVariableInfo("SignificanceLevel").Local = true;
48      AddVariable(new Variable("SignificanceLevel", new DoubleData(0.05)));
49    }
50
51    protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
52      IVariableInfo qualityInfo = GetVariableInfo("QualitySamples");
53      bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
54      int groupSize = GetVariableValue<IntData>("GroupSize", source, true).Data;
55      double alpha = GetVariableValue<DoubleData>("SignificanceLevel", source, true).Data;
56
57      int insignificantCount = 0;
58      int equalRankListSize = 0;
59      for (int i = 0; i < selected; i++) {
60        if (source.SubScopes.Count < 1) throw new InvalidOperationException("No source scopes available to select.");
61
62        double[][] tournamentGroup = new double[groupSize][];
63        int[] tournamentGroupIndices = new int[groupSize];
64        double[] tournamentGroupAverages = new double[groupSize];
65        for (int j = 0; j < groupSize; j++) {
66          tournamentGroupIndices[j] = random.Next(source.SubScopes.Count);
67          tournamentGroup[j] = source.SubScopes[tournamentGroupIndices[j]].GetVariableValue<DoubleArrayData>(qualityInfo.FormalName, false).Data;
68          double sum = 0.0;
69          for (int k = 0; k < tournamentGroup[j].Length; k++) {
70            sum += tournamentGroup[j][k];
71          }
72          tournamentGroupAverages[j] = sum / (double)tournamentGroup[j].Length;
73        }
74
75        int[] rankList = new int[groupSize];
76        int highestRank = 0;
77        IList<int> equalRankList = new List<int>(groupSize);
78        for (int j = 0; j < groupSize - 1; j++) {
79          for (int k = j + 1; k < groupSize; k++) {
80            if (MannWhitneyWilcoxonTest.TwoTailedTest(tournamentGroup[j], tournamentGroup[k], alpha)) { // if a 2-tailed test is successful it means that two solutions are likely different
81              if (maximization && tournamentGroupAverages[j] > tournamentGroupAverages[k]
82                || !maximization && tournamentGroupAverages[j] < tournamentGroupAverages[k]) {
83                rankList[j]++;
84                if (rankList[j] > highestRank) {
85                  highestRank = rankList[j];
86                  equalRankList.Clear();
87                  equalRankList.Add(j);
88                } else if (rankList[j] == highestRank) {
89                  equalRankList.Add(j);
90                }
91              } else if (maximization && tournamentGroupAverages[j] < tournamentGroupAverages[k]
92                || !maximization && tournamentGroupAverages[j] > tournamentGroupAverages[k]) {
93                rankList[k]++;
94                if (rankList[k] > highestRank) {
95                  highestRank = rankList[k];
96                  equalRankList.Clear();
97                  equalRankList.Add(k);
98                } else if (rankList[k] == highestRank) {
99                  equalRankList.Add(k);
100                }
101              }
102              // else there's a statistical significant difference, but equal average qualities... can that happen? in any case, nobody gets a rank increase
103            }
104          }
105        }
106        int selectedScopeIndex = 0;
107        if (equalRankList.Count == 0) {
108          insignificantCount++;
109          selectedScopeIndex = tournamentGroupIndices[random.Next(groupSize)]; // no significance in all the solutions, select one randomly
110        } else {
111          equalRankListSize += equalRankList.Count;
112          selectedScopeIndex = tournamentGroupIndices[equalRankList[random.Next(equalRankList.Count)]]; // select among those with the highest rank randomly
113        }
114        IScope selectedScope = source.SubScopes[selectedScopeIndex];
115
116        if (copySelected)
117          target.AddSubScope((IScope)selectedScope.Clone());
118        else {
119          source.RemoveSubScope(selectedScope);
120          target.AddSubScope(selectedScope);
121        }
122      }
123      Logger.Debug("Solutions selected: " + selected + ". Completely random selections: " + insignificantCount + ". Average size of highest rank pool: " + (double)equalRankListSize / (double)selected);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.