Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive1Shift.cs @ 14551

Last change on this file since 14551 was 14456, checked in by abeham, 8 years ago

#2701:

  • Using evaluated solutions from HC as max evaluations for tabu walking in MemPR
  • Fixed some bugs in MemPR (permutation) regarding subspace calculation (true means okay, false means no)
  • Fixed bug in TSP
File size: 3.3 KB
Line 
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
22using System;
23using System.Linq;
24using System.Threading;
25using HeuristicLab.Algorithms.MemPR.Util;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch {
31  public static class Exhaustive1Shift {
32    public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm,
33      ref double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, double> eval,
34      CancellationToken token, bool[,] subspace = null) {
35      var evaluations = 0;
36      var current = perm;
37      if (double.IsNaN(quality)) {
38        quality = eval(current);
39        evaluations++;
40      }
41      TranslocationMove lastSuccessMove = null;
42      var steps = 0;
43      var neighborhood = ExhaustiveInsertionMoveGenerator.Generate(current).Shuffle(random).ToList();
44      while (true) {
45        foreach (var shift in neighborhood) {
46          if (lastSuccessMove != null && shift.Index1 == lastSuccessMove.Index1 && shift.Index2 == lastSuccessMove.Index2 && shift.Index3 == lastSuccessMove.Index3) {
47            // been there, done that
48            lastSuccessMove = null;
49            break;
50          }
51          var prev1 = shift.Index1 - 1;
52          var next1 = (shift.Index1 + 1) % current.Length;
53          if (prev1 < 0) prev1 += current.Length;
54          var prev3 = shift.Index3 - 1;
55          var next3 = (shift.Index3 + 1) % current.Length;
56          if (prev3 < 0) prev3 += current.Length;
57          if (subspace != null && !(subspace[current[prev1], current[shift.Index1]] && subspace[current[shift.Index1], current[next1]]
58                                && subspace[current[prev3], current[shift.Index3]] && subspace[current[shift.Index3], current[next3]]))
59            continue;
60          TranslocationManipulator.Apply(current, shift.Index1, shift.Index2, shift.Index3);
61          var q = eval(current);
62          evaluations++;
63          if (FitnessComparer.IsBetter(maximization, q, quality)) {
64            steps++;
65            quality = q;
66            lastSuccessMove = shift;
67          } else TranslocationManipulator.Apply(current, shift.Index3, shift.Index3, shift.Index1);
68
69          if (token.IsCancellationRequested) {
70            lastSuccessMove = null;
71            break;
72          }
73        }
74        if (lastSuccessMove == null) break;
75      }
76      return Tuple.Create(evaluations, steps);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.