- Timestamp:
- 03/18/15 12:01:21 (10 years ago)
- Location:
- branches/PTSP/HeuristicLab.Problems.PTSP/3.3
- Files:
-
- 3 added
- 1 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/AnalyticalPTSP.cs
r12191 r12219 33 33 public override double Evaluate(Individual individual, IRandom random) { 34 34 Permutation p = individual.Permutation(); 35 // Compute A and B matrices 36 DoubleMatrix A = new DoubleMatrix(p.Length, p.Length - 1); 37 DoubleMatrix B = new DoubleMatrix(p.Length, p.Length - 1); 38 //for (int i = 0; i < p.Length; i++) { 39 // for (int k = 0; k < p.Length - 1; k++) { 40 // double firstPartial = 0; 41 // for (int r = k; k < p.Length - 1; r++) { 42 // double sum0 = DistanceMatrix[p[i],p[i+r]]* ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[i+r]]; 43 // for(int s = i+1; s < i+r; s++) { 44 // sum0 = sum0 * (1 - ProbabilityMatrix[0, p[s]]); 45 // } 46 // firstPartial+=sum0; 47 // } 48 // double secondPartial = 0; 49 // for (int r = k; k < p.Length - 1; r++) { 50 // double sum1 = DistanceMatrix[p[i-r],p[i]]* ProbabilityMatrix[0, p[i-r]] * ProbabilityMatrix[0, p[i]]; 51 // for (int s = i + 1; s < p.Length-1; s++) { 52 // sum1 = sum1 * (1 - ProbabilityMatrix[0, p[s]]); 53 // } 54 // for (int t = 1; t < i-1; t++) { 55 // sum1 = sum1 * (1 - ProbabilityMatrix[0, p[t]]); 56 // } 57 // secondPartial+=sum1; 58 // } 59 // A[i, k] = firstPartial; 60 // B[i, k] = secondPartial; 61 // } 62 //} 63 35 64 // Analytical evaluation 36 65 double firstSum = 0; 37 66 for (int i = 0; i < p.Length - 1; i++) { 38 67 for (int j = i + 1; j < p.Length - 1; j++) { 39 double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[ 0, p[i]] * ProbabilityMatrix[0, p[j]];68 double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[p[i]].Value * ProbabilityMatrix[p[j]].Value; 40 69 for (int k = i + 1; k < j; k++) { 41 sum1 = sum1 * (1 - ProbabilityMatrix[ 0, p[k]]);70 sum1 = sum1 * (1 - ProbabilityMatrix[p[k]].Value); 42 71 } 72 A[i, j - 1] = sum1; 43 73 firstSum += sum1; 44 74 } … … 47 77 for (int j = 0; j < p.Length - 1; j++) { 48 78 for (int i = 0; i < j; i++) { 49 double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[ 0, p[i]] * ProbabilityMatrix[0, p[j]];79 double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[p[i]].Value * ProbabilityMatrix[p[j]].Value; 50 80 for (int k = j + 1; k < p.Length - 1; k++) { 51 sum2 = sum2 * (1 - ProbabilityMatrix[ 0, p[k]]);81 sum2 = sum2 * (1 - ProbabilityMatrix[p[k]].Value); 52 82 } 53 83 for (int k = 1; k < i; k++) { 54 sum2 = sum2 * (1 - ProbabilityMatrix[ 0, p[k]]);84 sum2 = sum2 * (1 - ProbabilityMatrix[p[k]].Value); 55 85 } 86 B[i, j] = sum2; 56 87 secondSum += sum2; 57 88 } 89 } 90 foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) { 91 op.AParameter.Value = A; 92 op.BParameter.Value = B; 58 93 } 59 94 return firstSum + secondSum; … … 61 96 62 97 public AnalyticalProbabilisticTravelingSalesmanProblem() { 63 Operators.Add(new PTSP InversionMovePathEvaluator());98 Operators.Add(new PTSPAnalyticalInversionMovePathEvaluator()); 64 99 } 65 100 66 101 public override void Load(TSPData data) { 67 102 base.Load(data); 68 // Compute A and B matrices 103 foreach (var op in Operators.OfType<PTSPAnalyticalInversionMovePathEvaluator>()) { 104 op.ProbabilitiesParameter.Value = ProbabilityMatrix; 105 } 69 106 } 70 107 -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/EstimatedPTSP.cs
r12191 r12219 11 11 using HeuristicLab.Data; 12 12 using System; 13 using System.Linq; 14 using HeuristicLab.Problems.PTSP.MoveEvaluators.OneShift; 13 15 14 16 namespace HeuristicLab.Problems.PTSP { … … 18 20 public sealed class EstimatedProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem, IStorableContent, 19 21 IProblemInstanceConsumer<TSPData> { 22 23 private ItemList<ItemList<IntValue>> realizations; 20 24 21 25 [StorableConstructor] … … 34 38 Random r = new Random(); 35 39 double estimatedSum = 0; 36 for (int i = 0; i < SampleSize.Value; i++) {40 for (int i = 0; i < realizations.Count; i++) { 37 41 int singleRealization = -1; 38 for (int j = 0; j < p.Length - 1; j++) {39 if ( ProbabilityMatrix[0, p[j]] > r.NextDouble()) {42 for (int j = 0; j < realizations[i].Count; j++) { 43 if (realizations[i][j].Value == 1) { 40 44 if (singleRealization != -1) { 41 45 estimatedSum += DistanceMatrix[singleRealization, p[j]]; … … 50 54 public EstimatedProbabilisticTravelingSalesmanProblem() { 51 55 Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Size of the sample for the estimation-based evaluation")); 52 Operators.Add(new PTSPInversionMovePathEvaluator()); 56 Operators.Add(new PTSPEstimatedInversionMovePathEvaluator()); 57 Operators.Add(new PTSPEstimatedInsertionEvaluator()); 58 Encoding.ConfigureOperators(Operators.OfType<IOperator>()); 53 59 } 54 60 … … 57 63 // For now uses sample size of 20 but should use Student's t-test 58 64 SampleSize = new IntValue(20); 65 realizations = new ItemList<ItemList<IntValue>>(SampleSize.Value); 66 Random r = new Random(); 67 for (int i = 0; i < SampleSize.Value; i++) { 68 realizations.Add(new ItemList<IntValue>()); 69 for (int j = 0; j < data.Dimension; j++) { 70 if (ProbabilityMatrix[j].Value > r.NextDouble()) { 71 realizations.ElementAt(i).Add(new IntValue(1)); 72 } else { 73 realizations.ElementAt(i).Add(new IntValue(0)); 74 } 75 } 76 77 } 78 foreach (var op in Operators.OfType<PTSPPathMoveEvaluator>()) { 79 op.RealizationsParameter.Value = realizations; 80 } 59 81 } 60 82 } -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/HeuristicLab.Problems.PTSP-3.3.csproj
r12191 r12219 114 114 <Compile Include="Interfaces\IPTSPPathMoveEvaluator.cs" /> 115 115 <Compile Include="Interfaces\IPTSPMoveEvaluator.cs" /> 116 <Compile Include="MoveEvaluators\OneShift\PTSPEstimatedInsertionEvaluator.cs" /> 116 117 <Compile Include="MoveEvaluators\PTSPMoveEvaluator.cs" /> 117 118 <Compile Include="MoveEvaluators\PTSPPathMoveEvaluator.cs" /> 118 <Compile Include="MoveEvaluators\TwoOpt\PTSPInversionMovePathEvaluator.cs" /> 119 <Compile Include="MoveEvaluators\TwoOpt\PTSPAnalyticalInversionMovePathEvaluator.cs" /> 120 <Compile Include="MoveEvaluators\TwoOpt\PTSPEstimatedInversionMovePathEvaluator.cs" /> 119 121 <Compile Include="PTSP.cs" /> 120 122 <Compile Include="Plugin.cs" /> … … 124 126 <None Include="HeuristicLab.snk" /> 125 127 </ItemGroup> 126 <ItemGroup> 127 <Folder Include="MoveEvaluators\OneShift\" /> 128 </ItemGroup> 128 <ItemGroup /> 129 129 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 130 130 <PropertyGroup> -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/Interfaces/IPTSPPathMoveEvaluator.cs
r12191 r12219 29 29 ILookupParameter<DistanceMatrix> DistanceMatrixParameter { get; } 30 30 ILookupParameter<BoolValue> UseDistanceMatrixParameter { get; } 31 31 32 } 32 33 } -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/MoveEvaluators/PTSPPathMoveEvaluator.cs
r12191 r12219 47 47 get { return (ILookupParameter<BoolValue>)Parameters["UseDistanceMatrix"]; } 48 48 } 49 public IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter { 50 get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; } 51 } 49 52 50 53 [StorableConstructor] … … 57 60 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 58 61 Parameters.Add(new LookupParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated (if it does not exist already) and used for evaluation, otherwise false.")); 62 Parameters.Add(new ValueParameter<ItemList<ItemList<IntValue>>>("Realizations", "The concrete...")); 59 63 } 60 64 -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs
r12191 r12219 53 53 get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; } 54 54 } 55 public OptionalValueParameter< DistanceMatrix> ProbabilityMatrixParameter {56 get { return (OptionalValueParameter< DistanceMatrix>)Parameters["ProbabilityMatrix"]; }55 public OptionalValueParameter<ItemList<DoubleValue>> ProbabilityMatrixParameter { 56 get { return (OptionalValueParameter<ItemList<DoubleValue>>)Parameters["ProbabilityMatrix"]; } 57 57 } 58 58 public ValueParameter<IntValue> SampleSizeParameter { … … 79 79 set { BestKnownSolutionParameter.Value = value; } 80 80 } 81 public DistanceMatrixProbabilityMatrix {81 public ItemList<DoubleValue> ProbabilityMatrix { 82 82 get { return ProbabilityMatrixParameter.Value; } 83 83 set { ProbabilityMatrixParameter.Value = value; } … … 105 105 Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true))); 106 106 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance.")); 107 Parameters.Add(new OptionalValueParameter< DistanceMatrix>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));107 Parameters.Add(new OptionalValueParameter<ItemList<DoubleValue>>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities.")); 108 108 109 109 } … … 112 112 DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix()); 113 113 // Get Probabilities of cities using random with seed from hash function on the Name of the instance 114 ProbabilityMatrix = new DistanceMatrix(1,data.Dimension);114 ProbabilityMatrix = new ItemList<DoubleValue>(data.Dimension); 115 115 Random r = new Random(data.Name.GetHashCode()); 116 116 for (int i = 0; i < data.Dimension; i++) { 117 ProbabilityMatrix [0,i] = r.NextDouble();117 ProbabilityMatrix.Add(new DoubleValue(r.NextDouble())); 118 118 } 119 119 Encoding.Length = data.Dimension;
Note: See TracChangeset
for help on using the changeset viewer.