Add copy constructor to network

This commit is contained in:
Ricardo Montañana Gómez 2023-07-02 16:15:14 +02:00
parent 59e5794e5d
commit d6d683bb5c
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
3 changed files with 80 additions and 0 deletions

View File

@ -25,6 +25,7 @@ The process is repeated until there are no more variables to eliminate.
## Code for combination
```cpp
// Combinations of length 2
vector<string> combinations(vector<string> source)
{
@ -37,3 +38,75 @@ vector<string> combinations(vector<string> source)
}
return result;
}
```
## Code for Variable Elimination
```cpp
// Variable Elimination
vector<string> variableElimination(vector<string> source, map<string, vector<string>> graph)
{
vector<string> variables = source;
vector<string> factors = source;
while (variables.size() > 0) {
string variable = minFill(variables, graph);
vector<string> neighbors = graph[variable];
vector<string> combinations = combinations(neighbors);
vector<string> factorsToMultiply;
for (int i = 0; i < factors.size(); ++i) {
string factor = factors[i];
for (int j = 0; j < combinations.size(); ++j) {
string combination = combinations[j];
if (factor.find(combination) != string::npos) {
factorsToMultiply.push_back(factor);
break;
}
}
}
string newFactor = multiplyFactors(factorsToMultiply);
factors.push_back(newFactor);
variables.erase(remove(variables.begin(), variables.end(), variable), variables.end());
}
return factors;
}
```
## Network copy constructor
```cpp
// Network copy constructor
Network::Network(const Network& network)
{
this->variables = network.variables;
this->factors = network.factors;
this->graph = network.graph;
}
```
## Code for MinFill
```cpp
// MinFill
string minFill(vector<string> source, map<string, vector<string>> graph)
{
string result;
int min = INT_MAX;
for (int i = 0; i < source.size(); ++i) {
string temp = source[i];
int count = 0;
vector<string> neighbors = graph[temp];
vector<string> combinations = combinations(neighbors);
for (int j = 0; j < combinations.size(); ++j) {
string combination = combinations[j];
if (graph[combination].size() == 0) {
count++;
}
}
if (count < min) {
min = count;
result = temp;
}
}
return result;
}
```

View File

@ -2,6 +2,12 @@
namespace bayesnet {
Network::Network() : laplaceSmoothing(1), root(nullptr), features(vector<string>()), className("") {}
Network::Network(int smoothing) : laplaceSmoothing(smoothing), root(nullptr), features(vector<string>()), className("") {}
Network::Network(Network& other) : laplaceSmoothing(other.laplaceSmoothing), root(other.root), features(other.features), className(other.className)
{
for (auto& pair : other.nodes) {
nodes[pair.first] = new Node(*pair.second);
}
}
Network::~Network()
{
for (auto& pair : nodes) {

View File

@ -19,6 +19,7 @@ namespace bayesnet {
public:
Network();
Network(int);
Network(Network&);
~Network();
void addNode(string, int);
void addEdge(const string, const string);