Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12219 was 12219, checked in by apolidur, 10 years ago

#2221: First version of 1-shift and 2-p-opt moves

File size: 5.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;
36
37namespace 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    #region Parameter Properties
43    public OptionalValueParameter<DoubleMatrix> CoordinatesParameter {
44      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["Coordinates"]; }
45    }
46    public OptionalValueParameter<DistanceMatrix> DistanceMatrixParameter {
47      get { return (OptionalValueParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
48    }
49    public ValueParameter<BoolValue> UseDistanceMatrixParameter {
50      get { return (ValueParameter<BoolValue>)Parameters["UseDistanceMatrix"]; }
51    }
52    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
53      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
54    }
55    public OptionalValueParameter<ItemList<DoubleValue>> ProbabilityMatrixParameter {
56      get { return (OptionalValueParameter<ItemList<DoubleValue>>)Parameters["ProbabilityMatrix"]; }
57    }
58    public ValueParameter<IntValue> SampleSizeParameter {
59      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
60    }
61
62    #endregion
63
64    #region Properties
65    public DoubleMatrix Coordinates {
66      get { return CoordinatesParameter.Value; }
67      set { CoordinatesParameter.Value = value; }
68    }
69    public DistanceMatrix DistanceMatrix {
70      get { return DistanceMatrixParameter.Value; }
71      set { DistanceMatrixParameter.Value = value; }
72    }
73    public BoolValue UseDistanceMatrix {
74      get { return UseDistanceMatrixParameter.Value; }
75      set { UseDistanceMatrixParameter.Value = value; }
76    }
77    public Permutation BestKnownSolution {
78      get { return BestKnownSolutionParameter.Value; }
79      set { BestKnownSolutionParameter.Value = value; }
80    }
81    public ItemList<DoubleValue> ProbabilityMatrix {
82      get { return ProbabilityMatrixParameter.Value; }
83      set { ProbabilityMatrixParameter.Value = value; }
84    }
85    public IntValue SampleSize {
86      get { return SampleSizeParameter.Value; }
87      set { SampleSizeParameter.Value = value; }
88    }
89    #endregion
90   
91
92    public override bool Maximization {
93      get { return false; }
94    }
95
96    [StorableConstructor]
97    protected ProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
98    protected ProbabilisticTravelingSalesmanProblem(ProbabilisticTravelingSalesmanProblem original, Cloner cloner)
99      : base(original, cloner) {
100    }
101
102    public ProbabilisticTravelingSalesmanProblem() {
103      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("Coordinates", "The x- and y-Coordinates of the cities."));
104      Parameters.Add(new OptionalValueParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
105      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)));
106      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this TSP instance."));
107      Parameters.Add(new OptionalValueParameter<ItemList<DoubleValue>>("ProbabilityMatrix", "The matrix which contains the probabilities of each of the cities."));
108     
109    }
110
111    public virtual void Load(TSPData data) {
112      DistanceMatrix = new DistanceMatrix(data.GetDistanceMatrix());
113      // Get Probabilities of cities using random with seed from hash function on the Name of the instance
114      ProbabilityMatrix = new ItemList<DoubleValue>(data.Dimension);
115      Random r = new Random(data.Name.GetHashCode());
116      for (int i = 0; i < data.Dimension; i++) {
117        ProbabilityMatrix.Add(new DoubleValue(r.NextDouble()));
118      }
119      Encoding.Length = data.Dimension;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.