diff --git a/example/ex9.cpp b/example/ex9.cpp index f177430..0bdeb5e 100644 --- a/example/ex9.cpp +++ b/example/ex9.cpp @@ -46,10 +46,11 @@ int main(int argc, char const *argv[]) lgd_para p = e.default_lgd_para(); p.beta = 1.2; + p.seed = 125; e.set_lgd_para(p); array dist; - e.get_levy_distribution(dist, 125); + e.get_levy_distribution(dist); double m = dist.mean(); double s = dist.std(); diff --git a/lib/optimization/lgd.cpp b/lib/optimization/lgd.cpp index 6edfd93..ee46e48 100644 --- a/lib/optimization/lgd.cpp +++ b/lib/optimization/lgd.cpp @@ -30,7 +30,7 @@ /** * Default parameter for the Lévy-Gradient Descent (L-GD) method. */ -static const gctl::lgd_para lgd_defparam = {1000, 0, 1e-5, 1.0, 1.5, 0.01, 1e-8, -1.0}; +static const gctl::lgd_para lgd_defparam = {1000, 0, 0, 1e-5, 1.0, 1.5, 0.01, 1e-8, -1.0}; gctl::lgd_solver::lgd_solver() { @@ -154,7 +154,7 @@ void gctl::lgd_solver::save_lgd_trace(std::string trace_file) return; } -gctl::lgd_return_code gctl::lgd_solver::get_levy_distribution(array &dist, unsigned int seed) +gctl::lgd_return_code gctl::lgd_solver::get_levy_distribution(array &dist) { if (lgd_param_.flight_times <= 0) return LGD_INVALID_MAX_ITERATIONS; if (lgd_param_.beta <= 1.0 || lgd_param_.beta >= 2.0) return LGD_INVALID_BETA; @@ -164,8 +164,11 @@ gctl::lgd_return_code gctl::lgd_solver::get_levy_distribution(array &dis double stddev_u = pow((gamma1*sin(0.5*GCTL_Pi*lgd_param_.beta))/ (gamma2*lgd_param_.beta*pow(2, 0.5*(lgd_param_.beta-1.0))), 1.0/lgd_param_.beta); - if (seed == 0) seed = std::chrono::system_clock::now().time_since_epoch().count(); - std::default_random_engine generator(seed); + unsigned int sd; + if (lgd_param_.seed == 0) sd = std::chrono::system_clock::now().time_since_epoch().count(); + else sd = lgd_param_.seed; + + std::default_random_engine generator(sd); std::normal_distribution dist_u(0, stddev_u); std::normal_distribution dist_v(0, lgd_param_.stddev_v); std::uniform_real_distribution dist_s(1.0, 2.0); @@ -386,8 +389,11 @@ gctl::lgd_return_code gctl::lgd_solver::lgd(array &best_m, array double gamma2 = tgamma(0.5*(lgd_param_.beta + 1.0)); double stddev_u = pow((gamma1*sin(0.5*GCTL_Pi*lgd_param_.beta)) / (gamma2*lgd_param_.beta*pow(2, 0.5*(lgd_param_.beta-1.0))), 1.0/lgd_param_.beta); - unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); - std::default_random_engine generator(seed); + unsigned int sd; + if (lgd_param_.seed == 0) sd = std::chrono::system_clock::now().time_since_epoch().count(); + else sd = lgd_param_.seed; + + std::default_random_engine generator(sd); std::normal_distribution dist_u(0, stddev_u); std::normal_distribution dist_v(0, lgd_param_.stddev_v); std::uniform_real_distribution dist_s(1.0, 2.0); diff --git a/lib/optimization/lgd.h b/lib/optimization/lgd.h index c6c7d2f..24fbfe6 100644 --- a/lib/optimization/lgd.h +++ b/lib/optimization/lgd.h @@ -88,6 +88,12 @@ namespace gctl */ int batch; + /** + * Random seed for generating the Lévy distribution. Input zero for setting the seed according + * to the current time value. The default is 0. + */ + int seed; + /** * Epsilon for the mean convergence test. This parameter determines the accuracy * with which the mean solution is to be found. The default is 1e-5. @@ -176,11 +182,10 @@ namespace gctl * @brief 按照levy分布采样一组数据,参数可通过set_lgd_para函数设置 * * @param dist 返回的levy分布 - * @param seed 随机种子 * * @return 状态代码 */ - lgd_return_code get_levy_distribution(array &dist, unsigned int seed = 0); + lgd_return_code get_levy_distribution(array &dist); lgd_para default_lgd_para(); void set_lgd_para(const lgd_para ¶m);