Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs @ 14456

Last change on this file since 14456 was 14456, checked in by abeham, 7 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: 2.9 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 Exhaustive2Opt {
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      InversionMove lastSuccessMove = null;
42      var steps = 0;
43      var neighborhood = ExhaustiveInversionMoveGenerator.Generate(current).Shuffle(random).ToList();
44      while (true) {
45        foreach (var opt in neighborhood) {
46          if (lastSuccessMove != null && opt.Index1 == lastSuccessMove.Index1 && opt.Index2 == lastSuccessMove.Index2) {
47            // been there, done that
48            lastSuccessMove = null;
49            break;
50          }
51          var prev = opt.Index1 - 1;
52          var next = (opt.Index2 + 1) % current.Length;
53          if (prev < 0) prev += current.Length;
54          if (subspace != null && !(subspace[current[prev], current[opt.Index1]] && subspace[current[opt.Index2], current[next]]))
55            continue;
56
57          InversionManipulator.Apply(current, opt.Index1, opt.Index2);
58          var q = eval(current);
59          evaluations++;
60          if (FitnessComparer.IsBetter(maximization, q, quality)) {
61            steps++;
62            quality = q;
63            lastSuccessMove = opt;
64          } else InversionManipulator.Apply(current, opt.Index1, opt.Index2);
65
66          if (token.IsCancellationRequested) {
67            lastSuccessMove = null;
68            break;
69          }
70        }
71        if (lastSuccessMove == null) break;
72      }
73      return Tuple.Create(evaluations, steps);
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.