Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP/3.3/PTSP.cs @ 12269

Last change on this file since 12269 was 12261, checked in by apolidur, 9 years ago

#2221: Fixing MoveEvaluators after running unit tests

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Threading.Tasks;
27using HeuristicLab.Optimization;
28using HeuristicLab.PluginInfrastructure;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.Instances;
32using HeuristicLab.Encodings.PermutationEncoding;
33using HeuristicLab.Common;
34using HeuristicLab.Parameters;
35using HeuristicLab.Data;
36using HeuristicLab.Random;
37
38namespace HeuristicLab.Problems.PTSP {
39  [Item("Probabilistic Traveling Salesman Problem", "Represents a Probabilistic Traveling Salesman Problem.")]
40  [StorableClass]
41  public abstract class ProbabilisticTravelingSalesmanProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IStorableContent,
42  IProblemInstanceConsumer<TSPData> {
43    private static readonly int DistanceMatrixSizeLimit = 1000;
44
45    #region Parameter Properties
46    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
47      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
48    }
49    public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
50      get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
51    }
52    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
53      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
54    }
55    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
56      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
57    }
58    public ValueParameter<DoubleArray> ProbabilityMatrixParameter {
59      get { return (ValueParameter<DoubleArray>)Parameters["ProbabilityMatrix"]; }
60    }
61    public ValueParameter<IntValue> SampleSizeParameter {
62      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
63    }
64
65    #endregion
66
67    #region Properties
68    public DoubleMatrix Coordinates {
69      get { return CoordinatesParameter.Value; }
70      set { CoordinatesParameter.Value = value; }
71    }
72    public DistanceMatrix DistanceMatrix {
73      get { return DistanceMatrixParameter.Value; }
74      set { DistanceMatrixParameter.Value = value; }
75    }
76    public BoolValue UseDistanceMatrix {
77      get { return UseDistanceMatrixParameter.Value; }
78      set { UseDistanceMatrixParameter.Value = value; }
79    }
80    public Permutation BestKnownSolution {
81      get { return BestKnownSolutionParameter.Value; }
82      set { BestKnownSolutionParameter.Value = value; }
83    }
84    public DoubleArray ProbabilityMatrix {
85      get { return ProbabilityMatrixParameter.Value; }
86      set { ProbabilityMatrixParameter.Value = value; }
87    }
88    public IntValue SampleSize {
89      get { return SampleSizeParameter.Value; }
90      set { SampleSizeParameter.Value = value; }
91    }
92    #endregion
93   
94
95    public override bool Maximization {
96      get { return false; }
97    }
98
99    [StorableConstructor]
100    protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
101    protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
102      : base(original, cloner) {
103    }
104
105    public ProbabilisticTravelingSalesmanProblem() {
106      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
107      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
108      Parameters.Add(new ValueParameter<BoolValue>("UseDistanceMatrix", "True if the coordinates based evaluators should calculate the distance matrix from the coordinates and use it for evaluation similar to the distance matrix evaluator, otherwise false.", new BoolValue(true)));
109      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
110      Parameters.Add(new ValueParameter<DoubleArray>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
111
112      Coordinates = new DoubleMatrix(new double[,] {
113        { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
114        { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
115        { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
116        { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
117      });
118
119      ProbabilityMatrix = new DoubleArray(new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
120     
121    }
122
123    public virtual void Load(TSPData data) {
124      if (data.Coordinates == null && data.Distances == null)
125        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
126      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
127        || data.DistanceMeasure == DistanceMeasure.Manhattan
128        || data.DistanceMeasure == DistanceMeasure.Maximum))
129        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
130      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
131        throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates.");
132
133      Name = data.Name;
134      Description = data.Description;
135
136      // Get Probabilities of cities using random with seed from hash function on the Name of the instance
137      double[] tempMatrix = new double[data.Dimension];
138      MersenneTwister r = new MersenneTwister((uint)data.Name.GetHashCode());
139      for (int i = 0; i < data.Dimension; i++) {
140        tempMatrix[i] = r.NextDouble();
141      }
142      ProbabilityMatrix = new DoubleArray(tempMatrix);
143
144      if (data.BestKnownTour != null) {
145        BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
146      }
147     
148      bool clearCoordinates = false, clearDistanceMatrix = false;
149      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
150        Coordinates = new DoubleMatrix(data.Coordinates);
151      else clearCoordinates = true;
152
153      // reset them after assigning the evaluator
154      if (clearCoordinates) Coordinates = null;
155      if (clearDistanceMatrix) DistanceMatrix = null;
156
157      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
158     
159      Encoding.Length = data.Dimension;
160
161      OnReset();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.