00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef UNITPROPAGATION_H_
00067 #define UNITPROPAGATION_H_
00068
00069 #include "inference.h"
00070
00071 const int updebug = false;
00072
00073 class UnitPropagation : public Inference
00074 {
00075 public:
00076
00080 UnitPropagation(VariableState* state, long int seed,
00081 const bool& trackClauseTrueCnts)
00082 : Inference(state, seed, trackClauseTrueCnts) {}
00083
00084 ~UnitPropagation() {}
00085
00089 void init() { return; }
00090
00094 void infer()
00095 {
00096 if (updebug) cout << "Entering UnitPropagation::infer" << endl;
00097 bool done = false;
00098
00099 while (!done)
00100 {
00101 if (updebug) cout << endl << endl;
00102 done = true;
00103 for (int i = 0; i < state_->getNumClauses(); i++)
00104 {
00105
00106 if (state_->isDeadClause(i)) continue;
00107
00108 Array<int>* atoms = state_->simplifyClauseFromFixedAtoms(i);
00109 if (atoms->size() == 0)
00110 {
00111 delete atoms;
00112 continue;
00113 }
00114 if (updebug) cout << "Clause " << i << " ";
00115
00116 long double cost = state_->getClauseCost(i);
00117 if (atoms->size() == 1 || cost < 0)
00118 {
00119 if (updebug) cout << "unit or negative" << endl;
00120
00121 done = false;
00122
00123 for (int j = 0; j < atoms->size(); j++)
00124 {
00125 int lit = (*atoms)[j];
00126
00127
00128 bool value = ((lit > 0 && cost > 0) || (lit < 0 && cost < 0))
00129 ? true : false;
00130 state_->fixAtom(abs(lit), value);
00131 }
00132 }
00133 else
00134 {
00135 if (updebug) cout << endl;
00136 }
00137 delete atoms;
00138 }
00139 }
00140
00141 state_->saveLowState();
00142 if (updebug) cout << "Leaving UnitPropagation::infer" << endl;
00143 }
00144
00148 void printProbabilities(ostream& out)
00149 {
00150 for (int i = 0; i < state_->getNumAtoms(); i++)
00151 {
00152 state_->printGndPred(i, out);
00153 out << " " << state_->getValueOfLowAtom(i + 1) << endl;
00154 }
00155 }
00156
00166 void getPredsWithNonZeroProb(vector<string>& nonZeroPreds,
00167 vector<float>& probs)
00168 {
00169 nonZeroPreds.clear();
00170 probs.clear();
00171 for (int i = 0; i < state_->getNumAtoms(); i++)
00172 {
00173 if (state_->getValueOfLowAtom(i + 1))
00174 {
00175 ostringstream oss(ostringstream::out);
00176 state_->printGndPred(i, oss);
00177 nonZeroPreds.push_back(oss.str());
00178 probs.push_back(1);
00179 }
00180 }
00181 }
00182
00186 double getProbability(GroundPredicate* const& gndPred)
00187 {
00188 int idx = state_->getGndPredIndex(gndPred);
00189 int truthValue = 0;
00190 if (idx >= 0) truthValue = state_->getValueOfLowAtom(idx + 1);
00191 return truthValue;
00192 }
00193
00197 void printTruePreds(ostream& out)
00198 {
00199 for (int i = 0; i < state_->getNumAtoms(); i++)
00200 {
00201 if (state_->getValueOfLowAtom(i + 1))
00202 {
00203 state_->printGndPred(i, out);
00204 out << endl;
00205 }
00206 }
00207 }
00208
00209 private:
00210
00211
00212 };
00213
00214
00215 #endif