Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection.Uncertainty/v3.2/UncertainTournamentSelector.cs @ 1740

Last change on this file since 1740 was 1740, checked in by abeham, 15 years ago

Initial import of selection operators under uncertain conditions (#611)

File size: 3.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;
30
31namespace HeuristicLab.Selection.Uncertainty {
32  public class UncertainTournamentSelector : StochasticSelectorBase {
33    public override string Description {
34      get { return @"Selects an individual from a tournament group, based on tests of statistical significance of quality arrays."; }
35    }
36
37    public UncertainTournamentSelector()
38      : base() {
39      AddVariableInfo(new VariableInfo("QualitySamples", "The array of quality samples resulting from several evaluations", typeof(DoubleArrayData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("Maximization", "Maximization problem", typeof(BoolData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("GroupSize", "Size of the tournament group", typeof(IntData), VariableKind.In));
42      GetVariableInfo("GroupSize").Local = true;
43      AddVariable(new Variable("GroupSize", new IntData(2)));
44      GetVariable("CopySelected").GetValue<BoolData>().Data = true;
45    }
46
47    protected override void Select(IRandom random, IScope source, int selected, IScope target, bool copySelected) {
48      IVariableInfo qualityInfo = GetVariableInfo("QualitySamples");
49      bool maximization = GetVariableValue<BoolData>("Maximization", source, true).Data;
50      int groupSize = GetVariableValue<IntData>("GroupSize", source, true).Data;
51      for (int i = 0; i < selected; i++) {
52        if (source.SubScopes.Count < 1) throw new InvalidOperationException("No source scopes available to select.");
53
54        IScope selectedScope = source.SubScopes[random.Next(source.SubScopes.Count)];
55        double best = selectedScope.GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
56        for (int j = 1; j < groupSize; j++) {
57          IScope scope = source.SubScopes[random.Next(source.SubScopes.Count)];
58          double quality = scope.GetVariableValue<DoubleData>(qualityInfo.FormalName, false).Data;
59          if (((maximization) && (quality > best)) ||
60              ((!maximization) && (quality < best))) {
61            best = quality;
62            selectedScope = scope;
63          }
64        }
65
66        if (copySelected)
67          target.AddSubScope((IScope)selectedScope.Clone());
68        else {
69          source.RemoveSubScope(selectedScope);
70          target.AddSubScope(selectedScope);
71        }
72      }
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.