Changeset 12261 for branches/PTSP
- Timestamp:
- 03/26/15 16:14:52 (10 years ago)
- Location:
- branches/PTSP
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/EstimatedPTSP.cs
r12228 r12261 10 10 using HeuristicLab.Parameters; 11 11 using HeuristicLab.Data; 12 using HeuristicLab.Random; 12 13 using System; 13 14 using System.Linq; 14 using HeuristicLab.Problems.PTSP.MoveEvaluators.OneShift;15 15 16 16 namespace HeuristicLab.Problems.PTSP { … … 36 36 Permutation p = individual.Permutation(); 37 37 // Estimation-based evaluation 38 Random r = new Random();38 MersenneTwister r = new MersenneTwister(); 39 39 double estimatedSum = 0; 40 40 for (int i = 0; i < realizations.Count; i++) { 41 int singleRealization = -1 ;41 int singleRealization = -1, firstNode = -1; 42 42 for (int j = 0; j < realizations[i].Count; j++) { 43 if (realizations[i][ j].Value == 1) {43 if (realizations[i][p[j]].Value == 1) { 44 44 if (singleRealization != -1) { 45 45 estimatedSum += DistanceMatrix[singleRealization, p[j]]; 46 } else { 47 firstNode = p[j]; 46 48 } 47 49 singleRealization = p[j]; 48 50 } 49 51 } 52 if (singleRealization != -1) { 53 estimatedSum += DistanceMatrix[singleRealization, firstNode]; 54 } 50 55 } 51 56 return estimatedSum / SampleSize.Value; 57 } 58 59 public double[] EvaluateWithParams(DistanceMatrix distances, DoubleArray probabilities, ItemList<ItemList<IntValue>> realizations, Permutation individual ) { 60 // Estimation-based evaluation 61 MersenneTwister r = new MersenneTwister(); 62 double estimatedSum = 0; 63 double[] partialSums = new double[realizations.Count]; 64 for (int i = 0; i < realizations.Count; i++) { 65 partialSums[i] = 0; 66 int singleRealization = -1, firstNode = -1; 67 for (int j = 0; j < realizations[i].Count; j++) { 68 if (realizations[i][individual[j]].Value == 1) { 69 if (singleRealization != -1) { 70 partialSums[i] += distances[singleRealization, individual[j]]; 71 } else { 72 firstNode = individual[j]; 73 } 74 singleRealization = individual[j]; 75 } 76 } 77 if (singleRealization != -1) { 78 partialSums[i] += distances[singleRealization, firstNode]; 79 } 80 estimatedSum += partialSums[i]; 81 } 82 double mean = estimatedSum / realizations.Count; 83 double variance = 0; 84 for (int i = 0; i < realizations.Count; i++) { 85 variance += Math.Pow((partialSums[i] - mean), 2); 86 } 87 variance = variance / realizations.Count; 88 return new double[] {mean,variance}; 52 89 } 53 90 … … 61 98 } 62 99 100 private int Ttest(int ProblemSize) { 101 MersenneTwister random = new MersenneTwister(); 102 Permutation p1 = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random); 103 Permutation p2 = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random); 104 ItemList<ItemList<IntValue>> realizations = new ItemList<ItemList<IntValue>>(); 105 int index = -1; 106 while (true) { 107 for (int i = index+1; i < index+5; i++) { 108 realizations.Add(new ItemList<IntValue>()); 109 for (int j = 0; j < ProblemSize; j++) { 110 if (ProbabilityMatrix[j] > random.NextDouble()) { 111 realizations.ElementAt(i).Add(new IntValue(1)); 112 } else { 113 realizations.ElementAt(i).Add(new IntValue(0)); 114 } 115 } 116 } 117 index += 4; 118 double[] eval1 = EvaluateWithParams(DistanceMatrix, ProbabilityMatrix, realizations, p1); 119 double[] eval2 = EvaluateWithParams(DistanceMatrix, ProbabilityMatrix, realizations, p2); 120 double sx1x2 = Math.Sqrt((eval1[1]+eval2[1])/2); 121 int degrees = 2 * realizations.Count - 2; 122 double t = (eval1[0]-eval2[0])/(sx1x2*Math.Sqrt(2.0/(double)realizations.Count)); 123 } 124 } 125 63 126 public override void Load(TSPData data) { 64 127 base.Load(data); 65 128 // For now uses sample size of 20 but should use Student's t-test 66 SampleSize = new IntValue(20); 129 //Ttest(data.Dimension); 130 SampleSize = new IntValue(25); 131 MersenneTwister r = new MersenneTwister(); 132 int countOnes = 0; 67 133 realizations = new ItemList<ItemList<IntValue>>(SampleSize.Value); 68 Random r = new Random();69 134 for (int i = 0; i < SampleSize.Value; i++) { 70 realizations.Add(new ItemList<IntValue>()); 71 for (int j = 0; j < data.Dimension; j++) { 72 if (ProbabilityMatrix[j] > r.NextDouble()) { 73 realizations.ElementAt(i).Add(new IntValue(1)); 74 } else { 75 realizations.ElementAt(i).Add(new IntValue(0)); 135 ItemList<IntValue> newRealization = new ItemList<IntValue>(); 136 while (countOnes < 4) { //only generate realizations with at least 4 cities visited 137 countOnes = 0; 138 newRealization.Clear(); 139 for (int j = 0; j < data.Dimension; j++) { 140 if (ProbabilityMatrix[j] > r.NextDouble()) { 141 newRealization.Add(new IntValue(1)); 142 countOnes++; 143 } else { 144 newRealization.Add(new IntValue(0)); 145 } 76 146 } 77 147 } 78 148 countOnes = 0; 149 realizations.Add(newRealization); 79 150 } 80 151 -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/HeuristicLab.Problems.PTSP-3.3.csproj
r12228 r12261 98 98 <Private>False</Private> 99 99 </Reference> 100 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 101 <SpecificVersion>False</SpecificVersion> 102 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 103 </Reference> 100 104 <Reference Include="System" /> 101 105 <Reference Include="System.Core" /> -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPExhaustiveInsertionLocalImprovement.cs
r12228 r12261 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 using System.Threading; 32 using HeuristicLab.Problems.PTSP.MoveEvaluators.OneShift;33 32 34 33 namespace HeuristicLab.Problems.PTSP { -
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 -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs
r12228 r12261 34 34 using HeuristicLab.Parameters; 35 35 using HeuristicLab.Data; 36 using HeuristicLab.Random; 36 37 37 38 namespace HeuristicLab.Problems.PTSP { … … 133 134 Description = data.Description; 134 135 136 // Get Probabilities of cities using random with seed from hash function on the Name of the instance 137 double[] tempMatrix = new double[data.Dimension]; 138 MersenneTwister r = new MersenneTwister((uint)data.Name.GetHashCode()); 139 for (int i = 0; i < data.Dimension; i++) { 140 tempMatrix[i] = r.NextDouble(); 141 } 142 ProbabilityMatrix = new DoubleArray(tempMatrix); 143 144 if (data.BestKnownTour != null) { 145 BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour); 146 } 147 135 148 bool clearCoordinates = false, clearDistanceMatrix = false; 136 149 if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0) … … 143 156 144 157 DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix()); 145 // Get Probabilities of cities using random with seed from hash function on the Name of the instance 146 ProbabilityMatrix = new DoubleArray(data.Dimension); 147 Random r = new Random(data.Name.GetHashCode()); 148 for (int i = 0; i < data.Dimension; i++) { 149 ProbabilityMatrix[i] = r.NextDouble(); 150 } 158 151 159 Encoding.Length = data.Dimension; 152 160 -
branches/PTSP/PTSP.sln
r12228 r12261 7 7 EndProject 8 8 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.PTSP.Views-3.3", "HeuristicLab.Problems.PTSP.Views\3.3\HeuristicLab.Problems.PTSP.Views-3.3.csproj", "{90B6CA12-9791-4430-B2D7-CD3ED7F75E2B}" 9 EndProject 10 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Problems.PTSP.Tests-3.3", "HeuristicLab.Problems.PTSP.Tests-3.3\HeuristicLab.Problems.PTSP.Tests-3.3.csproj", "{CBEC171A-F7EC-460D-94E2-D58625811D99}" 9 11 EndProject 10 12 Global … … 22 24 {90B6CA12-9791-4430-B2D7-CD3ED7F75E2B}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 25 {90B6CA12-9791-4430-B2D7-CD3ED7F75E2B}.Release|Any CPU.Build.0 = Release|Any CPU 26 {CBEC171A-F7EC-460D-94E2-D58625811D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 {CBEC171A-F7EC-460D-94E2-D58625811D99}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 {CBEC171A-F7EC-460D-94E2-D58625811D99}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 {CBEC171A-F7EC-460D-94E2-D58625811D99}.Release|Any CPU.Build.0 = Release|Any CPU 24 30 EndGlobalSection 25 31 GlobalSection(SolutionProperties) = preSolution
Note: See TracChangeset
for help on using the changeset viewer.