00001 package edu.tum.cs.srl.bayesnets.inference;
00002
00003 import java.util.HashMap;
00004 import java.util.HashSet;
00005 import java.util.Random;
00006
00007 import edu.ksu.cis.bnj.ver3.core.BeliefNode;
00008 import edu.tum.cs.bayesnets.core.BeliefNetworkEx;
00009 import edu.tum.cs.bayesnets.inference.SATIS_BSampler;
00010 import edu.tum.cs.bayesnets.inference.Sampler;
00011 import edu.tum.cs.logic.GroundLiteral;
00012 import edu.tum.cs.logic.PossibleWorld;
00013 import edu.tum.cs.logic.WorldVariables;
00014 import edu.tum.cs.logic.sat.ClausalKB;
00015 import edu.tum.cs.logic.sat.Clause;
00016 import edu.tum.cs.logic.sat.SampleSAT;
00017 import edu.tum.cs.srl.AbstractVariable;
00018 import edu.tum.cs.srl.bayesnets.bln.GroundBLN;
00019 import edu.tum.cs.srl.bayesnets.bln.coupling.VariableLogicCoupling;
00020
00026 public class SATIS extends BNSampler {
00027
00028 GroundBLN gbln;
00032 SampleSAT ss;
00036 HashSet<BeliefNode> determinedVars;
00037 boolean unitPropagation = false;
00038
00039 public SATIS(GroundBLN bln) throws Exception {
00040 super(bln, SATIS_BSampler.class);
00041 gbln = bln;
00042 this.paramHandler.add("unitPropagation", "setUnitPropagation");
00043
00044
00045 PossibleWorld state = new PossibleWorld(gbln.getWorldVars());
00046 ss = new SampleSAT(state, gbln.getWorldVars(), gbln.getDatabase().getEntries());
00047
00048 paramHandler.addSubhandler(ss.getParameterHandler());
00049 }
00050
00051 public void setUnitPropagation(boolean enabled) {
00052 unitPropagation = enabled;
00053 }
00054
00055 protected void initSATSampler() throws Exception {
00056 System.out.println("initializing SAT sampler...");
00057
00058 if(unitPropagation) ss.enableUnitPropagation();
00059 this.ss.setDebugMode(debug);
00060 ClausalKB ckb = getClausalKB();
00061 ss.initConstraints(ckb);
00062
00063
00064 determinedVars = new HashSet<BeliefNode>();
00065 for(Clause c : ckb) {
00066 for(GroundLiteral lit : c.lits) {
00067 BeliefNode var = gbln.getVariable(lit.gndAtom);
00068 if(var == null)
00069 throw new Exception("Could not find node corresponding to ground atom '" + lit.gndAtom.toString() + "' with index " + lit.gndAtom.index + "; set of mapped ground atoms is " + gbln.getCoupling().getCoupledGroundAtoms());
00070 determinedVars.add(var);
00071 }
00072 }
00073 }
00074
00075 protected ClausalKB getClausalKB() throws Exception {
00076 return new ClausalKB(gbln.getKB());
00077 }
00078
00079 @Override
00080 protected Sampler getSampler() throws Exception {
00081 initSATSampler();
00082 return new SATIS_BSampler(gbln.getGroundNetwork(), ss, gbln.getCoupling(), determinedVars);
00083 }
00084
00093 protected class SampleSATPriors extends SampleSAT {
00094
00095 BeliefNetworkEx bn;
00096 HashMap<BeliefNode, double[]> priors = null;
00097 Random generator;
00098
00099 public SampleSATPriors(PossibleWorld state, WorldVariables vars, Iterable<? extends AbstractVariable> db, BeliefNetworkEx bn) throws Exception {
00100 super(state, vars, db);
00101 this.bn = bn;
00102 generator = new Random();
00103 }
00104
00105 protected void setRandomState() {
00106 if(priors == null)
00107 priors = bn.computePriors(evidenceDomainIndices);
00108 VariableLogicCoupling coupling = gbln.getCoupling();
00109 for(BeliefNode node : bn.bn.getNodes()) {
00110 if(!coupling.hasCoupling(node))
00111 continue;
00112 int domIdx = Sampler.sample(priors.get(node), generator);
00113 coupling.setVariableValue(node, domIdx, this.state);
00114 }
00115 }
00116 }
00117 }