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.Algorithms.MemPR.Util;
|
---|
28 | using HeuristicLab.Collections;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.MemPR.LinearLinkage {
|
---|
38 | [Item("MemPR (linear linkage)", "MemPR implementation for linear linkage vectors.")]
|
---|
39 | [StorableClass]
|
---|
40 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
|
---|
41 | public class LinearLinkageMemPR : MemPRAlgorithm<SingleObjectiveBasicProblem<LinearLinkageEncoding>, Encodings.LinearLinkageEncoding.LinearLinkage, LinearLinkageMemPRPopulationContext, LinearLinkageMemPRSolutionContext> {
|
---|
42 | private const double UncommonBitSubsetMutationProbabilityMagicConst = 0.05;
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected LinearLinkageMemPR(bool deserializing) : base(deserializing) { }
|
---|
46 | protected LinearLinkageMemPR(LinearLinkageMemPR original, Cloner cloner) : base(original, cloner) { }
|
---|
47 | public LinearLinkageMemPR() {
|
---|
48 | foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<LinearLinkageMemPRPopulationContext>>())
|
---|
49 | SolutionModelTrainerParameter.ValidValues.Add(trainer);
|
---|
50 |
|
---|
51 | foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<LinearLinkageMemPRSolutionContext>>()) {
|
---|
52 | LocalSearchParameter.ValidValues.Add(localSearch);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
57 | return new LinearLinkageMemPR(this, cloner);
|
---|
58 | }
|
---|
59 |
|
---|
60 | protected override bool Eq(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
61 | var s1 = a.Solution;
|
---|
62 | var s2 = b.Solution;
|
---|
63 | if (s1.Length != s2.Length) return false;
|
---|
64 | for (var i = 0; i < s1.Length; i++)
|
---|
65 | if (s1[i] != s2[i]) return false;
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override double Dist(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b) {
|
---|
70 | return HammingSimilarityCalculator.CalculateSimilarity(a.Solution, b.Solution);
|
---|
71 | }
|
---|
72 |
|
---|
73 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> ToScope(Encodings.LinearLinkageEncoding.LinearLinkage code, double fitness = double.NaN) {
|
---|
74 | var creator = Problem.SolutionCreator as ILinearLinkageCreator;
|
---|
75 | if (creator == null) throw new InvalidOperationException("Can only solve linear linkage encoded problems with MemPR (linear linkage)");
|
---|
76 | return new SingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>(code, creator.LLEParameter.ActualName, fitness, Problem.Evaluator.QualityParameter.ActualName) {
|
---|
77 | Parent = Context.Scope
|
---|
78 | };
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> CalculateSubspace(IEnumerable<Encodings.LinearLinkageEncoding.LinearLinkage> solutions, bool inverse = false) {
|
---|
82 | var pop = solutions.ToList();
|
---|
83 | var N = pop[0].Length;
|
---|
84 | var subspace = new bool[N];
|
---|
85 | for (var i = 0; i < N; i++) {
|
---|
86 | var val = pop[0][i];
|
---|
87 | if (inverse) subspace[i] = true;
|
---|
88 | for (var p = 1; p < pop.Count; p++) {
|
---|
89 | if (pop[p][i] != val) subspace[i] = !inverse;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | return new LinearLinkageSolutionSubspace(subspace);
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected override int TabuWalk(
|
---|
96 | ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> scope,
|
---|
97 | int maxEvals, CancellationToken token,
|
---|
98 | ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> sub_space = null) {
|
---|
99 | var maximization = Context.Problem.Maximization;
|
---|
100 | var subspace = sub_space is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)sub_space).Subspace : null;
|
---|
101 | var evaluations = 0;
|
---|
102 | var quality = scope.Fitness;
|
---|
103 | if (double.IsNaN(quality)) {
|
---|
104 | Evaluate(scope, token);
|
---|
105 | quality = scope.Fitness;
|
---|
106 | evaluations++;
|
---|
107 | if (evaluations >= maxEvals) return evaluations;
|
---|
108 | }
|
---|
109 | var bestQuality = quality;
|
---|
110 | var currentScope = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)scope.Clone();
|
---|
111 | var current = currentScope.Solution;
|
---|
112 | Encodings.LinearLinkageEncoding.LinearLinkage bestOfTheWalk = null;
|
---|
113 | var bestOfTheWalkF = double.NaN;
|
---|
114 |
|
---|
115 | var tabu = new double[current.Length, current.Length];
|
---|
116 | for (var i = 0; i < current.Length; i++) {
|
---|
117 | for (var j = i; j < current.Length; j++) {
|
---|
118 | tabu[i, j] = tabu[j, i] = maximization ? double.MinValue : double.MaxValue;
|
---|
119 | }
|
---|
120 | tabu[i, current[i]] = quality;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // this dictionary holds the last relevant links
|
---|
124 | var links = new BidirectionalDictionary<int, int>();
|
---|
125 | Move bestOfTheRest = null;
|
---|
126 | var bestOfTheRestF = double.NaN;
|
---|
127 | var lastAppliedMove = -1;
|
---|
128 | for (var iter = 0; iter < int.MaxValue; iter++) {
|
---|
129 | // clear the dictionary before a new pass through the array is made
|
---|
130 | links.Clear();
|
---|
131 | for (var i = 0; i < current.Length; i++) {
|
---|
132 | if (subspace != null && !subspace[i]) {
|
---|
133 | links.RemoveBySecond(i);
|
---|
134 | links.Add(i, current[i]);
|
---|
135 | continue;
|
---|
136 | }
|
---|
137 |
|
---|
138 | var next = current[i];
|
---|
139 |
|
---|
140 | if (lastAppliedMove == i) {
|
---|
141 | if (bestOfTheRest != null) {
|
---|
142 | bestOfTheRest.Apply(current);
|
---|
143 | currentScope.Fitness = bestOfTheRestF;
|
---|
144 | quality = bestOfTheRestF;
|
---|
145 | if (maximization) {
|
---|
146 | tabu[i, next] = Math.Max(tabu[i, next], bestOfTheRestF);
|
---|
147 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], bestOfTheRestF);
|
---|
148 | } else {
|
---|
149 | tabu[i, next] = Math.Min(tabu[i, next], bestOfTheRestF);
|
---|
150 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], bestOfTheRestF);
|
---|
151 | }
|
---|
152 | if (FitnessComparer.IsBetter(maximization, bestOfTheRestF, bestOfTheWalkF)) {
|
---|
153 | bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
|
---|
154 | bestOfTheWalkF = bestOfTheRestF;
|
---|
155 | }
|
---|
156 | bestOfTheRest = null;
|
---|
157 | bestOfTheRestF = double.NaN;
|
---|
158 | lastAppliedMove = i;
|
---|
159 | } else {
|
---|
160 | lastAppliedMove = -1;
|
---|
161 | }
|
---|
162 | break;
|
---|
163 | } else {
|
---|
164 | foreach (var move in MoveGenerator.GenerateForItem(i, next, links)) {
|
---|
165 | // we intend to break link i -> next
|
---|
166 | var qualityToBreak = tabu[i, next];
|
---|
167 | move.Apply(current);
|
---|
168 | var qualityToRestore = tabu[i, current[i]]; // current[i] is new next
|
---|
169 | Evaluate(currentScope, token);
|
---|
170 | evaluations++;
|
---|
171 | var moveF = currentScope.Fitness;
|
---|
172 | var isNotTabu = FitnessComparer.IsBetter(maximization, moveF, qualityToBreak)
|
---|
173 | && FitnessComparer.IsBetter(maximization, moveF, qualityToRestore);
|
---|
174 | var isImprovement = FitnessComparer.IsBetter(maximization, moveF, quality);
|
---|
175 | var isAspired = FitnessComparer.IsBetter(maximization, moveF, bestQuality);
|
---|
176 | if ((isNotTabu && isImprovement) || isAspired) {
|
---|
177 | if (maximization) {
|
---|
178 | tabu[i, next] = Math.Max(tabu[i, next], moveF);
|
---|
179 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], moveF);
|
---|
180 | } else {
|
---|
181 | tabu[i, next] = Math.Min(tabu[i, next], moveF);
|
---|
182 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], moveF);
|
---|
183 | }
|
---|
184 | quality = moveF;
|
---|
185 | if (isAspired) bestQuality = quality;
|
---|
186 |
|
---|
187 | move.UpdateLinks(links);
|
---|
188 |
|
---|
189 | if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheWalkF)) {
|
---|
190 | bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
|
---|
191 | bestOfTheWalkF = moveF;
|
---|
192 | }
|
---|
193 |
|
---|
194 | bestOfTheRest = null;
|
---|
195 | bestOfTheRestF = double.NaN;
|
---|
196 | lastAppliedMove = i;
|
---|
197 | break;
|
---|
198 | } else {
|
---|
199 | if (isNotTabu) {
|
---|
200 | if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheRestF)) {
|
---|
201 | bestOfTheRest = move;
|
---|
202 | bestOfTheRestF = moveF;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | move.Undo(current);
|
---|
206 | currentScope.Fitness = quality;
|
---|
207 | }
|
---|
208 | if (evaluations >= maxEvals) break;
|
---|
209 | }
|
---|
210 | }
|
---|
211 | links.RemoveBySecond(i);
|
---|
212 | links.Add(i, current[i]);
|
---|
213 | if (evaluations >= maxEvals) break;
|
---|
214 | if (token.IsCancellationRequested) break;
|
---|
215 | }
|
---|
216 | if (lastAppliedMove == -1) { // no move has been applied
|
---|
217 | if (bestOfTheRest != null) {
|
---|
218 | var i = bestOfTheRest.Item;
|
---|
219 | var next = current[i];
|
---|
220 | bestOfTheRest.Apply(current);
|
---|
221 | currentScope.Fitness = bestOfTheRestF;
|
---|
222 | quality = bestOfTheRestF;
|
---|
223 | if (maximization) {
|
---|
224 | tabu[i, next] = Math.Max(tabu[i, next], bestOfTheRestF);
|
---|
225 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], bestOfTheRestF);
|
---|
226 | } else {
|
---|
227 | tabu[i, next] = Math.Min(tabu[i, next], bestOfTheRestF);
|
---|
228 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], bestOfTheRestF);
|
---|
229 | }
|
---|
230 | if (FitnessComparer.IsBetter(maximization, bestOfTheRestF, bestOfTheWalkF)) {
|
---|
231 | bestOfTheWalk = (Encodings.LinearLinkageEncoding.LinearLinkage)current.Clone();
|
---|
232 | bestOfTheWalkF = bestOfTheRestF;
|
---|
233 | }
|
---|
234 |
|
---|
235 | bestOfTheRest = null;
|
---|
236 | bestOfTheRestF = double.NaN;
|
---|
237 | } else break;
|
---|
238 | }
|
---|
239 | if (evaluations >= maxEvals) break;
|
---|
240 | if (token.IsCancellationRequested) break;
|
---|
241 | }
|
---|
242 | if (bestOfTheWalk != null) {
|
---|
243 | scope.Solution = bestOfTheWalk;
|
---|
244 | scope.Fitness = bestOfTheWalkF;
|
---|
245 | }
|
---|
246 | return evaluations;
|
---|
247 | }
|
---|
248 |
|
---|
249 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Cross(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p1Scope, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> p2Scope, CancellationToken token) {
|
---|
250 | var p1 = p1Scope.Solution;
|
---|
251 | var p2 = p2Scope.Solution;
|
---|
252 | var transfered = new bool[p1.Length];
|
---|
253 | var subspace = new bool[p1.Length];
|
---|
254 | var lleeChild = new int[p1.Length];
|
---|
255 | var lleep1 = p1.ToLLEe();
|
---|
256 | var lleep2 = p2.ToLLEe();
|
---|
257 | for (var i = p1.Length - 1; i >= 0; i--) {
|
---|
258 | // Step 1
|
---|
259 | subspace[i] = p1[i] != p2[i];
|
---|
260 | var p1IsEnd = p1[i] == i;
|
---|
261 | var p2IsEnd = p2[i] == i;
|
---|
262 | if (p1IsEnd & p2IsEnd) {
|
---|
263 | transfered[i] = true;
|
---|
264 | } else if (p1IsEnd | p2IsEnd) {
|
---|
265 | transfered[i] = Context.Random.NextDouble() < 0.5;
|
---|
266 | }
|
---|
267 | lleeChild[i] = i;
|
---|
268 |
|
---|
269 | // Step 2
|
---|
270 | if (transfered[i]) continue;
|
---|
271 | var end1 = lleep1[i];
|
---|
272 | var end2 = lleep2[i];
|
---|
273 | var containsEnd1 = transfered[end1];
|
---|
274 | var containsEnd2 = transfered[end2];
|
---|
275 | if (containsEnd1 & containsEnd2) {
|
---|
276 | if (Context.Random.NextDouble() < 0.5) {
|
---|
277 | lleeChild[i] = end1;
|
---|
278 | } else {
|
---|
279 | lleeChild[i] = end2;
|
---|
280 | }
|
---|
281 | } else if (containsEnd1) {
|
---|
282 | lleeChild[i] = end1;
|
---|
283 | } else if (containsEnd2) {
|
---|
284 | lleeChild[i] = end2;
|
---|
285 | } else {
|
---|
286 | if (Context.Random.NextDouble() < 0.5) {
|
---|
287 | lleeChild[i] = lleeChild[p1[i]];
|
---|
288 | } else {
|
---|
289 | lleeChild[i] = lleeChild[p2[i]];
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | var child = new Encodings.LinearLinkageEncoding.LinearLinkage(lleeChild.Length);
|
---|
294 | child.FromLLEe(lleeChild);
|
---|
295 |
|
---|
296 | return ToScope(child);
|
---|
297 | }
|
---|
298 |
|
---|
299 | protected override void Mutate(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> offspring, CancellationToken token, ISolutionSubspace<Encodings.LinearLinkageEncoding.LinearLinkage> subspace = null) {
|
---|
300 | var lle = offspring.Solution;
|
---|
301 | var subset = subspace is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)subspace).Subspace : null;
|
---|
302 | for (var i = 0; i < lle.Length - 1; i++) {
|
---|
303 | if (subset == null || subset[i]) continue; // mutation works against crossover so aims to mutate noTouch points
|
---|
304 | if (Context.Random.NextDouble() < UncommonBitSubsetMutationProbabilityMagicConst) {
|
---|
305 | subset[i] = true;
|
---|
306 | var index = Context.Random.Next(i, lle.Length);
|
---|
307 | for (var j = index - 1; j >= i; j--) {
|
---|
308 | if (lle[j] == index) index = j;
|
---|
309 | }
|
---|
310 | lle[i] = index;
|
---|
311 | index = i;
|
---|
312 | var idx2 = i;
|
---|
313 | for (var j = i - 1; j >= 0; j--) {
|
---|
314 | if (lle[j] == lle[index]) {
|
---|
315 | lle[j] = idx2;
|
---|
316 | index = idx2 = j;
|
---|
317 | } else if (lle[j] == idx2) idx2 = j;
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | protected override ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> Relink(ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> a, ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage> b, CancellationToken token) {
|
---|
324 | var maximization = Context.Problem.Maximization;
|
---|
325 | if (double.IsNaN(a.Fitness)) {
|
---|
326 | Evaluate(a, token);
|
---|
327 | Context.IncrementEvaluatedSolutions(1);
|
---|
328 | }
|
---|
329 | if (double.IsNaN(b.Fitness)) {
|
---|
330 | Evaluate(b, token);
|
---|
331 | Context.IncrementEvaluatedSolutions(1);
|
---|
332 | }
|
---|
333 | var child = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)a.Clone();
|
---|
334 | var cgroups = child.Solution.GetGroups().Select(x => new HashSet<int>(x)).ToList();
|
---|
335 | var g2 = b.Solution.GetGroups().ToList();
|
---|
336 | var order = Enumerable.Range(0, g2.Count).Shuffle(Context.Random).ToList();
|
---|
337 | ISingleObjectiveSolutionScope <Encodings.LinearLinkageEncoding.LinearLinkage> bestChild = null;
|
---|
338 | for (var j = 0; j < g2.Count; j++) {
|
---|
339 | var g = g2[order[j]];
|
---|
340 | var changed = false;
|
---|
341 | for (var k = j; k < cgroups.Count; k++) {
|
---|
342 | foreach (var f in g) if (cgroups[k].Remove(f)) changed = true;
|
---|
343 | if (cgroups[k].Count == 0) {
|
---|
344 | cgroups.RemoveAt(k);
|
---|
345 | k--;
|
---|
346 | }
|
---|
347 | }
|
---|
348 | cgroups.Insert(0, new HashSet<int>(g));
|
---|
349 | child.Solution.SetGroups(cgroups);
|
---|
350 | if (changed) {
|
---|
351 | Evaluate(child, token);
|
---|
352 | if (bestChild == null || FitnessComparer.IsBetter(maximization, child.Fitness, bestChild.Fitness)) {
|
---|
353 | bestChild = (ISingleObjectiveSolutionScope<Encodings.LinearLinkageEncoding.LinearLinkage>)child.Clone();
|
---|
354 | }
|
---|
355 | }
|
---|
356 | };
|
---|
357 | return bestChild;
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|