Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14453 was 14453, checked in by abeham, 7 years ago

#2701:

  • Worked on MemPR algorithm for permutations
    • Made Evaluate() method mostly thread-safe and moved evaluated solutions counting to caller
  • Removed TSP specific similarity calculator (it is actually not specific to the TSP) and added it to the permutation encoding as HammingSimilarityCalculator
  • Fixed bug in qap basicproblem
File size: 3.1 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.Threading;
24using HeuristicLab.Algorithms.MemPR.Util;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27
28namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch {
29  public static class Exhaustive1Shift {
30    public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm,
31      ref double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, double> eval,
32      CancellationToken token, bool[,] noTouch = null) {
33      var current = perm;
34      if (double.IsNaN(quality)) quality = eval(current);
35      TranslocationMove lastSuccessMove = null;
36      int steps = 0, evaluations = 0;
37      while (true) {
38        foreach (var shift in ExhaustiveInsertionMoveGenerator.Generate(current)) {
39          if (lastSuccessMove != null && shift.Index1 == lastSuccessMove.Index1 && shift.Index2 == lastSuccessMove.Index2 && shift.Index3 == lastSuccessMove.Index3) {
40            // been there, done that
41            lastSuccessMove = null;
42            break;
43          }
44          var prev1 = shift.Index1 - 1;
45          var next1 = (shift.Index1 + 1) % current.Length;
46          if (prev1 < 0) prev1 += current.Length;
47          var prev3 = shift.Index3 - 1;
48          var next3 = (shift.Index3 + 1) % current.Length;
49          if (prev3 < 0) prev3 += current.Length;
50          if (noTouch != null && ((noTouch[current[prev1], current[shift.Index1]] || noTouch[current[shift.Index1], current[next1]]
51                                || noTouch[current[prev3], current[shift.Index3]] || noTouch[current[shift.Index3], current[next3]])))
52            continue;
53          TranslocationManipulator.Apply(current, shift.Index1, shift.Index2, shift.Index3);
54          var q = eval(current);
55          evaluations++;
56          if (FitnessComparer.IsBetter(maximization, q, quality)) {
57            steps++;
58            quality = q;
59            lastSuccessMove = shift;
60          } else TranslocationManipulator.Apply(current, shift.Index3, shift.Index3, shift.Index1);
61
62          if (token.IsCancellationRequested) {
63            lastSuccessMove = null;
64            break;
65          }
66        }
67        if (lastSuccessMove == null) break;
68      }
69      return Tuple.Create(evaluations, steps);
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.