Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs @ 3098

Last change on this file since 3098 was 3080, checked in by swagner, 15 years ago

Implemented first version of best and best known quality handling (#920)

File size: 12.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Problems.TSP {
36  [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
37  [Creatable("Problems")]
38  [StorableClass]
39  public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem {
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
42    }
43
44    #region Parameter Properties
45    public ValueParameter<BoolValue> MaximizationParameter {
46      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    IParameter ISingleObjectiveProblem.MaximizationParameter {
49      get { return MaximizationParameter; }
50    }
51    public ValueParameter<DoubleMatrix> CoordinatesParameter {
52      get { return (ValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
53    }
54    public OptionalValueParameter<DoubleMatrix> DistanceMatrixParameter {
55      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["DistanceMatrix"]; }
56    }
57    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
58      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
59    }
60    public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
61      get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
62    }
63    IParameter IProblem.SolutionCreatorParameter {
64      get { return SolutionCreatorParameter; }
65    }
66    public ValueParameter<ITSPEvaluator> EvaluatorParameter {
67      get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
68    }
69    IParameter IProblem.EvaluatorParameter {
70      get { return EvaluatorParameter; }
71    }
72    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
73      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
74    }
75    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
76      get { return BestKnownQualityParameter; }
77    }
78    #endregion
79
80    #region Properties
81    public DoubleMatrix Coordinates {
82      get { return CoordinatesParameter.Value; }
83      set { CoordinatesParameter.Value = value; }
84    }
85    public DoubleMatrix DistanceMatrix {
86      get { return DistanceMatrixParameter.Value; }
87      set { DistanceMatrixParameter.Value = value; }
88    }
89    public BoolValue UseDistanceMatrix {
90      get { return UseDistanceMatrixParameter.Value; }
91      set { UseDistanceMatrixParameter.Value = value; }
92    }
93    public IPermutationCreator SolutionCreator {
94      get { return SolutionCreatorParameter.Value; }
95      set { SolutionCreatorParameter.Value = value; }
96    }
97    ISolutionCreator IProblem.SolutionCreator {
98      get { return SolutionCreatorParameter.Value; }
99    }
100    public ITSPEvaluator Evaluator {
101      get { return EvaluatorParameter.Value; }
102      set { EvaluatorParameter.Value = value; }
103    }
104    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
105      get { return EvaluatorParameter.Value; }
106    }
107    IEvaluator IProblem.Evaluator {
108      get { return EvaluatorParameter.Value; }
109    }
110    public DoubleValue BestKnownQuality {
111      get { return BestKnownQualityParameter.Value; }
112      set { BestKnownQualityParameter.Value = value; }
113    }
114    private List<IPermutationOperator> operators;
115    public IEnumerable<IOperator> Operators {
116      get { return operators.Cast<IOperator>(); }
117    }
118    #endregion
119
120    public TSP()
121      : base() {
122      RandomPermutationCreator creator = new RandomPermutationCreator();
123      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
124
125      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolValue(false)));
126      Parameters.Add(new ValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrix(0, 0)));
127      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
128      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if a distance matrix should be calculated and used for evaluation, otherwise false.", new BoolValue(true)));
129      Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
130      Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
131      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
132
133      creator.PermutationParameter.ActualName = "TSPTour";
134      evaluator.QualityParameter.ActualName = "TSPTourLength";
135      ParameterizeSolutionCreator();
136      ParameterizeEvaluator();
137
138      Initialize();
139    }
140    [StorableConstructor]
141    private TSP(bool deserializing) : base() { }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      TSP clone = (TSP)base.Clone(cloner);
145      clone.Initialize();
146      return clone;
147    }
148
149    public void ImportFromTSPLIB(string filename) {
150      TSPLIBParser parser = new TSPLIBParser(filename);
151      parser.Parse();
152      Coordinates = new DoubleMatrix(parser.Vertices);
153    }
154
155    #region Events
156    public event EventHandler SolutionCreatorChanged;
157    private void OnSolutionCreatorChanged() {
158      if (SolutionCreatorChanged != null)
159        SolutionCreatorChanged(this, EventArgs.Empty);
160    }
161    public event EventHandler EvaluatorChanged;
162    private void OnEvaluatorChanged() {
163      if (EvaluatorChanged != null)
164        EvaluatorChanged(this, EventArgs.Empty);
165    }
166    public event EventHandler OperatorsChanged;
167    private void OnOperatorsChanged() {
168      if (OperatorsChanged != null)
169        OperatorsChanged(this, EventArgs.Empty);
170    }
171
172    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
173      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
174      Coordinates.Reset += new EventHandler(Coordinates_Reset);
175      ParameterizeSolutionCreator();
176      ClearDistanceMatrix();
177    }
178    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
179      ClearDistanceMatrix();
180    }
181    private void Coordinates_Reset(object sender, EventArgs e) {
182      ParameterizeSolutionCreator();
183      ClearDistanceMatrix();
184    }
185    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
186      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
187      ParameterizeSolutionCreator();
188      ParameterizeEvaluator();
189      ParameterizeOperators();
190      OnSolutionCreatorChanged();
191    }
192    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
193      ParameterizeEvaluator();
194      ParameterizeOperators();
195    }
196    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
197      ParameterizeEvaluator();
198      ClearDistanceMatrix();
199      OnEvaluatorChanged();
200    }
201    private void MoveGenerator_TwoOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
202      string name = ((ILookupParameter<TwoOptMove>)sender).ActualName;
203      foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
204        if (!(op is IMoveGenerator)) op.TwoOptMoveParameter.ActualName = name;
205      }
206    }
207    private void MoveGenerator_ThreeOptMoveParameter_ActualNameChanged(object sender, EventArgs e) {
208      string name = ((ILookupParameter<ThreeOptMove>)sender).ActualName;
209      foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
210        if (!(op is IMoveGenerator)) op.ThreeOptMoveParameter.ActualName = name;
211      }
212    }
213    #endregion
214
215    #region Helpers
216    [StorableHook(HookType.AfterDeserialization)]
217    private void Initialize() {
218      InitializeOperators();
219      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
220      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
221      Coordinates.Reset += new EventHandler(Coordinates_Reset);
222      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
223      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
224      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
225    }
226    private void ParameterizeSolutionCreator() {
227      SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);
228    }
229    private void ParameterizeEvaluator() {
230      if (Evaluator is ITSPPathEvaluator)
231        ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
232      if (Evaluator is ITSPCoordinatesPathEvaluator) {
233        ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;
234        evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
235        evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
236        evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
237      }
238    }
239    private void InitializeOperators() {
240      operators = new List<IPermutationOperator>();
241      if (ApplicationManager.Manager != null) {
242        operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
243        ParameterizeOperators();
244      }
245      InitializeMoveGenerators();
246    }
247    private void ParameterizeOperators() {
248      foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
249        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
250        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
251      }
252      foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
253        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
254      }
255      foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {
256        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
257      }
258      foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {
259        op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
260        op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;
261        op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;
262      }
263    }
264    private void InitializeMoveGenerators() {
265      foreach (ITwoOptPermutationMoveOperator op in Operators.OfType<ITwoOptPermutationMoveOperator>()) {
266        if (op is IMoveGenerator) {
267          op.TwoOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TwoOptMoveParameter_ActualNameChanged);
268        }
269      }
270      foreach (IThreeOptPermutationMoveOperator op in Operators.OfType<IThreeOptPermutationMoveOperator>()) {
271        if (op is IMoveGenerator) {
272          op.ThreeOptMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_ThreeOptMoveParameter_ActualNameChanged);
273        }
274      }
275    }
276
277    private void ClearDistanceMatrix() {
278      DistanceMatrixParameter.Value = null;
279    }
280    #endregion
281  }
282}
Note: See TracBrowser for help on using the repository browser.