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

Last change on this file since 12387 was 12387, checked in by apolidur, 6 years ago

#2221: Fixing RealizationsSize parameter not updating

File size: 6.8 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<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   
62
63    #endregion
64
65    #region Properties
66    public DoubleMatrix Coordinates {
67      get { return CoordinatesParameter.Value; }
68      set { CoordinatesParameter.Value = value; }
69    }
70    public DistanceMatrix DistanceMatrix {
71      get { return DistanceMatrixParameter.Value; }
72      set { DistanceMatrixParameter.Value = value; }
73    }
74    public BoolValue UseDistanceMatrix {
75      get { return UseDistanceMatrixParameter.Value; }
76      set { UseDistanceMatrixParameter.Value = value; }
77    }
78    public Permutation BestKnownSolution {
79      get { return BestKnownSolutionParameter.Value; }
80      set { BestKnownSolutionParameter.Value = value; }
81    }
82    public DoubleArray ProbabilityMatrix {
83      get { return ProbabilityMatrixParameter.Value; }
84      set { ProbabilityMatrixParameter.Value = value; }
85    }
86   
87    #endregion
88   
89
90    public override bool Maximization {
91      get { return false; }
92    }
93
94    [StorableConstructor]
95    protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
96    protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
97      : base(original, cloner) {
98    }
99
100    public ProbabilisticTravelingSalesmanProblem() {
101      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
102      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
103      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)));
104      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
105      Parameters.Add(new ValueParameter<DoubleArray>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
106
107      Coordinates = new DoubleMatrix(new double[,] {
108        { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 },
109        { 200, 100 }, { 200, 200 }, { 200, 300 }, { 200, 400 },
110        { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 },
111        { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 }
112      });
113
114      ProbabilityMatrix = new DoubleArray(new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
115     
116    }
117
118    public virtual void Load(PTSPData data) {
119      if (data.Coordinates == null && data.Distances == null)
120        throw new System.IO.InvalidDataException("The given instance specifies neither coordinates nor distances!");
121      if (data.Dimension > DistanceMatrixSizeLimit && (data.DistanceMeasure == DistanceMeasure.Att
122        || data.DistanceMeasure == DistanceMeasure.Manhattan
123        || data.DistanceMeasure == DistanceMeasure.Maximum))
124        throw new System.IO.InvalidDataException("The given instance uses an unsupported distance measure and is too large for using a distance matrix.");
125      if (data.Coordinates != null && data.Coordinates.GetLength(1) != 2)
126        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.");
127
128      Name = data.Name;
129      Description = data.Description;
130
131      ProbabilityMatrix = new DoubleArray(data.Probabilities);
132
133      if (data.BestKnownTour != null) {
134        BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
135      }
136     
137      bool clearCoordinates = false, clearDistanceMatrix = false;
138      if (data.Coordinates != null && data.Coordinates.GetLength(0) > 0)
139        Coordinates = new DoubleMatrix(data.Coordinates);
140      else clearCoordinates = true;
141
142      // reset them after assigning the evaluator
143      if (clearCoordinates) Coordinates = null;
144      if (clearDistanceMatrix) DistanceMatrix = null;
145
146      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
147     
148      Encoding.Length = data.Dimension;
149
150      OnReset();
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.