简体   繁体   English

如何使用 Boost 序列化 mersenne_twister_engine?

[英]How can a mersenne_twister_engine be serialized using Boost?

I have a class that implements std::mt19937 and I would like to be able to save and load the state of the random number generator for consistency.我有一个实现 std::mt19937 的 class 并且我希望能够保存和加载随机数生成器的 state 以保持一致性。 I'm aware that the << and >> operators can be used to save/load the engine's state and that I can separate the save and load functions when I archive the file if necessary.我知道 << 和 >> 运算符可用于保存/加载引擎的 state,并且在必要时存档文件时可以将保存和加载功能分开。 My guess at how to do this would be to use the << and >> operators to store the state into/extract the state from an object that can be serialized.我对如何做到这一点的猜测是使用 << 和 >> 运算符将 state 存储到可以序列化的 object 中/从 state 中提取。 My questions are我的问题是

  1. What object can be used to store the state of the mersenne_twister_engine that can also be serialized using Boost?什么 object 可以用来存储 mersenne_twister_engine 的 state 也可以用 Boost 序列化?
  2. Is my approach safe and generally considered to be good practice?我的方法是否安全并且通常被认为是良好的做法?
  3. Is there an approach to this that is generally considered to be better?有没有一种通常被认为更好的方法?

From this output/input operator reference regarding the output operator:这个关于 output 运算符的输出/输入运算符参考

Serializes the internal state of the pseudo-random number engine e as a sequence of decimal numbers separated by one or more spaces, and inserts it to the stream ost .将伪随机数引擎 e 的内部 state 序列化为由一个或多个空格分隔的十进制数序列,并将其插入到 stream ost中。

The stream could be any kind of output stream, for example an output string stream . The stream could be any kind of output stream, for example an output string stream . You can then use the string that the string-streams created and pass it on to whatever serialization framework you need.然后,您可以使用字符串流创建的字符串并将其传递给您需要的任何序列化框架。

Or if the serialization framework support direct output/input stream operations, you can use it directly with the engine operators.或者,如果序列化框架支持直接输出/输入 stream 操作,您可以直接与引擎操作符一起使用。

不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine <long unsigned int,< div><div id="text_translate"><p> 有我的.hpp:</p><pre> #include &lt;random&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #ifndef RECUIT_HPP #define RECUIT_HPP template &lt; class E, class Func, class TempSeq, class RandomY, class RNG&gt; E recuit_simule(const Func &amp; phi, E x0, const TempSeq &amp; T, const RandomY &amp; Y, RNG &amp; G, long unsigned N) { std::uniform_real_distribution&lt;double&gt; U(0,1); E y; double u; for(int i=0; i&lt;N; i++) { y=Y(x0, G); u=U(G); if(u &lt;= fmin(1, exp((phi(x0) - phi(y))/T(N)))) { x0=y; } } return x0; } #endif</pre><p> 和我的.cpp:</p><pre> #include "recuit.hpp" #include &lt;iostream&gt; class Y { private: std::normal_distribution&lt;double&gt; N; public: Y(): N(0,1) {} double operator()(const double &amp; x, std::mt19937 &amp; G) { return x + N(G); } }; int main() { auto phi=[](const double &amp; x) { return x*x*x*x*x*x - 48*x*x; }; auto T=[] (long unsigned n) { return 10 * pow(0.9, n); }; YA; std::mt19937 G; double x = recuit_simule(phi, 0, T, A, G, 1000); std::cout &lt;&lt; x &lt;&lt; std::endl; return 0; }</pre><p> 当我编译 my.cpp 时,my.hpp 中有以下错误:</p><blockquote><p> recuit.hpp:17:6: 错误: 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine&amp;)'</p></blockquote><p> 对于该行:</p><pre> y=Y(x0, G);</pre><p> 我不明白为什么</p></div></long> - no match for call to ‘(const Y) (int&, std::mersenne_twister_engine<long unsigned int,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何为小于标准的 std::mersenne_twister_engine 选择参数? - How should I choose parameters for a smaller-than-standard std::mersenne_twister_engine? 为什么 mersenne_twister_engine 保证某些结果? - Why does the mersenne_twister_engine guarantee certain results? std::mersenne_twister_engine 和随机数生成 - std::mersenne_twister_engine and random number generation 如何保存std :: mersenne_twister_engine的状态以便以后恢复? - How do I save the state of std::mersenne_twister_engine to restore it later? C ++ 11 mersenne_twister_engine类的问题 - Issues with c++ 11 mersenne_twister_engine class 在不同的浮点精度之间切换时,使用我自己的std :: mersenne_twister_engine模板参数 - Using my own template parameters for std::mersenne_twister_engine when switching between different floating point precisions 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine <long unsigned int,< div><div id="text_translate"><p> 有我的.hpp:</p><pre> #include &lt;random&gt; #include &lt;ctime&gt; #include &lt;cmath&gt; #ifndef RECUIT_HPP #define RECUIT_HPP template &lt; class E, class Func, class TempSeq, class RandomY, class RNG&gt; E recuit_simule(const Func &amp; phi, E x0, const TempSeq &amp; T, const RandomY &amp; Y, RNG &amp; G, long unsigned N) { std::uniform_real_distribution&lt;double&gt; U(0,1); E y; double u; for(int i=0; i&lt;N; i++) { y=Y(x0, G); u=U(G); if(u &lt;= fmin(1, exp((phi(x0) - phi(y))/T(N)))) { x0=y; } } return x0; } #endif</pre><p> 和我的.cpp:</p><pre> #include "recuit.hpp" #include &lt;iostream&gt; class Y { private: std::normal_distribution&lt;double&gt; N; public: Y(): N(0,1) {} double operator()(const double &amp; x, std::mt19937 &amp; G) { return x + N(G); } }; int main() { auto phi=[](const double &amp; x) { return x*x*x*x*x*x - 48*x*x; }; auto T=[] (long unsigned n) { return 10 * pow(0.9, n); }; YA; std::mt19937 G; double x = recuit_simule(phi, 0, T, A, G, 1000); std::cout &lt;&lt; x &lt;&lt; std::endl; return 0; }</pre><p> 当我编译 my.cpp 时,my.hpp 中有以下错误:</p><blockquote><p> recuit.hpp:17:6: 错误: 不匹配调用 '(const Y) (int&amp;, std::mersenne_twister_engine&amp;)'</p></blockquote><p> 对于该行:</p><pre> y=Y(x0, G);</pre><p> 我不明白为什么</p></div></long> - no match for call to ‘(const Y) (int&, std::mersenne_twister_engine<long unsigned int, 提升Mersenne Twister:如何使用多个值种子? - Boost Mersenne Twister: how to seed with more than one value? boost c ++使用Mersenne Twister算法生成0到1之间的相同随机数吗? - boost c++ is generating the same random number between 0 and 1, using the Mersenne Twister algorithm? 如何在函数中运行Mersenne Twister? - How to run Mersenne Twister inside a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM