Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/CrowdedTournamentSelector.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 4.9 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;
29
30namespace HeuristicLab.Algorithms.NSGA2 {
31  public class CrowdedTournamentSelector : Selector, IMultiObjectiveSelector, IStochasticOperator {
32    public ILookupParameter<BoolArray> MaximizationParameter {
33      get { return (ILookupParameter<BoolArray>)Parameters["Maximization"]; }
34    }
35    public IValueLookupParameter<IntValue> NumberOfSelectedSubScopesParameter {
36      get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfSelectedSubScopes"]; }
37    }
38    public IValueParameter<BoolValue> CopySelectedParameter {
39      get { return (IValueParameter<BoolValue>)Parameters["CopySelected"]; }
40    }
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44    public ILookupParameter<ItemArray<DoubleArray>> QualitiesParameter {
45      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters["Qualities"]; }
46    }
47    public IScopeTreeLookupParameter<IntValue> RankParameter {
48      get { return (IScopeTreeLookupParameter<IntValue>)Parameters["Rank"]; }
49    }
50    public IScopeTreeLookupParameter<DoubleValue> CrowdingDistanceParameter {
51      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["CrowdingDistance"]; }
52    }
53    public IValueLookupParameter<IntValue> GroupSizeParameter {
54      get { return (IValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
55    }
56
57    public BoolValue CopySelected {
58      get { return CopySelectedParameter.Value; }
59      set { CopySelectedParameter.Value = value; }
60    }
61
62    public CrowdedTournamentSelector()
63      : base() {
64      Parameters.Add(new LookupParameter<BoolArray>("Maximization", "For each objective determines whether it should be maximized or minimized."));
65      Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfSelectedSubScopes", "The number of sub-scopes that should be selected."));
66      Parameters.Add(new ValueParameter<BoolValue>("CopySelected", "True if the selected scopes are to be copied (cloned) otherwise they're moved."));
67      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
68      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("Qualities", "The solutions' qualities vector."));
69      Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Rank", "The solutions' domination rank."));
70      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("CrowdingDistance", "The solutions' crowding distance values."));
71      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the group from which the best will be chosen.", new IntValue(2)));
72    }
73
74    protected override IScope[] Select(List<IScope> scopes) {
75      IRandom random = RandomParameter.ActualValue;
76      List<int> ranks = RankParameter.ActualValue.Select(x => x.Value).ToList();
77      List<double> crowdingDistance = CrowdingDistanceParameter.ActualValue.Select(x => x.Value).ToList();
78      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
79      int groupSize = GroupSizeParameter.ActualValue.Value;
80      bool copy = CopySelected.Value;
81      IScope[] selected = new IScope[count];
82
83      for (int i = 0; i < count; i++) {
84        int best = random.Next(scopes.Count);
85        int index;
86        for (int j = 1; j < groupSize; j++) {
87          index = random.Next(scopes.Count);
88          if (ranks[best] > ranks[index]
89            || ranks[best] == ranks[index]
90              && crowdingDistance[best] < crowdingDistance[index]) {
91            best = index;
92          }
93        }
94
95        if (copy)
96          selected[i] = (IScope)scopes[best].Clone();
97        else {
98          selected[i] = scopes[best];
99          scopes.RemoveAt(best);
100          ranks.RemoveAt(best);
101          crowdingDistance.RemoveAt(best);
102        }
103      }
104
105      return selected;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.