Changeset 6938 for trunk/sources/HeuristicLab.Problems.VehicleRouting
- Timestamp:
- 10/26/11 23:36:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs
r6441 r6938 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.Linq; 26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; 28 27 using HeuristicLab.Data; 29 using HeuristicLab.Encodings.PermutationEncoding;30 28 using HeuristicLab.Optimization; 31 29 using HeuristicLab.Parameters; … … 40 38 [Creatable("Problems")] 41 39 [StorableClass] 42 public sealed class VehicleRoutingProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {40 public sealed class VehicleRoutingProblem : SingleObjectiveHeuristicOptimizationProblem<IVRPEvaluator, IVRPCreator>, IStorableContent { 43 41 public string Filename { get; set; } 44 42 45 public override Image ItemImage {46 get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }47 }48 49 43 #region Parameter Properties 50 public ValueParameter<BoolValue> MaximizationParameter {51 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }52 }53 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {54 get { return MaximizationParameter; }55 }56 44 public ValueParameter<DoubleMatrix> CoordinatesParameter { 57 45 get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; } … … 81 69 get { return (ValueParameter<DoubleArray>)Parameters["ServiceTime"]; } 82 70 } 83 ValueParameter<IVRPCreator> SolutionCreatorParameter {84 get { return (ValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }85 }86 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {87 get { return SolutionCreatorParameter; }88 }89 ValueParameter<IVRPEvaluator> EvaluatorParameter {90 get { return (ValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }91 }92 IParameter IHeuristicOptimizationProblem.EvaluatorParameter {93 get { return EvaluatorParameter; }94 }95 71 public IValueParameter<DoubleValue> FleetUsageFactorParameter { 96 72 get { return (IValueParameter<DoubleValue>)Parameters["EvalFleetUsageFactor"]; } … … 107 83 public IValueParameter<DoubleValue> TardinessPenaltyParameter { 108 84 get { return (IValueParameter<DoubleValue>)Parameters["EvalTardinessPenalty"]; } 109 }110 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {111 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }112 }113 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {114 get { return BestKnownQualityParameter; }115 85 } 116 86 public OptionalValueParameter<IVRPEncoding> BestKnownSolutionParameter { … … 156 126 set { ServiceTimeParameter.Value = value; } 157 127 } 158 public DoubleValue BestKnownQuality {159 get { return BestKnownQualityParameter.Value; }160 set { BestKnownQualityParameter.Value = value; }161 }162 128 public IVRPEncoding BestKnownSolution { 163 129 get { return BestKnownSolutionParameter.Value; } 164 130 set { BestKnownSolutionParameter.Value = value; } 165 131 } 166 public IVRPCreator SolutionCreator {167 get { return SolutionCreatorParameter.Value; }168 set { SolutionCreatorParameter.Value = value; }169 }170 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {171 get { return SolutionCreatorParameter.Value; }172 }173 public IVRPEvaluator Evaluator {174 get { return EvaluatorParameter.Value; }175 set { EvaluatorParameter.Value = value; }176 }177 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {178 get { return EvaluatorParameter.Value; }179 }180 IEvaluator IHeuristicOptimizationProblem.Evaluator {181 get { return EvaluatorParameter.Value; }182 }183 public IEnumerable<IOperator> Operators {184 get { return operators; }185 }186 132 private BestVRPSolutionAnalyzer BestVRPSolutionAnalyzer { 187 get { return operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); }133 get { return Operators.OfType<BestVRPSolutionAnalyzer>().FirstOrDefault(); } 188 134 } 189 135 private BestAverageWorstVRPToursAnalyzer BestAverageWorstVRPToursAnalyzer { 190 get { return operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); }136 get { return Operators.OfType<BestAverageWorstVRPToursAnalyzer>().FirstOrDefault(); } 191 137 } 192 138 #endregion 193 139 194 [Storable] 195 private List<IOperator> operators; 140 // BackwardsCompatibility3.3 141 #region Backwards compatible code, remove with 3.4 142 [Obsolete] 143 [Storable(Name = "operators")] 144 private IEnumerable<IOperator> oldOperators { 145 get { return null; } 146 set { 147 if (value != null && value.Any()) 148 Operators.AddRange(value); 149 } 150 } 151 #endregion 196 152 197 153 [StorableConstructor] … … 199 155 private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner) 200 156 : base(original, cloner) { 201 operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();202 157 AttachEventHandlers(); 203 158 } 204 159 public VehicleRoutingProblem() 205 : base() { 206 IVRPCreator creator = new RandomCreator(); 207 IVRPEvaluator evaluator = new VRPEvaluator(); 208 209 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false))); 160 : base(new VRPEvaluator(), new RandomCreator()) { 210 161 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix())); 211 162 Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); … … 217 168 Parameters.Add(new ValueParameter<DoubleArray>("DueTime", "The due time of each customer.", new DoubleArray())); 218 169 Parameters.Add(new ValueParameter<DoubleArray>("ServiceTime", "The service time of each customer.", new DoubleArray())); 219 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));220 170 Parameters.Add(new OptionalValueParameter<IVRPEncoding>("BestKnownSolution", "The best known solution of this VRP instance.")); 221 171 Parameters.Add(new ValueParameter<DoubleValue>("EvalFleetUsageFactor", "The fleet usage factor considered in the evaluation.", new DoubleValue(100))); … … 225 175 Parameters.Add(new ValueParameter<DoubleValue>("EvalTardinessPenalty", "The tardiness penalty considered in the evaluation.", new DoubleValue(100))); 226 176 227 Parameters.Add(new ValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions.", creator)); 228 Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions.", evaluator)); 229 230 creator.VRPToursParameter.ActualName = "VRPTours"; 231 evaluator.QualityParameter.ActualName = "VRPQuality"; 177 SolutionCreator.VRPToursParameter.ActualName = "VRPTours"; 178 Evaluator.QualityParameter.ActualName = "VRPQuality"; 232 179 233 180 InitializeRandomVRPInstance(); … … 245 192 246 193 #region Events 247 public event EventHandler SolutionCreatorChanged; 248 private void OnSolutionCreatorChanged() { 249 EventHandler handler = SolutionCreatorChanged; 250 if (handler != null) handler(this, EventArgs.Empty); 251 } 252 public event EventHandler EvaluatorChanged; 253 private void OnEvaluatorChanged() { 254 EventHandler handler = EvaluatorChanged; 255 if (handler != null) handler(this, EventArgs.Empty); 256 } 257 public event EventHandler OperatorsChanged; 258 private void OnOperatorsChanged() { 259 EventHandler handler = OperatorsChanged; 260 if (handler != null) handler(this, EventArgs.Empty); 261 } 262 public event EventHandler Reset; 263 private void OnReset() { 264 EventHandler handler = Reset; 265 if (handler != null) handler(this, EventArgs.Empty); 266 } 267 void VehiclesValue_ValueChanged(object sender, EventArgs e) { 194 protected override void OnSolutionCreatorChanged() { 195 base.OnSolutionCreatorChanged(); 196 ParameterizeSolutionCreator(); 197 ParameterizeEvaluator(); 198 ParameterizeAnalyzer(); 199 ParameterizeOperators(); 200 } 201 protected override void OnEvaluatorChanged() { 202 base.OnEvaluatorChanged(); 203 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 204 ParameterizeEvaluator(); 205 UpdateMoveEvaluators(); 206 ParameterizeAnalyzer(); 207 } 208 private void VehiclesValue_ValueChanged(object sender, EventArgs e) { 268 209 ParameterizeSolutionCreator(); 269 210 } … … 287 228 BestKnownSolution = null; 288 229 } 289 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {290 ParameterizeSolutionCreator();291 ParameterizeEvaluator();292 ParameterizeAnalyzer();293 ParameterizeOperators();294 OnSolutionCreatorChanged();295 }296 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {297 ParameterizeEvaluator();298 ParameterizeAnalyzer();299 ParameterizeOperators();300 }301 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {302 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);303 ParameterizeEvaluator();304 UpdateMoveEvaluators();305 ParameterizeAnalyzer();306 OnEvaluatorChanged();307 }308 230 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 309 231 ParameterizeAnalyzer(); 310 }311 private void TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {312 string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;313 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {314 op.TranslocationMoveParameter.ActualName = name;315 }316 232 } 317 233 … … 453 369 Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged); 454 370 455 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);456 457 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);458 371 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 459 372 … … 494 407 495 408 private void InitializeOperators() { 496 operators = new List<IOperator>(); 497 operators.Add(new BestVRPSolutionAnalyzer()); 498 operators.Add(new BestAverageWorstVRPToursAnalyzer()); 409 Operators.Add(new BestVRPSolutionAnalyzer()); 410 Operators.Add(new BestAverageWorstVRPToursAnalyzer()); 499 411 ParameterizeAnalyzer(); 500 operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name));412 Operators.AddRange(ApplicationManager.Manager.GetInstances<IVRPOperator>().Cast<IOperator>().OrderBy(op => op.Name)); 501 413 ParameterizeOperators(); 502 414 UpdateMoveEvaluators(); 503 InitializeMoveGenerators();504 }505 private void InitializeMoveGenerators() {506 foreach (IAlbaTranslocationMoveOperator op in Operators.OfType<IAlbaTranslocationMoveOperator>()) {507 if (op is IMoveGenerator) {508 op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(TranslocationMoveParameter_ActualNameChanged);509 }510 }511 512 415 } 513 416 private void UpdateMoveEvaluators() {
Note: See TracChangeset
for help on using the changeset viewer.