Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking/3.3/Random/SRand48.cs @ 16147

Last change on this file since 16147 was 15617, checked in by rhanghof, 7 years ago

#2817:

  • The items can be rotated and tilted now.
  • Added pruning of extreme points in packed bins.
  • Added new packer which packs items by positioning them on the point with the minimum of wasted space. He uses rotating and tilting of items.
  • Added classes for sorting given items.
File size: 6.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
23using HeuristicLab.Core;
24using System;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.BinPacking.Random {
29  /*
30   * C# port of the implementation of the srand48 random generator.
31   * First implemented in test3dbpp.c by S. Martello, D. Pisinger, D. Vigo, E. den Boef, J. Korst (2003)
32   */
33  [Item("SRand", "A pseudo random number generator which creates uniformly distributed random numbers.")]
34  [StorableClass]
35  public sealed class SRand48 : Item, IRandom {
36    private object locker = new object();
37
38    [Storable]
39    private bool init = false;
40    [Storable]
41    private uint _h48;
42    [Storable]
43    private uint _l48;
44   
45
46    /// <summary>
47    /// Used by HeuristicLab.Persistence to initialize new instances during deserialization.
48    /// </summary>
49    /// <param name="deserializing">true, if the constructor is called during deserialization.</param>
50    [StorableConstructor]
51    private SRand48(bool deserializing) : base(deserializing) { }
52
53    /// <summary>
54    /// Initializes a new instance from an existing one (copy constructor).
55    /// </summary>
56    /// <param name="original">The original <see cref="SRand48"/> instance which is used to initialize the new instance.</param>
57    /// <param name="cloner">A <see cref="Cloner"/> which is used to track all already cloned objects in order to avoid cycles.</param>
58    private SRand48(SRand48 original, Cloner cloner)
59      : base(original, cloner) {
60      _h48 = original._h48;
61      _l48 = original._l48;
62      init = original.init;
63    }
64
65
66    /// <summary>
67    /// Initializes a new instance of <see cref="SRand48"/>.
68    /// </summary>
69    public SRand48() {
70      if (!init) {
71        Seed((uint)DateTime.Now.Ticks);
72        init = true;
73      }
74    }
75
76    /// <summary>
77    /// Initializes a new instance of <see cref="SRand48"/>
78    /// with the given seed <paramref name="seed"/>.
79    /// </summary>
80    /// <param name="seed">The seed with which to initialize the random number generator.</param>
81    public SRand48(uint seed) {
82      if (!init) {
83        Seed(seed);
84        init = true;
85      }
86    }
87
88    /// <summary>
89    /// Clones the current instance (deep clone).
90    /// </summary>
91    /// <param name="cloner">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
92    /// <returns>The cloned object as <see cref="SRand48"/>.</returns>
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new SRand48(this, cloner);
95    }
96
97    /// <summary>
98    /// Gets a new random number.
99    /// </summary>
100    /// <returns>A new int random number.</returns>
101    public int Next() {
102      lock (locker) {
103        return (int)LRand48x();
104      }
105    }
106
107    /// <summary>
108    /// Gets a new random number being smaller than the given <paramref name="maxVal"/>.
109    /// </summary>
110    /// <exception cref="ArgumentException">Thrown when the given maximum value is
111    /// smaller or equal to zero.</exception>
112    /// <param name="maxVal">The maximum value of the generated random number.</param>
113    /// <returns>A new int random number.</returns>
114    public int Next(int maxVal) {
115      lock (locker) {
116        if (maxVal <= 0)
117          throw new ArgumentException("The interval [0, " + maxVal + ") is empty");
118        return (int)(LRand48x() % maxVal);
119      }
120    }
121
122    /// <summary>
123    /// Gets a new random number being in the given interval <paramref name="minVal"/> and
124    /// <paramref name="maxVal"/>.
125    /// </summary>
126    /// <param name="minVal">The minimum value of the generated random number.</param>
127    /// <param name="maxVal">The maximum value of the generated random number.</param>
128    /// <returns>A new int random number.</returns>
129    public int Next(int minVal, int maxVal) {
130      lock (locker) {
131        if (maxVal <= minVal)
132          throw new ArgumentException("The interval [" + minVal + ", " + maxVal + ") is empty");
133        return Next(maxVal - minVal + 1) + minVal;
134      }
135    }
136
137    /// <summary>
138    /// Gets a new double random variable.
139    /// </summary>
140    /// <returns></returns>
141    public double NextDouble() {
142      lock (locker) {
143        return ((double)Next()) * (1.0 / 4294967296.0);
144      }
145    }
146
147    /// <summary>
148    /// Resets the current random number generator.
149    /// </summary>
150    public void Reset() {
151      lock (locker) {
152        Seed((uint)DateTime.Now.Ticks);
153      }
154    }
155
156    /// <summary>
157    /// Resets the current random number generator with the given seed <paramref name="s"/>.
158    /// </summary>
159    /// <param name="seed">The seed with which to reset the current instance.</param>
160    public void Reset(int seed) {
161      lock (locker) {
162        Seed((uint)seed);
163      }
164    }
165
166    /// <summary>
167    /// Initializes current instance with random seed.
168    /// </summary>
169    /// <param name="seed">A starting seed.</param>
170    private void Seed(uint seed) {
171      _h48 = seed;
172      _l48 = 0x330E;
173    }
174
175    private int LRand48x() {
176      _h48 = (_h48 * 0xDEECE66D) + (_l48 * 0x5DEEC);
177      _l48 = _l48 * 0xE66D + 0xB;
178      _h48 = _h48 + (_l48 >> 16);
179      _l48 = _l48 & 0xFFFF;
180      return (int)(_h48 >> 1);
181    }
182   
183  }
184}
Note: See TracBrowser for help on using the repository browser.