Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/ExhaustiveSwap2.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: 2.7 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 ExhaustiveSwap2 {
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      Swap2Move lastSuccessMove = null;
36      int steps = 0, evaluations = 0;
37      while (true) {
38        foreach (var swap in ExhaustiveSwap2MoveGenerator.Generate(current)) {
39          if (lastSuccessMove != null && swap.Index1 == lastSuccessMove.Index1 && swap.Index2 == lastSuccessMove.Index2) {
40            // been there, done that
41            lastSuccessMove = null;
42            break;
43          }
44          if (noTouch != null && (noTouch[swap.Index1, 0] || noTouch[swap.Index2, 0]))
45            continue;
46
47          var h = current[swap.Index1];
48          current[swap.Index1] = current[swap.Index2];
49          current[swap.Index2] = h;
50          var q = eval(current);
51          evaluations++;
52          if (FitnessComparer.IsBetter(maximization, q, quality)) {
53            steps++;
54            quality = q;
55            lastSuccessMove = swap;
56          } else {
57            current[swap.Index2] = current[swap.Index1];
58            current[swap.Index1] = h;
59          }
60
61          if (token.IsCancellationRequested) {
62            lastSuccessMove = null;
63            break;
64          }
65        }
66        if (lastSuccessMove == null) break;
67      }
68      return Tuple.Create(evaluations, steps);
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.