FeatureNode.cpp 3.58 KB
Newer Older
1
// Copyright 2021 Thomas A. R. Purcell
2
//
3
4
5
// 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
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// 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.
14
15
16
17
18
19
20
21
22
23

/** @file feature_creation/node/FeatureNode.cpp
 *  @brief Implements the class that represent the primary features in SISSO
 *
 *  @author Thomas A. R. Purcell (tpurcell90)
 *  @bug No known bugs.
 *
 *  This class represents the leaves of all of the binary expression trees.
 */

Thomas Purcell's avatar
Thomas Purcell committed
24
#include "feature_creation/node/FeatureNode.hpp"
Thomas Purcell's avatar
Thomas Purcell committed
25

Thomas Purcell's avatar
Thomas Purcell committed
26
27
28
FeatureNode::FeatureNode()
{}

29
FeatureNode::FeatureNode(const unsigned long int feat_ind, const std::string expr, const std::vector<double> value, const std::vector<double> test_value, const Unit unit, const bool set_val) :
30
31
    Node(feat_ind, value.size(), test_value.size()),
    _value(value),
32
33
    _test_value(test_value),
    _unit(unit),
34
    _expr(expr)
35
{
Thomas Purcell's avatar
Thomas Purcell committed
36
37
    if(set_val)
    {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
        // Automatically resize the storage arrays
        if(node_value_arrs::N_STORE_FEATURES == 0)
        {
            node_value_arrs::initialize_values_arr(_n_samp, _n_samp_test, 1);
        }
        else if((_n_samp != node_value_arrs::N_SAMPLES) || (_n_samp_test != node_value_arrs::N_SAMPLES_TEST))
        {
            throw std::logic_error(
                "Number of samples in current feature is not the same as the others, (" +
                std::to_string(_n_samp) +
                " and " + std::to_string(_n_samp_test) +
                " vs. "  +
                std::to_string(node_value_arrs::N_SAMPLES) +
                " and " +
                std::to_string(node_value_arrs::N_SAMPLES_TEST) +
                ")"
            );
        }
56
        else if(feat_ind >= node_value_arrs::N_PRIMARY_FEATURES)
57
        {
58
            node_value_arrs::resize_values_arr(0, node_value_arrs::N_PRIMARY_FEATURES + 1);
59
        }
Thomas Purcell's avatar
Thomas Purcell committed
60
61
62
        set_value();
        set_test_value();
    }
63
}
Thomas Purcell's avatar
Thomas Purcell committed
64

Thomas Purcell's avatar
Thomas Purcell committed
65
FeatureNode::~FeatureNode()
66
{}
Thomas Purcell's avatar
Thomas Purcell committed
67

68
bool FeatureNode::is_const() const
Thomas Purcell's avatar
Thomas Purcell committed
69
70
71
72
73
74
75
{
    bool is_c = false;
    int pos = 0;

    double* val_ptr = value_ptr();
    for(auto& sz : node_value_arrs::TASK_SZ_TRAIN)
    {
76
        double mean = util_funcs::mean<double>(val_ptr + pos, sz);
Thomas Purcell's avatar
Thomas Purcell committed
77
78
79
80
81
82
        is_c = is_c || std::all_of(val_ptr + pos, val_ptr + pos + sz, [&mean](double d){return std::abs(d - mean) < 1e-12;});
        pos += sz;
    }
    return is_c;
}

83
void FeatureNode::update_add_sub_leaves(std::map<std::string, int>& add_sub_leaves, const int pl_mn, int& expected_abs_tot) const
84
85
{
    if(add_sub_leaves.count(_expr) > 0)
86
    {
87
        add_sub_leaves[_expr] += pl_mn;
88
    }
89
    else
90
    {
91
        add_sub_leaves[_expr] = pl_mn;
92
    }
93
94
95
96

    ++expected_abs_tot;
}

97
void FeatureNode::update_div_mult_leaves(std::map<std::string, double>& div_mult_leaves, const double fact, double& expected_abs_tot) const
98
99
{
    if(div_mult_leaves.count(_expr) > 0)
100
    {
101
        div_mult_leaves[_expr] += fact;
102
    }
103
    else
104
    {
105
        div_mult_leaves[_expr] = fact;
106
    }
107
108
109
110

    expected_abs_tot += std::abs(fact);
}

Thomas Purcell's avatar
Thomas Purcell committed
111
void FeatureNode::update_primary_feature_decomp(std::map<std::string, int>& pf_decomp) const
112
{
Thomas Purcell's avatar
Thomas Purcell committed
113
    if(pf_decomp.count(_expr) > 0)
114
    {
Thomas Purcell's avatar
Thomas Purcell committed
115
        pf_decomp[_expr] += 1;
116
    }
117
    else
118
    {
Thomas Purcell's avatar
Thomas Purcell committed
119
        pf_decomp[_expr] = 1;
120
    }
121
122
}

Thomas Purcell's avatar
Thomas Purcell committed
123
// BOOST_CLASS_EXPORT(FeatureNode)