Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.NSGA2/3.3/CrowdedTournamentSelector.cs @ 11293

Last change on this file since 11293 was 4902, checked in by vdorfer, 14 years ago

#1040:

  • adapted NSGAII to new cloning implementation
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Selection;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Common;
31
32namespace HeuristicLab.Algorithms.NSGA2 {
33  [Item("CrowdedTournamentSelector", "Selects solutions using tournament selection by using the partial order defined in Deb et al. 2002. A Fast and Elitist Multiobjective Genetic Algorithm: NSGA-II. IEEE Transactions on Evolutionary Computation, 6(2), pp. 182-197.")]
34  [StorableClass]
35  public class CrowdedTournamentSelector : Selector, IMultiObjectiveSelector, IStochasticOperator {
36    public ILookupParameter<BoolArray> MaximizationParameter {
37      get { return (ILookupParameter<BoolArray>)Parameters["Maximization"]; }
38    }
39    public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
40      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
41    }
42    public IValueParameter<BoolValue> CopySelectedParameter {
43      get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
44    }
45    public ILookupParameter<IRandom> RandomParameter {
46      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
47    }
48    public ILookupParameter<ItemArray<DoubleArray>> QualitiesParameter {
49      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters["Qualities"]; }
50    }
51    public IScopeTreeLookupParameter<IntValue> RankParameter {
52      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
53    }
54    public IScopeTreeLookupParameter<DoubleValue> CrowdingDistanceParameter {
55      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["CrowdingDistance"]; }
56    }
57    public IValueLookupParameter<IntValue> GroupSizeParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
59    }
60
61    public BoolValue CopySelected {
62      get { return CopySelectedParameter.Value; }
63      set { CopySelectedParameter.Value = value; }
64    }
65
66    [StorableConstructor]
67    protected CrowdedTournamentSelector(bool deserializing) : base(deserializing) { }
68    protected CrowdedTournamentSelector(CrowdedTournamentSelector original, Cloner cloner) : base(original, cloner) { }
69    public CrowdedTournamentSelector()
70      : base() {
71      Parameters.Add(new LookupParameter<BoolArray>("Maximization", "For each objective determines whether it should be maximized or minimized."));
72      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of sub-scopes that should be selected."));
73      Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "True if the selected scopes are to be copied (cloned) otherwise they're moved."));
74      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The solutions' qualities vector."));
76      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The solutions' domination rank."));
77      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "The solutions' crowding distance values."));
78      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the group from which the best will be chosen.", new IntValue(2)));
79    }
80
81    protected override IScope[] Select(List<IScope> scopes) {
82      IRandom random = RandomParameter.ActualValue;
83      List<int> ranks = RankParameter.ActualValue.Select(x => x.Value).ToList();
84      List<double> crowdingDistance = CrowdingDistanceParameter.ActualValue.Select(x => x.Value).ToList();
85      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
86      int groupSize = GroupSizeParameter.ActualValue.Value;
87      bool copy = CopySelected.Value;
88      IScope[] selected = new IScope[count];
89
90      for (int i = 0; i < count; i++) {
91        int best = random.Next(scopes.Count);
92        int index;
93        for (int j = 1; j < groupSize; j++) {
94          index = random.Next(scopes.Count);
95          if (ranks[best] > ranks[index]
96            || ranks[best] == ranks[index]
97              && crowdingDistance[best] < crowdingDistance[index]) {
98            best = index;
99          }
100        }
101
102        if (copy)
103          selected[i] = (IScope)scopes[best].Clone();
104        else {
105          selected[i] = scopes[best];
106          scopes.RemoveAt(best);
107          ranks.RemoveAt(best);
108          crowdingDistance.RemoveAt(best);
109        }
110      }
111
112      return selected;
113    }
114
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new CrowdedTournamentSelector(this, cloner);
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.