Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2701_MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LocalSearch/StaticAPI/ExhaustiveLocalSearch.cs

Last change on this file 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: 4.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.Collections.Generic;
24using System.Linq;
25using System.Threading;
26using HeuristicLab.Algorithms.MemPR.Util;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.LinearLinkageEncoding;
29
30namespace HeuristicLab.Algorithms.MemPR.Grouping.LocalSearch {
31  public static class ExhaustiveLocalSearch {
32    public static Tuple<int, int> Optimize(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, CancellationToken, double> eval, CancellationToken token, bool[] subspace = null) {
33      var evaluations = 0;
34      if (double.IsNaN(quality)) {
35        quality = eval(solution, token);
36        evaluations++;
37      }
38      var steps = 0;
39      var lleb = solution.ToBackLinks();
40      for (var iter = 0; iter < int.MaxValue; iter++) {
41        var change = false;
42        var groupItems = new List<int>();
43        for (var i = 0; i < solution.Length; i++) {
44          foreach (var move in MoveGenerator.GenerateForItem(i, groupItems, solution, lleb).ToList()) {
45            move.Apply(solution);
46            var moveF = eval(solution, token);
47            evaluations++;
48            if (FitnessComparer.IsBetter(maximization, moveF, quality)) {
49              move.ApplyToLLEb(lleb);
50              steps++;
51              quality = moveF;
52              change = true;
53              break;
54            } else {
55              move.Undo(solution);
56            }
57            if (token.IsCancellationRequested) break;
58          }
59          if (lleb[i] != i)
60            groupItems.Remove(lleb[i]);
61          groupItems.Add(i);
62          if (token.IsCancellationRequested) break;
63        }
64        if (!change || token.IsCancellationRequested) break;
65      }
66
67      return Tuple.Create(evaluations, steps);
68    }
69
70    public static Tuple<int, int> OptimizeSwap(IRandom random, LinearLinkage solution, ref double quality, bool maximization, Func<LinearLinkage, IRandom, double> eval, CancellationToken token) {
71      var evaluations = 0;
72      var current = solution;
73      if (double.IsNaN(quality)) {
74        quality = eval(current, random);
75        evaluations++;
76      }
77      var groups = current.GetGroups().ToList();
78      var exclude = new bool[groups.Count, groups.Count];
79      var steps = 0;
80      for (var iter = 0; iter < int.MaxValue; iter++) {
81        var change = false;
82        for (var i = 0; i < groups.Count - 1; i++) {
83          for (var j = i + 1; j < groups.Count; j++) {
84            if (exclude[i, j]) continue;
85            var groupsChanged = false;
86            if (groups[i].Count == 1 && groups[j].Count == 1) continue; // groups are anonymous, swap has no effect
87            for (var x = 0; x < groups[i].Count; x++) {
88              for (var y = 0; y < groups[j].Count; y++) {
89                var a = groups[i][x];
90                var b = groups[j][y];
91                groups[i][x] = b;
92                groups[j][y] = a;
93                current.SetGroups(groups);
94                var moveF = eval(current, random);
95                evaluations++;
96                if (FitnessComparer.IsBetter(maximization, moveF, quality)) {
97                  steps++;
98                  quality = moveF;
99                  groupsChanged = true;
100                } else {
101                  // undo
102                  groups[i][x] = a;
103                  groups[j][y] = b;
104                  current.SetGroups(groups);
105                }
106              }
107            }
108            if (!groupsChanged) {
109              exclude[i, j] = true;
110            } else {
111              change = true;
112              for (var k = 0; k < groups.Count; k++) {
113                exclude[k, j] = false;
114                exclude[i, k] = false;
115              }
116            }
117            if (token.IsCancellationRequested) break;
118          }
119          if (token.IsCancellationRequested) break;
120        }
121        if (!change || token.IsCancellationRequested) break;
122      }
123      return Tuple.Create(evaluations, steps);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.