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.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Permutation;
|
---|
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 | 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 | var ops = ApplicationManager.Manager.GetInstances<IPermutationOperator>().ToList();
|
---|
104 | ops.ForEach(x => {
|
---|
105 | IPermutationCrossover y = x as IPermutationCrossover;
|
---|
106 | if (y != null) {
|
---|
107 | y.ParentsParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
108 | y.ChildParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
109 | }
|
---|
110 | });
|
---|
111 | ops.ForEach(x => {
|
---|
112 | IPermutationManipulator y = x as IPermutationManipulator;
|
---|
113 | if (y != null)
|
---|
114 | y.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;
|
---|
115 | });
|
---|
116 | operators = new OperatorSet(ops.Cast<IOperator>());
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void ImportFromTSPLIB(string filename) {
|
---|
120 | TSPLIBParser parser = new TSPLIBParser(filename);
|
---|
121 | parser.Parse();
|
---|
122 | CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices);
|
---|
123 | int cities = CoordinatesParameter.Value.Rows;
|
---|
124 | SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities);
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void MaximizationParameter_ValueChanged(object sender, EventArgs e) {
|
---|
128 | OnMaximizationChanged();
|
---|
129 | }
|
---|
130 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
131 | OnSolutionCreatorChanged();
|
---|
132 | }
|
---|
133 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
134 | OnEvaluatorChanged();
|
---|
135 | }
|
---|
136 |
|
---|
137 | public event EventHandler MaximizationChanged;
|
---|
138 | private void OnMaximizationChanged() {
|
---|
139 | if (MaximizationChanged != null)
|
---|
140 | MaximizationChanged(this, EventArgs.Empty);
|
---|
141 | }
|
---|
142 | public event EventHandler SolutionCreatorChanged;
|
---|
143 | private void OnSolutionCreatorChanged() {
|
---|
144 | if (SolutionCreatorChanged != null)
|
---|
145 | SolutionCreatorChanged(this, EventArgs.Empty);
|
---|
146 | }
|
---|
147 | public event EventHandler EvaluatorChanged;
|
---|
148 | private void OnEvaluatorChanged() {
|
---|
149 | if (EvaluatorChanged != null)
|
---|
150 | EvaluatorChanged(this, EventArgs.Empty);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|