Changeset 6938 for trunk/sources/HeuristicLab.Problems.TravelingSalesman
- Timestamp:
- 10/26/11 23:36:10 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs
r6051 r6938 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Drawing;25 24 using System.IO; 26 25 using System.Linq; … … 38 37 [Creatable("Problems")] 39 38 [StorableClass] 40 public sealed class TravelingSalesmanProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {39 public sealed class TravelingSalesmanProblem : SingleObjectiveHeuristicOptimizationProblem<ITSPEvaluator, IPermutationCreator>, IStorableContent { 41 40 public string Filename { get; set; } 42 41 43 public override Image ItemImage {44 get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }45 }46 47 42 #region Parameter Properties 48 public ValueParameter<BoolValue> MaximizationParameter {49 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }50 }51 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {52 get { return MaximizationParameter; }53 }54 43 public ValueParameter<DoubleMatrix> CoordinatesParameter { 55 44 get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; } … … 60 49 public ValueParameter<BoolValue> UseDistanceMatrixParameter { 61 50 get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; } 62 }63 public ValueParameter<IPermutationCreator> SolutionCreatorParameter {64 get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }65 }66 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {67 get { return SolutionCreatorParameter; }68 }69 public ValueParameter<ITSPEvaluator> EvaluatorParameter {70 get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }71 }72 IParameter IHeuristicOptimizationProblem.EvaluatorParameter {73 get { return EvaluatorParameter; }74 }75 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {76 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }77 }78 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {79 get { return BestKnownQualityParameter; }80 51 } 81 52 public OptionalValueParameter<Permutation> BestKnownSolutionParameter { … … 97 68 set { UseDistanceMatrixParameter.Value = value; } 98 69 } 99 public IPermutationCreator SolutionCreator {100 get { return SolutionCreatorParameter.Value; }101 set { SolutionCreatorParameter.Value = value; }102 }103 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {104 get { return SolutionCreatorParameter.Value; }105 }106 public ITSPEvaluator Evaluator {107 get { return EvaluatorParameter.Value; }108 set { EvaluatorParameter.Value = value; }109 }110 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {111 get { return EvaluatorParameter.Value; }112 }113 IEvaluator IHeuristicOptimizationProblem.Evaluator {114 get { return EvaluatorParameter.Value; }115 }116 public DoubleValue BestKnownQuality {117 get { return BestKnownQualityParameter.Value; }118 set { BestKnownQualityParameter.Value = value; }119 }120 70 public Permutation BestKnownSolution { 121 71 get { return BestKnownSolutionParameter.Value; } 122 72 set { BestKnownSolutionParameter.Value = value; } 123 73 } 124 public IEnumerable<IOperator> Operators {125 get { return operators; }126 }127 74 private BestTSPSolutionAnalyzer BestTSPSolutionAnalyzer { 128 get { return operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); }75 get { return Operators.OfType<BestTSPSolutionAnalyzer>().FirstOrDefault(); } 129 76 } 130 77 private TSPAlleleFrequencyAnalyzer TSPAlleleFrequencyAnalyzer { 131 get { return operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); }78 get { return Operators.OfType<TSPAlleleFrequencyAnalyzer>().FirstOrDefault(); } 132 79 } 133 80 private TSPPopulationDiversityAnalyzer TSPPopulationDiversityAnalyzer { 134 get { return operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); } 135 } 136 #endregion 137 138 [Storable] 139 private List<IOperator> operators; 81 get { return Operators.OfType<TSPPopulationDiversityAnalyzer>().FirstOrDefault(); } 82 } 83 #endregion 84 85 // BackwardsCompatibility3.3 86 #region Backwards compatible code, remove with 3.4 87 [Obsolete] 88 [Storable(Name = "operators")] 89 private IEnumerable<IOperator> oldOperators { 90 get { return null; } 91 set { 92 if (value != null && value.Any()) 93 Operators.AddRange(value); 94 } 95 } 96 #endregion 140 97 141 98 [StorableConstructor] … … 143 100 private TravelingSalesmanProblem(TravelingSalesmanProblem original, Cloner cloner) 144 101 : base(original, cloner) { 145 this.operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();146 102 AttachEventHandlers(); 147 103 } … … 150 106 } 151 107 public TravelingSalesmanProblem() 152 : base() { 153 RandomPermutationCreator creator = new RandomPermutationCreator(); 154 TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator(); 155 156 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false))); 108 : base(new TSPRoundedEuclideanPathEvaluator(), new RandomPermutationCreator()) { 109 157 110 Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.")); 158 111 Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 159 112 Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true))); 160 Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));161 Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));162 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));163 113 Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance.")); 164 114 … … 173 123 }); 174 124 175 creator.PermutationParameter.ActualName = "TSPTour";176 evaluator.QualityParameter.ActualName = "TSPTourLength";125 SolutionCreator.PermutationParameter.ActualName = "TSPTour"; 126 Evaluator.QualityParameter.ActualName = "TSPTourLength"; 177 127 ParameterizeSolutionCreator(); 178 128 ParameterizeEvaluator(); … … 183 133 184 134 #region Events 185 public event EventHandler SolutionCreatorChanged; 186 private void OnSolutionCreatorChanged() { 187 EventHandler handler = SolutionCreatorChanged; 188 if (handler != null) handler(this, EventArgs.Empty); 189 } 190 public event EventHandler EvaluatorChanged; 191 private void OnEvaluatorChanged() { 192 EventHandler handler = EvaluatorChanged; 193 if (handler != null) handler(this, EventArgs.Empty); 194 } 195 public event EventHandler OperatorsChanged; 196 private void OnOperatorsChanged() { 197 EventHandler handler = OperatorsChanged; 198 if (handler != null) handler(this, EventArgs.Empty); 199 } 200 public event EventHandler Reset; 201 private void OnReset() { 202 EventHandler handler = Reset; 203 if (handler != null) handler(this, EventArgs.Empty); 204 } 205 135 protected override void OnSolutionCreatorChanged() { 136 base.OnSolutionCreatorChanged(); 137 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 138 ParameterizeSolutionCreator(); 139 ParameterizeEvaluator(); 140 ParameterizeAnalyzers(); 141 ParameterizeOperators(); 142 } 143 protected override void OnEvaluatorChanged() { 144 base.OnEvaluatorChanged(); 145 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 146 ParameterizeEvaluator(); 147 UpdateMoveEvaluators(); 148 ParameterizeAnalyzers(); 149 ClearDistanceMatrix(); 150 } 206 151 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) { 207 152 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); … … 217 162 ClearDistanceMatrix(); 218 163 } 219 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {220 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);221 ParameterizeSolutionCreator();222 ParameterizeEvaluator();223 ParameterizeAnalyzers();224 ParameterizeOperators();225 OnSolutionCreatorChanged();226 }227 164 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) { 228 165 ParameterizeEvaluator(); … … 230 167 ParameterizeOperators(); 231 168 } 232 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {233 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);234 ParameterizeEvaluator();235 UpdateMoveEvaluators();236 ParameterizeAnalyzers();237 ClearDistanceMatrix();238 OnEvaluatorChanged();239 }240 169 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 241 170 ParameterizeAnalyzers(); 242 }243 private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {244 string name = ((ILookupParameter<InversionMove>)sender).ActualName;245 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {246 op.InversionMoveParameter.ActualName = name;247 }248 }249 private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {250 string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;251 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {252 op.TranslocationMoveParameter.ActualName = name;253 }254 171 } 255 172 #endregion … … 277 194 } 278 195 279 if ( operators == null) InitializeOperators();196 if (Operators.Count == 0) InitializeOperators(); 280 197 #endregion 281 198 AttachEventHandlers(); … … 286 203 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 287 204 Coordinates.Reset += new EventHandler(Coordinates_Reset); 288 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);289 205 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 290 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);291 206 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 292 207 } 293 208 294 209 private void InitializeOperators() { 295 operators = new List<IOperator>(); 296 operators.Add(new BestTSPSolutionAnalyzer()); 297 operators.Add(new TSPAlleleFrequencyAnalyzer()); 298 operators.Add(new TSPPopulationDiversityAnalyzer()); 299 ParameterizeAnalyzers(); 300 operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>()); 210 Operators.Add(new BestTSPSolutionAnalyzer()); 211 Operators.Add(new TSPAlleleFrequencyAnalyzer()); 212 Operators.Add(new TSPPopulationDiversityAnalyzer()); 213 ParameterizeAnalyzers(); 214 Operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>()); 301 215 ParameterizeOperators(); 302 216 UpdateMoveEvaluators(); 303 InitializeMoveGenerators();304 }305 private void InitializeMoveGenerators() {306 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {307 if (op is IMoveGenerator) {308 op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);309 }310 }311 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {312 if (op is IMoveGenerator) {313 op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);314 }315 }316 217 } 317 218 private void UpdateMoveEvaluators() { 318 operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);219 Operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator); 319 220 foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>()) 320 221 if (op.EvaluatorType == Evaluator.GetType()) { 321 operators.Add(op);222 Operators.Add(op); 322 223 } 323 224 ParameterizeOperators(); … … 400 301 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 401 302 op.PermutationParameter.Hidden = true; 402 }403 string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;404 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {405 op.InversionMoveParameter.ActualName = inversionMove;406 op.InversionMoveParameter.Hidden = true;407 }408 string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;409 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {410 op.TranslocationMoveParameter.ActualName = translocationMove;411 op.TranslocationMoveParameter.Hidden = true;412 303 } 413 304 foreach (IPermutationMultiNeighborhoodShakingOperator op in Operators.OfType<IPermutationMultiNeighborhoodShakingOperator>()) {
Note: See TracChangeset
for help on using the changeset viewer.