aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/random_normal_stable_feature.cpp
blob: 19d9e84c588a5d9b2a34db5adb8ef240f9e4e65c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "random_normal_stable_feature.h"
#include "utils.h"
#include <vespa/searchlib/fef/properties.h>
#include <vespa/vespalib/util/stash.h>
#include <cinttypes>

#include <vespa/log/log.h>
LOG_SETUP(".features.randomnormalstablefeature");

namespace search::features {

RandomNormalStableExecutor::RandomNormalStableExecutor(uint64_t seed, double mean, double stddev) :
    search::fef::FeatureExecutor(),
    _rnd(mean, stddev, false),  // don't use spares, as we reset seed on every generation
    _seed(seed)
{
    LOG(debug, "RandomNormalStableExecutor: seed=%" PRIu64 ", mean=%f, stddev=%f", seed, mean, stddev);
}

void
RandomNormalStableExecutor::execute(uint32_t docId)
{
    _rnd.seed(_seed + docId);
    outputs().set_number(0, _rnd.next());
}

RandomNormalStableBlueprint::RandomNormalStableBlueprint() :
    search::fef::Blueprint("randomNormalStable"),
    _seed(0),
    _mean(0.0),
    _stddev(1.0)
{
}

void
RandomNormalStableBlueprint::visitDumpFeatures(const search::fef::IIndexEnvironment &,
                                   search::fef::IDumpFeatureVisitor &) const
{
}

search::fef::Blueprint::UP
RandomNormalStableBlueprint::createInstance() const
{
    return std::make_unique<RandomNormalStableBlueprint>();
}

bool
RandomNormalStableBlueprint::setup(const search::fef::IIndexEnvironment & env,
                       const search::fef::ParameterList & params)
{
    search::fef::Property p = env.getProperties().lookup(getName(), "seed");
    if (p.found()) {
        _seed = util::strToNum<uint64_t>(p.get());
    }
    if (params.size() > 0) {
        _mean = params[0].asDouble();
    }
    if (params.size() > 1) {
        _stddev = params[1].asDouble();
    }

    describeOutput("out" , "A random value drawn from the Gaussian distribution that is stable for a given match (document and query)");

    return true;
}

search::fef::FeatureExecutor &
RandomNormalStableBlueprint::createExecutor(const search::fef::IQueryEnvironment &env, vespalib::Stash &stash) const
{
    uint64_t seed = _seed;
    if (seed == 0) {
        seed = util::strToNum<uint64_t>
                (env.getProperties().lookup(getName(), "seed").get("1024")); // default seed
    }
    return stash.create<RandomNormalStableExecutor>(seed, _mean, _stddev);
}

}