Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.Instances.TSPLIB/3.3/TSPLIBHeterogeneousPTSPInstanceProvider.cs @ 17912

Last change on this file since 17912 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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;
25
26namespace HeuristicLab.Problems.Instances.TSPLIB {
27  public class TSPLIBHeterogeneousPTSPInstanceProvider : TSPLIBPTSPInstanceProvider {
28
29    public override string Name {
30      get { return "TSPLIB (heterogeneous symmetric PTSP)"; }
31    }
32
33    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
34      foreach (var desc in base.GetDataDescriptors().OfType<TSPLIBDataDescriptor>()) {
35        desc.SolutionIdentifier = null;
36        yield return desc;
37      }
38    }
39
40    protected override double[] GetProbabilities(IDataDescriptor descriptor, PTSPData instance) {
41      var random = new MarsagliaRandom(GetInstanceHash(instance.Name));
42      return Enumerable.Range(0, instance.Dimension).Select(_ => (int)Math.Round((0.1 + 0.9 * random.NextDouble()) * 100) / 100.0).ToArray();
43    }
44
45    // Bernstein's hash function
46    private uint GetInstanceHash(string name) {
47      uint hash = 5381;
48      var len = name.Length;
49      for (var i = 0; i < len; i++)
50        unchecked { hash = ((hash * 33) + name[i]); }
51      return hash;
52    }
53
54    /// <summary>
55    /// This class is used to randomly generate PTSP instances given the TSPLIB instances.
56    /// An own implementation of a RNG was used in order to avoid possible implementation changes
57    /// in future .NET versions which would result in entirely different instances.
58    /// </summary>
59    /// <remarks>
60    /// RNG is implemented according to George Marsaglia https://en.wikipedia.org/wiki/Multiply-with-carry
61    /// </remarks>
62    private class MarsagliaRandom {
63      /*
64       * S = 2111111111*X[n-4] + 1492*X[n-3] + 1776*X[n-2] + 5115*X[n-1] + C
65       * X[n] = S modulo 2^32
66       * C = floor(S / 2^32)
67       *
68       */
69      private readonly uint[] mem = new uint[4];
70      private uint c;
71
72      public MarsagliaRandom(uint s) {
73        int i;
74        for (i = 0; i < mem.Length; i++) {
75          unchecked { s = s * 31294061 + 1; }
76          mem[i] = s;
77        }
78        unchecked { c = s * 31294061 + 1; }
79      }
80
81      private uint Next() {
82        unchecked {
83          ulong wsum = 2111111111 * mem[0]
84                      + 1492 * mem[1]
85                      + 1776 * mem[2]
86                      + 5115 * mem[3]
87                      + c;
88
89          mem[0] = mem[1];
90          mem[1] = mem[2];
91          mem[2] = mem[3];
92          mem[3] = (uint)wsum;
93          c = (uint)(wsum >> 32);
94          return mem[3];
95        }
96      }
97
98      public double NextDouble() {
99        return (double)Next() / uint.MaxValue;
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.