- Timestamp:
- 03/26/15 16:14:52 (10 years ago)
- Location:
- branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/OneShift/PTSPEstimatedInsertionEvaluator.cs
r12219 r12261 1 using HeuristicLab.Common; 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 2002-2015 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 HeuristicLab.Common; 2 23 using HeuristicLab.Core; 3 24 using HeuristicLab.Data; … … 7 28 using System; 8 29 9 namespace HeuristicLab.Problems.PTSP.MoveEvaluators.OneShift { 10 class PTSPEstimatedInsertionEvaluator : PTSPPathMoveEvaluator, IPermutationTranslocationMoveOperator { 30 namespace HeuristicLab.Problems.PTSP { 31 [Item("PTSPEstimatedInsertionEvaluator", "Evaluates an insertion move (1-shift)")] 32 [StorableClass] 33 public class PTSPEstimatedInsertionEvaluator : PTSPPathMoveEvaluator, IPermutationTranslocationMoveOperator { 11 34 12 35 public override Type EvaluatorType { 13 get { return typeof(PTSPEstimatedIn versionMovePathEvaluator); }36 get { return typeof(PTSPEstimatedInsertionEvaluator); } 14 37 } 15 38 public ILookupParameter<TranslocationMove> TranslocationMoveParameter { … … 54 77 55 78 public static double EvaluateByDistanceMatrix(Permutation permutation, TranslocationMove move, DistanceMatrix distanceMatrix, ItemList<ItemList<IntValue>> realizations) { 56 //permutation = new Permutation(PermutationTypes.Absolute, new int[]{0,1,2,3,4,5,6,7}); TODO remove test57 //move = new TranslocationMove(0, 0, 4); TODO remove test79 Permutation afterMove = new Permutation(PermutationTypes.RelativeUndirected,permutation); 80 TranslocationManipulator.Apply(afterMove, move.Index1, move.Index1, move.Index3); 58 81 double moveQuality = 0; 59 82 int[] edges = new int[12]; 60 83 int[] indices = new int[12]; 61 84 edges[0] = permutation.GetCircular(move.Index1 - 1); 62 edges[6] = edges[0]; 63 indices[0] = move.Index1-1; 64 if (indices[0] == -1) indices[0] = permutation.Length - 1; 65 indices[6] = indices[0]; 85 indices[0] = decreaseCircularIndex(permutation.Length, move.Index1); 66 86 edges[1] = permutation[move.Index1]; 67 87 indices[1] = move.Index1; 68 edges[2] = edges[1]; 69 indices[2] = indices[1]; 70 edges[9] = edges[1]; 71 indices[9] = indices[1]; 72 edges[10] = edges[1]; 73 indices[10] = indices[1]; 88 edges[2] = permutation[move.Index1]; 89 indices[2] = move.Index1; 74 90 edges[3] = permutation.GetCircular(move.Index1 + 1); 75 edges[7] = edges[3]; 76 indices[3] = move.Index1+1; 77 if (indices[3] == permutation.Length + 1) indices[3] = 0; 78 indices[7] = indices[3]; 79 edges[4] = permutation[move.Index3]; 80 indices[4] = move.Index3; 81 edges[8] = edges[4]; 82 indices[8] = indices[4]; 83 edges[5] = permutation.GetCircular(move.Index3 + 1); 84 edges[11] = edges[5]; 85 indices[5] = move.Index3+1; 86 if (indices[5] == permutation.Length + 1) indices[5] = 0; 87 indices[11] = indices[5]; 91 indices[3] = increaseCircularIndex(permutation.Length, move.Index1); 92 93 edges[6] = afterMove.GetCircular(move.Index3 - 1); 94 indices[6] = decreaseCircularIndex(afterMove.Length, move.Index3); 95 edges[7] = afterMove[move.Index3]; 96 indices[7] = move.Index3; 97 edges[8] = afterMove[move.Index3]; 98 indices[8] = move.Index3; 99 edges[9] = afterMove.GetCircular(move.Index3 + 1); 100 indices[9] = increaseCircularIndex(afterMove.Length, move.Index3); 101 102 if (move.Index3 > move.Index1) { 103 edges[4] = permutation[move.Index3]; 104 indices[4] = move.Index3; 105 edges[5] = permutation.GetCircular(move.Index3 + 1); 106 indices[5] = indices[9]; 107 edges[10] = afterMove.GetCircular(move.Index1 - 1); 108 indices[10] = indices[0]; 109 edges[11] = afterMove[move.Index1]; 110 indices[11] = move.Index1; 111 } else { 112 edges[4] = permutation.GetCircular(move.Index3 - 1); 113 indices[4] = indices[6]; 114 edges[5] = permutation[move.Index3]; 115 indices[5] = move.Index3; 116 edges[10] = afterMove[move.Index1]; 117 indices[10] = move.Index1; 118 edges[11] = afterMove.GetCircular(move.Index1 + 1); 119 indices[11] = indices[3]; 120 } 88 121 int[] aPosteriori = new int[12]; 122 Permutation tempPermutation; 89 123 foreach (ItemList<IntValue> realization in realizations) { 90 //int[] realization2 = new int[] { 1, 0, 1, 1, 0, 0, 1, 1 }; TODO remove test91 124 for (int i = 0; i < edges.Length; i++) { 125 if (i < 6) { 126 tempPermutation = permutation; 127 } else { 128 tempPermutation = afterMove; 129 } 92 130 if (realization[edges[i]].Value == 1) { 93 131 aPosteriori[i] = edges[i]; … … 96 134 if (i%2==0) { 97 135 // find nearest predecessor in realization if source edge 98 while (realization[permutation.GetCircular(indices[i]-j)].Value == 0) {99 j --;136 while (realization[tempPermutation.GetCircular(indices[i] - j)].Value == 0) { 137 j++; 100 138 } 101 aPosteriori[i] = permutation.GetCircular(indices[i] - j);139 aPosteriori[i] = tempPermutation.GetCircular(indices[i] - j); 102 140 } else { 103 141 // find nearest successor in realization if target edge 104 while (realization[permutation.GetCircular(indices[i]+j)].Value == 0) {142 while (realization[tempPermutation.GetCircular(indices[i] + j)].Value == 0) { 105 143 j++; 106 144 } 107 aPosteriori[i] = permutation.GetCircular(indices[i] + j);145 aPosteriori[i] = tempPermutation.GetCircular(indices[i] + j); 108 146 } 109 147 } 110 148 } 111 // compute cost difference between the two a posteriori solutions 112 moveQuality += distanceMatrix[aPosteriori[6], aPosteriori[7]] + distanceMatrix[aPosteriori[8], aPosteriori[9]] + distanceMatrix[aPosteriori[10], aPosteriori[11]]; 113 moveQuality -= distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]] - distanceMatrix[aPosteriori[4], aPosteriori[5]]; 149 if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3]) && 150 !(aPosteriori[0] == aPosteriori[4] && aPosteriori[1] == aPosteriori[5]) && 151 !(aPosteriori[2] == aPosteriori[4] && aPosteriori[3] == aPosteriori[5])) { 152 // compute cost difference between the two a posteriori solutions 153 moveQuality = moveQuality + distanceMatrix[aPosteriori[6], aPosteriori[7]] + distanceMatrix[aPosteriori[8], aPosteriori[9]] + distanceMatrix[aPosteriori[10], aPosteriori[11]]; 154 moveQuality = moveQuality - distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]] - distanceMatrix[aPosteriori[4], aPosteriori[5]]; 155 } 156 Array.Clear(aPosteriori, 0, aPosteriori.Length); 114 157 } 115 158 // return average of cost differences 116 return moveQuality / realizations.Capacity; 159 return Math.Abs(moveQuality) / realizations.Capacity; 160 } 161 162 private static int decreaseCircularIndex(int length, int index) { 163 int result = index - 1; 164 if (result == -1) { 165 result = length - 1; 166 } 167 return result; 168 } 169 170 private static int increaseCircularIndex(int length, int index) { 171 int result = index + 1; 172 if (result == length + 1) { 173 result = 0; 174 } 175 return result; 117 176 } 118 177 -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/TwoOpt/PTSPEstimatedInversionMovePathEvaluator.cs
r12219 r12261 100 100 // find nearest predecessor in realization if source edge 101 101 while(realization[permutation.GetCircular(indices[i]-j)].Value==0) { 102 j --;102 j++; 103 103 } 104 104 aPosteriori[i] = permutation.GetCircular(indices[i] - j); … … 113 113 } 114 114 // compute cost difference between the two a posteriori solutions 115 moveQuality += distanceMatrix[aPosteriori[0], aPosteriori[2]] + distanceMatrix[aPosteriori[1], aPosteriori[3]]; 116 moveQuality -= distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]]; 115 if (!(aPosteriori[0] == aPosteriori[2] && aPosteriori[1] == aPosteriori[3])) { 116 moveQuality = moveQuality + distanceMatrix[aPosteriori[0], aPosteriori[2]] + distanceMatrix[aPosteriori[1], aPosteriori[3]] - distanceMatrix[aPosteriori[0], aPosteriori[1]] - distanceMatrix[aPosteriori[2], aPosteriori[3]]; 117 } 118 Array.Clear(aPosteriori, 0, aPosteriori.Length); 117 119 } 118 120 // return average of cost differences 119 return moveQuality/ realizations.Capacity;121 return Math.Abs(moveQuality) / realizations.Capacity; 120 122 } 121 123
Note: See TracChangeset
for help on using the changeset viewer.