Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2830 was 2830, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 3.3 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 HeuristicLab.Core;
23using HeuristicLab.Data;
24using HeuristicLab.Parameters;
25using HeuristicLab.Permutation;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Routing.TSP {
29  [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
30  [Creatable("Problems")]
31  [EmptyStorableClass]
32  public sealed class TSP : Problem {
33    private ValueParameter<DoubleMatrixData> CoordinatesParameter {
34      get { return (ValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; }
35    }
36    private OperatorParameter SolutionCreatorParameter {
37      get { return (OperatorParameter)Parameters["SolutionCreator"]; }
38    }
39    private OperatorParameter SolutionEvaluatorParameter {
40      get { return (OperatorParameter)Parameters["SolutionEvaluator"]; }
41    }
42
43    public DoubleMatrixData Coordinates {
44      get { return CoordinatesParameter.Value; }
45      set { CoordinatesParameter.Value = value; }
46    }
47    public IOperator SolutionCreator {
48      get { return SolutionCreatorParameter.Value; }
49      set { SolutionCreatorParameter.Value = value; }
50    }
51    public IOperator SolutionEvaluator {
52      get { return SolutionEvaluatorParameter.Value; }
53      set { SolutionEvaluatorParameter.Value = value; }
54    }
55
56    public TSP()
57      : base() {
58      Parameters.Add(new ValueParameter<BoolData>("Maximization", "Set to false as the TSP is a minimization problem.", new BoolData(false)));
59      Parameters.Add(new ValueParameter<DoubleMatrixData>("Coordinates", "The x- and y-Coordinates of the cities.", new DoubleMatrixData(0, 0)));
60      Parameters.Add(new ValueParameter<DoubleData>("BestKnownQuality", "The quality of the best known solution of this TSP instance."));
61      Parameters.Add(new OperatorParameter("SolutionCreator", "The operator which should be used to create new solutions."));
62      Parameters.Add(new OperatorParameter("SolutionEvaluator", "The operator which should be used to evaluate solutions."));
63    }
64
65    public void ImportFromTSPLIB(string filename) {
66      TSPLIBParser parser = new TSPLIBParser(filename);
67      parser.Parse();
68      Coordinates = new DoubleMatrixData(parser.Vertices);
69      int cities = Coordinates.Rows;
70      RandomPermutationCreator creator = new RandomPermutationCreator();
71      creator.LengthParameter.Value = new IntData(cities);
72      SolutionCreator = creator;
73      TSPRoundedEuclideanPathEvaluator evaluator = new TSPRoundedEuclideanPathEvaluator();
74      SolutionEvaluator = evaluator;
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.