This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/CLPBN/horus/LiftedCircuit.h

275 lines
6.1 KiB
C
Raw Normal View History

#ifndef HORUS_LIFTEDCIRCUIT_H
#define HORUS_LIFTEDCIRCUIT_H
#include <stack>
#include "LiftedWCNF.h"
2012-10-24 21:22:49 +01:00
enum CircuitNodeType {
OR_NODE,
AND_NODE,
SET_OR_NODE,
SET_AND_NODE,
INC_EXC_NODE,
LEAF_NODE,
SMOOTH_NODE,
TRUE_NODE,
2012-10-30 12:41:00 +00:00
COMPILATION_FAILED_NODE
2012-10-24 21:22:49 +01:00
};
class CircuitNode
{
public:
CircuitNode (const Clauses& clauses, string explanation = "")
: clauses_(clauses), explanation_(explanation) { }
const Clauses& clauses (void) const { return clauses_; }
Clauses clauses (void) { return clauses_; }
2012-10-30 12:41:00 +00:00
virtual double weight (void) const = 0;
string explanation (void) const { return explanation_; }
private:
Clauses clauses_;
string explanation_;
};
2012-10-24 21:22:49 +01:00
class OrNode : public CircuitNode
{
public:
2012-10-24 21:22:49 +01:00
OrNode (const Clauses& clauses, string explanation = "")
: CircuitNode (clauses, explanation),
2012-10-24 21:22:49 +01:00
leftBranch_(0), rightBranch_(0) { }
2012-10-24 21:22:49 +01:00
CircuitNode** leftBranch (void) { return &leftBranch_; }
CircuitNode** rightBranch (void) { return &rightBranch_; }
double weight (void) const;
2012-11-07 18:42:11 +00:00
private:
2012-11-07 23:45:43 +00:00
CircuitNode* leftBranch_;
CircuitNode* rightBranch_;
};
2012-10-24 21:22:49 +01:00
class AndNode : public CircuitNode
{
public:
2012-10-24 21:22:49 +01:00
AndNode (const Clauses& clauses, string explanation = "")
: CircuitNode (clauses, explanation),
2012-10-24 21:22:49 +01:00
leftBranch_(0), rightBranch_(0) { }
2012-10-24 21:22:49 +01:00
AndNode (
const Clauses& clauses,
CircuitNode* leftBranch,
CircuitNode* rightBranch,
string explanation = "")
: CircuitNode (clauses, explanation),
leftBranch_(leftBranch), rightBranch_(rightBranch) { }
2012-10-24 21:22:49 +01:00
AndNode (
CircuitNode* leftBranch,
CircuitNode* rightBranch,
string explanation = "")
: CircuitNode ({}, explanation),
leftBranch_(leftBranch), rightBranch_(rightBranch) { }
2012-10-31 23:58:07 +00:00
CircuitNode** leftBranch (void) { return &leftBranch_; }
2012-10-24 21:22:49 +01:00
CircuitNode** rightBranch (void) { return &rightBranch_; }
double weight (void) const;
private:
2012-11-07 23:45:43 +00:00
CircuitNode* leftBranch_;
CircuitNode* rightBranch_;
};
class SetOrNode : public CircuitNode
{
public:
SetOrNode (unsigned nrGroundings, const Clauses& clauses)
: CircuitNode (clauses, " AC"), follow_(0),
nrGroundings_(nrGroundings) { }
CircuitNode** follow (void) { return &follow_; }
static unsigned nrPositives (void) { return nrGrsStack.top().first; }
static unsigned nrNegatives (void) { return nrGrsStack.top().second; }
double weight (void) const;
private:
CircuitNode* follow_;
unsigned nrGroundings_;
static stack<pair<unsigned, unsigned>> nrGrsStack;
};
class SetAndNode : public CircuitNode
{
public:
SetAndNode (unsigned nrGroundings, const Clauses& clauses)
: CircuitNode (clauses, " IPG"), follow_(0),
nrGroundings_(nrGroundings) { }
CircuitNode** follow (void) { return &follow_; }
double weight (void) const;
private:
CircuitNode* follow_;
unsigned nrGroundings_;
};
2012-10-30 00:21:10 +00:00
class IncExcNode : public CircuitNode
{
public:
2012-11-07 23:45:43 +00:00
IncExcNode (const Clauses& clauses, string explanation)
: CircuitNode (clauses, explanation), plus1Branch_(0),
2012-10-30 00:21:10 +00:00
plus2Branch_(0), minusBranch_(0) { }
2012-10-31 23:58:07 +00:00
2012-10-30 00:21:10 +00:00
CircuitNode** plus1Branch (void) { return &plus1Branch_; }
CircuitNode** plus2Branch (void) { return &plus2Branch_; }
CircuitNode** minusBranch (void) { return &minusBranch_; }
double weight (void) const;
2012-10-31 23:58:07 +00:00
private:
2012-11-07 23:45:43 +00:00
CircuitNode* plus1Branch_;
CircuitNode* plus2Branch_;
CircuitNode* minusBranch_;
};
class LeafNode : public CircuitNode
{
public:
LeafNode (const Clause& clause, const LiftedWCNF& lwcnf)
: CircuitNode (Clauses() = {clause}), lwcnf_(lwcnf) { }
double weight (void) const;
private:
const LiftedWCNF& lwcnf_;
2012-10-24 21:22:49 +01:00
};
class SmoothNode : public CircuitNode
{
public:
SmoothNode (const Clauses& clauses, const LiftedWCNF& lwcnf)
: CircuitNode (clauses), lwcnf_(lwcnf) { }
double weight (void) const;
private:
const LiftedWCNF& lwcnf_;
};
class TrueNode : public CircuitNode
{
public:
2012-10-30 00:21:10 +00:00
TrueNode (void) : CircuitNode ({}) { }
double weight (void) const;
};
2012-10-30 12:41:00 +00:00
class CompilationFailedNode : public CircuitNode
{
public:
2012-11-07 23:45:43 +00:00
CompilationFailedNode (const Clauses& clauses)
: CircuitNode (clauses) { }
2012-10-30 12:41:00 +00:00
double weight (void) const;
};
class LiftedCircuit
{
public:
2012-10-31 23:43:39 +00:00
LiftedCircuit (const LiftedWCNF* lwcnf);
double getWeightedModelCount (void) const;
2012-10-24 21:22:49 +01:00
void exportToGraphViz (const char*);
private:
void compile (CircuitNode** follow, Clauses& clauses);
bool tryUnitPropagation (CircuitNode** follow, Clauses& clauses);
2012-11-07 12:37:22 +00:00
bool tryIndependence (CircuitNode** follow, Clauses& clauses);
2012-11-07 12:37:22 +00:00
bool tryShannonDecomp (CircuitNode** follow, Clauses& clauses);
2012-10-30 00:21:10 +00:00
bool tryInclusionExclusion (CircuitNode** follow, Clauses& clauses);
bool tryIndepPartialGrounding (CircuitNode** follow, Clauses& clauses);
bool tryIndepPartialGroundingAux (Clauses& clauses, ConstraintTree& ct,
LogVars& rootLogVars);
2012-10-31 23:43:39 +00:00
bool tryAtomCounting (CircuitNode** follow, Clauses& clauses);
2012-11-01 13:56:12 +00:00
bool tryGrounding (CircuitNode** follow, Clauses& clauses);
2012-11-01 13:56:12 +00:00
void shatterCountedLogVars (Clauses& clauses);
2012-11-01 13:56:12 +00:00
bool shatterCountedLogVarsAux (Clauses& clauses);
2012-10-31 23:43:39 +00:00
2012-11-01 13:56:12 +00:00
bool shatterCountedLogVarsAux (Clauses& clauses, size_t idx1, size_t idx2);
2012-11-07 23:45:43 +00:00
bool independentClause (Clause& clause, Clauses& otherClauses) const;
2012-11-07 23:45:43 +00:00
bool independentLiteral (const Literal& lit,
const Literals& otherLits) const;
2012-10-31 23:43:39 +00:00
LitLvTypesSet smoothCircuit (CircuitNode* node);
void createSmoothNode (const LitLvTypesSet& lids,
CircuitNode** prev);
2012-11-07 12:37:22 +00:00
vector<LogVarTypes> getAllPossibleTypes (unsigned nrLogVars) const;
2012-11-07 12:37:22 +00:00
bool containsTypes (const LogVarTypes& typesA,
const LogVarTypes& typesB) const;
CircuitNodeType getCircuitNodeType (const CircuitNode* node) const;
2012-10-24 21:22:49 +01:00
void exportToGraphViz (CircuitNode* node, ofstream&);
2012-10-30 14:31:52 +00:00
void printClauses (const CircuitNode* node, ofstream&,
string extraOptions = "");
string escapeNode (const CircuitNode* node) const;
CircuitNode* root_;
const LiftedWCNF* lwcnf_;
};
#endif // HORUS_LIFTEDCIRCUIT_H
2012-10-24 21:22:49 +01:00