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