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
00170 void getChangedPreds(vector<string>& changedPreds, vector<float>& probs,
00171 vector<float>& oldProbs, const float& probDelta)
00172 {
00173 changedPreds.clear();
00174 probs.clear();
00175 int numAtoms = state_->getNumAtoms();
00176
00177 oldProbs.resize(numAtoms, 0);
00178 for (int i = 0; i < numAtoms; i++)
00179 {
00180 int tv = state_->getValueOfLowAtom(i + 1);
00181 if (tv != oldProbs[i])
00182 {
00183
00184
00185 oldProbs[i] = tv;
00186 ostringstream oss(ostringstream::out);
00187 state_->printGndPred(i, oss);
00188 changedPreds.push_back(oss.str());
00189 probs.push_back(tv);
00190 }
00191 }
00192 }
00193
00197 double getProbability(GroundPredicate* const& gndPred)
00198 {
00199 int idx = state_->getGndPredIndex(gndPred);
00200 int truthValue = 0;
00201 if (idx >= 0) truthValue = state_->getValueOfLowAtom(idx + 1);
00202 return truthValue;
00203 }
00204
00208 void printTruePreds(ostream& out)
00209 {
00210 for (int i = 0; i < state_->getNumAtoms(); i++)
00211 {
00212 if (state_->getValueOfLowAtom(i + 1))
00213 {
00214 state_->printGndPred(i, out);
00215 out << endl;
00216 }
00217 }
00218 }
00219
00220 private:
00221
00222
00223 };
00224
00225
00226 #endif