00001
00002
00003
00004
00005
00006
00007 package edu.tum.cs.logic.sat;
00008
00009 import java.util.HashMap;
00010 import java.util.HashSet;
00011 import java.util.Random;
00012 import java.util.Set;
00013 import java.util.Vector;
00014 import java.util.Map.Entry;
00015
00016 import edu.tum.cs.logic.GroundAtom;
00017 import edu.tum.cs.logic.PossibleWorld;
00018 import edu.tum.cs.logic.WorldVariables;
00019 import edu.tum.cs.logic.WorldVariables.Block;
00020 import edu.tum.cs.srl.AbstractVariable;
00021 import edu.tum.cs.util.datastruct.Map2Set;
00022
00023
00024
00025 public class EvidenceHandler {
00026
00027 protected static boolean verbose = false;
00028 protected HashMap<Integer,Boolean> evidence;
00029 protected HashSet<Block> evidenceBlocks;
00030 protected Map2Set<Block, GroundAtom> blockExclusions;
00031 protected WorldVariables vars;
00032 protected Random rand;
00033
00034 public EvidenceHandler(WorldVariables vars, Iterable<? extends AbstractVariable> db) throws Exception {
00035 this.vars = vars;
00036 this.rand = new Random();
00037
00038 this.evidence = new HashMap<Integer,Boolean>();
00039 evidenceBlocks = new HashSet<Block>();
00040 blockExclusions = new Map2Set<Block,GroundAtom>();
00041 for(AbstractVariable var : db) {
00042 String strGndAtom = var.getPredicate();
00043 GroundAtom gndAtom = vars.get(strGndAtom);
00044 if(gndAtom == null)
00045 throw new Exception("Evidence ground atom '" + strGndAtom + "' not in set of world variables.");
00046 if(!var.isBoolean()) {
00047 Block block = vars.getBlock(gndAtom.index);
00048 if(block == null)
00049 throw new Exception(String.format("There is no variable block to which the non-boolean variable assignment '%s' can be mapped.", var.toString()));
00050 for(GroundAtom ga : block)
00051 this.evidence.put(ga.index, var.value.equals(ga.args[ga.args.length-1]));
00052 evidenceBlocks.add(block);
00053 }
00054 else {
00055 boolean truthValue = var.isTrue();
00056 this.evidence.put(gndAtom.index, truthValue);
00057 Block block = vars.getBlock(gndAtom.index);
00058 if(block != null) {
00059 if(truthValue == true) {
00060 for(GroundAtom ga : block)
00061 this.evidence.put(ga.index, ga.index == gndAtom.index);
00062 evidenceBlocks.add(block);
00063 }
00064 else {
00065 blockExclusions.add(block, gndAtom);
00066 }
00067 }
00068 }
00069 }
00070 }
00071
00072 public void setEvidenceInState(PossibleWorld state) {
00073 for(Entry<Integer, Boolean> e : this.evidence.entrySet())
00074 state.set(e.getKey(), e.getValue());
00075 }
00076
00081 public void setRandomState(PossibleWorld state) {
00082 HashSet<Block> handledBlocks = new HashSet<Block>();
00083 for(int i = 0; i < vars.size(); i++) {
00084
00085 Block block = vars.getBlock(i);
00086 if(block != null) {
00087 if(this.evidenceBlocks.contains(block) || handledBlocks.contains(block))
00088 continue;
00089
00090 Set<GroundAtom> excl = blockExclusions.get(block);
00091 if(excl == null) {
00092 int j = rand.nextInt(block.size());
00093 for(int k = 0; k < block.size(); k++) {
00094 boolean value = k == j;
00095 state.set(block.get(k), value);
00096 }
00097 }
00098
00099 else {
00100 Vector<GroundAtom> possibleTrueOnes = new Vector<GroundAtom>();
00101 for(GroundAtom gndAtom : block) {
00102 if(!excl.contains(gndAtom))
00103 possibleTrueOnes.add(gndAtom);
00104 }
00105 GroundAtom trueOne = possibleTrueOnes.get(rand.nextInt(possibleTrueOnes.size()));
00106 for(GroundAtom gndAtom : block)
00107 state.set(gndAtom, trueOne == gndAtom);
00108 }
00109 handledBlocks.add(block);
00110 }
00111 else {
00112 if(!this.evidence.containsKey(i))
00113 state.set(i, rand.nextBoolean());
00114 }
00115 }
00116 }
00117
00118 public HashMap<Integer, Boolean> getEvidence() {
00119 return evidence;
00120 }
00121 }