1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Optimization.Operators.LCS {
|
---|
33 | [Item("NichingTournamentSelector", "Description missing")]
|
---|
34 | [StorableClass]
|
---|
35 | public class NichingTournamentSelector : StochasticSingleObjectiveSelector, INichingSingleObjectiveSelector {
|
---|
36 |
|
---|
37 | #region Parameter Properties
|
---|
38 | public ValueLookupParameter<IntValue> GroupSizeParameter {
|
---|
39 | get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
|
---|
40 | }
|
---|
41 | public ILookupParameter<IGAssistProblemData> GAssistNichesProblemDataParameter {
|
---|
42 | get { return (LookupParameter<IGAssistProblemData>)Parameters["GAssistNichesProblemData"]; }
|
---|
43 | }
|
---|
44 | public ILookupParameter<BoolValue> NichingParameter {
|
---|
45 | get { return (LookupParameter<BoolValue>)Parameters["Niching"]; }
|
---|
46 | }
|
---|
47 | public IValueLookupParameter<IntValue> ParentsPerChildParameter {
|
---|
48 | get { return (IValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
|
---|
49 | }
|
---|
50 | public ILookupParameter<ItemArray<IGAssistIndividual>> IndividualParameter {
|
---|
51 | get { return (ILookupParameter<ItemArray<IGAssistIndividual>>)Parameters["Individual"]; }
|
---|
52 | }
|
---|
53 | #endregion
|
---|
54 |
|
---|
55 | [StorableConstructor]
|
---|
56 | protected NichingTournamentSelector(bool deserializing) : base(deserializing) { }
|
---|
57 | protected NichingTournamentSelector(NichingTournamentSelector original, Cloner cloner)
|
---|
58 | : base(original, cloner) {
|
---|
59 | }
|
---|
60 | public NichingTournamentSelector()
|
---|
61 | : base() {
|
---|
62 | Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
|
---|
63 | Parameters.Add(new LookupParameter<IGAssistProblemData>("GAssistNichesProblemData", ""));
|
---|
64 | Parameters.Add(new LookupParameter<BoolValue>("Niching", ""));
|
---|
65 | Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", ""));
|
---|
66 | Parameters.Add(new ScopeTreeLookupParameter<IGAssistIndividual>("Individual", ""));
|
---|
67 | }
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new NichingTournamentSelector(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
73 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
74 | bool copy = CopySelectedParameter.Value.Value;
|
---|
75 | IRandom random = RandomParameter.ActualValue;
|
---|
76 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
77 | List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
|
---|
78 | List<IGAssistIndividual> individuals = IndividualParameter.ActualValue.ToList();
|
---|
79 | int groupSize = GroupSizeParameter.ActualValue.Value;
|
---|
80 | IScope[] selected = new IScope[count];
|
---|
81 | bool doNiching = NichingParameter.ActualValue.Value;
|
---|
82 |
|
---|
83 | //check if list with indexes is as long as the original scope list
|
---|
84 | //otherwise invalid quality values were filtered
|
---|
85 | if (qualities.Count != scopes.Count || individuals.Count != scopes.Count) {
|
---|
86 | throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
|
---|
87 | }
|
---|
88 |
|
---|
89 | int parentsPerChild = ParentsPerChildParameter.ActualValue.Value;
|
---|
90 | var nicheComparer = GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().First().Comparer;
|
---|
91 | var selectPerNiche = new Dictionary<IGAssistNiche, int>(nicheComparer);
|
---|
92 | var nicheScope = new Dictionary<IGAssistNiche, List<int>>(nicheComparer);
|
---|
93 |
|
---|
94 | for (int i = 0; i < individuals.Count; i++) {
|
---|
95 | if (!nicheScope.ContainsKey(individuals[i].Niche)) {
|
---|
96 | nicheScope.Add(individuals[i].Niche, new List<int>());
|
---|
97 | }
|
---|
98 | nicheScope[individuals[i].Niche].Add(i);
|
---|
99 | }
|
---|
100 |
|
---|
101 | var possibleNiches = nicheScope.Keys.ToList();
|
---|
102 | foreach (var niche in possibleNiches) {
|
---|
103 | selectPerNiche.Add(niche, count / possibleNiches.Count);
|
---|
104 | }
|
---|
105 |
|
---|
106 | int curCount = 0;
|
---|
107 | while (curCount < count) {
|
---|
108 | IGAssistNiche niche = null;
|
---|
109 | int best = -1;
|
---|
110 | if (doNiching) {
|
---|
111 | niche = GetNiche(random, selectPerNiche, possibleNiches);
|
---|
112 | } else {
|
---|
113 | best = random.Next(scopes.Count);
|
---|
114 | }
|
---|
115 | for (int i = 0; i < parentsPerChild; i++) {
|
---|
116 | int index;
|
---|
117 | if (doNiching) {
|
---|
118 | best = nicheScope[niche][random.Next(nicheScope[niche].Count)];
|
---|
119 | }
|
---|
120 | for (int j = 1; j < groupSize; j++) {
|
---|
121 | if (niche != null) {
|
---|
122 | index = nicheScope[niche][random.Next(nicheScope[niche].Count)];
|
---|
123 | } else {
|
---|
124 | index = random.Next(scopes.Count);
|
---|
125 | }
|
---|
126 | if (((maximization) && (qualities[index] > qualities[best])) ||
|
---|
127 | ((!maximization) && (qualities[index] < qualities[best]))) {
|
---|
128 | best = index;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | niche = individuals[best].Niche;
|
---|
133 |
|
---|
134 | if (copy)
|
---|
135 | selected[curCount] = (IScope)scopes[best].Clone();
|
---|
136 | else {
|
---|
137 | selected[curCount] = scopes[best];
|
---|
138 | scopes.RemoveAt(best);
|
---|
139 | qualities.RemoveAt(best);
|
---|
140 | }
|
---|
141 | selectPerNiche[niche]--;
|
---|
142 | curCount++;
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return selected;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private IGAssistNiche GetNiche(IRandom random, Dictionary<IGAssistNiche, int> selectPerNiche, List<IGAssistNiche> possibleNiches) {
|
---|
149 | int sum = selectPerNiche.Values.Sum();
|
---|
150 | if (sum <= 0) { return possibleNiches[random.Next(possibleNiches.Count)]; }
|
---|
151 | int pos = random.Next(sum);
|
---|
152 | int total = 0;
|
---|
153 | IGAssistNiche niche = selectPerNiche.Keys.First();
|
---|
154 | foreach (var item in selectPerNiche) {
|
---|
155 | total += item.Value;
|
---|
156 | niche = item.Key;
|
---|
157 | if (pos < total) {
|
---|
158 | return niche;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | throw new ArgumentException("error in code");
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|