- Timestamp:
- 03/11/15 14:34:11 (10 years ago)
- Location:
- branches/PTSP/HeuristicLab.Problems.PTSP/3.3
- Files:
-
- 11 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/DistanceMatrix.cs
r12183 r12191 1 1 using System; 2 using System;3 2 using System.Collections.Generic; 4 3 using HeuristicLab.Common; -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/HeuristicLab.Problems.PTSP-3.3.csproj
r12183 r12191 99 99 <Reference Include="System" /> 100 100 <Reference Include="System.Core" /> 101 <Reference Include="System.Drawing" /> 101 102 <Reference Include="System.Xml.Linq" /> 102 103 <Reference Include="System.Data.DataSetExtensions" /> … … 108 109 <None Include="Properties\AssemblyInfo.cs.frame" /> 109 110 <None Include="Plugin.cs.frame" /> 111 <Compile Include="AnalyticalPTSP.cs" /> 110 112 <Compile Include="DistanceMatrix.cs" /> 113 <Compile Include="EstimatedPTSP.cs" /> 114 <Compile Include="Interfaces\IPTSPPathMoveEvaluator.cs" /> 115 <Compile Include="Interfaces\IPTSPMoveEvaluator.cs" /> 116 <Compile Include="MoveEvaluators\PTSPMoveEvaluator.cs" /> 117 <Compile Include="MoveEvaluators\PTSPPathMoveEvaluator.cs" /> 118 <Compile Include="MoveEvaluators\TwoOpt\PTSPInversionMovePathEvaluator.cs" /> 111 119 <Compile Include="PTSP.cs" /> 112 120 <Compile Include="Plugin.cs" /> … … 115 123 <ItemGroup> 116 124 <None Include="HeuristicLab.snk" /> 125 </ItemGroup> 126 <ItemGroup> 127 <Folder Include="MoveEvaluators\OneShift\" /> 117 128 </ItemGroup> 118 129 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> -
branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs
r12183 r12191 37 37 namespace HeuristicLab.Problems.PTSP { 38 38 [Item("Probabilistic Traveling Salesman Problem", "Represents a Probabilistic Traveling Salesman Problem.")] 39 [Creatable("Problems")]40 39 [StorableClass] 41 public sealedclass ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,40 public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent, 42 41 IProblemInstanceConsumer<TSPData> { 43 42 #region Parameter Properties … … 53 52 public OptionalValueParameter<Permutation> BestKnownSolutionParameter { 54 53 get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; } 55 }56 public ValueParameter<BoolValue> UseAnalyticalEvaluationParameter {57 get { return (ValueParameter<BoolValue>)Parameters["UseAnalyticalEvaluation"]; }58 54 } 59 55 public OptionalValueParameter<DistanceMatrix> ProbabilityMatrixParameter { … … 83 79 set { BestKnownSolutionParameter.Value = value; } 84 80 } 85 public BoolValue UseAnalyticalEvaluation {86 get { return UseAnalyticalEvaluationParameter.Value; }87 set { UseAnalyticalEvaluationParameter.Value = value; }88 }89 81 public DistanceMatrix ProbabilityMatrix { 90 82 get { return ProbabilityMatrixParameter.Value; } … … 96 88 } 97 89 #endregion 98 public override double Evaluate(Individual individual, IRandom random) { 99 Permutation p = individual.Permutation(); 100 if (UseAnalyticalEvaluation.Value) { 101 // Analytical evaluation 102 double firstSum = 0; 103 for (int i = 0; i < p.Length - 1; i++) { 104 for (int j = i + 1; j < p.Length - 1; j++) { 105 double sum1 = DistanceMatrix[p[i], p[j]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]]; 106 for (int k = i + 1; k < j; k++) { 107 sum1 = sum1 * (1 - ProbabilityMatrix[0, p[k]]); 108 } 109 firstSum += sum1; 110 } 111 } 112 double secondSum = 0; 113 for (int j = 0; j < p.Length - 1; j++) { 114 for (int i = 0; i < j; i++) { 115 double sum2 = DistanceMatrix[p[j], p[i]] * ProbabilityMatrix[0, p[i]] * ProbabilityMatrix[0, p[j]]; 116 for (int k = j + 1; k < p.Length - 1; k++) { 117 sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]); 118 } 119 for (int k = 1; k < i; k++) { 120 sum2 = sum2 * (1 - ProbabilityMatrix[0, p[k]]); 121 } 122 secondSum += sum2; 123 } 124 } 125 return firstSum+secondSum; 126 } else { 127 // Estimation-based evaluation 128 Random r = new Random(); 129 double estimatedSum = 0; 130 for (int i = 0; i < SampleSize.Value; i++) { 131 int singleRealization = -1; 132 for (int j = 0; j < p.Length - 1; j++) { 133 if (ProbabilityMatrix[0, p[j]] > r.NextDouble()) { 134 if (singleRealization != -1) { 135 estimatedSum += DistanceMatrix[singleRealization, p[j]]; 136 } 137 singleRealization = p[j]; 138 } 139 } 140 } 141 return estimatedSum / SampleSize.Value; 142 } 143 } 90 144 91 145 92 public override bool Maximization { … … 148 95 149 96 [StorableConstructor] 150 pr ivateProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }151 pr ivateProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)97 protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { } 98 protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner) 152 99 : base(original, cloner) { 153 100 } 154 public override IDeepCloneable Clone(Cloner cloner) { 155 return new ProbabilisticTravelingSalesmanProblem(this, cloner); 156 } 101 157 102 public ProbabilisticTravelingSalesmanProblem() { 158 103 Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.")); … … 160 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))); 161 106 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance.")); 162 Parameters.Add(new ValueParameter<BoolValue>("UseAnalyticalEvaluation", "Check to use analytical evaluation, uncheck to use estimation-based approach."));163 107 Parameters.Add(new OptionalValueParameter<DistanceMatrix>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities.")); 164 Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Size of the sample for the estimation-based evaluation"));108 165 109 } 166 110 167 public void Load(TSPData data) { 168 SampleSize = new IntValue(20); 111 public virtual void Load(TSPData data) { 169 112 DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix()); 170 113 // Get Probabilities of cities using random with seed from hash function on the Name of the instance
Note: See TracChangeset
for help on using the changeset viewer.