Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Routing.TSP/3.3/TSP.cs @ 2865

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

Operator architecture refactoring (#95)

  • worked on algorithms
File size: 6.6 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.Drawing;
24using System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Permutation;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.PluginInfrastructure;
32
33namespace HeuristicLab.Routing.TSP {
34  [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
35  [Creatable("Problems")]
36  [EmptyStorableClass]
37  public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem {
38    public override Image ItemImage {
39      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
40    }
41
42    private IValueParameter<BoolData> MaximizationParameter {
43      get { return (IValueParameter<BoolData>)Parameters["Maximization"]; }
44    }
45    public IValueParameter<DoubleMatrixData> CoordinatesParameter {
46      get { return (IValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; }
47    }
48    public IValueParameter<DoubleData> BestKnownQualityParameter {
49      get { return (IValueParameter<DoubleData>)Parameters["BestKnownQuality"]; }
50    }
51    public IValueParameter<IPermutationCreator> SolutionCreatorParameter {
52      get { return (IValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; }
53    }
54    public IValueParameter<ITSPEvaluator> EvaluatorParameter {
55      get { return (IValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; }
56    }
57
58    public BoolData Maximization {
59      get { return MaximizationParameter.Value; }
60    }
61    public ISolutionCreator SolutionCreator {
62      get { return SolutionCreatorParameter.Value; }
63    }
64    public ISingleObjectiveEvaluator Evaluator {
65      get { return EvaluatorParameter.Value; }
66    }
67    IEvaluator IProblem.Evaluator {
68      get { return EvaluatorParameter.Value; }
69    }
70    private OperatorSet operators;
71    public OperatorSet Operators {
72      get { return operators; }
73    }
74
75    public TSP()
76      : base() {
77      ValueParameter<BoolData> maximizationParameter = new ValueParameter<BoolData>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolData(false));
78      maximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged);
79      Parameters.Add(maximizationParameter);
80
81      Parameters.Add(new ValueParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrixData(0, 0)));
82
83      ValueParameter<IPermutationCreator> solutionCreatorParameter = new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.");
84      solutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
85      Parameters.Add(solutionCreatorParameter);
86
87      ValueParameter<ITSPEvaluator> evaluatorParameter = new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.");
88      evaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
89      Parameters.Add(evaluatorParameter);
90
91      Parameters.Add(new ValueParameter<DoubleData>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
92
93      RandomPermutationCreator creator = new RandomPermutationCreator();
94      creator.PermutationParameter.ActualName = "TSPTour";
95      creator.LengthParameter.Value = new IntData(0);
96      SolutionCreatorParameter.Value = creator;
97      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
98      evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
99      evaluator.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
100      evaluator.QualityParameter.ActualName = "TSPTourLength";
101      EvaluatorParameter.Value = evaluator;
102
103      operators = new OperatorSet();
104      var crossovers = ApplicationManager.Manager.GetInstances<IPermutationCrossover>().ToList();
105      crossovers.ForEach(x => {
106        x.ParentsParameter.ActualName = creator.PermutationParameter.ActualName;
107        x.ChildParameter.ActualName = creator.PermutationParameter.ActualName;
108      });
109      var manipulators = ApplicationManager.Manager.GetInstances<IPermutationManipulator>().ToList();
110      manipulators.ForEach(x => x.PermutationParameter.ActualName = creator.PermutationParameter.ActualName);
111      operators = new OperatorSet(crossovers.Cast<IOperator>().Concat(manipulators.Cast<IOperator>()));
112    }
113
114    public void ImportFromTSPLIB(string filename) {
115      TSPLIBParser parser = new TSPLIBParser(filename);
116      parser.Parse();
117      CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices);
118      int cities = CoordinatesParameter.Value.Rows;
119      SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities);
120    }
121
122    private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
123      OnMaximizationChanged();
124    }
125    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
126      OnSolutionCreatorChanged();
127    }
128    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
129      OnEvaluatorChanged();
130    }
131
132    public event EventHandler MaximizationChanged;
133    private void OnMaximizationChanged() {
134      if (MaximizationChanged != null)
135        MaximizationChanged(this, EventArgs.Empty);
136    }
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  }
148}
Note: See TracBrowser for help on using the repository browser.