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.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Encodings.LinearLinkageEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 | using HeuristicLab.Random;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.MemPR.Grouping {
|
---|
37 | [Item("MemPR (linear linkage)", "MemPR implementation for linear linkage vectors.")]
|
---|
38 | [StorableClass]
|
---|
39 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 999)]
|
---|
40 | public class LinearLinkageMemPR : MemPRAlgorithm<ISingleObjectiveHeuristicOptimizationProblem, LinearLinkage, LinearLinkageMemPRPopulationContext, LinearLinkageMemPRSolutionContext> {
|
---|
41 | [StorableConstructor]
|
---|
42 | protected LinearLinkageMemPR(bool deserializing) : base(deserializing) { }
|
---|
43 | protected LinearLinkageMemPR(LinearLinkageMemPR original, Cloner cloner) : base(original, cloner) { }
|
---|
44 | public LinearLinkageMemPR() {
|
---|
45 | foreach (var trainer in ApplicationManager.Manager.GetInstances<ISolutionModelTrainer<LinearLinkageMemPRPopulationContext>>())
|
---|
46 | SolutionModelTrainerParameter.ValidValues.Add(trainer);
|
---|
47 |
|
---|
48 | if (SolutionModelTrainerParameter.ValidValues.Count > 0) {
|
---|
49 | var unbiased = SolutionModelTrainerParameter.ValidValues.FirstOrDefault(x => !x.Bias);
|
---|
50 | if (unbiased != null) SolutionModelTrainerParameter.Value = unbiased;
|
---|
51 | }
|
---|
52 |
|
---|
53 | foreach (var localSearch in ApplicationManager.Manager.GetInstances<ILocalSearch<LinearLinkageMemPRSolutionContext>>()) {
|
---|
54 | LocalSearchParameter.ValidValues.Add(localSearch);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new LinearLinkageMemPR(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected override bool Eq(LinearLinkage a, LinearLinkage b) {
|
---|
63 | if (a.Length != b.Length) return false;
|
---|
64 | for (var i = 0; i < a.Length; i++)
|
---|
65 | if (a[i] != b[i]) return false;
|
---|
66 | return true;
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override double Dist(ISingleObjectiveSolutionScope<LinearLinkage> a, ISingleObjectiveSolutionScope<LinearLinkage> b) {
|
---|
70 | return Dist(a.Solution, b.Solution);
|
---|
71 | }
|
---|
72 |
|
---|
73 | private double Dist(LinearLinkage a, LinearLinkage b) {
|
---|
74 | return 1.0 - HammingSimilarityCalculator.CalculateSimilarity(a, b);
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override ISolutionSubspace<LinearLinkage> CalculateSubspace(IEnumerable<LinearLinkage> solutions, bool inverse = false) {
|
---|
78 | var pop = solutions.ToList();
|
---|
79 | var N = pop[0].Length;
|
---|
80 | var subspace = new bool[N];
|
---|
81 | for (var i = 0; i < N; i++) {
|
---|
82 | var val = pop[0][i];
|
---|
83 | if (inverse) subspace[i] = true;
|
---|
84 | for (var p = 1; p < pop.Count; p++) {
|
---|
85 | if (pop[p][i] != val) subspace[i] = !inverse;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return new LinearLinkageSolutionSubspace(subspace);
|
---|
89 | }
|
---|
90 |
|
---|
91 | protected override void AdaptiveWalk(
|
---|
92 | ISingleObjectiveSolutionScope<LinearLinkage> scope,
|
---|
93 | int maxEvals, CancellationToken token,
|
---|
94 | ISolutionSubspace<LinearLinkage> sub_space = null) {
|
---|
95 | var maximization = Context.Maximization;
|
---|
96 | var subspace = sub_space is LinearLinkageSolutionSubspace ? ((LinearLinkageSolutionSubspace)sub_space).Subspace : null;
|
---|
97 | var evaluations = 0;
|
---|
98 | var quality = scope.Fitness;
|
---|
99 | if (double.IsNaN(quality)) {
|
---|
100 | Context.Evaluate(scope, token);
|
---|
101 | quality = scope.Fitness;
|
---|
102 | evaluations++;
|
---|
103 | if (evaluations >= maxEvals) return;
|
---|
104 | }
|
---|
105 | var bestQuality = quality;
|
---|
106 | var currentScope = (ISingleObjectiveSolutionScope<LinearLinkage>)scope.Clone();
|
---|
107 | var current = currentScope.Solution;
|
---|
108 | LinearLinkage bestOfTheWalk = null;
|
---|
109 | var bestOfTheWalkF = double.NaN;
|
---|
110 |
|
---|
111 | var tabu = new double[current.Length, current.Length];
|
---|
112 | for (var i = 0; i < current.Length; i++) {
|
---|
113 | for (var j = i; j < current.Length; j++) {
|
---|
114 | tabu[i, j] = tabu[j, i] = maximization ? double.MinValue : double.MaxValue;
|
---|
115 | }
|
---|
116 | tabu[i, current[i]] = quality;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // this dictionary holds the last relevant links
|
---|
120 | var groupItems = new List<int>();
|
---|
121 | var lleb = current.ToBackLinks();
|
---|
122 | Move bestOfTheRest = null;
|
---|
123 | var bestOfTheRestF = double.NaN;
|
---|
124 | var lastAppliedMove = -1;
|
---|
125 | for (var iter = 0; iter < int.MaxValue; iter++) {
|
---|
126 | // clear the dictionary before a new pass through the array is made
|
---|
127 | groupItems.Clear();
|
---|
128 | for (var i = 0; i < current.Length; i++) {
|
---|
129 | if (subspace != null && !subspace[i]) {
|
---|
130 | if (lleb[i] != i)
|
---|
131 | groupItems.Remove(lleb[i]);
|
---|
132 | groupItems.Add(i);
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 |
|
---|
136 | var next = current[i];
|
---|
137 |
|
---|
138 | if (lastAppliedMove == i) {
|
---|
139 | if (bestOfTheRest != null) {
|
---|
140 | bestOfTheRest.Apply(current);
|
---|
141 | bestOfTheRest.ApplyToLLEb(lleb);
|
---|
142 | currentScope.Fitness = bestOfTheRestF;
|
---|
143 | quality = bestOfTheRestF;
|
---|
144 | if (maximization) {
|
---|
145 | tabu[i, next] = Math.Max(tabu[i, next], bestOfTheRestF);
|
---|
146 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], bestOfTheRestF);
|
---|
147 | } else {
|
---|
148 | tabu[i, next] = Math.Min(tabu[i, next], bestOfTheRestF);
|
---|
149 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], bestOfTheRestF);
|
---|
150 | }
|
---|
151 | if (FitnessComparer.IsBetter(maximization, bestOfTheRestF, bestOfTheWalkF)) {
|
---|
152 | bestOfTheWalk = (LinearLinkage)current.Clone();
|
---|
153 | bestOfTheWalkF = bestOfTheRestF;
|
---|
154 | }
|
---|
155 | bestOfTheRest = null;
|
---|
156 | bestOfTheRestF = double.NaN;
|
---|
157 | lastAppliedMove = i;
|
---|
158 | } else {
|
---|
159 | lastAppliedMove = -1;
|
---|
160 | }
|
---|
161 | break;
|
---|
162 | } else {
|
---|
163 | foreach (var move in MoveGenerator.GenerateForItem(i, groupItems, current, lleb)) {
|
---|
164 | // we intend to break link i -> next
|
---|
165 | var qualityToBreak = tabu[i, next];
|
---|
166 | move.Apply(current);
|
---|
167 | var qualityToRestore = tabu[i, current[i]]; // current[i] is new next
|
---|
168 | Context.Evaluate(currentScope, token);
|
---|
169 | evaluations++;
|
---|
170 | var moveF = currentScope.Fitness;
|
---|
171 | var isNotTabu = FitnessComparer.IsBetter(maximization, moveF, qualityToBreak)
|
---|
172 | && FitnessComparer.IsBetter(maximization, moveF, qualityToRestore);
|
---|
173 | var isImprovement = FitnessComparer.IsBetter(maximization, moveF, quality);
|
---|
174 | var isAspired = FitnessComparer.IsBetter(maximization, moveF, bestQuality);
|
---|
175 | if ((isNotTabu && isImprovement) || isAspired) {
|
---|
176 | if (maximization) {
|
---|
177 | tabu[i, next] = Math.Max(tabu[i, next], moveF);
|
---|
178 | tabu[i, current[i]] = Math.Max(tabu[i, current[i]], moveF);
|
---|
179 | } else {
|
---|
180 | tabu[i, next] = Math.Min(tabu[i, next], moveF);
|
---|
181 | tabu[i, current[i]] = Math.Min(tabu[i, current[i]], moveF);
|
---|
182 | }
|
---|
183 | quality = moveF;
|
---|
184 | if (isAspired) bestQuality = quality;
|
---|
185 |
|
---|
186 | move.ApplyToLLEb(lleb);
|
---|
187 |
|
---|
188 | if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheWalkF)) {
|
---|
189 | bestOfTheWalk = (LinearLinkage)current.Clone();
|
---|
190 | bestOfTheWalkF = moveF;
|
---|
191 | }
|
---|
192 |
|
---|
193 | bestOfTheRest = null;
|
---|
194 | bestOfTheRestF = double.NaN;
|
---|
195 | lastAppliedMove = i;
|
---|
196 | break;
|
---|
197 | } else {
|
---|
198 | if (isNotTabu) {
|
---|
199 | if (FitnessComparer.IsBetter(maximization, moveF, bestOfTheRestF)) {
|
---|
200 | bestOfTheRest = move;
|
---|
201 | bestOfTheRestF = moveF;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | move.Undo(current);
|
---|
205 | currentScope.Fitness = quality;
|
---|
206 | }
|
---|
207 | if (evaluations >= maxEvals) break;
|
---|
208 | }
|
---|
209 | }
|
---|
210 | if (lleb[i] != i)
|
---|
211 | groupItems.Remove(lleb[i]);
|
---|
212 | groupItems.Add(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 = (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 | }
|
---|
247 |
|
---|
248 | protected override ISingleObjectiveSolutionScope<LinearLinkage> Breed(ISingleObjectiveSolutionScope<LinearLinkage> p1, ISingleObjectiveSolutionScope<LinearLinkage> p2, CancellationToken token) {
|
---|
249 | var cache = new HashSet<LinearLinkage>(new LinearLinkageEqualityComparer());
|
---|
250 | cache.Add(p1.Solution);
|
---|
251 | cache.Add(p2.Solution);
|
---|
252 |
|
---|
253 | var cacheHits = new Dictionary<int, int>() { { 0, 0 }, { 1, 0 } };
|
---|
254 | var evaluations = 0;
|
---|
255 | var probe = Context.ToScope(null);
|
---|
256 | ISingleObjectiveSolutionScope<LinearLinkage> offspring = null;
|
---|
257 | while (evaluations < p1.Solution.Length) {
|
---|
258 | LinearLinkage c = null;
|
---|
259 | var xochoice = cacheHits.SampleRandom(Context.Random).Key;
|
---|
260 | switch (xochoice) {
|
---|
261 | case 0: c = GroupCrossover.Apply(Context.Random, p1.Solution, p2.Solution); break;
|
---|
262 | case 1: c = SinglePointCrossover.Apply(Context.Random, p1.Solution, p2.Solution); break;
|
---|
263 | }
|
---|
264 | if (cache.Contains(c)) {
|
---|
265 | cacheHits[xochoice]++;
|
---|
266 | if (cacheHits[xochoice] > 10) {
|
---|
267 | cacheHits.Remove(xochoice);
|
---|
268 | if (cacheHits.Count == 0) break;
|
---|
269 | }
|
---|
270 | continue;
|
---|
271 | }
|
---|
272 | probe.Solution = c;
|
---|
273 | Context.Evaluate(probe, token);
|
---|
274 | evaluations++;
|
---|
275 | cache.Add(c);
|
---|
276 | if (offspring == null || Context.IsBetter(probe, offspring)) {
|
---|
277 | offspring = probe;
|
---|
278 | if (Context.IsBetter(offspring, p1) && Context.IsBetter(offspring, p2))
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | Context.IncrementEvaluatedSolutions(evaluations);
|
---|
283 | return offspring ?? probe;
|
---|
284 | }
|
---|
285 |
|
---|
286 | protected override ISingleObjectiveSolutionScope<LinearLinkage> Link(ISingleObjectiveSolutionScope<LinearLinkage> a, ISingleObjectiveSolutionScope<LinearLinkage> b, CancellationToken token, bool delink = false) {
|
---|
287 | var evaluations = 0;
|
---|
288 | var probe = (ISingleObjectiveSolutionScope<LinearLinkage>)a.Clone();
|
---|
289 | ISingleObjectiveSolutionScope<LinearLinkage> best = null;
|
---|
290 | while (true) {
|
---|
291 | Move bestMove = null;
|
---|
292 | var bestMoveQ = double.NaN;
|
---|
293 | // this approach may not fully relink the two solutions
|
---|
294 | foreach (var m in MoveGenerator.Generate(probe.Solution)) {
|
---|
295 | var distBefore = Dist(probe, b);
|
---|
296 | m.Apply(probe.Solution);
|
---|
297 | var distAfter = Dist(probe, b);
|
---|
298 | // consider all moves that would increase the distance between probe and b
|
---|
299 | // or decrease it depending on whether we do delinking or relinking
|
---|
300 | if (delink && distAfter > distBefore || !delink && distAfter < distBefore) {
|
---|
301 | var beforeQ = probe.Fitness;
|
---|
302 | Context.Evaluate(probe, token);
|
---|
303 | evaluations++;
|
---|
304 | var q = probe.Fitness;
|
---|
305 | m.Undo(probe.Solution);
|
---|
306 | probe.Fitness = beforeQ;
|
---|
307 |
|
---|
308 | if (Context.IsBetter(q, bestMoveQ)) {
|
---|
309 | bestMove = m;
|
---|
310 | bestMoveQ = q;
|
---|
311 | }
|
---|
312 | if (Context.IsBetter(q, beforeQ)) break;
|
---|
313 | } else m.Undo(probe.Solution);
|
---|
314 | }
|
---|
315 | if (bestMove == null) break;
|
---|
316 | bestMove.Apply(probe.Solution);
|
---|
317 | probe.Fitness = bestMoveQ;
|
---|
318 | if (best == null || Context.IsBetter(probe, best))
|
---|
319 | best = (ISingleObjectiveSolutionScope<LinearLinkage>)probe.Clone();
|
---|
320 | }
|
---|
321 | Context.IncrementEvaluatedSolutions(evaluations);
|
---|
322 |
|
---|
323 | return best ?? probe;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|