1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 System.Threading;
|
---|
26 | using HeuristicLab.Algorithms.MemPR.Interfaces;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Algorithms.MemPR.Binary {
|
---|
36 | [Item("MemPR (binary)", "MemPR implementation for binary vectors.")]
|
---|
37 | [StorableClass]
|
---|
38 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
|
---|
39 | public class BinaryMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<BinaryVectorEncoding>, BinaryVector, BinaryMemPRPopulationContext, BinaryMemPRSolutionContext> {
|
---|
40 | private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
|
---|
41 |
|
---|
42 | [StorableConstructor]
|
---|
43 | protected BinaryMemPR(bool deserializing) : base(deserializing) { }
|
---|
44 | protected BinaryMemPR(BinaryMemPR original, Cloner cloner) : base(original, cloner) { }
|
---|
45 | public BinaryMemPR() {
|
---|
46 | foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<BinaryMemPRPopulationContext>>())
|
---|
47 | SolutionModelTrainerParameter.ValidValues.Add(trainer);
|
---|
48 |
|
---|
49 | foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<BinaryMemPRSolutionContext>>()) {
|
---|
50 | LocalSearchParameter.ValidValues.Add(localSearch);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
55 | return new BinaryMemPR(this, cloner);
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override bool Eq(ISingleObjectiveSolutionScope<BinaryVector> a, ISingleObjectiveSolutionScope<BinaryVector> b) {
|
---|
59 | var len = a.Solution.Length;
|
---|
60 | var acode = a.Solution;
|
---|
61 | var bcode = b.Solution;
|
---|
62 | for (var i = 0; i < len; i++)
|
---|
63 | if (acode[i] != bcode[i]) return false;
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override double Dist(ISingleObjectiveSolutionScope<BinaryVector> a, ISingleObjectiveSolutionScope<BinaryVector> b) {
|
---|
68 | var len = a.Solution.Length;
|
---|
69 | var acode = a.Solution;
|
---|
70 | var bcode = b.Solution;
|
---|
71 | var hamming = 0;
|
---|
72 | for (var i = 0; i < len; i++)
|
---|
73 | if (acode[i] != bcode[i]) hamming++;
|
---|
74 | return hamming / (double)len;
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override ISingleObjectiveSolutionScope<BinaryVector> ToScope(BinaryVector code, double fitness = double.NaN) {
|
---|
78 | var creator = Problem.SolutionCreator as IBinaryVectorCreator;
|
---|
79 | if (creator == null) throw new InvalidOperationException("Can only solve binary encoded problems with MemPR (binary)");
|
---|
80 | return new SingleObjectiveSolutionScope<BinaryVector>(code, creator.BinaryVectorParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
|
---|
81 | Parent = Context.Scope
|
---|
82 | };
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected override ISolutionSubspace<BinaryVector> CalculateSubspace(IEnumerable<BinaryVector> solutions, bool inverse = false) {
|
---|
86 | var pop = solutions.ToList();
|
---|
87 | var N = pop[0].Length;
|
---|
88 | var subspace = new bool[N];
|
---|
89 | for (var i = 0; i < N; i++) {
|
---|
90 | var val = pop[0][i];
|
---|
91 | if (inverse) subspace[i] = true;
|
---|
92 | for (var p = 1; p < pop.Count; p++) {
|
---|
93 | if (pop[p][i] != val) subspace[i] = !inverse;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return new BinarySolutionSubspace(subspace);
|
---|
97 | }
|
---|
98 |
|
---|
99 | protected override int TabuWalk(ISingleObjectiveSolutionScope<BinaryVector> scope, int maxEvals, CancellationToken token, ISolutionSubspace<BinaryVector> subspace = null) {
|
---|
100 | var evaluations = 0;
|
---|
101 | var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
|
---|
102 | if (double.IsNaN(scope.Fitness)) {
|
---|
103 | Evaluate(scope, token);
|
---|
104 | evaluations++;
|
---|
105 | }
|
---|
106 | SingleObjectiveSolutionScope<BinaryVector> bestOfTheWalk = null;
|
---|
107 | var currentScope = (SingleObjectiveSolutionScope<BinaryVector>)scope.Clone();
|
---|
108 | var current = currentScope.Solution;
|
---|
109 | var N = current.Length;
|
---|
110 | var tabu = new Tuple<double, double>[N];
|
---|
111 | for (var i = 0; i < N; i++) tabu[i] = Tuple.Create(current[i] ? double.NaN : currentScope.Fitness, !current[i] ? double.NaN : currentScope.Fitness);
|
---|
112 | var subN = subset != null ? subset.Count(x => x) : N;
|
---|
113 | if (subN == 0) return 0;
|
---|
114 | var order = Enumerable.Range(0, N).Where(x => subset == null || subset[x]).Shuffle(Context.Random).ToArray();
|
---|
115 |
|
---|
116 | var steps = 0;
|
---|
117 | var stepsUntilBestOfWalk = 0;
|
---|
118 | for (var iter = 0; iter < int.MaxValue; iter++) {
|
---|
119 | var allTabu = true;
|
---|
120 | var bestOfTheRestF = double.NaN;
|
---|
121 | int bestOfTheRest = -1;
|
---|
122 | var improved = false;
|
---|
123 |
|
---|
124 | for (var i = 0; i < subN; i++) {
|
---|
125 | var idx = order[i];
|
---|
126 | var before = currentScope.Fitness;
|
---|
127 | current[idx] = !current[idx];
|
---|
128 | Evaluate(currentScope, token);
|
---|
129 | evaluations++;
|
---|
130 | var after = currentScope.Fitness;
|
---|
131 |
|
---|
132 | if (IsBetter(after, before) && (bestOfTheWalk == null || IsBetter(after, bestOfTheWalk.Fitness))) {
|
---|
133 | bestOfTheWalk = (SingleObjectiveSolutionScope<BinaryVector>)currentScope.Clone();
|
---|
134 | stepsUntilBestOfWalk = steps;
|
---|
135 | }
|
---|
136 |
|
---|
137 | var qualityToBeat = current[idx] ? tabu[idx].Item2 : tabu[idx].Item1;
|
---|
138 | var isTabu = !IsBetter(after, qualityToBeat);
|
---|
139 | if (!isTabu) allTabu = false;
|
---|
140 |
|
---|
141 | if (IsBetter(after, before) && !isTabu) {
|
---|
142 | improved = true;
|
---|
143 | steps++;
|
---|
144 | tabu[idx] = current[idx] ? Tuple.Create(after, tabu[idx].Item2) : Tuple.Create(tabu[idx].Item1, after);
|
---|
145 | } else { // undo the move
|
---|
146 | if (!isTabu && IsBetter(after, bestOfTheRestF)) {
|
---|
147 | bestOfTheRest = idx;
|
---|
148 | bestOfTheRestF = after;
|
---|
149 | }
|
---|
150 | current[idx] = !current[idx];
|
---|
151 | currentScope.Fitness = before;
|
---|
152 | }
|
---|
153 | if (evaluations >= maxEvals) break;
|
---|
154 | }
|
---|
155 | if (!allTabu && !improved) {
|
---|
156 | var better = currentScope.Fitness;
|
---|
157 | current[bestOfTheRest] = !current[bestOfTheRest];
|
---|
158 | tabu[bestOfTheRest] = current[bestOfTheRest] ? Tuple.Create(better, tabu[bestOfTheRest].Item2) : Tuple.Create(tabu[bestOfTheRest].Item1, better);
|
---|
159 | currentScope.Fitness = bestOfTheRestF;
|
---|
160 | steps++;
|
---|
161 | } else if (allTabu) break;
|
---|
162 | if (evaluations >= maxEvals) break;
|
---|
163 | }
|
---|
164 |
|
---|
165 | Context.IncrementEvaluatedSolutions(evaluations);
|
---|
166 | scope.Adopt(bestOfTheWalk ?? currentScope);
|
---|
167 | return stepsUntilBestOfWalk;
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected override ISingleObjectiveSolutionScope<BinaryVector> Cross(ISingleObjectiveSolutionScope<BinaryVector> p1, ISingleObjectiveSolutionScope<BinaryVector> p2, CancellationToken token) {
|
---|
171 | var offspring = (ISingleObjectiveSolutionScope<BinaryVector>)p1.Clone();
|
---|
172 | offspring.Fitness = double.NaN;
|
---|
173 | var code = offspring.Solution;
|
---|
174 | var p2Code = p2.Solution;
|
---|
175 | var bp = 0;
|
---|
176 | var lastbp = 0;
|
---|
177 | for (var i = 0; i < code.Length; i++) {
|
---|
178 | if (bp % 2 == 1) {
|
---|
179 | code[i] = p2Code[i];
|
---|
180 | }
|
---|
181 | if (Context.Random.Next(code.Length) < i - lastbp + 1) {
|
---|
182 | bp = (bp + 1) % 2;
|
---|
183 | lastbp = i;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | return offspring;
|
---|
187 | }
|
---|
188 |
|
---|
189 | protected override void Mutate(ISingleObjectiveSolutionScope<BinaryVector> offspring, CancellationToken token, ISolutionSubspace<BinaryVector> subspace = null) {
|
---|
190 | var subset = subspace != null ? ((BinarySolutionSubspace)subspace).Subspace : null;
|
---|
191 | offspring.Fitness = double.NaN;
|
---|
192 | var code = offspring.Solution;
|
---|
193 | for (var i = 0; i < code.Length; i++) {
|
---|
194 | if (subset != null && subset[i]) continue;
|
---|
195 | if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
|
---|
196 | code[i] = !code[i];
|
---|
197 | if (subset != null) subset[i] = true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | protected override ISingleObjectiveSolutionScope<BinaryVector> Relink(ISingleObjectiveSolutionScope<BinaryVector> a, ISingleObjectiveSolutionScope<BinaryVector> b, CancellationToken token) {
|
---|
203 | if (double.IsNaN(a.Fitness)) {
|
---|
204 | Evaluate(a, token);
|
---|
205 | Context.IncrementEvaluatedSolutions(1);
|
---|
206 | }
|
---|
207 | if (double.IsNaN(b.Fitness)) {
|
---|
208 | Evaluate(b, token);
|
---|
209 | Context.IncrementEvaluatedSolutions(1);
|
---|
210 | }
|
---|
211 | if (Context.Random.NextDouble() < 0.5)
|
---|
212 | return IsBetter(a, b) ? Relink(a, b, token, false) : Relink(b, a, token, true);
|
---|
213 | else return IsBetter(a, b) ? Relink(b, a, token, true) : Relink(a, b, token, false);
|
---|
214 | }
|
---|
215 |
|
---|
216 | protected virtual ISingleObjectiveSolutionScope<BinaryVector> Relink(ISingleObjectiveSolutionScope<BinaryVector> betterScope, ISingleObjectiveSolutionScope<BinaryVector> worseScope, CancellationToken token, bool fromWorseToBetter) {
|
---|
217 | var evaluations = 0;
|
---|
218 | var childScope = (ISingleObjectiveSolutionScope<BinaryVector>)(fromWorseToBetter ? worseScope : betterScope).Clone();
|
---|
219 | var child = childScope.Solution;
|
---|
220 | var better = betterScope.Solution;
|
---|
221 | var worse = worseScope.Solution;
|
---|
222 | ISingleObjectiveSolutionScope<BinaryVector> best = null;
|
---|
223 | var cF = fromWorseToBetter ? worseScope.Fitness : betterScope.Fitness;
|
---|
224 | var bF = double.NaN;
|
---|
225 | var order = Enumerable.Range(0, better.Length).Shuffle(Context.Random).ToArray();
|
---|
226 | while (true) {
|
---|
227 | var bestS = double.NaN;
|
---|
228 | var bestIdx = -1;
|
---|
229 | for (var i = 0; i < child.Length; i++) {
|
---|
230 | var idx = order[i];
|
---|
231 | // either move from worse to better or move from better away from worse
|
---|
232 | if (fromWorseToBetter && child[idx] == better[idx] ||
|
---|
233 | !fromWorseToBetter && child[idx] != worse[idx]) continue;
|
---|
234 | child[idx] = !child[idx]; // move
|
---|
235 | Evaluate(childScope, token);
|
---|
236 | evaluations++;
|
---|
237 | var s = childScope.Fitness;
|
---|
238 | childScope.Fitness = cF;
|
---|
239 | child[idx] = !child[idx]; // undo move
|
---|
240 | if (IsBetter(s, cF)) {
|
---|
241 | bestS = s;
|
---|
242 | bestIdx = idx;
|
---|
243 | break; // first-improvement
|
---|
244 | }
|
---|
245 | if (double.IsNaN(bestS) || IsBetter(s, bestS)) {
|
---|
246 | // least-degrading
|
---|
247 | bestS = s;
|
---|
248 | bestIdx = idx;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | if (bestIdx < 0) break;
|
---|
252 | child[bestIdx] = !child[bestIdx];
|
---|
253 | cF = bestS;
|
---|
254 | childScope.Fitness = cF;
|
---|
255 | if (IsBetter(cF, bF)) {
|
---|
256 | bF = cF;
|
---|
257 | best = (ISingleObjectiveSolutionScope<BinaryVector>)childScope.Clone();
|
---|
258 | }
|
---|
259 | }
|
---|
260 | Context.IncrementEvaluatedSolutions(evaluations);
|
---|
261 | return best ?? childScope;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | }
|
---|