Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs @ 16099

Last change on this file since 16099 was 14690, checked in by abeham, 8 years ago

#2457: working on identification of problem instances

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.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Algorithms.MemPR.Util;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch {
32  public static class Exhaustive2Opt {
33    public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>>
34      HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm,
35      double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval,
36      CancellationToken token, bool[,] subspace = null) {
37      var evaluations = 0;
38      var current = perm;
39      if (double.IsNaN(quality)) {
40        quality = eval(current, token);
41        evaluations++;
42      }
43      InversionMove lastSuccessMove = null;
44      var neighborhood = ExhaustiveInversionMoveGenerator.Generate(current).Shuffle(random).ToList();
45      while (true) {
46        foreach (var opt in neighborhood) {
47          if (lastSuccessMove != null && opt.Index1 == lastSuccessMove.Index1 && opt.Index2 == lastSuccessMove.Index2) {
48            // been there, done that
49            lastSuccessMove = null;
50            break;
51          }
52          var prev = opt.Index1 - 1;
53          var next = (opt.Index2 + 1) % current.Length;
54          if (prev < 0) prev += current.Length;
55          if (subspace != null && !(subspace[current[prev], current[opt.Index1]] && subspace[current[opt.Index2], current[next]]))
56            continue;
57
58          current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
59          var q = eval(current, token);
60          evaluations++;
61          if (FitnessComparer.IsBetter(maximization, q, quality)) {
62            yield return Tuple.Create(current, q, evaluations);
63            quality = q;
64            lastSuccessMove = opt;
65          } else current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1);
66
67          if (token.IsCancellationRequested) {
68            lastSuccessMove = null;
69            break;
70          }
71        }
72        if (lastSuccessMove == null) break;
73      }
74      yield return Tuple.Create(current, quality, evaluations);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.