Free cookie consent management tool by TermsFeed Policy Generator

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