Changeset 14604 for branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief
- Timestamp:
- 01/25/17 14:30:18 (8 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3
- Files:
-
- 1 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/HeuristicLab.Networks.IntegratedOptimization.TravelingThief-3.3.csproj
r14601 r14604 171 171 <Compile Include="Problems\LootProfitProblem.cs" /> 172 172 <Compile Include="Problems\TourProfitProblem.cs" /> 173 <Compile Include="TtpImporter.cs" />174 173 <Compile Include="TtpNetwork1.cs" /> 175 174 <Compile Include="TtpNetwork2.cs" /> -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Plugin.cs.frame
r14601 r14604 28 28 [Plugin("HeuristicLab.Networks.IntegratedOptimization.TravelingThief", "3.3.14.$WCREV$")] 29 29 [PluginFile("HeuristicLab.Networks.IntegratedOptimization.TravelingThief-3.3.dll", PluginFileType.Assembly)] 30 [PluginDependency("HeuristicLab.Algorithms.CMAEvolutionStrategy", "3.4")] 31 [PluginDependency("HeuristicLab.Algorithms.LocalSearch", "3.3")] 32 [PluginDependency("HeuristicLab.Algorithms.ParameterlessPopulationPyramid", "3.3")] 33 [PluginDependency("HeuristicLab.Collections", "3.3")] 34 [PluginDependency("HeuristicLab.Common", "3.3")] 35 [PluginDependency("HeuristicLab.Core", "3.3")] 36 [PluginDependency("HeuristicLab.Data", "3.3")] 37 [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")] 38 [PluginDependency("HeuristicLab.Encodings.PermutationEncoding", "3.3")] 39 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 40 [PluginDependency("HeuristicLab.Networks", "3.3")] 41 [PluginDependency("HeuristicLab.Networks.IntegratedOptimization", "3.3")] 42 [PluginDependency("HeuristicLab.Optimization", "3.3")] 43 [PluginDependency("HeuristicLab.Parameters", "3.3")] 44 [PluginDependency("HeuristicLab.Persistence", "3.3")] 45 [PluginDependency("HeuristicLab.Problems.Binary", "3.3")] 46 [PluginDependency("HeuristicLab.Problems.Knapsack", "3.3")] 47 [PluginDependency("HeuristicLab.Problems.TravelingSalesman", "3.3")] 30 48 public class HeuristicLabNetworksIntegratedOptimizationTravelingThiefPlugin : PluginBase { 31 49 } -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/LootProfitProblem.cs
r14601 r14604 54 54 [Storable] 55 55 public double MaxSpeed { get; set; } 56 [Storable] 57 public TtpUtils.DistanceType DistanceType { get; set; } 56 58 57 59 [StorableConstructor] … … 65 67 MinSpeed = original.MinSpeed; 66 68 MaxSpeed = original.MaxSpeed; 69 DistanceType = original.DistanceType; 67 70 } 68 71 public LootProfitProblem() : base() { … … 75 78 76 79 public override double Evaluate(BinaryVector vector, IRandom random) { 77 return TtpUtils.Evaluate Ttp(Tsp, FixedTspSolution.ToArray(),80 return TtpUtils.Evaluate(Tsp, FixedTspSolution.ToArray(), 78 81 Ksp, vector.ToArray(), 79 Availability, RentingRatio, MinSpeed, MaxSpeed );82 Availability, RentingRatio, MinSpeed, MaxSpeed, DistanceType); 80 83 } 81 84 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Problems/TourProfitProblem.cs
r14601 r14604 53 53 [Storable] 54 54 public double MaxSpeed { get; set; } 55 [Storable] 56 public TtpUtils.DistanceType DistanceType { get; set; } 55 57 56 58 [StorableConstructor] … … 64 66 MinSpeed = original.MinSpeed; 65 67 MaxSpeed = original.MaxSpeed; 68 DistanceType = original.DistanceType; 66 69 } 67 70 public TourProfitProblem() : base() { … … 74 77 75 78 public override double Evaluate(Individual individual, IRandom random) { 76 return TtpUtils.Evaluate Ttp(Tsp, individual.Permutation().ToArray(),79 return TtpUtils.Evaluate(Tsp, individual.Permutation().ToArray(), 77 80 Ksp, FixedKspSolution.ToArray(), 78 Availability, RentingRatio, MinSpeed, MaxSpeed );81 Availability, RentingRatio, MinSpeed, MaxSpeed, DistanceType); 79 82 } 80 83 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/Properties/AssemblyInfo.cs.frame
r14601 r14604 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork1.cs
r14601 r14604 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Core.Networks; 30 using HeuristicLab.Data;31 30 using HeuristicLab.Optimization; 32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 99 98 orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort; 100 99 Nodes.Add(kspSolverNode); 101 102 #region Import103 DoubleMatrix tspCoordinates;104 IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues;105 IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio;106 TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp",107 out tspCoordinates,108 out kspCapacity, out kspItemValues, out kspItemWeights,109 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);110 111 var tsp = orchestratorNode.TspParameter.Value;112 tsp.Coordinates = tspCoordinates;113 114 var ksp = orchestratorNode.KspParameter.Value;115 ksp.KnapsackCapacity = kspCapacity;116 ksp.Encoding.Length = kspItemValues.Length;117 ksp.Values = kspItemValues;118 ksp.Weights = kspItemWeights;119 120 orchestratorNode.AvailabilityParameter.Value = ttpAvailability;121 orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed;122 orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed;123 orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio;124 #endregion125 100 126 101 RegisterEvents(); -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork2.cs
r14601 r14604 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Core.Networks; 30 using HeuristicLab.Data;31 30 using HeuristicLab.Optimization; 32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 99 98 orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort; 100 99 Nodes.Add(kspSolverNode); 101 102 #region Import103 DoubleMatrix tspCoordinates;104 IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues;105 IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio;106 TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp",107 out tspCoordinates,108 out kspCapacity, out kspItemValues, out kspItemWeights,109 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);110 111 var tsp = orchestratorNode.TspParameter.Value;112 tsp.Coordinates = tspCoordinates;113 114 var ksp = orchestratorNode.KspParameter.Value;115 ksp.KnapsackCapacity = kspCapacity;116 ksp.Encoding.Length = kspItemValues.Length;117 ksp.Values = kspItemValues;118 ksp.Weights = kspItemWeights;119 120 orchestratorNode.AvailabilityParameter.Value = ttpAvailability;121 orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed;122 orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed;123 orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio;124 #endregion125 100 126 101 RegisterEvents(); -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpNetwork3.cs
r14601 r14604 28 28 using HeuristicLab.Core; 29 29 using HeuristicLab.Core.Networks; 30 using HeuristicLab.Data;31 30 using HeuristicLab.Optimization; 32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 99 98 orchestratorNode.KspSolverOrchestrationPort.ConnectedPort = kspSolverNode.OrchestrationPort; 100 99 Nodes.Add(kspSolverNode); 101 102 #region Import103 DoubleMatrix tspCoordinates;104 IntValue kspCapacity; IntArray kspItemWeights; IntArray kspItemValues;105 IntArray ttpAvailability; DoubleValue ttpMinSpeed; DoubleValue ttpMaxSpeed; DoubleValue ttpRentingRatio;106 TtpImporter.ImportTtpInstance(@"ttp-instances\berlin52-ttp\berlin52_n51_uncorr_01.ttp",107 out tspCoordinates,108 out kspCapacity, out kspItemValues, out kspItemWeights,109 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio);110 111 var tsp = orchestratorNode.TspParameter.Value;112 tsp.Coordinates = tspCoordinates;113 114 var ksp = orchestratorNode.KspParameter.Value;115 ksp.KnapsackCapacity = kspCapacity;116 ksp.Encoding.Length = kspItemValues.Length;117 ksp.Values = kspItemValues;118 ksp.Weights = kspItemWeights;119 120 orchestratorNode.AvailabilityParameter.Value = ttpAvailability;121 orchestratorNode.MinSpeedParameter.Value = ttpMinSpeed;122 orchestratorNode.MaxSpeedParameter.Value = ttpMaxSpeed;123 orchestratorNode.RentingRatioParameter.Value = ttpRentingRatio;124 #endregion125 100 126 101 RegisterEvents(); -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode1.cs
r14601 r14604 43 43 public sealed class TtpOrchestratorNode1 : OrchestratorNode { 44 44 #region Constants 45 private const string InstanceParameterName = "Instance"; 45 46 private const string TspParameterName = "TSP"; 46 47 private const string KspParameterName = "KSP"; … … 49 50 private const string MaxSpeedParameterName = "MaxSpeed"; 50 51 private const string RentingRatioParameterName = "RentingRatio"; 51 private const string IterationsParameterName = "Iterations";52 52 private const string MetaSolverName = "MetaSolver"; 53 53 private const string TspSolverName = "TspSolver"; … … 55 55 #endregion 56 56 57 private CancellationTokenSource cts; 57 58 private ResultCollection tspResults, kspResults; 58 59 59 private CancellationTokenSource cts; 60 [Storable] 61 double[,] tspCoordinates; 62 [Storable] 63 TtpUtils.DistanceType distanceType; 64 [Storable] 65 int kspCapacity; 66 [Storable] 67 int[] kspItemWeights, kspItemValues, ttpAvailability; 68 [Storable] 69 double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio; 60 70 61 71 #region Parameters 62 public I ValueParameter<IntValue> IterationsParameter {63 get { return (I ValueParameter<IntValue>)Parameters[IterationsParameterName]; }72 public IFixedValueParameter<TextFileValue> InstanceParameter { 73 get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; } 64 74 } 65 75 … … 118 128 private TtpOrchestratorNode1(bool deserializing) : base(deserializing) { } 119 129 private TtpOrchestratorNode1(TtpOrchestratorNode1 original, Cloner cloner) : base(original, cloner) { 130 tspCoordinates = (double[,])original.tspCoordinates.Clone(); 131 kspCapacity = original.kspCapacity; 132 kspItemWeights = (int[])original.kspItemWeights.Clone(); 133 kspItemValues = (int[])original.kspItemValues.Clone(); 134 ttpAvailability = (int[])original.ttpAvailability.Clone(); 135 ttpMinSpeed = original.ttpMinSpeed; 136 ttpMaxSpeed = original.ttpMaxSpeed; 137 ttpRentingRatio = original.ttpRentingRatio; 138 120 139 RegisterEvents(); 121 140 } … … 123 142 public TtpOrchestratorNode1(string name) : base(name) { 124 143 #region Configure Parameters 125 Parameters.Add(new ValueParameter<IntValue>(IterationsParameterName, new IntValue(20)));144 Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName)); 126 145 Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem())); 127 146 Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem())); … … 154 173 155 174 private void RegisterEvents() { 175 InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged; 156 176 MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged; 157 177 TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged; 158 178 KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged; 179 } 180 181 private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) { 182 string filePath = InstanceParameter.Value.Value; 183 TtpUtils.Import(filePath, out tspCoordinates, out distanceType, 184 out kspCapacity, out kspItemValues, out kspItemWeights, 185 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio); 186 187 var tsp = TspParameter.Value; 188 tsp.Coordinates = new DoubleMatrix(tspCoordinates); 189 190 var ksp = KspParameter.Value; 191 ksp.KnapsackCapacity.Value = kspCapacity; 192 ksp.Encoding.Length = kspItemValues.Length; 193 ksp.Values = new IntArray(kspItemValues); 194 ksp.Weights = new IntArray(kspItemWeights); 195 196 AvailabilityParameter.Value = new IntArray(ttpAvailability); 197 MinSpeedParameter.Value.Value = ttpMinSpeed; 198 MaxSpeedParameter.Value.Value = ttpMaxSpeed; 199 RentingRatioParameter.Value.Value = ttpRentingRatio; 159 200 } 160 201 … … 234 275 235 276 var ksp = (BinaryKnapsackProblem)KspParameter.Value.Clone(); 236 for (int i = 0; i < factors.Length; i++)277 for (int i = 0; i < ksp.Values.Length; i++) 237 278 ksp.Values[i] = (int)Math.Ceiling(ksp.Values[i] * factors[i]); 238 279 … … 243 284 cts.Token.ThrowIfCancellationRequested(); 244 285 245 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value ;286 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone(); 246 287 var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone(); 247 288 var kspPenalty = new DoubleValue(0.0); … … 260 301 RentingRatio = RentingRatioParameter.Value.Value, 261 302 MinSpeed = MinSpeedParameter.Value.Value, 262 MaxSpeed = MaxSpeedParameter.Value.Value 303 MaxSpeed = MaxSpeedParameter.Value.Value, 304 DistanceType = distanceType 263 305 }; 264 306 tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows; … … 267 309 cts.Token.ThrowIfCancellationRequested(); 268 310 269 var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value ;311 var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value.Clone(); 270 312 var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone(); 271 313 var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution))); 272 314 273 315 #region Analyze 274 double objectiveValue = TtpUtils.Evaluate Ttp(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),275 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value );316 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 317 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType); 276 318 ((DoubleValue)message["Quality"]).Value = objectiveValue; 277 319 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode2.cs
r14601 r14604 43 43 public sealed class TtpOrchestratorNode2 : OrchestratorNode { 44 44 #region Constants 45 private const string InstanceParameterName = "Instance"; 45 46 private const string TspParameterName = "TSP"; 46 47 private const string KspParameterName = "KSP"; … … 49 50 private const string MaxSpeedParameterName = "MaxSpeed"; 50 51 private const string RentingRatioParameterName = "RentingRatio"; 51 private const string IterationsParameterName = "Iterations";52 52 private const string MetaSolverName = "MetaSolver"; 53 53 private const string TspSolverName = "TspSolver"; … … 55 55 #endregion 56 56 57 private CancellationTokenSource cts; 57 58 private ResultCollection tspResults, kspResults; 58 59 59 private CancellationTokenSource cts; 60 [Storable] 61 double[,] tspCoordinates; 62 [Storable] 63 TtpUtils.DistanceType distanceType; 64 [Storable] 65 int kspCapacity; 66 [Storable] 67 int[] kspItemWeights, kspItemValues, ttpAvailability; 68 [Storable] 69 double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio; 60 70 61 71 #region Parameters 62 public I ValueParameter<IntValue> IterationsParameter {63 get { return (I ValueParameter<IntValue>)Parameters[IterationsParameterName]; }72 public IFixedValueParameter<TextFileValue> InstanceParameter { 73 get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; } 64 74 } 65 75 … … 118 128 private TtpOrchestratorNode2(bool deserializing) : base(deserializing) { } 119 129 private TtpOrchestratorNode2(TtpOrchestratorNode2 original, Cloner cloner) : base(original, cloner) { 130 tspCoordinates = (double[,])original.tspCoordinates.Clone(); 131 kspCapacity = original.kspCapacity; 132 kspItemWeights = (int[])original.kspItemWeights.Clone(); 133 kspItemValues = (int[])original.kspItemValues.Clone(); 134 ttpAvailability = (int[])original.ttpAvailability.Clone(); 135 ttpMinSpeed = original.ttpMinSpeed; 136 ttpMaxSpeed = original.ttpMaxSpeed; 137 ttpRentingRatio = original.ttpRentingRatio; 138 120 139 RegisterEvents(); 121 140 } … … 123 142 public TtpOrchestratorNode2(string name) : base(name) { 124 143 #region Configure Parameters 125 Parameters.Add(new ValueParameter<IntValue>(IterationsParameterName, new IntValue(20)));144 Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName)); 126 145 Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem())); 127 146 Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem())); … … 137 156 AddOrchestrationPort<TravelingSalesmanProblem>(TspSolverName); 138 157 AddEvaluationPort<Permutation>(TspSolverName, "TSPTour", "TSPTourLength"); 139 AddOrchestrationPort< lootpro>(KspSolverName);158 AddOrchestrationPort<LootProfitProblem>(KspSolverName); 140 159 AddEvaluationPort<BinaryVector>(KspSolverName, "KnapsackSolution", "Quality"); 141 160 … … 154 173 155 174 private void RegisterEvents() { 175 InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged; 156 176 MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged; 157 177 TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged; 158 178 KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged; 179 } 180 181 private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) { 182 string filePath = InstanceParameter.Value.Value; 183 TtpUtils.Import(filePath, out tspCoordinates, out distanceType, 184 out kspCapacity, out kspItemValues, out kspItemWeights, 185 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio); 186 187 var tsp = TspParameter.Value; 188 tsp.Coordinates = new DoubleMatrix(tspCoordinates); 189 190 var ksp = KspParameter.Value; 191 ksp.KnapsackCapacity.Value = kspCapacity; 192 ksp.Encoding.Length = kspItemValues.Length; 193 ksp.Values = new IntArray(kspItemValues); 194 ksp.Weights = new IntArray(kspItemWeights); 195 196 AvailabilityParameter.Value = new IntArray(ttpAvailability); 197 MinSpeedParameter.Value.Value = ttpMinSpeed; 198 MaxSpeedParameter.Value.Value = ttpMaxSpeed; 199 RentingRatioParameter.Value.Value = ttpRentingRatio; 159 200 } 160 201 … … 234 275 235 276 var tsp = (TravelingSalesmanProblem)TspParameter.Value.Clone(); 236 for (int i = 0; i < factors.Length / 2; i++) {277 for (int i = 0; i < tsp.Coordinates.Rows; i++) { 237 278 tsp.Coordinates[i, 0] = (int)Math.Ceiling(tsp.Coordinates[i, 0] * factors[i * 2]); 238 279 tsp.Coordinates[i, 1] = (int)Math.Ceiling(tsp.Coordinates[i, 1] * factors[i * 2 + 1]); … … 245 286 cts.Token.ThrowIfCancellationRequested(); 246 287 247 var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value ;288 var bestTspSolution = (PathTSPTour)tspResults["Best TSP Solution"].Value.Clone(); 248 289 var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone(); 249 290 var tour = new PathTSPTour(coordinates, bestTspSolution.Permutation, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution.Permutation))); … … 258 299 RentingRatio = RentingRatioParameter.Value.Value, 259 300 MinSpeed = MinSpeedParameter.Value.Value, 260 MaxSpeed = MaxSpeedParameter.Value.Value 301 MaxSpeed = MaxSpeedParameter.Value.Value, 302 DistanceType = distanceType 261 303 }; 262 304 lpp.Encoding.Length = KspParameter.Value.Length; … … 265 307 cts.Token.ThrowIfCancellationRequested(); 266 308 267 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value ;309 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone(); 268 310 var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone(); 269 311 var kspPenalty = new DoubleValue(0.0); … … 274 316 275 317 #region Analyze 276 double objectiveValue = TtpUtils.Evaluate Ttp(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),277 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value );318 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 319 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType); 278 320 ((DoubleValue)message["Quality"]).Value = objectiveValue; 279 321 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpOrchestratorNode3.cs
r14601 r14604 43 43 public sealed class TtpOrchestratorNode3 : OrchestratorNode { 44 44 #region Constants 45 private const string InstanceParameterName = "Instance"; 45 46 private const string TspParameterName = "TSP"; 46 47 private const string KspParameterName = "KSP"; … … 49 50 private const string MaxSpeedParameterName = "MaxSpeed"; 50 51 private const string RentingRatioParameterName = "RentingRatio"; 51 private const string IterationsParameterName = "Iterations";52 52 private const string MetaSolverName = "MetaSolver"; 53 53 private const string TspSolverName = "TspSolver"; … … 55 55 #endregion 56 56 57 private CancellationTokenSource cts; 57 58 private ResultCollection tspResults, kspResults; 58 59 59 private CancellationTokenSource cts; 60 [Storable] 61 double[,] tspCoordinates; 62 [Storable] 63 TtpUtils.DistanceType distanceType; 64 [Storable] 65 int kspCapacity; 66 [Storable] 67 int[] kspItemWeights, kspItemValues, ttpAvailability; 68 [Storable] 69 double ttpMinSpeed, ttpMaxSpeed, ttpRentingRatio; 60 70 61 71 #region Parameters 62 public I ValueParameter<IntValue> IterationsParameter {63 get { return (I ValueParameter<IntValue>)Parameters[IterationsParameterName]; }72 public IFixedValueParameter<TextFileValue> InstanceParameter { 73 get { return (IFixedValueParameter<TextFileValue>)Parameters[InstanceParameterName]; } 64 74 } 65 75 … … 118 128 private TtpOrchestratorNode3(bool deserializing) : base(deserializing) { } 119 129 private TtpOrchestratorNode3(TtpOrchestratorNode3 original, Cloner cloner) : base(original, cloner) { 130 tspCoordinates = (double[,])original.tspCoordinates.Clone(); 131 kspCapacity = original.kspCapacity; 132 kspItemWeights = (int[])original.kspItemWeights.Clone(); 133 kspItemValues = (int[])original.kspItemValues.Clone(); 134 ttpAvailability = (int[])original.ttpAvailability.Clone(); 135 ttpMinSpeed = original.ttpMinSpeed; 136 ttpMaxSpeed = original.ttpMaxSpeed; 137 ttpRentingRatio = original.ttpRentingRatio; 138 120 139 RegisterEvents(); 121 140 } … … 123 142 public TtpOrchestratorNode3(string name) : base(name) { 124 143 #region Configure Parameters 125 Parameters.Add(new ValueParameter<IntValue>(IterationsParameterName, new IntValue(20)));144 Parameters.Add(new FixedValueParameter<TextFileValue>(InstanceParameterName)); 126 145 Parameters.Add(new ValueParameter<TravelingSalesmanProblem>(TspParameterName, new TravelingSalesmanProblem())); 127 146 Parameters.Add(new ValueParameter<BinaryKnapsackProblem>(KspParameterName, new BinaryKnapsackProblem())); … … 154 173 155 174 private void RegisterEvents() { 175 InstanceParameter.Value.ToStringChanged += InstanceParameter_Value_ToStringChanged; 156 176 MetaSolverOrchestrationPort.ConnectedPortChanged += MetaSolverOrchestrationPort_ConnectedPortChanged; 157 177 TspSolverOrchestrationPort.ConnectedPortChanged += TspSolverOrchestrationPort_ConnectedPortChanged; 158 178 KspSolverOrchestrationPort.ConnectedPortChanged += KspSolverOrchestrationPort_ConnectedPortChanged; 179 } 180 181 private void InstanceParameter_Value_ToStringChanged(object sender, EventArgs e) { 182 string filePath = InstanceParameter.Value.Value; 183 TtpUtils.Import(filePath, out tspCoordinates, out distanceType, 184 out kspCapacity, out kspItemValues, out kspItemWeights, 185 out ttpAvailability, out ttpMinSpeed, out ttpMaxSpeed, out ttpRentingRatio); 186 187 var tsp = TspParameter.Value; 188 tsp.Coordinates = new DoubleMatrix(tspCoordinates); 189 190 var ksp = KspParameter.Value; 191 ksp.KnapsackCapacity.Value = kspCapacity; 192 ksp.Encoding.Length = kspItemValues.Length; 193 ksp.Values = new IntArray(kspItemValues); 194 ksp.Weights = new IntArray(kspItemWeights); 195 196 AvailabilityParameter.Value = new IntArray(ttpAvailability); 197 MinSpeedParameter.Value.Value = ttpMinSpeed; 198 MaxSpeedParameter.Value.Value = ttpMaxSpeed; 199 RentingRatioParameter.Value.Value = ttpRentingRatio; 159 200 } 160 201 … … 246 287 cts.Token.ThrowIfCancellationRequested(); 247 288 248 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value ;289 var bestKspSolution = (BinaryVector)kspResults["Best Solution"].Value.Clone(); 249 290 var kspCapacity = (IntValue)KspParameter.Value.KnapsackCapacity.Clone(); 250 291 var kspPenalty = new DoubleValue(0.0); … … 269 310 RentingRatio = RentingRatioParameter.Value.Value, 270 311 MinSpeed = MinSpeedParameter.Value.Value, 271 MaxSpeed = MaxSpeedParameter.Value.Value 312 MaxSpeed = MaxSpeedParameter.Value.Value, 313 DistanceType = distanceType 272 314 }; 273 315 tpp.Encoding.Length = TspParameter.Value.Coordinates.Rows; … … 276 318 cts.Token.ThrowIfCancellationRequested(); 277 319 278 var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value ;320 var bestTspSolution = (Permutation)tspResults["Best TSP Solution"].Value.Clone(); 279 321 var coordinates = (DoubleMatrix)TspParameter.Value.Coordinates.Clone(); 280 322 var tour = new PathTSPTour(coordinates, bestTspSolution, new DoubleValue(TSPCoordinatesPathEvaluator.Apply(new TSPEuclideanPathEvaluator(), coordinates, bestTspSolution))); 281 323 282 324 #region Analyze 283 double objectiveValue = TtpUtils.Evaluate Ttp(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(),284 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value );325 double objectiveValue = TtpUtils.Evaluate(TspParameter.Value, tour.Permutation.ToArray(), KspParameter.Value, loot.BinaryVector.ToArray(), 326 AvailabilityParameter.Value.ToArray(), RentingRatioParameter.Value.Value, MinSpeedParameter.Value.Value, MaxSpeedParameter.Value.Value, distanceType); 285 327 ((DoubleValue)message["Quality"]).Value = objectiveValue; 286 328 -
branches/OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.TravelingThief/3.3/TtpUtils.cs
r14601 r14604 1 1 using System; 2 using System.IO; 2 3 using System.Linq; 3 4 using HeuristicLab.Problems.TravelingSalesman; … … 5 6 namespace HeuristicLab.Networks.IntegratedOptimization.TravelingThief { 6 7 public static class TtpUtils { 7 public static double EvaluateTtp(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed) { 8 bool feasible; 9 return EvaluateTtp(tsp, tour, ksp, loot, availability, rentingRatio, minSpeed, maxSpeed, out feasible); 8 public enum DistanceType { 9 Unknown = int.MinValue, 10 CEIL_2D = 0, 11 EUC_2D = 1, 10 12 } 11 13 12 public static double EvaluateTtp(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, out bool feasible) { 14 public static void Import(string filePath, out double[,] tspCoordinates, 15 out DistanceType distanceType, 16 out int kspCapacity, 17 out int[] kspItemValues, 18 out int[] kspItemWeights, 19 out int[] ttpAvailability, 20 out double ttpMinSpeed, 21 out double ttpMaxSpeed, 22 out double ttpRentingRatio) { 23 int nrOfCities = 0, nrOfItems = 0; kspCapacity = 0; 24 ttpMinSpeed = 0.0; ttpMaxSpeed = 0.0; ttpRentingRatio = 0.0; 25 distanceType = DistanceType.Unknown; 26 27 using (var fs = new FileStream(filePath, FileMode.Open)) 28 using (var sr = new StreamReader(fs)) { 29 string input; 30 while ((input = sr.ReadLine()) != null && !input.Contains("NODE_COORD_SECTION")) { 31 if (input.Contains("DIMENSION:")) int.TryParse(input.Replace("DIMENSION:", string.Empty), out nrOfCities); 32 if (input.Contains("NUMBER OF ITEMS:")) int.TryParse(input.Replace("NUMBER OF ITEMS:", string.Empty), out nrOfItems); 33 if (input.Contains("CAPACITY OF KNAPSACK:")) int.TryParse(input.Replace("CAPACITY OF KNAPSACK:", string.Empty), out kspCapacity); 34 if (input.Contains("MIN SPEED:")) double.TryParse(input.Replace("MIN SPEED:", string.Empty), out ttpMinSpeed); 35 if (input.Contains("MAX SPEED:")) double.TryParse(input.Replace("MAX SPEED:", string.Empty), out ttpMaxSpeed); 36 if (input.Contains("RENTING RATIO:")) double.TryParse(input.Replace("RENTING RATIO:", string.Empty), out ttpRentingRatio); 37 if (input.Contains("EDGE_WEIGHT_TYPE:")) Enum.TryParse(input.Replace("EDGE_WEIGHT_TYPE:", string.Empty), out distanceType); 38 } 39 40 tspCoordinates = new double[nrOfCities, 2]; 41 kspItemValues = new int[nrOfItems]; 42 kspItemWeights = new int[nrOfItems]; 43 ttpAvailability = new int[nrOfItems]; 44 45 // read cities 46 while ((input = sr.ReadLine()) != null && !input.Contains("ITEMS SECTION")) { 47 string[] data = input.Split('\t'); 48 int index; double x, y; 49 int.TryParse(data[0], out index); 50 double.TryParse(data[1], out x); 51 double.TryParse(data[2], out y); 52 tspCoordinates[index - 1, 0] = x; 53 tspCoordinates[index - 1, 1] = y; 54 } 55 // read items 56 while ((input = sr.ReadLine()) != null) { 57 string[] data = input.Split('\t'); 58 int index, value, weight, city; 59 int.TryParse(data[0], out index); 60 int.TryParse(data[1], out value); 61 int.TryParse(data[2], out weight); 62 int.TryParse(data[3], out city); 63 kspItemValues[index - 1] = value; 64 kspItemWeights[index - 1] = weight; 65 ttpAvailability[index - 1] = city - 1; 66 } 67 } 68 } 69 70 public static double Evaluate(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, DistanceType distanceType) { 71 bool feasible; 72 return Evaluate(tsp, tour, ksp, loot, availability, rentingRatio, minSpeed, maxSpeed, distanceType, out feasible); 73 } 74 75 public static double Evaluate(TravelingSalesmanProblem tsp, int[] tour, BinaryKnapsackProblem ksp, bool[] loot, int[] availability, double rentingRatio, double minSpeed, double maxSpeed, DistanceType distanceType, out bool feasible) { 13 76 double collectedWeight = 0.0; 14 77 double objectiveValue = 0.0; … … 31 94 } 32 95 33 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx] ) * rentingRatio /96 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[cityIdx], distanceType) * rentingRatio / 34 97 (maxSpeed - speedCoefficient * oldCollectedWeight); 35 98 lastCityIdx = cityIdx; … … 37 100 } 38 101 39 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx] ) * rentingRatio /102 objectiveValue -= Distance(tsp.Coordinates.CloneAsMatrix(), tour[lastCityIdx], tour[hideoutIdx], distanceType) * rentingRatio / 40 103 (maxSpeed - speedCoefficient * collectedWeight); 41 104 … … 46 109 } 47 110 48 private static double Distance(double[,] coords, int fromIdx, int toIdx ) {111 private static double Distance(double[,] coords, int fromIdx, int toIdx, DistanceType distanceType) { 49 112 double fromX = coords[fromIdx, 0], fromY = coords[fromIdx, 1], 50 113 toX = coords[toIdx, 0], toY = coords[toIdx, 1]; 51 return (int)Math.Ceiling(Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY))); 114 double distance = Math.Sqrt((toX - fromX) * (toX - fromX) + (toY - fromY) * (toY - fromY)); 115 switch (distanceType) { 116 case DistanceType.CEIL_2D: return (int)Math.Ceiling(distance); 117 case DistanceType.EUC_2D: return distance; 118 default: return 0.0; 119 } 52 120 } 53 121 }
Note: See TracChangeset
for help on using the changeset viewer.