remove unecesary files.

This commit is contained in:
Vítor Santos Costa 2011-06-03 12:26:10 +01:00
parent 3cd4f399c5
commit 56d71e55b4
19 changed files with 2 additions and 1642 deletions

View File

@ -1,208 +0,0 @@
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#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<BayesianNode*> 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<BayesianNode*> parents,
double* params,
int nParams,
vector<string> 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<string> domain)
{
dists_.push_back (new Distribution (distId, params, nParams, domain));
}
vector<BayesianNode*>
BayesianNetwork::getNodes (void) const
{
return nodes_;
}
vector<BayesianNode*>
BayesianNetwork::getRootNodes (void) const
{
vector<BayesianNode*> roots;
for (unsigned int i = 0; i < nodes_.size(); i++) {
if (nodes_[i]->isRoot()) {
roots.push_back (nodes_[i]);
}
}
return roots;
}
vector<BayesianNode*>
BayesianNetwork::getLeafNodes (void) const
{
vector<BayesianNode*> 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<bool> 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<bool>& visited) const
{
visited[v] = true;
vector<int> 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<int>
BayesianNetwork::getAdjacentVertexes (int v) const
{
vector<int> adjs;
vector<BayesianNode*> parents = nodes_[v]->getParents();
vector<BayesianNode*> 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;
}

View File

@ -1,46 +0,0 @@
#ifndef BAYESIAN_NETWORK_H
#define BAYESIAN_NETWORK_H
#include <vector>
#include <string>
using namespace std;
class BayesianNode;
class Distribution;
class BayesianNetwork
{
public:
// constructs
BayesianNetwork (void);
// destruct
virtual ~BayesianNetwork (void);
// methods
virtual void addNode (string, vector<BayesianNode*>, int, int);
virtual void addNode (string, vector<BayesianNode*>,
double*, int, vector<string>);
BayesianNode* getNode (string) const;
void addDistribution (int, double*, int, vector<string>);
vector<BayesianNode*> getNodes (void) const;
vector<BayesianNode*> getRootNodes (void) const;
vector<BayesianNode*> getLeafNodes (void) const;
bool isPolyTree (void) const;
void printNetwork (void) const;
protected:
// members
vector<BayesianNode*> nodes_;
vector<Distribution*> dists_;
private:
BayesianNetwork (const BayesianNetwork&); // disallow copy
void operator= (const BayesianNetwork&); // disallow assign
bool containsCycle (void) const;
bool containsCycle (int, int, vector<bool>&) const;
int getIndexOf (const BayesianNode*) const;
vector<int> getAdjacentVertexes (int) const ;
};
#endif // BAYESIAN_NETWORK_H

View File

@ -1,382 +0,0 @@
#include <iostream>
#include <iomanip>
#include <cassert>
#include "BayesianNode.h"
#include "CptEntry.h"
BayesianNode::BayesianNode (string varName,
vector<BayesianNode*> 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*>
BayesianNode::getParents (void) const
{
return parents_;
}
vector<BayesianNode*>
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<string>
BayesianNode::getDomain (void) const
{
return dist_->domain;
}
int
BayesianNode::getDomainSize (void) const
{
return dist_->domain.size();
}
vector<CptEntry>
BayesianNode::getCptEntries (const vector<pair<int,int> >& constraints)
{
vector<CptEntry> matchedEntries;
if (constraints.size() > 0 && constraints[0].first == 0) {
vector<CptEntry> 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<CptEntry> 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<CptEntry>
BayesianNode::getCptEntriesOfRow (int rowIndex)
{
int rowSize = getRowSize();
int nParents = parents_.size();
vector<vector<int> > 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<CptEntry> 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<int> 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<string>
BayesianNode::getDomainHeaders (void) const
{
int rowSize = getRowSize();
int nParents = parents_.size();
int reps = 1;
vector<string> headers (rowSize);
for (int i = nParents - 1; i >= 0; i--) {
vector<string> 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<string> 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<BayesianNode*> 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<BayesianNode*> 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<int> widths;
int lineWidth = domainWidth;
vector<string> 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;
}

View File

@ -1,59 +0,0 @@
#ifndef BAYESIAN_NODE_H
#define BAYESIAN_NODE_H
#include <vector>
#include <string>
#include "Distribution.h"
#include "CptEntry.h"
using namespace std;
class BayesianNode
{
public:
// constructs
BayesianNode (string, vector<BayesianNode*>, Distribution*, int = -1);
// destruct
~BayesianNode (void);
// methods
string getVariableName (void) const;
vector<BayesianNode*> getParents (void) const;
vector<BayesianNode*> 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<string> getDomain (void) const;
int getDomainSize (void) const;
vector<CptEntry> getCptEntries (const vector<pair<int, int> >&);
vector<CptEntry> 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<string> getDomainHeaders (void) const;
friend ostream& operator << (ostream&, const BayesianNode&);
// members
string varName_; // variable name
vector<BayesianNode*> parents_; // parents of this node
vector<BayesianNode*> childs_; // children of this node
Distribution* dist_;
int evidence_;
};
ostream& operator << (ostream&, const BayesianNode&);
#endif // BAYESIAN_NODE_H

View File

@ -1,117 +0,0 @@
#include <iostream>
#include <sstream>
#include <map>
#include "xmlParser/xmlParser.h"
#include "BifInterface.h"
#include "BayesianNetwork.h"
#include "BayesianNode.h"
void
BifInterface::createNetworkFromXML (BayesianNetwork* bn, const char* fileName)
{
map<string, vector<string> > 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<string> 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<string, vector<string> >::const_iterator iter = domains.find (nodeName);
if (iter == domains.end()) {
cerr << "error: unknow variable `" << nodeName << "'" << endl;
abort();
}
vector<BayesianNode*> 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;
}

View File

@ -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

View File

@ -1,36 +0,0 @@
#include <iostream>
#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<BayesianNode*> 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;
}

View File

@ -1,125 +0,0 @@
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <YapInterface.h>
#include <BayesianNetwork.h>
#include <BayesianNode.h>
#include <BpNetwork.h>
#include <BpNode.h>
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<BayesianNode*> 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<string> 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<BayesianNode*> 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<double> 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);
}

View File

@ -1,102 +0,0 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--
Bayesian network in XMLBIF v0.3 (BayesNet Interchange Format)
Produced by JavaBayes (http://www.cs.cmu.edu/~javabayes/
Output created Wed Aug 12 21:16:40 GMT+01:00 1998
-->
<!-- DTD for the XMLBIF 0.3 format -->
<!DOCTYPE BIF [
<!ELEMENT BIF ( NETWORK )*>
<!ATTLIST BIF VERSION CDATA #REQUIRED>
<!ELEMENT NETWORK ( NAME, ( PROPERTY | VARIABLE | DEFINITION )* )>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT VARIABLE ( NAME, ( OUTCOME | PROPERTY )* ) >
<!ATTLIST VARIABLE TYPE (nature|decision|utility) "nature">
<!ELEMENT OUTCOME (#PCDATA)>
<!ELEMENT DEFINITION ( FOR | GIVEN | TABLE | PROPERTY )* >
<!ELEMENT FOR (#PCDATA)>
<!ELEMENT GIVEN (#PCDATA)>
<!ELEMENT TABLE (#PCDATA)>
<!ELEMENT PROPERTY (#PCDATA)>
]>
<BIF VERSION="0.3">
<NETWORK>
<NAME>Dog-Problem</NAME>
<!-- Variables -->
<VARIABLE TYPE="nature">
<NAME>light-on</NAME>
<OUTCOME>true</OUTCOME>
<OUTCOME>false</OUTCOME>
<PROPERTY>position = (73, 165)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>bowel-problem</NAME>
<OUTCOME>true</OUTCOME>
<OUTCOME>false</OUTCOME>
<PROPERTY>position = (190, 69)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>dog-out</NAME>
<OUTCOME>true</OUTCOME>
<OUTCOME>false</OUTCOME>
<PROPERTY>position = (155, 165)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>hear-bark</NAME>
<OUTCOME>true</OUTCOME>
<OUTCOME>false</OUTCOME>
<PROPERTY>position = (154, 241)</PROPERTY>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>family-out</NAME>
<OUTCOME>true</OUTCOME>
<OUTCOME>false</OUTCOME>
<PROPERTY>position = (112, 69)</PROPERTY>
</VARIABLE>
<!-- Probability distributions -->
<DEFINITION>
<FOR>light-on</FOR>
<GIVEN>family-out</GIVEN>
<TABLE>0.6 0.4 0.05 0.95 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>bowel-problem</FOR>
<TABLE>0.01 0.99 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>dog-out</FOR>
<GIVEN>bowel-problem</GIVEN>
<GIVEN>family-out</GIVEN>
<TABLE>0.99 0.01 0.97 0.03 0.9 0.1 0.3 0.7 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>hear-bark</FOR>
<GIVEN>dog-out</GIVEN>
<TABLE>0.7 0.3 0.01 0.99 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>family-out</FOR>
<TABLE>0.15 0.85 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>

View File

@ -1,106 +0,0 @@
<?XML VERSION="1.0"?>
<!--
Bayesian network in BIF (BayesNet Interchange Format)
Produced by JavaBayes (http://www.cs.cmu.edu/~javabayes/
Output created Fri Nov 14 13:14:15 GMT+00:00 1997
-->
<!-- DTD for the BIF format -->
<!DOCTYPE BIF [
<!ELEMENT PROPERTY (#PCDATA)>
<!ELEMENT TYPE (#PCDATA)>
<!ELEMENT VALUE (#PCDATA)>
<!ELEMENT NAME (#PCDATA)>
<!ELEMENT NETWORK
( NAME, ( PROPERTY | VARIABLE | PROBABILITY )* )>
<!ELEMENT VARIABLE ( NAME, TYPE, ( VALUE | PROPERTY )* ) >
<!ELEMENT PROBABILITY
( FOR | GIVEN | TABLE | ENTRY | DEFAULT | PROPERTY )* >
<!ELEMENT TABLE (#PCDATA)>
<!ELEMENT DEFAULT (TABLE)>
<!ELEMENT ENTRY ( VALUE* , TABLE )>
]>
<BIF>
<NETWORK>
<NAME>John-Mary-Call</NAME>
<!-- Variables -->
<VARIABLE>
<NAME>Burglary</NAME>
<TYPE>discrete</TYPE>
<VALUE>False</VALUE>
<VALUE>True</VALUE>
<PROPERTY>position = (145, 114)</PROPERTY>
</VARIABLE>
<VARIABLE>
<NAME>Earthquake</NAME>
<TYPE>discrete</TYPE>
<VALUE>False</VALUE>
<VALUE>True</VALUE>
<PROPERTY>position = (351, 110)</PROPERTY>
</VARIABLE>
<VARIABLE>
<NAME>Alarm</NAME>
<TYPE>discrete</TYPE>
<VALUE>False</VALUE>
<VALUE>True</VALUE>
<PROPERTY>position = (253, 224)</PROPERTY>
</VARIABLE>
<VARIABLE>
<NAME>JohnCalls</NAME>
<TYPE>discrete</TYPE>
<VALUE>False</VALUE>
<VALUE>True</VALUE>
<PROPERTY>position = (156, 343)</PROPERTY>
</VARIABLE>
<VARIABLE>
<NAME>MaryCalls</NAME>
<TYPE>discrete</TYPE>
<VALUE>False</VALUE>
<VALUE>True</VALUE>
<PROPERTY>position = (344, 341)</PROPERTY>
</VARIABLE>
<!-- Probability distributions -->
<PROBABILITY>
<FOR>Burglary</FOR>
<TABLE>0.999 0.0010 </TABLE>
</PROBABILITY>
<PROBABILITY>
<FOR>Earthquake</FOR>
<TABLE>0.998 0.0020 </TABLE>
</PROBABILITY>
<PROBABILITY>
<FOR>Alarm</FOR>
<GIVEN>Burglary</GIVEN>
<GIVEN>Earthquake</GIVEN>
<TABLE>0.999 0.71 0.06 0.05 0.0010 0.29 0.94 0.95 </TABLE>
</PROBABILITY>
<PROBABILITY>
<FOR>JohnCalls</FOR>
<GIVEN>Alarm</GIVEN>
<TABLE>0.95 0.1 0.05 0.9 </TABLE>
</PROBABILITY>
<PROBABILITY>
<FOR>MaryCalls</FOR>
<GIVEN>Alarm</GIVEN>
<TABLE>0.99 0.3 0.01 0.7 </TABLE>
</PROBABILITY>
</BIF>

View File

@ -1,116 +0,0 @@
<?xml version="1.0" encoding="US-ASCII"?>
<BIF VERSION="0.3">
<NETWORK>
<NAME>Test</NAME>
<VARIABLE TYPE="nature">
<NAME>A</NAME>
<OUTCOME>a1</OUTCOME>
<OUTCOME>a2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B</NAME>
<OUTCOME>b1</OUTCOME>
<OUTCOME>b2</OUTCOME>
<OUTCOME>b3</OUTCOME>
<OUTCOME>b4</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C</NAME>
<OUTCOME>c1</OUTCOME>
<OUTCOME>c2</OUTCOME>
<OUTCOME>c3</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>D</NAME>
<OUTCOME>d1</OUTCOME>
<OUTCOME>d2</OUTCOME>
<OUTCOME>d3</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>E</NAME>
<OUTCOME>e1</OUTCOME>
<OUTCOME>e2</OUTCOME>
<OUTCOME>e3</OUTCOME>
<OUTCOME>e4</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>F</NAME>
<OUTCOME>f1</OUTCOME>
<OUTCOME>f2</OUTCOME>
<OUTCOME>f3</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>G</NAME>
<OUTCOME>g1</OUTCOME>
<OUTCOME>g2</OUTCOME>
</VARIABLE>
<DEFINITION>
<FOR>A</FOR>
<TABLE> .1 .2 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>B</FOR>
<TABLE> .01 .02 .03 .04 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C</FOR>
<TABLE> .11 .22 .33 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>D</FOR>
<GIVEN>A</GIVEN>
<GIVEN>B</GIVEN>
<GIVEN>C</GIVEN>
<TABLE>
.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
</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>E</FOR>
<GIVEN>D</GIVEN>
<TABLE>
.111 .11 .1
.222 .22 .2
.333 .33 .3
.444 .44 .4
</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>F</FOR>
<GIVEN>D</GIVEN>
<TABLE>
.112 .111 .110
.223 .222 .221
.334 .333 .332
</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>G</FOR>
<GIVEN>D</GIVEN>
<TABLE>
.101 .102 .103
.201 .202 .203
</TABLE>
</DEFINITION>
</NETWORK>
</BIF>

View File

@ -1,84 +0,0 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--
H
/ \
/ \
B L
\ / \
\ / \
F C
-->
<BIF VERSION="0.3">
<NETWORK>
<NAME>Multiconnected</NAME>
<!-- Variables -->
<VARIABLE TYPE="nature">
<NAME>H</NAME>
<OUTCOME>h1</OUTCOME>
<OUTCOME>h2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B</NAME>
<OUTCOME>b1</OUTCOME>
<OUTCOME>b2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>L</NAME>
<OUTCOME>l1</OUTCOME>
<OUTCOME>l2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>F</NAME>
<OUTCOME>f1</OUTCOME>
<OUTCOME>f2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C</NAME>
<OUTCOME>c1</OUTCOME>
<OUTCOME>c2</OUTCOME>
</VARIABLE>
<DEFINITION>
<FOR>H</FOR>
<TABLE> .2 .8 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>B</FOR>
<GIVEN>H</GIVEN>
<TABLE> .25 .75 .05 .95 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>L</FOR>
<GIVEN>H</GIVEN>
<TABLE> .003 .997 .00005 .99995 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>F</FOR>
<GIVEN>B</GIVEN>
<GIVEN>L</GIVEN>
<TABLE> .75 .25 .1 .9 .5 .5 .05 .95 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C</FOR>
<GIVEN>L</GIVEN>
<TABLE> .6 .4 .02 .98 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>

View File

@ -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]).

View File

@ -1,52 +0,0 @@
<?xml version="1.0" encoding="US-ASCII"?>
<!--
Bayesian network in XMLBIF v0.3 (BayesNet Interchange Format)
Produced by JavaBayes (http://www.cs.cmu.edu/~javabayes/
Output created Wed Aug 12 21:16:40 GMT+01:00 1998
-->
<BIF VERSION="0.3">
<NETWORK>
<NAME>Neapolitan</NAME>
<!-- Variables -->
<VARIABLE TYPE="nature">
<NAME>Burglar</NAME>
<OUTCOME>b1</OUTCOME>
<OUTCOME>b2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>FreightTruck</NAME>
<OUTCOME>f1</OUTCOME>
<OUTCOME>f2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>Alarm</NAME>
<OUTCOME>a1</OUTCOME>
<OUTCOME>a2</OUTCOME>
</VARIABLE>
<DEFINITION>
<FOR>Burglar</FOR>
<TABLE> .005 .995 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>FreightTruck</FOR>
<TABLE> .03 .97 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>Alarm</FOR>
<GIVEN>Burglar</GIVEN>
<GIVEN>FreightTruck</GIVEN>
<TABLE> .992 .008 .99 .01 .2 .8 .003 .997 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>

View File

@ -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]).

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="US-ASCII"?>
<BIF VERSION="0.3">
<NETWORK>
<NAME>Simple Loop</NAME>
<VARIABLE TYPE="nature">
<NAME>A</NAME>
<OUTCOME>a1</OUTCOME>
<OUTCOME>a2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>B</NAME>
<OUTCOME>b1</OUTCOME>
<OUTCOME>b2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>C</NAME>
<OUTCOME>c1</OUTCOME>
<OUTCOME>c2</OUTCOME>
</VARIABLE>
<VARIABLE TYPE="nature">
<NAME>D</NAME>
<OUTCOME>d1</OUTCOME>
<OUTCOME>d2</OUTCOME>
</VARIABLE>
<DEFINITION>
<FOR>A</FOR>
<TABLE> .01 .09 </TABLE>
</DEFINITION>
<DEFINITION>
<FOR>B</FOR>
<GIVEN>A</GIVEN>
<TABLE> .03 .97 .6 .4</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>C</FOR>
<GIVEN>A</GIVEN>
<TABLE> .24 .76 .12 .88</TABLE>
</DEFINITION>
<DEFINITION>
<FOR>D</FOR>
<GIVEN>B</GIVEN>
<GIVEN>C</GIVEN>
<TABLE> .2 .8 .7 .3 .45 .55 .22 .78 </TABLE>
</DEFINITION>
</NETWORK>
</BIF>

View File

@ -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]).

View File

@ -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)) :-

@ -1 +1 @@
Subproject commit bf6525f85cfcf3c08fff8cf91fb189fe71dc34fd
Subproject commit b2eb894ce3e41925070215f800d6df3a356dc29d