Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Selection.Uncertainty/3.2/UncertainBestSelector.cs @ 6703

Last change on this file since 6703 was 1778, checked in by abeham, 16 years ago

Renamed version folder of Selection.Uncertainty plugin

File size: 4.8 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.StatisticalAnalysis;
29
30namespace HeuristicLab.Selection.Uncertainty {
31  public class UncertainBestSelector : StochasticSelectorBase {
32    public override string Description {
33      get { return @"Selects an individual from a tournament group, based on tests of statistical significance of quality arrays."; }
34    }
35
36    public UncertainBestSelector()
37      : base() {
38      AddVariableInfo(new VariableInfo("QualitySamples", "The array of quality samples resulting from several evaluations", typeof(DoubleArrayData), VariableKind.In));
39      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
40      GetVariable("CopySelected").GetValue<BoolData>().Data = true;
41      AddVariableInfo(new VariableInfo("SignificanceLevel", "The significance level for the mann whitney wilcoxon rank sum test", typeof(DoubleData), VariableKind.In));
42      GetVariableInfo("SignificanceLevel").Local = true;
43      AddVariable(new Variable("SignificanceLevel", new DoubleData(0.05)));
44    }
45
46    protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
47      if (source.SubScopes.Count < 1) throw new InvalidOperationException("No source scopes available to select.");
48      IVariableInfo qualityInfo = GetVariableInfo("QualitySamples");
49      bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
50      double alpha = GetVariableValue<DoubleData>("SignificanceLevel", source, true).Data;
51
52      int poolSize = source.SubScopes.Count;
53      double[][] selectionGroupSamples = new double[poolSize][];
54      double[] selectionGroupAverages = new double[poolSize];
55      int[] selectionGroupIndices = new int[poolSize];
56      for (int j = 0; j < poolSize; j++) {
57        selectionGroupIndices[j] = j;
58        selectionGroupSamples[j] = source.SubScopes[j].GetVariableValue<DoubleArrayData>(qualityInfo.FormalName, false).Data;
59        double sum = 0.0;
60        for (int k = 0; k < selectionGroupSamples[j].Length; k++) {
61          sum += selectionGroupSamples[j][k];
62        }
63        selectionGroupAverages[j] = sum / (double)selectionGroupSamples[j].Length;
64      }
65
66      int[] rankList = new int[poolSize];
67      for (int j = 0; j < poolSize - 1; j++) {
68        for (int k = j + 1; k < poolSize; k++) {
69          if (MannWhitneyWilcoxonTest.TwoTailedTest(selectionGroupSamples[j], selectionGroupSamples[k], alpha)) { // if a 2-tailed test is successful it means that two solutions are likely different
70            if (maximization && selectionGroupAverages[j] > selectionGroupAverages[k]
71              || !maximization && selectionGroupAverages[j] < selectionGroupAverages[k]) {
72              rankList[j]++;
73            } else if (maximization && selectionGroupAverages[j] < selectionGroupAverages[k]
74              || !maximization && selectionGroupAverages[j] > selectionGroupAverages[k]) {
75              rankList[k]++;
76            }
77            // else there's a statistical significant difference, but equal average qualities... can that happen? in any case, nobody gets a rank increase
78          }
79        }
80      }
81
82      Array.Sort<int, int>(rankList, selectionGroupIndices);
83      Array.Sort<int>(rankList);
84
85      List<IScope> selectedScopes = new List<IScope>();
86      for (int i = 0; i < selected; i++) {       
87        int selectedScopeIndex = selectionGroupIndices[poolSize - i - 1];
88        IScope selectedScope = source.SubScopes[selectedScopeIndex];
89        target.AddSubScope((IScope)selectedScope.Clone());
90        selectedScopes.Add(selectedScope);
91      }
92      if (!copySelected) {
93        while (selectedScopes.Count > 0) {
94          source.RemoveSubScope(selectedScopes[0]);
95          selectedScopes.RemoveAt(0);
96        }
97      }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.