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 ExhaustiveSwap2 {
|
---|
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 | Swap2Move lastSuccessMove = null;
|
---|
44 | var neighborhood = ExhaustiveSwap2MoveGenerator.Generate(current).Shuffle(random).ToList();
|
---|
45 | while (true) {
|
---|
46 | foreach (var swap in neighborhood) {
|
---|
47 | if (lastSuccessMove != null && swap.Index1 == lastSuccessMove.Index1 && swap.Index2 == lastSuccessMove.Index2) {
|
---|
48 | // been there, done that
|
---|
49 | lastSuccessMove = null;
|
---|
50 | break;
|
---|
51 | }
|
---|
52 | if (subspace != null && !(subspace[swap.Index1, 0] && subspace[swap.Index2, 0]))
|
---|
53 | continue;
|
---|
54 |
|
---|
55 | var h = current[swap.Index1];
|
---|
56 | current[swap.Index1] = current[swap.Index2];
|
---|
57 | current[swap.Index2] = h;
|
---|
58 | var q = eval(current, token);
|
---|
59 | evaluations++;
|
---|
60 | if (FitnessComparer.IsBetter(maximization, q, quality)) {
|
---|
61 | yield return Tuple.Create(current, q, evaluations);
|
---|
62 | quality = q;
|
---|
63 | lastSuccessMove = swap;
|
---|
64 | } else {
|
---|
65 | current[swap.Index2] = current[swap.Index1];
|
---|
66 | current[swap.Index1] = h;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (token.IsCancellationRequested) {
|
---|
70 | lastSuccessMove = null;
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | if (lastSuccessMove == null) break;
|
---|
75 | }
|
---|
76 | yield return Tuple.Create(current, quality, evaluations);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|