00001 #include <assert.h>
00002 #include <math.h>
00003
00004 #include "logichelper.h"
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 void GetClauseRandomSolution(int clause_size, bool satisfied, bool conjunction_disjunction, Array<bool>* solution)
00016 {
00017 solution->clear();
00018 if (satisfied && conjunction_disjunction)
00019 {
00020 solution->growToSize(clause_size, true);
00021 }
00022 else if(!satisfied && !conjunction_disjunction)
00023 {
00024 solution->growToSize(clause_size, false);
00025 }
00026 else
00027 {
00028 bool good_solution = false;
00029 while (!good_solution)
00030 {
00031 solution->clear();
00032 for(int i = 0; i < clause_size; i++)
00033 {
00034 bool b = random()%2;
00035 if (((b && satisfied) || (!b && !satisfied)))
00036 {
00037 good_solution = true;
00038 }
00039 solution->append(b);
00040 }
00041 }
00042 }
00043 }
00044
00045 void RecordAtomMakeClauseFalse(const Array<int>& clause, const Array<bool>& atoms, bool conj_disj, Array<int>* record) {
00046 assert(record);
00047 if (conj_disj) {
00048
00049 for (int i = 0; i < clause.size(); ++i) {
00050 if (atoms[abs(clause[i])] != (clause[i] > 0))
00051 return;
00052 }
00053 record->append(abs(clause[random() % clause.size()]));
00054 } else {
00055 for (int i = 0; i < clause.size(); ++i) {
00056 if(atoms[abs(clause[i])] == (clause[i] > 0)) {
00057 record->append(abs(clause[i]));
00058 }
00059 }
00060 }
00061 }
00062
00063 void RecordAtomMakeClauseTrue(const Array<int>& clause, const Array<bool>& atoms, bool conj_disj, Array<int>* record) {
00064 assert(record);
00065 if (!conj_disj) {
00066
00067 for (int i = 0; i < clause.size(); ++i) {
00068 if (atoms[abs(clause[i])] == (clause[i] > 0))
00069 return;
00070 }
00071 record->append(abs(clause[random() % clause.size()]));
00072 } else {
00073 for (int i = 0; i < clause.size(); ++i) {
00074 if(atoms[abs(clause[i])] != (clause[i] > 0)) {
00075 record->append(abs(clause[i]));
00076 }
00077 }
00078 }
00079 }