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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.Permutation;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.PluginInfrastructure;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.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 | RandomPermutationCreator creator = new RandomPermutationCreator();
|
---|
78 | TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
|
---|
79 |
|
---|
80 | Parameters.Add(new ValueParameter<BoolData>("Maximization", "Set to false as the Traveling Salesman Problem is a minimization problem.", new BoolData(false)));
|
---|
81 | Parameters.Add(new ValueParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrixData(0, 0)));
|
---|
82 | Parameters.Add(new ValueParameter<IPermutationCreator>("SolutionCreator", "The operator which should be used to create new TSP solutions.", creator));
|
---|
83 | Parameters.Add(new ValueParameter<ITSPEvaluator>("Evaluator", "The operator which should be used to evaluate TSP solutions.", evaluator));
|
---|
84 | Parameters.Add(new OptionalValueParameter<DoubleData>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
|
---|
85 |
|
---|
86 | creator.PermutationParameter.ActualName = "TSPTour";
|
---|
87 | creator.LengthParameter.Value = new IntData(0);
|
---|
88 | evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;
|
---|
89 | evaluator.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
90 | evaluator.QualityParameter.ActualName = "TSPTourLength";
|
---|
91 |
|
---|
92 | MaximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged);
|
---|
93 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
94 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
95 |
|
---|
96 | var ops = ApplicationManager.Manager.GetInstances<IPermutationOperator>();
|
---|
97 | foreach (IPermutationCrossover op in ops.OfType<IPermutationCrossover>()) {
|
---|
98 | op.ParentsParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
99 | op.ChildParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
100 | }
|
---|
101 | foreach (IPermutationManipulator op in ops.OfType<IPermutationManipulator>()) {
|
---|
102 | op.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
103 | }
|
---|
104 | operators = new OperatorSet(ops.Cast<IOperator>());
|
---|
105 | }
|
---|
106 |
|
---|
107 | public void ImportFromTSPLIB(string filename) {
|
---|
108 | TSPLIBParser parser = new TSPLIBParser(filename);
|
---|
109 | parser.Parse();
|
---|
110 | CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices);
|
---|
111 | int cities = CoordinatesParameter.Value.Rows;
|
---|
112 | SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
|
---|
116 | OnMaximizationChanged();
|
---|
117 | }
|
---|
118 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
119 | OnSolutionCreatorChanged();
|
---|
120 | }
|
---|
121 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
122 | OnEvaluatorChanged();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public event EventHandler MaximizationChanged;
|
---|
126 | private void OnMaximizationChanged() {
|
---|
127 | if (MaximizationChanged != null)
|
---|
128 | MaximizationChanged(this, EventArgs.Empty);
|
---|
129 | }
|
---|
130 | public event EventHandler SolutionCreatorChanged;
|
---|
131 | private void OnSolutionCreatorChanged() {
|
---|
132 | if (SolutionCreatorChanged != null)
|
---|
133 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
134 | }
|
---|
135 | public event EventHandler EvaluatorChanged;
|
---|
136 | private void OnEvaluatorChanged() {
|
---|
137 | if (EvaluatorChanged != null)
|
---|
138 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|