Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2999 was 2994, checked in by epitzer, 14 years ago

Make StorableClass attribute compulsory for StorableSerializer to work, add named property StorableClassType to choose between Empty and MarkedOnly, later other options will be added. (#548)

File size: 9.2 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.Permutation;
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(StorableClassType.Empty)]
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<BoolData> MaximizationParameter {
46      get { return (ValueParameter<BoolData>)Parameters["Maximization"]; }
47    }
48    IParameter ISingleObjectiveProblem.MaximizationParameter {
49      get { return MaximizationParameter; }
50    }
51    public ValueParameter<DoubleMatrixData> CoordinatesParameter {
52      get { return (ValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; }
53    }
54    public ValueParameter<IPermutationCreator> SolutionCreatorParameter {
55      get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
56    }
57    IParameter IProblem.SolutionCreatorParameter {
58      get { return SolutionCreatorParameter; }
59    }
60    public ValueParameter<ITSPEvaluator> EvaluatorParameter {
61      get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
62    }
63    IParameter IProblem.EvaluatorParameter {
64      get { return EvaluatorParameter; }
65    }
66    public OptionalValueParameter<DoubleData> BestKnownQualityParameter {
67      get { return (OptionalValueParameter<DoubleData>)Parameters["BestKnownQuality"]; }
68    }
69    #endregion
70
71    #region Properties
72    public DoubleMatrixData Coordinates {
73      get { return CoordinatesParameter.Value; }
74      set { CoordinatesParameter.Value = value; }
75    }
76    public IPermutationCreator SolutionCreator {
77      get { return SolutionCreatorParameter.Value; }
78      set { SolutionCreatorParameter.Value = value; }
79    }
80    ISolutionCreator IProblem.SolutionCreator {
81      get { return SolutionCreatorParameter.Value; }
82    }
83    public ITSPEvaluator Evaluator {
84      get { return EvaluatorParameter.Value; }
85      set { EvaluatorParameter.Value = value; }
86    }
87    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
88      get { return EvaluatorParameter.Value; }
89    }
90    IEvaluator IProblem.Evaluator {
91      get { return EvaluatorParameter.Value; }
92    }
93    public DoubleData BestKnownQuality {
94      get { return BestKnownQualityParameter.Value; }
95      set { BestKnownQualityParameter.Value = value; }
96    }
97    private List<IPermutationOperator> operators;
98    public IEnumerable<IOperator> Operators {
99      get { return operators.Cast<IOperator>(); }
100    }
101    #endregion
102
103    public TSP()
104      : base() {
105      RandomPermutationCreator creator = new RandomPermutationCreator();
106      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
107
108      Parameters.Add(new ValueParameter<BoolData>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolData(false)));
109      Parameters.Add(new ValueParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrixData(0, 0)));
110      Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
111      Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
112      Parameters.Add(new OptionalValueParameter<DoubleData>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
113
114      creator.PermutationParameter.ActualName = "TSPTour";
115      evaluator.QualityParameter.ActualName = "TSPTourLength";
116      ParameterizeSolutionCreator();
117      ParameterizeEvaluator();
118
119      Initialize();
120    }
121    [StorableConstructor]
122    private TSP(bool deserializing) : base() { }
123
124    public override IDeepCloneable Clone(Cloner cloner) {
125      TSP clone = (TSP)base.Clone(cloner);
126      clone.Initialize();
127      return clone;
128    }
129
130    public void ImportFromTSPLIB(string filename) {
131      TSPLIBParser parser = new TSPLIBParser(filename);
132      parser.Parse();
133      Coordinates = new DoubleMatrixData(parser.Vertices);
134    }
135
136    #region Events
137    public event EventHandler SolutionCreatorChanged;
138    private void OnSolutionCreatorChanged() {
139      if (SolutionCreatorChanged != null)
140        SolutionCreatorChanged(this, EventArgs.Empty);
141    }
142    public event EventHandler EvaluatorChanged;
143    private void OnEvaluatorChanged() {
144      if (EvaluatorChanged != null)
145        EvaluatorChanged(this, EventArgs.Empty);
146    }
147    public event EventHandler OperatorsChanged;
148    private void OnOperatorsChanged() {
149      if (OperatorsChanged != null)
150        OperatorsChanged(this, EventArgs.Empty);
151    }
152
153    private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {
154      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
155      Coordinates.Reset += new EventHandler(Coordinates_Reset);
156      ParameterizeSolutionCreator();
157    }
158    private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {
159    }
160    private void Coordinates_Reset(object sender, EventArgs e) {
161      ParameterizeSolutionCreator();
162    }
163    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
164      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
165      ParameterizeSolutionCreator();
166      ParameterizeEvaluator();
167      ParameterizeOperators();
168      OnSolutionCreatorChanged();
169    }
170    private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {
171      ParameterizeEvaluator();
172      ParameterizeOperators();
173    }
174    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
175      ParameterizeEvaluator();
176      OnEvaluatorChanged();
177    }
178    #endregion
179
180    #region Helpers
181    [StorableHook(HookType.AfterDeserialization)]
182    private void Initialize() {
183      InitializeOperators();
184      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
185      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
186      Coordinates.Reset += new EventHandler(Coordinates_Reset);
187      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
188      SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);
189      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
190    }
191    private void ParameterizeSolutionCreator() {
192      SolutionCreator.LengthParameter.Value = new IntData(Coordinates.Rows);
193    }
194    private void ParameterizeEvaluator() {
195      if (Evaluator is ITSPPathEvaluator)
196        ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
197      if (Evaluator is ITSPCoordinatesPathEvaluator)
198        ((ITSPCoordinatesPathEvaluator)Evaluator).CoordinatesParameter.ActualName = CoordinatesParameter.Name;
199    }
200    private void InitializeOperators() {
201      operators = new List<IPermutationOperator>();
202      if (ApplicationManager.Manager != null) {
203        operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
204        ParameterizeOperators();
205      }
206    }
207    private void ParameterizeOperators() {
208      foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {
209        op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
210        op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
211      }
212      foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {
213        op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;
214      }
215    }
216    #endregion
217  }
218}
Note: See TracBrowser for help on using the repository browser.