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