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 | operators = new OperatorSet();
|
---|
97 | if (ApplicationManager.Manager != null) {
|
---|
98 | var ops = ApplicationManager.Manager.GetInstances<IPermutationOperator>();
|
---|
99 | foreach (IPermutationCrossover op in ops.OfType<IPermutationCrossover>()) {
|
---|
100 | op.ParentsParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
101 | op.ChildParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
102 | }
|
---|
103 | foreach (IPermutationManipulator op in ops.OfType<IPermutationManipulator>()) {
|
---|
104 | op.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
105 | }
|
---|
106 | foreach (IPermutationOperator op in ops)
|
---|
107 | operators.Add(op);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | public void ImportFromTSPLIB(string filename) {
|
---|
112 | TSPLIBParser parser = new TSPLIBParser(filename);
|
---|
113 | parser.Parse();
|
---|
114 | CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices);
|
---|
115 | int cities = CoordinatesParameter.Value.Rows;
|
---|
116 | SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities);
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
|
---|
120 | OnMaximizationChanged();
|
---|
121 | }
|
---|
122 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
123 | OnSolutionCreatorChanged();
|
---|
124 | }
|
---|
125 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
126 | OnEvaluatorChanged();
|
---|
127 | }
|
---|
128 |
|
---|
129 | public event EventHandler MaximizationChanged;
|
---|
130 | private void OnMaximizationChanged() {
|
---|
131 | if (MaximizationChanged != null)
|
---|
132 | MaximizationChanged(this, EventArgs.Empty);
|
---|
133 | }
|
---|
134 | public event EventHandler SolutionCreatorChanged;
|
---|
135 | private void OnSolutionCreatorChanged() {
|
---|
136 | if (SolutionCreatorChanged != null)
|
---|
137 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
138 | }
|
---|
139 | public event EventHandler EvaluatorChanged;
|
---|
140 | private void OnEvaluatorChanged() {
|
---|
141 | if (EvaluatorChanged != null)
|
---|
142 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|