Begin Network build
This commit is contained in:
47
simple/Network.cc
Normal file
47
simple/Network.cc
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "Network.h"
|
||||
|
||||
namespace bayesnet {
|
||||
Network::Network() {}
|
||||
|
||||
Network::~Network()
|
||||
{
|
||||
for (auto& pair : nodes) {
|
||||
delete pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
void Network::addNode(std::string name)
|
||||
{
|
||||
nodes[name] = new Node(name);
|
||||
}
|
||||
|
||||
void Network::addEdge(std::string parentName, std::string childName)
|
||||
{
|
||||
Node* parent = nodes[parentName];
|
||||
Node* child = nodes[childName];
|
||||
|
||||
if (parent == nullptr || child == nullptr) {
|
||||
throw std::invalid_argument("Parent or child node not found.");
|
||||
}
|
||||
|
||||
child->addParent(parent);
|
||||
}
|
||||
|
||||
// to be implemented
|
||||
void Network::fit(const std::vector<std::vector<double>>& dataset)
|
||||
{
|
||||
// ... learn parameters (i.e., CPTs) using the dataset
|
||||
}
|
||||
|
||||
// to be implemented
|
||||
std::vector<double> Network::predict(const std::vector<std::vector<double>>& testset)
|
||||
{
|
||||
std::vector<double> predictions;
|
||||
// ... use the CPTs and network structure to predict values
|
||||
return predictions;
|
||||
}
|
||||
}
|
||||
|
21
simple/Network.h
Normal file
21
simple/Network.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "Node.h"
|
||||
|
||||
namespace bayesnet {
|
||||
class Network {
|
||||
private:
|
||||
std::map<std::string, Node*> nodes;
|
||||
public:
|
||||
Network();
|
||||
~Network();
|
||||
void addNode(std::string);
|
||||
void addEdge(std::string, std::string);
|
||||
void fit(const std::vector<std::vector<double>>&);
|
||||
std::vector<double> predict(const std::vector<std::vector<double>>&);
|
||||
};
|
||||
}
|
||||
#endif
|
14
simple/Node.cc
Normal file
14
simple/Node.cc
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "Node.h"
|
||||
|
||||
namespace bayesnet {
|
||||
Node::Node(std::string name) : name(name) {}
|
||||
|
||||
void Node::addParent(Node* parent)
|
||||
{
|
||||
parents.push_back(parent);
|
||||
parent->children.push_back(this);
|
||||
}
|
||||
}
|
18
simple/Node.h
Normal file
18
simple/Node.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef NODE_H
|
||||
#define NODE_H
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
namespace bayesnet {
|
||||
class Node {
|
||||
private:
|
||||
std::string name;
|
||||
std::vector<Node*> parents;
|
||||
std::vector<Node*> children;
|
||||
std::map<std::vector<bool>, double> cpt; // Conditional Probability Table
|
||||
public:
|
||||
Node(std::string);
|
||||
void addParent(Node*);
|
||||
};
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user