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 | using HeuristicLab.Random;
|
---|
37 |
|
---|
38 | namespace 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<PTSPData> {
|
---|
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> RealizationsSizeParameter {
|
---|
62 | get { return (ValueParameter<IntValue>)Parameters["RealizationsSize"]; }
|
---|
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 RealizationsSize {
|
---|
89 | get { return RealizationsSizeParameter.Value; }
|
---|
90 | set { RealizationsSizeParameter.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(PTSPData 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 | ProbabilityMatrix = new DoubleArray(data.Probabilities);
|
---|
137 |
|
---|
138 | if (data.BestKnownTour != null) {
|
---|
139 | BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
|
---|
140 | }
|
---|
141 |
|
---|
142 | bool clearCoordinates = false, clearDistanceMatrix = false;
|
---|
143 | if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
|
---|
144 | Coordinates = new DoubleMatrix(data.Coordinates);
|
---|
145 | else clearCoordinates = true;
|
---|
146 |
|
---|
147 | // reset them after assigning the evaluator
|
---|
148 | if (clearCoordinates) Coordinates = null;
|
---|
149 | if (clearDistanceMatrix) DistanceMatrix = null;
|
---|
150 |
|
---|
151 | DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
|
---|
152 |
|
---|
153 | Encoding.Length = data.Dimension;
|
---|
154 |
|
---|
155 | OnReset();
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|