diff --git a/packages/CLPBN/clpbn/bp/BayesianNetwork.cpp b/packages/CLPBN/clpbn/bp/BayesianNetwork.cpp deleted file mode 100644 index 1dd17af03..000000000 --- a/packages/CLPBN/clpbn/bp/BayesianNetwork.cpp +++ /dev/null @@ -1,208 +0,0 @@ -#include -#include -#include -#include - -#include "BayesianNetwork.h" -#include "BayesianNode.h" - -BayesianNetwork::BayesianNetwork (void) -{ -} - - - -BayesianNetwork::~BayesianNetwork (void) -{ - for (unsigned int i = 0; i < nodes_.size(); i++) { - delete nodes_[i]; - } - for (unsigned int i = 0; i < dists_.size(); i++) { - delete dists_[i]; - } -} - - - -void -BayesianNetwork::addNode (string varName, - vector parents, - int evidence, - int distId) -{ - for (unsigned int i = 0; i < dists_.size(); i++) { - if (dists_[i]->id == distId) { - BayesianNode* node = new BayesianNode (varName, parents, - dists_[i], evidence); - nodes_.push_back (node); - break; - } - } -} - - -void -BayesianNetwork::addNode (string varName, - vector parents, - double* params, - int nParams, - vector domain) -{ - Distribution* dist = new Distribution (params, nParams, domain); - BayesianNode* node = new BayesianNode (varName, parents, dist); - dists_.push_back (dist); - nodes_.push_back (node); -} - - - -BayesianNode* -BayesianNetwork::getNode (string varName) const -{ - for (unsigned int i = 0; i < nodes_.size(); i++) { - if (nodes_[i]->getVariableName() == varName) { - return nodes_[i]; - } - } - return 0; -} - - - -void -BayesianNetwork::addDistribution (int distId, - double* params, - int nParams, - vector domain) -{ - dists_.push_back (new Distribution (distId, params, nParams, domain)); -} - - - -vector -BayesianNetwork::getNodes (void) const -{ - return nodes_; -} - - - -vector -BayesianNetwork::getRootNodes (void) const -{ - vector roots; - for (unsigned int i = 0; i < nodes_.size(); i++) { - if (nodes_[i]->isRoot()) { - roots.push_back (nodes_[i]); - } - } - return roots; -} - - - -vector -BayesianNetwork::getLeafNodes (void) const -{ - vector leafs; - for (unsigned int i = 0; i < nodes_.size(); i++) { - if (nodes_[i]->isLeaf()) { - leafs.push_back (nodes_[i]); - } - } - return leafs; -} - - - -bool -BayesianNetwork::isPolyTree (void) const -{ - return !containsCycle(); -} - - - -void -BayesianNetwork::printNetwork (void) const -{ - for (unsigned int i = 0; i < nodes_.size(); i++) { - cout << *nodes_[i]; - } -} - - - -bool -BayesianNetwork::containsCycle (void) const -{ - vector visited (nodes_.size()); - for (unsigned int v = 0; v < nodes_.size(); v++) { - visited[v] = false; - } - - for (unsigned int v = 0; v < nodes_.size(); v++) { - if (!visited[v]) { - if (containsCycle (v, -1, visited)) { - return true; - } - } - } - - return false; -} - - - -bool -BayesianNetwork::containsCycle (int v, - int predecessor, - vector& visited) const -{ - visited[v] = true; - vector adjs = getAdjacentVertexes (v); - for (unsigned int i = 0; i < adjs.size(); i++) { - int w = adjs[i]; - if (!visited[w]) { - if (containsCycle (w, v, visited)) { - return true; - } - } - else if (visited[w] && w != predecessor) { - return true; - } - } - return false; // no cycle detected in this component -} - - - -int -BayesianNetwork::getIndexOf (const BayesianNode* node) const -{ - for (unsigned int i = 0; i < nodes_.size(); i++) { - if (node == nodes_[i]) { - return i; - } - } - return -1; -} - - - -vector -BayesianNetwork::getAdjacentVertexes (int v) const -{ - vector adjs; - vector parents = nodes_[v]->getParents(); - vector childs = nodes_[v]->getChilds(); - for (unsigned int i = 0; i < parents.size(); i++) { - adjs.push_back (getIndexOf (parents[i])); - } - for (unsigned int i = 0; i < childs.size(); i++) { - adjs.push_back (getIndexOf (childs[i])); - } - return adjs; -} - diff --git a/packages/CLPBN/clpbn/bp/BayesianNetwork.h b/packages/CLPBN/clpbn/bp/BayesianNetwork.h deleted file mode 100644 index 5003faaed..000000000 --- a/packages/CLPBN/clpbn/bp/BayesianNetwork.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef BAYESIAN_NETWORK_H -#define BAYESIAN_NETWORK_H - -#include -#include - -using namespace std; - -class BayesianNode; -class Distribution; - -class BayesianNetwork -{ - public: - // constructs - BayesianNetwork (void); - // destruct - virtual ~BayesianNetwork (void); - // methods - virtual void addNode (string, vector, int, int); - virtual void addNode (string, vector, - double*, int, vector); - BayesianNode* getNode (string) const; - void addDistribution (int, double*, int, vector); - vector getNodes (void) const; - vector getRootNodes (void) const; - vector getLeafNodes (void) const; - bool isPolyTree (void) const; - void printNetwork (void) const; - - protected: - // members - vector nodes_; - vector dists_; - - private: - BayesianNetwork (const BayesianNetwork&); // disallow copy - void operator= (const BayesianNetwork&); // disallow assign - bool containsCycle (void) const; - bool containsCycle (int, int, vector&) const; - int getIndexOf (const BayesianNode*) const; - vector getAdjacentVertexes (int) const ; -}; - -#endif // BAYESIAN_NETWORK_H - diff --git a/packages/CLPBN/clpbn/bp/BayesianNode.cpp b/packages/CLPBN/clpbn/bp/BayesianNode.cpp deleted file mode 100644 index 26a3106e5..000000000 --- a/packages/CLPBN/clpbn/bp/BayesianNode.cpp +++ /dev/null @@ -1,382 +0,0 @@ -#include -#include -#include - -#include "BayesianNode.h" -#include "CptEntry.h" - - -BayesianNode::BayesianNode (string varName, - vector parents, - Distribution* dist, - int evidence) -{ - varName_ = varName; - parents_ = parents; - dist_ = dist; - evidence_ = evidence; - for (unsigned int i = 0; i < parents.size(); i++) { - parents[i]->addChild (this); - } -} - - - -BayesianNode::~BayesianNode (void) -{ -} - - - -string -BayesianNode::getVariableName (void) const -{ - return varName_; -} - - - -vector -BayesianNode::getParents (void) const -{ - return parents_; -} - - - -vector -BayesianNode::getChilds (void) const -{ - return childs_; -} - - - -void -BayesianNode::addChild (BayesianNode* node) -{ - childs_.push_back (node); -} - - - -double* -BayesianNode::getParameters (void) -{ - return dist_->params; -} - - - -double* -BayesianNode::getRow (int rowIndex) const -{ - int offset = getRowSize() * rowIndex; - return &dist_->params[offset]; -} - - - -double -BayesianNode::getProbability (CptEntry& entry) -{ - int index = entry.getCptIndex(); - return dist_->params[index]; -} - - - -void -BayesianNode::setProbability (CptEntry& entry, double prob) -{ - int index = entry.getCptIndex(); - dist_->params[index] = prob; -} - - - -bool -BayesianNode::isRoot (void) -{ - return parents_.empty(); -} - - - -bool -BayesianNode::isLeaf (void) -{ - return childs_.empty(); -} - - - -int -BayesianNode::getRowSize (void) const -{ - return dist_->nParams / dist_->domain.size(); -} - - - -int -BayesianNode::getCptSize (void) -{ - return dist_->nParams; -} - - - -vector -BayesianNode::getDomain (void) const -{ - return dist_->domain; -} - - - -int -BayesianNode::getDomainSize (void) const -{ - return dist_->domain.size(); -} - - - -vector -BayesianNode::getCptEntries (const vector >& constraints) -{ - vector matchedEntries; - if (constraints.size() > 0 && constraints[0].first == 0) { - vector entries = getCptEntriesOfRow (constraints[0].second); - for (unsigned int i = 0; i < entries.size(); i++) { - if (entries[i].matchConstraints (constraints)) { - matchedEntries.push_back (entries[i]); - } - } - } - else { - for (unsigned int i = 0; i < dist_->domain.size(); i++) { - vector entries = getCptEntriesOfRow (i); - for (unsigned int j = 0; j < entries.size(); j++) { - if (entries[j].matchConstraints (constraints)) { - matchedEntries.push_back (entries[j]); - } - } - } - } - return matchedEntries; -} - - - -vector -BayesianNode::getCptEntriesOfRow (int rowIndex) -{ - int rowSize = getRowSize(); - int nParents = parents_.size(); - vector > insts (rowSize); - - for (int i = 0; i < rowSize; i++) { - insts[i].resize (nParents + 1); - insts[i][0] = rowIndex; - } - - int reps = 1; - for (int i = nParents - 1; i >= 0; i--) { - int index = 0; - while (index < rowSize) { - for (int j = 0; j < parents_[i]->getDomainSize(); j++) { - for (int k = 0; k < reps; k++) { - insts[index][i + 1] = j; - index++; - } - } - } - reps *= parents_[i]->getDomainSize(); - } - - vector entries; - for (int i = 0; i < rowSize; i++ ) { - entries.push_back (CptEntry ((rowIndex * rowSize) + i, insts[i])); - } - - return entries; -} - - - -int -BayesianNode::getIndexOfParent (const BayesianNode* myParent) const -{ - for (unsigned int i = 0; i < parents_.size(); i++) { - if (myParent == parents_[i]) { - return i; - } - } - return -1; -} - - - -bool -BayesianNode::hasEvidence (void) -{ - return evidence_ != -1; -} - - - -int -BayesianNode::getEvidence (void) -{ - return evidence_; -} - - - -void -BayesianNode::setEvidence (int evidence) -{ - evidence_ = evidence; -} - - - -string -BayesianNode::entryToString (const CptEntry& entry) const -{ - string s = "p(" ; - vector insts = entry.getDomainInstantiations(); - s += getDomain()[insts[0]]; - if (parents_.size() > 0) { - s += "|" ; - for (unsigned int i = 1; i < insts.size() - 1; i++) { - s += parents_[i - 1]->getDomain()[insts[i]] + ","; - } - BayesianNode* lastParent = parents_[parents_.size() - 1]; - int lastIndex = insts[insts.size() - 1]; - s += lastParent->getDomain()[lastIndex]; - } - s += ")" ; - return s; -} - - - -vector -BayesianNode::getDomainHeaders (void) const -{ - int rowSize = getRowSize(); - int nParents = parents_.size(); - int reps = 1; - vector headers (rowSize); - for (int i = nParents - 1; i >= 0; i--) { - vector domain = parents_[i]->getDomain(); - int index = 0; - while (index < rowSize) { - for (int j = 0; j < parents_[i]->getDomainSize(); j++) { - for (int k = 0; k < reps; k++) { - if (headers[index] != "") { - headers[index] = domain[j] + "," + headers[index]; - } else { - headers[index] = domain[j]; - } - index++; - } - } - } - reps *= parents_[i]->getDomainSize(); - } - return headers; -} - - - -ostream& -operator << (ostream& o, const BayesianNode& node) -{ - o << "Variable: " << node.getVariableName() << endl; - - o << "Domain: " ; - vector domain = node.dist_->domain; - for (unsigned int i = 0; i < domain.size() - 1; i++) { - o << domain[i] << ", " ; - } - if (domain.size() != 0) { - o << domain[domain.size() - 1]; - } - o << endl; - - o << "Parents: " ; - vector parents = node.getParents(); - if (parents.size() != 0) { - for (unsigned int i = 0; i < parents.size() - 1; i++) { - o << parents[i]->getVariableName() << ", " ; - } - o << parents[parents.size() - 1]->getVariableName(); - } - o << endl; - - o << "Childs: " ; - vector childs = node.getChilds(); - if (childs.size() != 0) { - for (unsigned int i = 0; i < childs.size() - 1; i++) { - o << childs[i]->getVariableName() << ", " ; - } - o << childs[childs.size() - 1]->getVariableName(); - } - o << endl; - - const unsigned int MIN_DOMAIN_WIDTH = 4; // min width of first column - const unsigned int MIN_COMBO_WIDTH = 12; // min width of following columns - - unsigned int domainWidth = domain[0].length(); - for (unsigned int i = 1; i < domain.size(); i++) { - if (domain[i].length() > domainWidth) { - domainWidth = domain[i].length(); - } - } - - domainWidth = (domainWidth < MIN_DOMAIN_WIDTH) - ? MIN_DOMAIN_WIDTH - : domainWidth; - - o << left << setw (domainWidth) << "cpt" << right; - - vector widths; - int lineWidth = domainWidth; - vector headers = node.getDomainHeaders(); - if (!headers.empty()) { - for (unsigned int i = 0; i < headers.size(); i++) { - unsigned int len = headers[i].length(); - int w = (len < MIN_COMBO_WIDTH) ? MIN_COMBO_WIDTH : len; - widths.push_back (w); - o << setw (w) << headers[i]; - lineWidth += w; - } - o << endl; - } else { - cout << endl; - widths.push_back (domainWidth); - lineWidth += MIN_COMBO_WIDTH; - } - - for (int i = 0; i < lineWidth; i++) { - o << "-" ; - } - o << endl; - - for (unsigned int i = 0; i < domain.size(); i++) { - double* row = node.getRow (i); - o << left << setw (domainWidth) << domain[i] << right; - for (int j = 0; j < node.getRowSize(); j++) { - o << setw (widths[j]) << row[j]; - } - o << endl; - } - o << endl; - - return o; -} - diff --git a/packages/CLPBN/clpbn/bp/BayesianNode.h b/packages/CLPBN/clpbn/bp/BayesianNode.h deleted file mode 100644 index 63b1dc301..000000000 --- a/packages/CLPBN/clpbn/bp/BayesianNode.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef BAYESIAN_NODE_H -#define BAYESIAN_NODE_H - -#include -#include - -#include "Distribution.h" -#include "CptEntry.h" - -using namespace std; - -class BayesianNode -{ - public: - // constructs - BayesianNode (string, vector, Distribution*, int = -1); - // destruct - ~BayesianNode (void); - // methods - string getVariableName (void) const; - vector getParents (void) const; - vector getChilds (void) const; - void addChild (BayesianNode*); - double* getParameters (void); - double* getRow (int) const; - double getProbability (CptEntry&); - void setProbability (CptEntry&, double); - bool isRoot (void); - bool isLeaf (void); - int getRowSize (void) const; - int getCptSize (void); - vector getDomain (void) const; - int getDomainSize (void) const; - vector getCptEntries (const vector >&); - vector getCptEntriesOfRow (int); - int getIndexOfParent (const BayesianNode*) const; - bool hasEvidence (void); - int getEvidence (void); - void setEvidence (int); - string entryToString (const CptEntry& entry) const; - - private: - BayesianNode (const BayesianNode&); // disallow copy - void operator= (const BayesianNode&); // disallow assign - // methods - vector getDomainHeaders (void) const; - friend ostream& operator << (ostream&, const BayesianNode&); - // members - string varName_; // variable name - vector parents_; // parents of this node - vector childs_; // children of this node - Distribution* dist_; - int evidence_; -}; - -ostream& operator << (ostream&, const BayesianNode&); - -#endif // BAYESIAN_NODE_H - diff --git a/packages/CLPBN/clpbn/bp/BifInterface.cpp b/packages/CLPBN/clpbn/bp/BifInterface.cpp deleted file mode 100644 index ba38030af..000000000 --- a/packages/CLPBN/clpbn/bp/BifInterface.cpp +++ /dev/null @@ -1,117 +0,0 @@ -#include -#include -#include - -#include "xmlParser/xmlParser.h" - -#include "BifInterface.h" -#include "BayesianNetwork.h" -#include "BayesianNode.h" - - -void -BifInterface::createNetworkFromXML (BayesianNetwork* bn, const char* fileName) -{ - map > domains; - XMLNode xMainNode = XMLNode::openFileHelper (fileName, "BIF"); - // only the first network is parsed, others are ignored - XMLNode xNode = xMainNode.getChildNode ("NETWORK"); - int nVars = xNode.nChildNode ("VARIABLE"); - for (int i = 0; i < nVars; i++) { - XMLNode var = xNode.getChildNode ("VARIABLE", i); - string type = var.getAttribute ("TYPE"); - if (type != "nature") { - cerr << "error: only \"nature\" variables are supported" << endl; - abort(); - } - vector domain; - string varName = var.getChildNode("NAME").getText(); - int domainSize = var.nChildNode ("OUTCOME"); - for (int j = 0; j < domainSize; j++) { - domain.push_back (var.getChildNode("OUTCOME", j).getText()); - } - domains.insert (make_pair (varName, domain)); - } - - int nDefs = xNode.nChildNode ("DEFINITION"); - if (nVars != nDefs) { - cerr << "error: different number of variables and definitions"; - cerr << endl; - } - - for (int i = 0; i < nDefs; i++) { - XMLNode def = xNode.getChildNode ("DEFINITION", i); - string nodeName = def.getChildNode("FOR").getText(); - map >::const_iterator iter = domains.find (nodeName); - if (iter == domains.end()) { - cerr << "error: unknow variable `" << nodeName << "'" << endl; - abort(); - } - vector parents; - int nParams = iter->second.size(); - for (int j = 0; j < def.nChildNode ("GIVEN"); j++) { - string parentName = def.getChildNode("GIVEN", j).getText(); - BayesianNode* parentNode = bn->getNode (parentName); - if (parentNode) { - nParams *= parentNode->getDomainSize(); - parents.push_back (parentNode); - } - else { - cerr << "error: unknow variable `" << parentName << "'" << endl; - abort(); - } - } - - int c = 0; - double* params = new double [nParams]; - stringstream s (def.getChildNode("TABLE").getText()); - while (!s.eof() && c < nParams) { - s >> params[c]; - c++; - } - if (c != nParams) { - cerr << "error: invalid number of parameters " ; - cerr << "for variable `" << nodeName << "'" << endl; - abort(); - } - - params = reorderParameters (params, nParams, iter->second.size()); - bn->addNode (nodeName, parents, params, nParams, iter->second); - } -} - - -double* -BifInterface::reorderParameters (double* params, - int nParams, - int domainSize) -{ - // the interchange format for bayesian networks saves the probabilities - // in the following order: - // p(a1|b1,c1) p(a2|b1,c1) p(a1|b1,c2) p(a2|b1,c2) p(a1|b2,c1) p(a2|b2,c1) - // p(a1|b2,c2) p(a2|b2,c2). - // - // however, in clpbn we keep the probabilities in this order: - // p(a1|b1,c1) p(a1|b1,c2) p(a1|b2,c1) p(a1|b2,c2) p(a2|b1,c1) p(a2|b1,c2) - // p(a2|b2,c1) p(a2|b2,c2). - - int count = 0; - int index1 = 0; - int index2 = 0; - int rowSize = nParams / domainSize; - double* reordered = new double [nParams]; - - while (index1 < nParams) { - index2 = count; - for (int i = 0; i < domainSize; i++) { - reordered[index2] = params[index1]; - index1 += 1; - index2 += rowSize; - } - count++; - } - - delete [] params; - return reordered; -} - diff --git a/packages/CLPBN/clpbn/bp/BifInterface.h b/packages/CLPBN/clpbn/bp/BifInterface.h deleted file mode 100644 index 84f3cceb0..000000000 --- a/packages/CLPBN/clpbn/bp/BifInterface.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef BIF_INTERFACE_H -#define BIF_INTERFACE_H - -using namespace std; - -class BayesianNetwork; -class BayesianNode; - -class BifInterface -{ - public: - static void createNetworkFromXML (BayesianNetwork*, const char*); - - private: - static double* reorderParameters (double*, int, int); -}; - -#endif // BIF_INTERFACE_H - - diff --git a/packages/CLPBN/clpbn/bp/BifTest.cpp b/packages/CLPBN/clpbn/bp/BifTest.cpp deleted file mode 100644 index 13420e6aa..000000000 --- a/packages/CLPBN/clpbn/bp/BifTest.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#include "BayesianNetwork.h" -#include "BayesianNode.h" -#include "BpNetwork.h" -#include "BpNode.h" -#include "BifInterface.h" - -using namespace std; - -int main (int argc, char* argv[]) -{ - BpNetwork bn; - // BayesianNetwork bn; - BifInterface::createNetworkFromXML (&bn, argv[1]); - bn.printNetwork(); - - // bn.getNode("FreightTruck")->setEvidence (0); - // bn.getNode("Alarm")->setEvidence (0); - - // bn.setSolverParameters (SEQUENTIAL_SCHEDULE, 500, 0.001); - // bn.setSolverParameters (PARALLEL_SCHEDULE, 500, 0.00000000000001); - // bn.setSolverParameters (PARALLEL_SCHEDULE, 500, 0.0000000000000000000001); - - //bn.getNode ("F")->setEvidence (0); - vector queryVars; - //queryVars.push_back (bn.getNode ("D")); - //queryVars.push_back (bn.getNode ("Burglar")); - queryVars.push_back (bn.getNode ("FreightTruck")); - queryVars.push_back (bn.getNode ("Alarm")); - bn.runSolver (queryVars); - - // bn.printCurrentStatus(); - // bn.printBeliefs(); - return 0; -} diff --git a/packages/CLPBN/clpbn/bp/Horus.cpp b/packages/CLPBN/clpbn/bp/Horus.cpp deleted file mode 100755 index 43770b689..000000000 --- a/packages/CLPBN/clpbn/bp/Horus.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - - -using namespace std; - -int addVariables (BayesianNetwork&, YAP_Term, int); -int addDistributions (BayesianNetwork&, YAP_Term, int); - -int createNetwork (void) -{ - BayesianNetwork* bn = new BpNetwork(); - addDistributions (*bn, YAP_ARG3, (int) YAP_IntOfTerm (YAP_ARG4)); - addVariables (*bn, YAP_ARG1, (int) YAP_IntOfTerm (YAP_ARG2)); - YAP_Int p = (YAP_Int) (bn); - return YAP_Unify (YAP_MkIntTerm (p), YAP_ARG5); -} - - - -int addVariables (BayesianNetwork& bn, YAP_Term varList, int nVars) -{ - for (int i = 0; i < nVars; i++) { - YAP_Term var = YAP_HeadOfTerm (varList); - int varId = (int) YAP_IntOfTerm (YAP_ArgOfTerm (1, var)); - int distId = (int) YAP_IntOfTerm (YAP_ArgOfTerm (2, var)); - YAP_Term parentsList = YAP_ArgOfTerm (3, var); - int nParents = (int) YAP_IntOfTerm (YAP_ArgOfTerm (4, var)); - vector parents; - for (int j = 0; j < nParents; j++) { - int parentId = (int) YAP_IntOfTerm (YAP_HeadOfTerm (parentsList)); - stringstream parentName; - parentName << parentId; - parents.push_back (bn.getNode (parentName.str())); - parentsList = YAP_TailOfTerm (parentsList); - } - stringstream nodeName; - nodeName << varId; - int evidence = (int) YAP_IntOfTerm (YAP_ArgOfTerm (5, var)); - bn.addNode (nodeName.str(), parents, evidence, distId); - varList = YAP_TailOfTerm (varList); - } - return TRUE; -} - - - -int addDistributions (BayesianNetwork& bn, YAP_Term distList, int nDists) -{ - for (int i = 0; i < nDists; i++) { - YAP_Term dist = YAP_HeadOfTerm (distList); - int distId = (int) YAP_IntOfTerm (YAP_ArgOfTerm (1, dist)); - YAP_Term domainList = YAP_ArgOfTerm (2, dist); - int domainSize = (int) YAP_IntOfTerm (YAP_ArgOfTerm (3, dist)); - vector domain (domainSize); - for (int j = 0; j < domainSize; j++) { - YAP_Atom atom = YAP_AtomOfTerm (YAP_HeadOfTerm (domainList)); - domain[j] = (char*) YAP_AtomName (atom);; - domainList = YAP_TailOfTerm (domainList); - } - YAP_Term paramsList = YAP_ArgOfTerm (4, dist); - int nParams = (int) YAP_IntOfTerm (YAP_ArgOfTerm (5, dist)); - double* params = new double [nParams]; - for (int j = 0; j < nParams; j++) { - params[j] = (double) YAP_FloatOfTerm (YAP_HeadOfTerm (paramsList)); - paramsList = YAP_TailOfTerm (paramsList); - } - bn.addDistribution (distId, params, nParams, domain); - distList = YAP_TailOfTerm (distList); - } - return TRUE; -} - - - -int runSolver (void) -{ - BpNetwork* bn = (BpNetwork*) YAP_IntOfTerm (YAP_ARG1); - YAP_Term queryVarsList = YAP_ARG2; - int nQueryVars = (int) YAP_IntOfTerm (YAP_ARG3); - vector queryVars; - for (int i = 0; i < nQueryVars; i++) { - int queryVarId = (int) YAP_IntOfTerm (YAP_HeadOfTerm (queryVarsList)); - stringstream queryVarName; - queryVarName << queryVarId; - queryVars.push_back (bn->getNode (queryVarName.str())); - queryVarsList = YAP_TailOfTerm (queryVarsList); - } - bn->runSolver (queryVars); - vector beliefs = bn->getBeliefs(); - YAP_Term beliefsList = YAP_TermNil(); - for (int i = beliefs.size() - 1; i >= 0; i--) { - YAP_Term belief = YAP_MkFloatTerm (beliefs[i]); - beliefsList = YAP_MkPairTerm (belief, beliefsList); - } - return YAP_Unify (beliefsList, YAP_ARG4); -} - - - -int freeMemory (void) -{ - BpNetwork* bn = (BpNetwork*) YAP_IntOfTerm (YAP_ARG1); - delete bn; - return TRUE; -} - - - -extern "C" void init_predicates (void) -{ - YAP_UserCPredicate ("create_network", createNetwork, 5); - YAP_UserCPredicate ("run_solver", runSolver, 4); - YAP_UserCPredicate ("free_memory", freeMemory, 1); -} - diff --git a/packages/CLPBN/clpbn/bp/bnets/dog-net.xml b/packages/CLPBN/clpbn/bp/bnets/dog-net.xml deleted file mode 100644 index 74f0a0e1e..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/dog-net.xml +++ /dev/null @@ -1,102 +0,0 @@ - - - - - - - - - - - - - - - - - - - - -]> - - - - -Dog-Problem - - - - light-on - true - false - position = (73, 165) - - - - bowel-problem - true - false - position = (190, 69) - - - - dog-out - true - false - position = (155, 165) - - - - hear-bark - true - false - position = (154, 241) - - - - family-out - true - false - position = (112, 69) - - - - - - light-on - family-out - 0.6 0.4 0.05 0.95
-
- - - bowel-problem - 0.01 0.99
-
- - - dog-out - bowel-problem - family-out - 0.99 0.01 0.97 0.03 0.9 0.1 0.3 0.7
-
- - - hear-bark - dog-out - 0.7 0.3 0.01 0.99
-
- - - family-out - 0.15 0.85
-
- - -
-
diff --git a/packages/CLPBN/clpbn/bp/bnets/john-mary-call.xml b/packages/CLPBN/clpbn/bp/bnets/john-mary-call.xml deleted file mode 100644 index 037134fc5..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/john-mary-call.xml +++ /dev/null @@ -1,106 +0,0 @@ - - - - - - - - - - - - - - - - - - -]> - - - - -John-Mary-Call - - - - Burglary - discrete - False - True - position = (145, 114) - - - - Earthquake - discrete - False - True - position = (351, 110) - - - - Alarm - discrete - False - True - position = (253, 224) - - - - JohnCalls - discrete - False - True - position = (156, 343) - - - - MaryCalls - discrete - False - True - position = (344, 341) - - - - - - Burglary - 0.999 0.0010
-
- - - Earthquake - 0.998 0.0020
-
- - - Alarm - Burglary - Earthquake - 0.999 0.71 0.06 0.05 0.0010 0.29 0.94 0.95
-
- - - JohnCalls - Alarm - 0.95 0.1 0.05 0.9
-
- - - MaryCalls - Alarm - 0.99 0.3 0.01 0.7
-
- - -
diff --git a/packages/CLPBN/clpbn/bp/bnets/more-than-2-parents-and-childs.xml b/packages/CLPBN/clpbn/bp/bnets/more-than-2-parents-and-childs.xml deleted file mode 100644 index 9d1794264..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/more-than-2-parents-and-childs.xml +++ /dev/null @@ -1,116 +0,0 @@ - - - - -Test - - - A - a1 - a2 - - - - B - b1 - b2 - b3 - b4 - - - - C - c1 - c2 - c3 - - - - D - d1 - d2 - d3 - - - - E - e1 - e2 - e3 - e4 - - - - F - f1 - f2 - f3 - - - - G - g1 - g2 - - - - - A - .1 .2
-
- - - B - .01 .02 .03 .04
-
- - - C - .11 .22 .33
-
- - - D - A - B - C - - .522 .008 .99 .01 .2 .8 .003 .457 .423 .007 .92 .04 .5 .232 .033 .227 .112 .048 .91 .21 .24 .18 .005 .227 - .212 .04 .59 .21 .6 .1 .023 .215 .913 .017 .96 .01 .55 .422 .013 .417 .272 .068 .61 .11 .26 .28 .205 .322 - .142 .028 .19 .11 .5 .67 .013 .437 .163 .067 .12 .06 .1 .262 .063 .167 .512 .028 .11 .41 .14 .68 .015 .92 -
-
- - - E - D - - .111 .11 .1 - .222 .22 .2 - .333 .33 .3 - .444 .44 .4 -
-
- - - F - D - - .112 .111 .110 - .223 .222 .221 - .334 .333 .332 -
-
- - - G - D - - .101 .102 .103 - .201 .202 .203 -
-
- -
-
- diff --git a/packages/CLPBN/clpbn/bp/bnets/multiconnected.xml b/packages/CLPBN/clpbn/bp/bnets/multiconnected.xml deleted file mode 100644 index 973679408..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/multiconnected.xml +++ /dev/null @@ -1,84 +0,0 @@ - - - - - - - - -Multiconnected - - - - H - h1 - h2 - - - - B - b1 - b2 - - - - L - l1 - l2 - - - - F - f1 - f2 - - - - C - c1 - c2 - - - - H - .2 .8
-
- - - B - H - .25 .75 .05 .95
-
- - - L - H - .003 .997 .00005 .99995
-
- - - F - B - L - .75 .25 .1 .9 .5 .5 .05 .95
-
- - - C - L - .6 .4 .02 .98
-
- -
-
- diff --git a/packages/CLPBN/clpbn/bp/bnets/multiconnected.yap b/packages/CLPBN/clpbn/bp/bnets/multiconnected.yap deleted file mode 100644 index 493ac7bf3..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/multiconnected.yap +++ /dev/null @@ -1,59 +0,0 @@ - -:- use_module(library(clpbn)). - -:- set_clpbn_flag(solver, bp). - - -% H -% / \ -% / \ -% B L -% \ / \ -% \ / \ -% F C - - -h(H) :- - h_table(HDist), - { H = h with p([h1, h2], HDist) }. - - -b(B) :- - h(H), - b_table(BDist), - { B = b with p([b1, b2], BDist, [H]) }. - - -l(L) :- - h(H), - l_table(LDist), - { L = l with p([l1, l2], LDist, [H]) }. - - -f(F) :- - b(B), - l(L), - f_table(FDist), - { F = f with p([f1, f2], FDist, [B, L]) }. - - -c(C) :- - l(L), - c_table(CDist), - { C = c with p([c1, c2], CDist, [L]) }. - - -h_table([0.2, 0.8]). - -b_table([0.25, 0.05, - 0.75, 0.95]). - -l_table([0.003, 0.00005, - 0.997, 0.99995]). - -f_table([0.75, 0.1, 0.5, 0.05, - 0.25, 0.9, 0.5, 0.95]). - -c_table([0.6, 0.02, - 0.4, 0.98]). - diff --git a/packages/CLPBN/clpbn/bp/bnets/neapolitan.xml b/packages/CLPBN/clpbn/bp/bnets/neapolitan.xml deleted file mode 100644 index b3a2e9612..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/neapolitan.xml +++ /dev/null @@ -1,52 +0,0 @@ - - - - - - - -Neapolitan - - - - Burglar - b1 - b2 - - - - FreightTruck - f1 - f2 - - - - Alarm - a1 - a2 - - - - Burglar - .005 .995
-
- - - FreightTruck - .03 .97
-
- - - Alarm - Burglar - FreightTruck - .992 .008 .99 .01 .2 .8 .003 .997
-
- -
-
- diff --git a/packages/CLPBN/clpbn/bp/bnets/neapolitan.yap b/packages/CLPBN/clpbn/bp/bnets/neapolitan.yap deleted file mode 100644 index 7a2155931..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/neapolitan.yap +++ /dev/null @@ -1,35 +0,0 @@ - -:- use_module(library(clpbn)). - -:- set_clpbn_flag(solver, bp). - - -% B F -% \ / -% \ / -% A - - -a(A) :- - b(B), - f(F), - a_table(ADist), - { A = a with p([a1, a2], ADist, [B, F]) }. - - - -b(B) :- - b_table(BDist), - { B = b with p([b1, b2], BDist) }. - -f(F) :- - f_table(FDist), - { F = f with p([f1, f2], FDist) }. - -b_table([0.005, 0.995]). - -f_table([0.03, 0.97]). - -a_table([0.992, 0.99, 0.2, 0.003, - 0.008, 0.01, 0.8, 0.997]). - diff --git a/packages/CLPBN/clpbn/bp/bnets/simple-loop.xml b/packages/CLPBN/clpbn/bp/bnets/simple-loop.xml deleted file mode 100644 index f3a52a087..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/simple-loop.xml +++ /dev/null @@ -1,57 +0,0 @@ - - - - -Simple Loop - - - A - a1 - a2 - - - - B - b1 - b2 - - - - C - c1 - c2 - - - - D - d1 - d2 - - - - A - .01 .09
-
- - - B - A - .03 .97 .6 .4
-
- - - C - A - .24 .76 .12 .88
-
- - - D - B - C - .2 .8 .7 .3 .45 .55 .22 .78
-
- -
-
- diff --git a/packages/CLPBN/clpbn/bp/bnets/test.yap b/packages/CLPBN/clpbn/bp/bnets/test.yap deleted file mode 100644 index d617ea5ae..000000000 --- a/packages/CLPBN/clpbn/bp/bnets/test.yap +++ /dev/null @@ -1,36 +0,0 @@ - -:- use_module(library(clpbn)). - -:- set_clpbn_flag(solver, bp). - - -% B F -% \ / -% \ / -% A - - -a(A) :- - b(B), - f(F), - a_table(ADist), - { A = a with p([a1, a2, a3], ADist, [B, F]) }. - - - -b(B) :- - b_table(BDist), - { B = b with p([b1, b2], BDist) }. - -f(F) :- - f_table(FDist), - { F = f with p([f1, f2], FDist) }. - -b_table([0.005, 0.995]). - -f_table([0.03, 0.97]). - -a_table([0.992, 0.99, 0.2, 0.003, - 0.008, 0.01, 0.8, 0.997, - 0.018, 0.21, 0.2, 0.927]). - diff --git a/packages/CLPBN/learning/example/school_params.yap b/packages/CLPBN/learning/example/school_params.yap index 199e4534f..6f96a9d38 100644 --- a/packages/CLPBN/learning/example/school_params.yap +++ b/packages/CLPBN/learning/example/school_params.yap @@ -41,7 +41,7 @@ graph([professor_ability(p0,_G131367),professor_ability(p1,h),professor_ability( % % change to 0.0, 0.1, 0.2 to make things simpler/harder % -missing(0.50). +missing(0.5). % miss 30% of the examples. goal(professor_ability(P,V)) :- diff --git a/packages/chr b/packages/chr index bf6525f85..b2eb894ce 160000 --- a/packages/chr +++ b/packages/chr @@ -1 +1 @@ -Subproject commit bf6525f85cfcf3c08fff8cf91fb189fe71dc34fd +Subproject commit b2eb894ce3e41925070215f800d6df3a356dc29d