Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/LocalSearch/StaticAPI/ExhaustiveBitflip.cs @ 15160

Last change on this file since 15160 was 14552, checked in by abeham, 8 years ago

#2701:

  • Added alternating bits binary test Problem
  • Refactored MemPR to work with programmable problem in current trunk
  • fixed a bug in permutation MemPR when crossover doesn't assign an offspring
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.Linq;
24using System.Threading;
25using HeuristicLab.Algorithms.MemPR.Util;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.BinaryVectorEncoding;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.Encodings.Binary.LocalSearch {
31  public static class ExhaustiveBitflip {
32    public static Tuple<int, int> Optimize(IRandom random, BinaryVector solution, ref double quality, bool maximization, Func<BinaryVector, CancellationToken, double> evalFunc, CancellationToken token, bool[] subspace = null) {
33      if (double.IsNaN(quality)) quality = evalFunc(solution, token);
34      var improved = false;
35      var order = Enumerable.Range(0, solution.Length).Shuffle(random).ToArray();
36      var lastImp = -1;
37      var steps = 0;
38      var evaluations = 0;
39      do {
40        improved = false;
41        for (var i = 0; i < solution.Length; i++) {
42          // in case we didn't make an improvement this round and arrived at the index of the last improvement
43          // break means we don't need to try the remaining moves again as they have brought no improvement
44          if (!improved && lastImp == i) break;
45          var idx = order[i];
46          if (subspace != null && !subspace[idx]) continue;
47          // bitflip the solution
48          solution[idx] = !solution[idx];
49          var after = evalFunc(solution, token);
50          evaluations++;
51          if (FitnessComparer.IsBetter(maximization, after, quality)) {
52            steps++;
53            quality = after;
54            lastImp = i;
55            improved = true;
56          } else {
57            // undo the bitflip in case no improvement was made
58            solution[idx] = !solution[idx];
59          }
60          token.ThrowIfCancellationRequested();
61        }
62      } while (improved);
63
64      return Tuple.Create(evaluations, steps);
65    }
66  }
67}
Note: See TracBrowser for help on using the repository browser.