Line data Source code
1 : // ***************************************************************
2 : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez
3 : // SPDX-FileType: SOURCE
4 : // SPDX-License-Identifier: MIT
5 : // ***************************************************************
6 :
7 : #include "Node.h"
8 :
9 : namespace bayesnet {
10 :
11 8887 : Node::Node(const std::string& name)
12 8887 : : name(name), numStates(0), cpTable(torch::Tensor()), parents(std::vector<Node*>()), children(std::vector<Node*>())
13 : {
14 8887 : }
15 1 : void Node::clear()
16 : {
17 1 : parents.clear();
18 1 : children.clear();
19 1 : cpTable = torch::Tensor();
20 1 : dimensions.clear();
21 1 : numStates = 0;
22 1 : }
23 13264685 : std::string Node::getName() const
24 : {
25 13264685 : return name;
26 : }
27 16951 : void Node::addParent(Node* parent)
28 : {
29 16951 : parents.push_back(parent);
30 16951 : }
31 3 : void Node::removeParent(Node* parent)
32 : {
33 3 : parents.erase(std::remove(parents.begin(), parents.end(), parent), parents.end());
34 3 : }
35 3 : void Node::removeChild(Node* child)
36 : {
37 3 : children.erase(std::remove(children.begin(), children.end(), child), children.end());
38 3 : }
39 16953 : void Node::addChild(Node* child)
40 : {
41 16953 : children.push_back(child);
42 16953 : }
43 1268 : std::vector<Node*>& Node::getParents()
44 : {
45 1268 : return parents;
46 : }
47 23263 : std::vector<Node*>& Node::getChildren()
48 : {
49 23263 : return children;
50 : }
51 18032 : int Node::getNumStates() const
52 : {
53 18032 : return numStates;
54 : }
55 9288 : void Node::setNumStates(int numStates)
56 : {
57 9288 : this->numStates = numStates;
58 9288 : }
59 105 : torch::Tensor& Node::getCPT()
60 : {
61 105 : return cpTable;
62 : }
63 : /*
64 : The MinFill criterion is a heuristic for variable elimination.
65 : The variable that minimizes the number of edges that need to be added to the graph to make it triangulated.
66 : This is done by counting the number of edges that need to be added to the graph if the variable is eliminated.
67 : The variable with the minimum number of edges is chosen.
68 : Here this is done computing the length of the combinations of the node neighbors taken 2 by 2.
69 : */
70 5 : unsigned Node::minFill()
71 : {
72 5 : std::unordered_set<std::string> neighbors;
73 13 : for (auto child : children) {
74 8 : neighbors.emplace(child->getName());
75 : }
76 12 : for (auto parent : parents) {
77 7 : neighbors.emplace(parent->getName());
78 : }
79 5 : auto source = std::vector<std::string>(neighbors.begin(), neighbors.end());
80 10 : return combinations(source).size();
81 5 : }
82 5 : std::vector<std::pair<std::string, std::string>> Node::combinations(const std::vector<std::string>& source)
83 : {
84 5 : std::vector<std::pair<std::string, std::string>> result;
85 20 : for (int i = 0; i < source.size(); ++i) {
86 15 : std::string temp = source[i];
87 31 : for (int j = i + 1; j < source.size(); ++j) {
88 16 : result.push_back({ temp, source[j] });
89 : }
90 15 : }
91 5 : return result;
92 0 : }
93 9288 : void Node::computeCPT(const torch::Tensor& dataset, const std::vector<std::string>& features, const double laplaceSmoothing, const torch::Tensor& weights)
94 : {
95 9288 : dimensions.clear();
96 : // Get dimensions of the CPT
97 9288 : dimensions.push_back(numStates);
98 26937 : transform(parents.begin(), parents.end(), back_inserter(dimensions), [](const auto& parent) { return parent->getNumStates(); });
99 :
100 : // Create a tensor of zeros with the dimensions of the CPT
101 9288 : cpTable = torch::zeros(dimensions, torch::kFloat) + laplaceSmoothing;
102 : // Fill table with counts
103 9288 : auto pos = find(features.begin(), features.end(), name);
104 9288 : if (pos == features.end()) {
105 0 : throw std::logic_error("Feature " + name + " not found in dataset");
106 : }
107 9288 : int name_index = pos - features.begin();
108 1756738 : for (int n_sample = 0; n_sample < dataset.size(1); ++n_sample) {
109 1747450 : c10::List<c10::optional<at::Tensor>> coordinates;
110 5242350 : coordinates.push_back(dataset.index({ name_index, n_sample }));
111 4997840 : for (auto parent : parents) {
112 3250390 : pos = find(features.begin(), features.end(), parent->getName());
113 3250390 : if (pos == features.end()) {
114 0 : throw std::logic_error("Feature parent " + parent->getName() + " not found in dataset");
115 : }
116 3250390 : int parent_index = pos - features.begin();
117 9751170 : coordinates.push_back(dataset.index({ parent_index, n_sample }));
118 : }
119 : // Increment the count of the corresponding coordinate
120 3494900 : cpTable.index_put_({ coordinates }, cpTable.index({ coordinates }) + weights.index({ n_sample }).item<double>());
121 1747450 : }
122 : // Normalize the counts
123 9288 : cpTable = cpTable / cpTable.sum(0);
124 6754578 : }
125 5647224 : float Node::getFactorValue(std::map<std::string, int>& evidence)
126 : {
127 5647224 : c10::List<c10::optional<at::Tensor>> coordinates;
128 : // following predetermined order of indices in the cpTable (see Node.h)
129 5647224 : coordinates.push_back(at::tensor(evidence[name]));
130 15624420 : transform(parents.begin(), parents.end(), std::back_inserter(coordinates), [&evidence](const auto& parent) { return at::tensor(evidence[parent->getName()]); });
131 11294448 : return cpTable.index({ coordinates }).item<float>();
132 5647224 : }
133 153 : std::vector<std::string> Node::graph(const std::string& className)
134 : {
135 153 : auto output = std::vector<std::string>();
136 153 : auto suffix = name == className ? ", fontcolor=red, fillcolor=lightblue, style=filled " : "";
137 153 : output.push_back(name + " [shape=circle" + suffix + "] \n");
138 394 : transform(children.begin(), children.end(), back_inserter(output), [this](const auto& child) { return name + " -> " + child->getName(); });
139 153 : return output;
140 0 : }
141 : }
|