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.Util;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
29 | using HeuristicLab.Random;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch {
|
---|
32 | public static class Exhaustive1Shift {
|
---|
33 | public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>>
|
---|
34 | HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm,
|
---|
35 | double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval,
|
---|
36 | CancellationToken token, bool[,] subspace = null) {
|
---|
37 | var evaluations = 0;
|
---|
38 | var current = perm;
|
---|
39 | if (double.IsNaN(quality)) {
|
---|
40 | quality = eval(current, token);
|
---|
41 | evaluations++;
|
---|
42 | }
|
---|
43 | TranslocationMove lastSuccessMove = null;
|
---|
44 | var neighborhood = ExhaustiveInsertionMoveGenerator.Generate(current).Shuffle(random).ToList();
|
---|
45 | while (true) {
|
---|
46 | foreach (var shift in neighborhood) {
|
---|
47 | if (lastSuccessMove != null && shift.Index1 == lastSuccessMove.Index1 && shift.Index2 == lastSuccessMove.Index2 && shift.Index3 == lastSuccessMove.Index3) {
|
---|
48 | // been there, done that
|
---|
49 | lastSuccessMove = null;
|
---|
50 | break;
|
---|
51 | }
|
---|
52 | var prev1 = shift.Index1 - 1;
|
---|
53 | var next1 = (shift.Index1 + 1) % current.Length;
|
---|
54 | if (prev1 < 0) prev1 += current.Length;
|
---|
55 | var prev3 = shift.Index3 - 1;
|
---|
56 | var next3 = (shift.Index3 + 1) % current.Length;
|
---|
57 | if (prev3 < 0) prev3 += current.Length;
|
---|
58 | if (subspace != null && !(subspace[current[prev1], current[shift.Index1]] && subspace[current[shift.Index1], current[next1]]
|
---|
59 | && subspace[current[prev3], current[shift.Index3]] && subspace[current[shift.Index3], current[next3]]))
|
---|
60 | continue;
|
---|
61 | TranslocationManipulator.Apply(current, shift.Index1, shift.Index2, shift.Index3);
|
---|
62 | var q = eval(current, token);
|
---|
63 | evaluations++;
|
---|
64 | if (FitnessComparer.IsBetter(maximization, q, quality)) {
|
---|
65 | yield return Tuple.Create(current, q, evaluations);
|
---|
66 | quality = q;
|
---|
67 | lastSuccessMove = shift;
|
---|
68 | } else TranslocationManipulator.Apply(current, shift.Index3, shift.Index3, shift.Index1);
|
---|
69 |
|
---|
70 | if (token.IsCancellationRequested) {
|
---|
71 | lastSuccessMove = null;
|
---|
72 | break;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | if (lastSuccessMove == null) break;
|
---|
76 | }
|
---|
77 | yield return Tuple.Create(current, quality, evaluations);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|