[16955] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 25 | using HeuristicLab.Problems.Instances;
|
---|
| 26 |
|
---|
| 27 | namespace 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 | }
|
---|