Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/WalkExporter/AdaptiveWalks.cs @ 18066

Last change on this file since 18066 was 16955, checked in by abeham, 6 years ago

#2457: worked on thesis

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.PermutationEncoding;
25using HeuristicLab.Problems.Instances;
26
27namespace WalkExporter {
28  static class AdaptiveWalks {
29   
30    public static IEnumerable<double> AdaptiveWalk(IRandom random, QAPData qap, int sampleSize) {
31      var sol = new Permutation(PermutationTypes.Absolute, qap.Dimension, random);
32      var fit = Util.Evaluate(sol, qap);
33      yield return fit;
34      while (true) {
35        var bestZ1 = -1;
36        var bestZ2 = -1;
37        var bestMove = double.MaxValue;
38        for (var s = 0; s < sampleSize; s++) {
39          var z1 = random.Next(qap.Dimension);
40          var z2 = (z1 + random.Next(1, qap.Dimension)) % qap.Dimension;
41          var move = Util.EvaluateSwap2Diff(sol, z1, z2, qap);
42          if (move < bestMove) {
43            bestZ1 = z1;
44            bestZ2 = z2;
45            bestMove = move;
46          }
47        }
48        fit += bestMove;
49        yield return fit;
50        sol.Swap(bestZ1, bestZ2);
51      }
52    }
53
54    public static IEnumerable<double> UpDownWalk(IRandom random, QAPData qap, int sampleSize) {
55      var sol = new Permutation(PermutationTypes.Absolute, qap.Dimension, random);
56      var fit = Util.Evaluate(sol, qap);
57      yield return fit;
58      var down = true;
59      while (true) {
60        int z1 = -1, z2 = -1, bestDownZ1 = -1, bestDownZ2 = -1, bestUpZ1 = -1, bestUpZ2 = -1;
61        double move = 0, bestDownMove = 0, bestUpMove = 0;
62        for (var s = 0; s < sampleSize; s++) {
63          z1 = random.Next(qap.Dimension);
64          z2 = (z1 + random.Next(1, qap.Dimension)) % qap.Dimension;
65          move = Util.EvaluateSwap2Diff(sol, z1, z2, qap);
66          if (move < bestDownMove) {
67            bestDownZ1 = z1;
68            bestDownZ2 = z2;
69            bestDownMove = move;
70          } else if (move > bestUpMove) {
71            bestUpZ1 = z1;
72            bestUpZ2 = z2;
73            bestUpMove = move;
74          }
75        }
76        // revert direction
77        if (down && bestDownZ1 < 0 || !down && bestUpZ1 < 0) down = !down;
78
79        if (down && bestDownZ1 >= 0) {
80          fit += bestDownMove;
81          yield return fit;
82          sol.Swap(bestDownZ1, bestDownZ2);
83        } else if (!down && bestUpZ1 >= 0) {
84          fit += bestUpMove;
85          yield return fit;
86          sol.Swap(bestUpZ1, bestUpZ2);
87        } else {
88          // all moves neutral, make a random move , i.e. last sampled one
89          fit += move;
90          yield return fit;
91          sol.Swap(z1, z2);
92        }
93      }
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.