gravatar
deba@inf.elte.hu
deba@inf.elte.hu
Fix include files in random.h
0 1 0
default
1 file changed with 1 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 384 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
/*
20 20
 * This file contains the reimplemented version of the Mersenne Twister
21 21
 * Generator of Matsumoto and Nishimura.
22 22
 *
23 23
 * See the appropriate copyright notice below.
24 24
 * 
25 25
 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
26 26
 * All rights reserved.                          
27 27
 *
28 28
 * Redistribution and use in source and binary forms, with or without
29 29
 * modification, are permitted provided that the following conditions
30 30
 * are met:
31 31
 *
32 32
 * 1. Redistributions of source code must retain the above copyright
33 33
 *    notice, this list of conditions and the following disclaimer.
34 34
 *
35 35
 * 2. Redistributions in binary form must reproduce the above copyright
36 36
 *    notice, this list of conditions and the following disclaimer in the
37 37
 *    documentation and/or other materials provided with the distribution.
38 38
 *
39 39
 * 3. The names of its contributors may not be used to endorse or promote 
40 40
 *    products derived from this software without specific prior written 
41 41
 *    permission.
42 42
 *
43 43
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 44
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 45
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 46
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
47 47
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 48
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 49
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 50
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 51
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 52
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 53
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 54
 * OF THE POSSIBILITY OF SUCH DAMAGE.
55 55
 *
56 56
 *
57 57
 * Any feedback is very welcome.
58 58
 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
59 59
 * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
60 60
 */
61 61

	
62 62
#ifndef LEMON_RANDOM_H
63 63
#define LEMON_RANDOM_H
64 64

	
65 65
#include <algorithm>
66 66
#include <iterator>
67 67
#include <vector>
68

	
69
#include <ctime>
68
#include <limits>
70 69

	
71 70
#include <lemon/math.h>
72 71
#include <lemon/dim2.h>
73 72

	
74 73
///\ingroup misc
75 74
///\file
76 75
///\brief Mersenne Twister random number generator
77 76

	
78 77
namespace lemon {
79 78

	
80 79
  namespace _random_bits {
81 80
    
82 81
    template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
83 82
    struct RandomTraits {};
84 83

	
85 84
    template <typename _Word>
86 85
    struct RandomTraits<_Word, 32> {
87 86

	
88 87
      typedef _Word Word;
89 88
      static const int bits = 32;
90 89

	
91 90
      static const int length = 624;
92 91
      static const int shift = 397;
93 92
      
94 93
      static const Word mul = 0x6c078965u;
95 94
      static const Word arrayInit = 0x012BD6AAu;
96 95
      static const Word arrayMul1 = 0x0019660Du;
97 96
      static const Word arrayMul2 = 0x5D588B65u;
98 97

	
99 98
      static const Word mask = 0x9908B0DFu;
100 99
      static const Word loMask = (1u << 31) - 1;
101 100
      static const Word hiMask = ~loMask;
102 101

	
103 102

	
104 103
      static Word tempering(Word rnd) {
105 104
        rnd ^= (rnd >> 11);
106 105
        rnd ^= (rnd << 7) & 0x9D2C5680u;
107 106
        rnd ^= (rnd << 15) & 0xEFC60000u;
108 107
        rnd ^= (rnd >> 18);
109 108
        return rnd;
110 109
      }
111 110

	
112 111
    };
113 112

	
114 113
    template <typename _Word>
115 114
    struct RandomTraits<_Word, 64> {
116 115

	
117 116
      typedef _Word Word;
118 117
      static const int bits = 64;
119 118

	
120 119
      static const int length = 312;
121 120
      static const int shift = 156;
122 121

	
123 122
      static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
124 123
      static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
125 124
      static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
126 125
      static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
127 126

	
128 127
      static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
129 128
      static const Word loMask = (Word(1u) << 31) - 1;
130 129
      static const Word hiMask = ~loMask;
131 130

	
132 131
      static Word tempering(Word rnd) {
133 132
        rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
134 133
        rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
135 134
        rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
136 135
        rnd ^= (rnd >> 43);
137 136
        return rnd;
138 137
      }
139 138

	
140 139
    };
141 140

	
142 141
    template <typename _Word>
143 142
    class RandomCore {
144 143
    public:
145 144

	
146 145
      typedef _Word Word;
147 146

	
148 147
    private:
149 148

	
150 149
      static const int bits = RandomTraits<Word>::bits;
151 150

	
152 151
      static const int length = RandomTraits<Word>::length;
153 152
      static const int shift = RandomTraits<Word>::shift;
154 153

	
155 154
    public:
156 155

	
157 156
      void initState() {
158 157
        static const Word seedArray[4] = {
159 158
          0x12345u, 0x23456u, 0x34567u, 0x45678u
160 159
        };
161 160
    
162 161
        initState(seedArray, seedArray + 4);
163 162
      }
164 163

	
165 164
      void initState(Word seed) {
166 165

	
167 166
        static const Word mul = RandomTraits<Word>::mul;
168 167

	
169 168
        current = state; 
170 169

	
171 170
        Word *curr = state + length - 1;
172 171
        curr[0] = seed; --curr;
173 172
        for (int i = 1; i < length; ++i) {
174 173
          curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
175 174
          --curr;
176 175
        }
177 176
      }
178 177

	
179 178
      template <typename Iterator>
180 179
      void initState(Iterator begin, Iterator end) {
181 180

	
182 181
        static const Word init = RandomTraits<Word>::arrayInit;
183 182
        static const Word mul1 = RandomTraits<Word>::arrayMul1;
184 183
        static const Word mul2 = RandomTraits<Word>::arrayMul2;
185 184

	
186 185

	
187 186
        Word *curr = state + length - 1; --curr;
188 187
        Iterator it = begin; int cnt = 0;
189 188
        int num;
190 189

	
191 190
        initState(init);
192 191

	
193 192
        num = length > end - begin ? length : end - begin;
194 193
        while (num--) {
195 194
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1)) 
196 195
            + *it + cnt;
197 196
          ++it; ++cnt;
198 197
          if (it == end) {
199 198
            it = begin; cnt = 0;
200 199
          }
201 200
          if (curr == state) {
202 201
            curr = state + length - 1; curr[0] = state[0];
203 202
          }
204 203
          --curr;
205 204
        }
206 205

	
207 206
        num = length - 1; cnt = length - (curr - state) - 1;
208 207
        while (num--) {
209 208
          curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
210 209
            - cnt;
211 210
          --curr; ++cnt;
212 211
          if (curr == state) {
213 212
            curr = state + length - 1; curr[0] = state[0]; --curr;
214 213
            cnt = 1;
215 214
          }
216 215
        }
217 216
        
218 217
        state[length - 1] = Word(1) << (bits - 1);
219 218
      }
220 219
      
221 220
      void copyState(const RandomCore& other) {
222 221
        std::copy(other.state, other.state + length, state);
223 222
        current = state + (other.current - other.state);
224 223
      }
225 224

	
226 225
      Word operator()() {
227 226
        if (current == state) fillState();
228 227
        --current;
229 228
        Word rnd = *current;
230 229
        return RandomTraits<Word>::tempering(rnd);
231 230
      }
232 231

	
233 232
    private:
234 233

	
235 234
  
236 235
      void fillState() {
237 236
        static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
238 237
        static const Word loMask = RandomTraits<Word>::loMask;
239 238
        static const Word hiMask = RandomTraits<Word>::hiMask;
240 239

	
241 240
        current = state + length; 
242 241

	
243 242
        register Word *curr = state + length - 1;
244 243
        register long num;
245 244
      
246 245
        num = length - shift;
247 246
        while (num--) {
248 247
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
249 248
            curr[- shift] ^ mask[curr[-1] & 1ul];
250 249
          --curr;
251 250
        }
252 251
        num = shift - 1;
253 252
        while (num--) {
254 253
          curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
255 254
            curr[length - shift] ^ mask[curr[-1] & 1ul];
256 255
          --curr;
257 256
        }
258 257
        state[0] = (((state[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
259 258
          curr[length - shift] ^ mask[curr[length - 1] & 1ul];
260 259

	
261 260
      }
0 comments (0 inline)