gaussian_sampling.cc 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

B
Bo Zhou 已提交
15
#include "evo_kit/gaussian_sampling.h"
16

B
Bo Zhou 已提交
17
namespace evo_kit {
18

B
Bo Zhou 已提交
19
bool GaussianSampling::load_config(const EvoKitConfig& config) {
20 21 22 23
    bool success = true;
    _std = config.gaussian_sampling().std();
    success = set_seed(config.seed());
    return success;
24 25
}

26
bool GaussianSampling::sampling(int* key, float* noise, int64_t size) {
27 28 29
    bool success = true;

    if (noise == nullptr) {
B
Bo Zhou 已提交
30
        LOG(ERROR) << "[EvoKit] Input noise array cannot be nullptr.";
31 32 33 34 35 36 37 38 39 40 41 42 43
        success = false;
        return success;
    }

    int rand_key = rand();
    *key = rand_key;
    std::default_random_engine generator(rand_key);
    std::normal_distribution<float> norm;

    for (int64_t i = 0; i < size; ++i) {
        *(noise + i) = norm(generator) * _std;
    }

44
    return success;
45 46
}

Z
zenghsh3 已提交
47
bool GaussianSampling::resampling(int key, float* noise, int64_t size) {
48 49 50
    bool success = true;

    if (noise == nullptr) {
B
Bo Zhou 已提交
51
        LOG(ERROR) << "[EvoKit] Input noise array cannot be nullptr.";
52 53 54 55 56 57 58 59
        success = false;
    } else {
        std::default_random_engine generator(key);
        std::normal_distribution<float> norm;

        for (int64_t i = 0; i < size; ++i) {
            *(noise + i) = norm(generator) * _std;
        }
60
    }
61 62

    return success;
63 64 65
}

}