logichelper.cpp

00001 #include <assert.h>
00002 #include <math.h>
00003 
00004 #include "logichelper.h"
00005 
00006 /*
00007 #include <climits>
00008 #include <stdlib.h>
00009 #include <ostream>
00010 using namespace std;
00011 */
00012 
00013 // The discrete clauses defined in this library are of the same clause format in alchemy.
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); // A conjunctive clause could only be satisfied when every variable is true.
00021         }
00022         else if(!satisfied && !conjunction_disjunction)
00023         {
00024                 solution->growToSize(clause_size, false); // A disjunctive clause could only be unsatisfied when every variable is false.
00025         }
00026         else // For cases where conjunctive clause not to be satisfied and disjunctive clause to be satisfied.
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) {  // If conjunction, any false atom will do.
00048                 // If there already exists false atom(s), no flip is necessary, otherwise, randomly pick an atom to flip.
00049                 for (int i = 0; i < clause.size(); ++i) {
00050                         if (atoms[abs(clause[i])] != (clause[i] > 0))  // False atom
00051                           return;
00052                 }
00053                 record->append(abs(clause[random() % clause.size()]));
00054         } else {  // If disjunction, every atom has to be false.
00055                 for (int i = 0; i < clause.size(); ++i) {
00056                         if(atoms[abs(clause[i])] == (clause[i] > 0)) {  // It's true literal, record its index as it is to be flipped.
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) {  // If disjunction, any true atom will do.
00066                 // If there already exists true atom(s), no flip is necessary, otherwise, randomly pick an atom to flip.
00067                 for (int i = 0; i < clause.size(); ++i) {
00068                         if (atoms[abs(clause[i])] == (clause[i] > 0))  // True atom
00069                            return;
00070                 }
00071                 record->append(abs(clause[random() % clause.size()]));
00072         } else {  // If conjunction, every atom has to be true.
00073                 for (int i = 0; i < clause.size(); ++i) {
00074                         if(atoms[abs(clause[i])] != (clause[i] > 0)) {  // It's an false literal, record its index as it is to be flipped.
00075                                 record->append(abs(clause[i]));
00076                         }
00077                 }
00078         }
00079 }

Generated on Sun Jun 7 11:55:16 2009 for Alchemy by  doxygen 1.5.1