arg_parser.h: A command line argument parser.
dist_log.h: A tool for measuring one and two dimensional distributions.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
20 * This file contains the reimplemented version of the Mersenne Twister
21 * Generator of Matsumoto and Nishimura.
23 * See the appropriate copyright notice below.
25 * Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
39 * 3. The names of its contributors may not be used to endorse or promote
40 * products derived from this software without specific prior written
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
47 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
49 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
52 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
54 * OF THE POSSIBILITY OF SUCH DAMAGE.
57 * Any feedback is very welcome.
58 * http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
59 * email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
62 #ifndef LEMON_RANDOM_H
63 #define LEMON_RANDOM_H
72 #include <lemon/dim2.h>
75 ///\brief Mersenne Twister random number generator
77 ///\author Balazs Dezso
81 namespace _random_bits {
83 template <typename _Word, int _bits = std::numeric_limits<_Word>::digits>
84 struct RandomTraits {};
86 template <typename _Word>
87 struct RandomTraits<_Word, 32> {
90 static const int bits = 32;
92 static const int length = 624;
93 static const int shift = 397;
95 static const Word mul = 0x6c078965u;
96 static const Word arrayInit = 0x012BD6AAu;
97 static const Word arrayMul1 = 0x0019660Du;
98 static const Word arrayMul2 = 0x5D588B65u;
100 static const Word mask = 0x9908B0DFu;
101 static const Word loMask = (1u << 31) - 1;
102 static const Word hiMask = ~loMask;
105 static Word tempering(Word rnd) {
107 rnd ^= (rnd << 7) & 0x9D2C5680u;
108 rnd ^= (rnd << 15) & 0xEFC60000u;
115 template <typename _Word>
116 struct RandomTraits<_Word, 64> {
119 static const int bits = 64;
121 static const int length = 312;
122 static const int shift = 156;
124 static const Word mul = Word(0x5851F42Du) << 32 | Word(0x4C957F2Du);
125 static const Word arrayInit = Word(0x00000000u) << 32 |Word(0x012BD6AAu);
126 static const Word arrayMul1 = Word(0x369DEA0Fu) << 32 |Word(0x31A53F85u);
127 static const Word arrayMul2 = Word(0x27BB2EE6u) << 32 |Word(0x87B0B0FDu);
129 static const Word mask = Word(0xB5026F5Au) << 32 | Word(0xA96619E9u);
130 static const Word loMask = (Word(1u) << 31) - 1;
131 static const Word hiMask = ~loMask;
133 static Word tempering(Word rnd) {
134 rnd ^= (rnd >> 29) & (Word(0x55555555u) << 32 | Word(0x55555555u));
135 rnd ^= (rnd << 17) & (Word(0x71D67FFFu) << 32 | Word(0xEDA60000u));
136 rnd ^= (rnd << 37) & (Word(0xFFF7EEE0u) << 32 | Word(0x00000000u));
143 template <typename _Word>
151 static const int bits = RandomTraits<Word>::bits;
153 static const int length = RandomTraits<Word>::length;
154 static const int shift = RandomTraits<Word>::shift;
159 static const Word seedArray[4] = {
160 0x12345u, 0x23456u, 0x34567u, 0x45678u
163 initState(seedArray, seedArray + 4);
166 void initState(Word seed) {
168 static const Word mul = RandomTraits<Word>::mul;
172 Word *curr = state + length - 1;
173 curr[0] = seed; --curr;
174 for (int i = 1; i < length; ++i) {
175 curr[0] = (mul * ( curr[1] ^ (curr[1] >> (bits - 2)) ) + i);
180 template <typename Iterator>
181 void initState(Iterator begin, Iterator end) {
183 static const Word init = RandomTraits<Word>::arrayInit;
184 static const Word mul1 = RandomTraits<Word>::arrayMul1;
185 static const Word mul2 = RandomTraits<Word>::arrayMul2;
188 Word *curr = state + length - 1; --curr;
189 Iterator it = begin; int cnt = 0;
194 num = length > end - begin ? length : end - begin;
196 curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul1))
203 curr = state + length - 1; curr[0] = state[0];
208 num = length - 1; cnt = length - (curr - state) - 1;
210 curr[0] = (curr[0] ^ ((curr[1] ^ (curr[1] >> (bits - 2))) * mul2))
214 curr = state + length - 1; curr[0] = state[0]; --curr;
219 state[length - 1] = Word(1) << (bits - 1);
222 void copyState(const RandomCore& other) {
223 std::copy(other.state, other.state + length, state);
224 current = state + (other.current - other.state);
228 if (current == state) fillState();
231 return RandomTraits<Word>::tempering(rnd);
238 static const Word mask[2] = { 0x0ul, RandomTraits<Word>::mask };
239 static const Word loMask = RandomTraits<Word>::loMask;
240 static const Word hiMask = RandomTraits<Word>::hiMask;
242 current = state + length;
244 register Word *curr = state + length - 1;
247 num = length - shift;
249 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
250 curr[- shift] ^ mask[curr[-1] & 1ul];
255 curr[0] = (((curr[0] & hiMask) | (curr[-1] & loMask)) >> 1) ^
256 curr[length - shift] ^ mask[curr[-1] & 1ul];
259 curr[0] = (((curr[0] & hiMask) | (curr[length - 1] & loMask)) >> 1) ^
260 curr[length - shift] ^ mask[curr[length - 1] & 1ul];
271 template <typename Result,
272 int shift = (std::numeric_limits<Result>::digits + 1) / 2>
274 static Result mask(const Result& result) {
275 return Masker<Result, (shift + 1) / 2>::
276 mask(static_cast<Result>(result | (result >> shift)));
280 template <typename Result>
281 struct Masker<Result, 1> {
282 static Result mask(const Result& result) {
283 return static_cast<Result>(result | (result >> 1));
287 template <typename Result, typename Word,
288 int rest = std::numeric_limits<Result>::digits, int shift = 0,
289 bool last = rest <= std::numeric_limits<Word>::digits>
290 struct IntConversion {
291 static const int bits = std::numeric_limits<Word>::digits;
293 static Result convert(RandomCore<Word>& rnd) {
294 return static_cast<Result>(rnd() >> (bits - rest)) << shift;
299 template <typename Result, typename Word, int rest, int shift>
300 struct IntConversion<Result, Word, rest, shift, false> {
301 static const int bits = std::numeric_limits<Word>::digits;
303 static Result convert(RandomCore<Word>& rnd) {
304 return (static_cast<Result>(rnd()) << shift) |
305 IntConversion<Result, Word, rest - bits, shift + bits>::convert(rnd);
310 template <typename Result, typename Word,
311 bool one_word = (std::numeric_limits<Word>::digits <
312 std::numeric_limits<Result>::digits) >
314 static Result map(RandomCore<Word>& rnd, const Result& bound) {
315 Word max = Word(bound - 1);
316 Result mask = Masker<Result>::mask(bound - 1);
319 num = IntConversion<Result, Word>::convert(rnd) & mask;
325 template <typename Result, typename Word>
326 struct Mapping<Result, Word, false> {
327 static Result map(RandomCore<Word>& rnd, const Result& bound) {
328 Word max = Word(bound - 1);
329 Word mask = Masker<Word, (std::numeric_limits<Result>::digits + 1) / 2>
339 template <typename Result, int exp, bool pos = (exp >= 0)>
340 struct ShiftMultiplier {
341 static const Result multiplier() {
342 Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
344 if ((exp & 1) == 1) res *= static_cast<Result>(2.0);
349 template <typename Result, int exp>
350 struct ShiftMultiplier<Result, exp, false> {
351 static const Result multiplier() {
352 Result res = ShiftMultiplier<Result, exp / 2>::multiplier();
354 if ((exp & 1) == 1) res *= static_cast<Result>(0.5);
359 template <typename Result>
360 struct ShiftMultiplier<Result, 0, true> {
361 static const Result multiplier() {
362 return static_cast<Result>(1.0);
366 template <typename Result>
367 struct ShiftMultiplier<Result, -20, true> {
368 static const Result multiplier() {
369 return static_cast<Result>(1.0/1048576.0);
373 template <typename Result>
374 struct ShiftMultiplier<Result, -32, true> {
375 static const Result multiplier() {
376 return static_cast<Result>(1.0/424967296.0);
380 template <typename Result>
381 struct ShiftMultiplier<Result, -53, true> {
382 static const Result multiplier() {
383 return static_cast<Result>(1.0/9007199254740992.0);
387 template <typename Result>
388 struct ShiftMultiplier<Result, -64, true> {
389 static const Result multiplier() {
390 return static_cast<Result>(1.0/18446744073709551616.0);
394 template <typename Result, int exp>
396 static Result shift(const Result& result) {
397 return result * ShiftMultiplier<Result, exp>::multiplier();
401 template <typename Result, typename Word,
402 int rest = std::numeric_limits<Result>::digits, int shift = 0,
403 bool last = rest <= std::numeric_limits<Word>::digits>
404 struct RealConversion{
405 static const int bits = std::numeric_limits<Word>::digits;
407 static Result convert(RandomCore<Word>& rnd) {
408 return Shifting<Result, - shift - rest>::
409 shift(static_cast<Result>(rnd() >> (bits - rest)));
413 template <typename Result, typename Word, int rest, int shift>
414 struct RealConversion<Result, Word, rest, shift, false> {
415 static const int bits = std::numeric_limits<Word>::digits;
417 static Result convert(RandomCore<Word>& rnd) {
418 return Shifting<Result, - shift - bits>::
419 shift(static_cast<Result>(rnd())) +
420 RealConversion<Result, Word, rest-bits, shift + bits>::
425 template <typename Result, typename Word>
428 template <typename Iterator>
429 static void init(RandomCore<Word>& rnd, Iterator begin, Iterator end) {
430 std::vector<Word> ws;
431 for (Iterator it = begin; it != end; ++it) {
432 ws.push_back(Word(*it));
434 rnd.initState(ws.begin(), ws.end());
437 static void init(RandomCore<Word>& rnd, Result seed) {
442 template <typename Word>
443 struct BoolConversion {
444 static bool convert(RandomCore<Word>& rnd) {
445 return (rnd() & 1) == 1;
449 template <typename Word>
450 struct BoolProducer {
454 BoolProducer() : num(0) {}
456 bool convert(RandomCore<Word>& rnd) {
459 num = RandomTraits<Word>::bits;
461 bool r = (buffer & 1);
472 /// \brief Mersenne Twister random number generator
474 /// The Mersenne Twister is a twisted generalized feedback
475 /// shift-register generator of Matsumoto and Nishimura. The period
476 /// of this generator is \f$ 2^{19937} - 1 \f$ and it is
477 /// equi-distributed in 623 dimensions for 32-bit numbers. The time
478 /// performance of this generator is comparable to the commonly used
481 /// This implementation is specialized for both 32-bit and 64-bit
482 /// architectures. The generators differ sligthly in the
483 /// initialization and generation phase so they produce two
484 /// completly different sequences.
486 /// The generator gives back random numbers of serveral types. To
487 /// get a random number from a range of a floating point type you
488 /// can use one form of the \c operator() or the \c real() member
489 /// function. If you want to get random number from the {0, 1, ...,
490 /// n-1} integer range use the \c operator[] or the \c integer()
491 /// method. And to get random number from the whole range of an
492 /// integer type you can use the argumentless \c integer() or \c
493 /// uinteger() functions. After all you can get random bool with
494 /// equal chance of true and false or given probability of true
495 /// result with the \c boolean() member functions.
498 /// // The commented code is identical to the other
499 /// double a = rnd(); // [0.0, 1.0)
500 /// // double a = rnd.real(); // [0.0, 1.0)
501 /// double b = rnd(100.0); // [0.0, 100.0)
502 /// // double b = rnd.real(100.0); // [0.0, 100.0)
503 /// double c = rnd(1.0, 2.0); // [1.0, 2.0)
504 /// // double c = rnd.real(1.0, 2.0); // [1.0, 2.0)
505 /// int d = rnd[100000]; // 0..99999
506 /// // int d = rnd.integer(100000); // 0..99999
507 /// int e = rnd[6] + 1; // 1..6
508 /// // int e = rnd.integer(1, 1 + 6); // 1..6
509 /// int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1
510 /// int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1
511 /// bool g = rnd.boolean(); // P(g = true) = 0.5
512 /// bool h = rnd.boolean(0.8); // P(h = true) = 0.8
515 /// The lemon provides a global instance of the random number
516 /// generator which name is \ref lemon::rnd "rnd". Usually it is a
517 /// good programming convenience to use this global generator to get
520 /// \author Balazs Dezso
525 typedef unsigned long Word;
527 _random_bits::RandomCore<Word> core;
528 _random_bits::BoolProducer<Word> bool_producer;
533 /// \brief Constructor
535 /// Constructor with constant seeding.
536 Random() { core.initState(); }
538 /// \brief Constructor
540 /// Constructor with seed. The current number type will be converted
541 /// to the architecture word type.
542 template <typename Number>
543 Random(Number seed) {
544 _random_bits::Initializer<Number, Word>::init(core, seed);
547 /// \brief Constructor
549 /// Constructor with array seeding. The given range should contain
550 /// any number type and the numbers will be converted to the
551 /// architecture word type.
552 template <typename Iterator>
553 Random(Iterator begin, Iterator end) {
554 typedef typename std::iterator_traits<Iterator>::value_type Number;
555 _random_bits::Initializer<Number, Word>::init(core, begin, end);
558 /// \brief Copy constructor
560 /// Copy constructor. The generated sequence will be identical to
561 /// the other sequence. It can be used to save the current state
562 /// of the generator and later use it to generate the same
564 Random(const Random& other) {
565 core.copyState(other.core);
568 /// \brief Assign operator
570 /// Assign operator. The generated sequence will be identical to
571 /// the other sequence. It can be used to save the current state
572 /// of the generator and later use it to generate the same
574 Random& operator=(const Random& other) {
575 if (&other != this) {
576 core.copyState(other.core);
581 /// \brief Returns a random real number from the range [0, 1)
583 /// It returns a random real number from the range [0, 1). The
584 /// default Number type is double.
585 template <typename Number>
587 return _random_bits::RealConversion<Number, Word>::convert(core);
591 return real<double>();
594 /// \brief Returns a random real number the range [0, b)
596 /// It returns a random real number from the range [0, b).
597 template <typename Number>
598 Number real(Number b) {
599 return real<Number>() * b;
602 /// \brief Returns a random real number from the range [a, b)
604 /// It returns a random real number from the range [a, b).
605 template <typename Number>
606 Number real(Number a, Number b) {
607 return real<Number>() * (b - a) + a;
610 /// \brief Returns a random real number from the range [0, 1)
612 /// It returns a random double from the range [0, 1).
613 double operator()() {
614 return real<double>();
617 /// \brief Returns a random real number from the range [0, b)
619 /// It returns a random real number from the range [0, b).
620 template <typename Number>
621 Number operator()(Number b) {
622 return real<Number>() * b;
625 /// \brief Returns a random real number from the range [a, b)
627 /// It returns a random real number from the range [a, b).
628 template <typename Number>
629 Number operator()(Number a, Number b) {
630 return real<Number>() * (b - a) + a;
633 /// \brief Returns a random integer from a range
635 /// It returns a random integer from the range {0, 1, ..., b - 1}.
636 template <typename Number>
637 Number integer(Number b) {
638 return _random_bits::Mapping<Number, Word>::map(core, b);
641 /// \brief Returns a random integer from a range
643 /// It returns a random integer from the range {a, a + 1, ..., b - 1}.
644 template <typename Number>
645 Number integer(Number a, Number b) {
646 return _random_bits::Mapping<Number, Word>::map(core, b - a) + a;
649 /// \brief Returns a random integer from a range
651 /// It returns a random integer from the range {0, 1, ..., b - 1}.
652 template <typename Number>
653 Number operator[](Number b) {
654 return _random_bits::Mapping<Number, Word>::map(core, b);
657 /// \brief Returns a random non-negative integer
659 /// It returns a random non-negative integer uniformly from the
660 /// whole range of the current \c Number type. The default result
661 /// type of this function is unsigned int.
662 template <typename Number>
664 return _random_bits::IntConversion<Number, Word>::convert(core);
667 unsigned int uinteger() {
668 return uinteger<unsigned int>();
671 /// \brief Returns a random integer
673 /// It returns a random integer uniformly from the whole range of
674 /// the current \c Number type. The default result type of this
676 template <typename Number>
678 static const int nb = std::numeric_limits<Number>::digits +
679 (std::numeric_limits<Number>::is_signed ? 1 : 0);
680 return _random_bits::IntConversion<Number, Word, nb>::convert(core);
684 return integer<int>();
687 /// \brief Returns a random bool
689 /// It returns a random bool. The generator holds a buffer for
690 /// random bits. Every time when it become empty the generator makes
691 /// a new random word and fill the buffer up.
693 return bool_producer.convert(core);
696 ///\name Nonuniform distributions
701 /// \brief Returns a random bool
703 /// It returns a random bool with given probability of true result
704 bool boolean(double p) {
705 return operator()() < p;
708 /// Standard Gauss distribution
710 /// Standard Gauss distribution.
711 /// \note The Cartesian form of the Box-Muller
712 /// transformation is used to generate a random normal distribution.
713 /// \todo Consider using the "ziggurat" method instead.
718 V1=2*real<double>()-1;
719 V2=2*real<double>()-1;
722 return std::sqrt(-2*std::log(S)/S)*V1;
724 /// Gauss distribution with given standard deviation and mean 0
728 double gauss(double std_dev)
730 return gauss()*std_dev;
732 /// Gauss distribution with given mean and standard deviation
736 double gauss(double mean,double std_dev)
738 return gauss()*std_dev+mean;
741 /// Exponential distribution with given mean
743 /// This function generates an exponential distribution random number
744 /// with mean <tt>1/lambda</tt>.
746 double exponential(double lambda=1.0)
748 return -std::log(real<double>())/lambda;
753 ///\name Two dimensional distributions
758 /// Uniform distribution on the full unit circle.
759 dim2::Point<double> disc()
763 V1=2*real<double>()-1;
764 V2=2*real<double>()-1;
766 } while(V1*V1+V2*V2>=1);
767 return dim2::Point<double>(V1,V2);
769 /// A kind of two dimensional Gauss distribution
771 /// This function provides a turning symmetric two-dimensional distribution.
772 /// Both coordinates are of standard normal distribution, but they are not
775 /// \note The coordinates are the two random variables provided by
776 /// the Box-Muller method.
777 dim2::Point<double> gauss2()
781 V1=2*real<double>()-1;
782 V2=2*real<double>()-1;
785 double W=std::sqrt(-2*std::log(S)/S);
786 return dim2::Point<double>(W*V1,W*V2);
788 /// A kind of two dimensional exponential distribution
790 /// This function provides a turning symmetric two-dimensional distribution.
791 /// The x-coordinate is of conditionally exponential distribution
792 /// with the condition that x is positive and y=0. If x is negative and
793 /// y=0 then, -x is of exponential distribution. The same is true for the
795 dim2::Point<double> exponential2()
799 V1=2*real<double>()-1;
800 V2=2*real<double>()-1;
803 double W=-std::log(S)/S;
804 return dim2::Point<double>(W*V1,W*V2);