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