1 | using System.Text;
|
---|
2 | using System.Threading.Tasks;
|
---|
3 | using HeuristicLab.Optimization;
|
---|
4 | using HeuristicLab.PluginInfrastructure;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Problems.Instances;
|
---|
8 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
9 | using HeuristicLab.Common;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 | using HeuristicLab.Data;
|
---|
12 | using HeuristicLab.Random;
|
---|
13 | using System;
|
---|
14 | using System.Linq;
|
---|
15 |
|
---|
16 | namespace HeuristicLab.Problems.PTSP {
|
---|
17 | [Item("Estimated Probabilistic Traveling Salesman Problem", "Represents an estimated Probabilistic Traveling Salesman Problem.")]
|
---|
18 | [Creatable("Problems")]
|
---|
19 | [StorableClass]
|
---|
20 | public sealed class EstimatedProbabilisticTravelingSalesmanProblem : ProbabilisticTravelingSalesmanProblem, IStorableContent,
|
---|
21 | IProblemInstanceConsumer<PTSPData> {
|
---|
22 |
|
---|
23 |
|
---|
24 | #region Parameter Properties
|
---|
25 | public IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter {
|
---|
26 | get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; }
|
---|
27 | }
|
---|
28 | #endregion
|
---|
29 |
|
---|
30 | #region Properties
|
---|
31 | public ItemList<ItemList<IntValue>> Realizations {
|
---|
32 | get { return RealizationsParameter.Value; }
|
---|
33 | set { RealizationsParameter.Value = value; }
|
---|
34 | }
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | [StorableConstructor]
|
---|
38 | private EstimatedProbabilisticTravelingSalesmanProblem(bool deserializing) : base(deserializing) { }
|
---|
39 | private EstimatedProbabilisticTravelingSalesmanProblem(EstimatedProbabilisticTravelingSalesmanProblem original, Cloner cloner)
|
---|
40 | : base(original, cloner) {
|
---|
41 | }
|
---|
42 |
|
---|
43 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
44 | return new EstimatedProbabilisticTravelingSalesmanProblem(this, cloner);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
48 | Permutation p = individual.Permutation();
|
---|
49 | // Estimation-based evaluation
|
---|
50 | MersenneTwister r = new MersenneTwister();
|
---|
51 | double estimatedSum = 0;
|
---|
52 | for (int i = 0; i < Realizations.Count; i++) {
|
---|
53 | int singleRealization = -1, firstNode = -1;
|
---|
54 | for (int j = 0; j < Realizations[i].Count; j++) {
|
---|
55 | if (Realizations[i][p[j]].Value == 1) {
|
---|
56 | if (singleRealization != -1) {
|
---|
57 | estimatedSum += DistanceMatrix[singleRealization, p[j]];
|
---|
58 | } else {
|
---|
59 | firstNode = p[j];
|
---|
60 | }
|
---|
61 | singleRealization = p[j];
|
---|
62 | }
|
---|
63 | }
|
---|
64 | if (singleRealization != -1) {
|
---|
65 | estimatedSum += DistanceMatrix[singleRealization, firstNode];
|
---|
66 | }
|
---|
67 | }
|
---|
68 | return estimatedSum / RealizationsSize.Value;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public double[] EvaluateWithParams(DistanceMatrix distances, DoubleArray probabilities, ItemList<ItemList<IntValue>> realizations, Permutation individual ) {
|
---|
72 | // Estimation-based evaluation
|
---|
73 | MersenneTwister r = new MersenneTwister();
|
---|
74 | double estimatedSum = 0;
|
---|
75 | double[] partialSums = new double[realizations.Count];
|
---|
76 | for (int i = 0; i < realizations.Count; i++) {
|
---|
77 | partialSums[i] = 0;
|
---|
78 | int singleRealization = -1, firstNode = -1;
|
---|
79 | for (int j = 0; j < realizations[i].Count; j++) {
|
---|
80 | if (realizations[i][individual[j]].Value == 1) {
|
---|
81 | if (singleRealization != -1) {
|
---|
82 | partialSums[i] += distances[singleRealization, individual[j]];
|
---|
83 | } else {
|
---|
84 | firstNode = individual[j];
|
---|
85 | }
|
---|
86 | singleRealization = individual[j];
|
---|
87 | }
|
---|
88 | }
|
---|
89 | if (singleRealization != -1) {
|
---|
90 | partialSums[i] += distances[singleRealization, firstNode];
|
---|
91 | }
|
---|
92 | estimatedSum += partialSums[i];
|
---|
93 | }
|
---|
94 | double mean = estimatedSum / realizations.Count;
|
---|
95 | double variance = 0;
|
---|
96 | for (int i = 0; i < realizations.Count; i++) {
|
---|
97 | variance += Math.Pow((partialSums[i] - mean), 2);
|
---|
98 | }
|
---|
99 | variance = variance / realizations.Count;
|
---|
100 | return new double[] {mean,variance};
|
---|
101 | }
|
---|
102 |
|
---|
103 | public EstimatedProbabilisticTravelingSalesmanProblem() {
|
---|
104 | Parameters.Add(new ValueParameter<IntValue>("RealizationsSize", "Size of the sample for the estimation-based evaluation"));
|
---|
105 | Parameters.Add(new ValueParameter<ItemList<ItemList<IntValue>>>("Realizations", "The concrete..."));
|
---|
106 | Operators.Add(new PTSPEstimatedInversionMovePathEvaluator());
|
---|
107 | Operators.Add(new PTSPEstimatedInsertionEvaluator());
|
---|
108 | Operators.Add(new PTSPExhaustiveInversionLocalImprovement());
|
---|
109 | Operators.Add(new PTSPExhaustiveInsertionLocalImprovement());
|
---|
110 |
|
---|
111 | Operators.Add(new Exhaustive25MoveGenerator());
|
---|
112 | Operators.Add(new Stochastic25MultiMoveGenerator());
|
---|
113 | Operators.Add(new Stochastic25SingleMoveGenerator());
|
---|
114 | Operators.Add(new TwoPointFiveMoveMaker());
|
---|
115 | Operators.Add(new PTSP25MoveEvaluator());
|
---|
116 |
|
---|
117 | Encoding.ConfigureOperators(Operators.OfType<IOperator>());
|
---|
118 | foreach (var twopointfiveMoveOperator in Operators.OfType<I25MoveOperator>()) {
|
---|
119 | twopointfiveMoveOperator.TwoPointFiveMoveParameter.ActualName = "Permutation.TwoPointFiveMove";
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private int Ttest(int ProblemSize) {
|
---|
124 | MersenneTwister random = new MersenneTwister();
|
---|
125 | Permutation p1 = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
|
---|
126 | Permutation p2 = new Permutation(PermutationTypes.RelativeUndirected, ProblemSize, random);
|
---|
127 | ItemList<ItemList<IntValue>> realizations = new ItemList<ItemList<IntValue>>();
|
---|
128 | int index = -1;
|
---|
129 | while (true) {
|
---|
130 | for (int i = index+1; i < index+5; i++) {
|
---|
131 | realizations.Add(new ItemList<IntValue>());
|
---|
132 | for (int j = 0; j < ProblemSize; j++) {
|
---|
133 | if (ProbabilityMatrix[j] > random.NextDouble()) {
|
---|
134 | realizations.ElementAt(i).Add(new IntValue(1));
|
---|
135 | } else {
|
---|
136 | realizations.ElementAt(i).Add(new IntValue(0));
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 | index += 4;
|
---|
141 | double[] eval1 = EvaluateWithParams(DistanceMatrix, ProbabilityMatrix, realizations, p1);
|
---|
142 | double[] eval2 = EvaluateWithParams(DistanceMatrix, ProbabilityMatrix, realizations, p2);
|
---|
143 | double sx1x2 = Math.Sqrt((eval1[1]+eval2[1])/2);
|
---|
144 | int degrees = 2 * realizations.Count - 2;
|
---|
145 | double t = (eval1[0]-eval2[0])/(sx1x2*Math.Sqrt(2.0/(double)realizations.Count));
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override void Load(PTSPData data) {
|
---|
150 | base.Load(data);
|
---|
151 | // For now uses sample size of 20 but should use Student's t-test
|
---|
152 | //Ttest(data.Dimension);
|
---|
153 | RealizationsSize = new IntValue(100);
|
---|
154 | MersenneTwister r = new MersenneTwister();
|
---|
155 | int countOnes = 0;
|
---|
156 | Realizations = new ItemList<ItemList<IntValue>>(RealizationsSize.Value);
|
---|
157 | for (int i = 0; i < RealizationsSize.Value; i++) {
|
---|
158 | ItemList<IntValue> newRealization = new ItemList<IntValue>();
|
---|
159 | while (countOnes < 4) { //only generate realizations with at least 4 cities visited
|
---|
160 | countOnes = 0;
|
---|
161 | newRealization.Clear();
|
---|
162 | for (int j = 0; j < data.Dimension; j++) {
|
---|
163 | if (ProbabilityMatrix[j] > r.NextDouble()) {
|
---|
164 | newRealization.Add(new IntValue(1));
|
---|
165 | countOnes++;
|
---|
166 | } else {
|
---|
167 | newRealization.Add(new IntValue(0));
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 | countOnes = 0;
|
---|
172 | Realizations.Add(newRealization);
|
---|
173 | }
|
---|
174 |
|
---|
175 | foreach (var op in Operators.OfType<PTSPPathMoveEvaluator>()) {
|
---|
176 | op.RealizationsParameter.Value = Realizations;
|
---|
177 | }
|
---|
178 | foreach (var op in Operators.OfType<PTSPExhaustiveInversionLocalImprovement>()) {
|
---|
179 | op.RealizationsParameter.Value = Realizations;
|
---|
180 | }
|
---|
181 | foreach (var op in Operators.OfType<PTSP25MoveEvaluator>()) {
|
---|
182 | op.RealizationsParameter.Value = Realizations;
|
---|
183 | }
|
---|
184 |
|
---|
185 | }
|
---|
186 | }
|
---|
187 | } |
---|