1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
| #include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <algorithm>
#include <map>
#include <list>
#include <ctime>
#include <cmath>
using id_t_ = long int;
class variable {
private:
id_t var;
bool checked;
public:
variable(id_t_ var_, bool checked_) : var(var_), checked(checked_) {}
id_t_ operator* () const {return var;}
operator bool() const {return checked;}
bool operator !() const {return !checked;}
bool isChecked() {return checked;};
void setChecked(bool boolean) {checked = boolean;};
id_t_ id() const {return var;};
bool toggle() {return checked = !checked;};
};
std::ostream& operator<<(std::ostream& os, variable const& that) {
if (!that) os << "non ";
return os << *that;
}
class clause {
private:
variable var_1;
variable var_2;
bool expression;
public:
bool verified() const {return expression;};
variable getFirstVar() const {return var_1;};
variable getSecondVar() const {return var_2;};
clause(variable var_1_, variable var_2_, bool clause_) : var_1(var_1_), var_2(var_2_), expression(clause_) {}
};
std::ostream& operator<<(std::ostream& flux, const clause& that) {
return flux << that.getFirstVar() << " ou " << that.getSecondVar() << " : " << that.verified() ;
}
/*struct clause_compare {
bool operator() (clause const& c1, clause const& c2){
return c1.getFirstVar().id() < c2.getFirstVar().id() ;
}
};*/
class problem {
private:
typedef std::map<id_t_, clause> clause_t;
typedef std::map<id_t_, clause_t> myClauses;
myClauses clauses;
typedef std::map<id_t_, int> myNegations;
myNegations negations;
typedef std::map<id_t_, int> myClaims;
myClaims claims;
typedef std::map<id_t_, bool> myVariables;
myVariables variables;
/*typedef std::set<clause, clause_compare> myfailedClauses;
myfailedClauses failedClauses;*/
typedef myClauses myfailedClauses;
myfailedClauses failedClauses;
public:
void create_clause(variable const& var_1, variable const& var_2, bool clause_) {
clauses[*var_1].insert(std::make_pair(*var_2, clause(var_1, var_2, clause_))); // on ajoute chaque contrainte deux fois
clauses[*var_2].insert(std::make_pair(*var_1, clause(var_2, var_1, clause_))); // sous la forme clauses[x1][x2] et clauses[x2][x1]
}
void create_negation(id_t_ neg) {
if(negations.find(neg) == no_more_negation()){
negations[neg] = 1;
}
else
negations[neg]++;
}
void create_claim(id_t_ claim) { // si la variable n'existe pas dans claim on la crée et l'ajoute
if(claims.find(claim) == no_more_claim()){
claims[claim] = 1;
}
else{
claims[claim]++; // sinon on ajoute 1 au nombre de fois que la variable apparait (en positif)
}
}
void remove_claim(id_t_ claim) {
if(claims.find(claim) != no_more_claim()){ // si la variable existe dans claim
if(claims[claim] == 1) // si elle est présente une fois elle va disparaitre donc on la supprime de la map
claims.erase(claim);
else
claims[claim]--; // sinon on décrémente le nombre d'apparitions de 1
}
}
void remove_all_claim(id_t_ claim) {
claims.erase(claim);
}
void remove_all_negation(id_t_ neg) {
negations.erase(neg);
}
void remove_negation(id_t_ neg) {
if(negations.find(neg) != no_more_negation()){
if(negations[neg] == 1)
negations.erase(neg);
else
negations[neg]--;
}
}
void remove_clause_(id_t_ variable) {
for(auto e = clauses[variable].begin(); e!=clauses[variable].end(); ++e){
if(e->second.getSecondVar().isChecked()){
remove_claim(e->second.getSecondVar().id());
}
else{ //on décrémente le nbr d'apparations dans claims ou négations de la variation impliquée
remove_negation(e->second.getSecondVar().id());
} // dans la contrainte avec la variable "variable"
clauses[e->second.getSecondVar().id()].erase(variable); // on supprime les contraintes du style map[x][variable] qui sont les symétriques de
} // celles qu'on supprime juste après
clauses.erase(variable); // on supprime les contraintes liées à variable dans map[variable] donc ça supprime une map<long int(la variable), clause>
}
void rem_clause(id_t_ var) {
clauses.erase(var);
}
myClauses get_clauses() {return clauses;};
myNegations get_negations() {return negations;};
myClaims get_claims() {return claims;};
myfailedClauses get_failed_clauses() {return failedClauses;};
myVariables get_variables() {return variables;};
bool contains_claim(id_t var) {if(claims.find(var) != no_more_claim()) return true; else return false;};
bool contains_neg(id_t neg) {if(negations.find(neg) != no_more_negation()) return true; else return false;};
myClauses::iterator map_iterator() {return clauses.begin();};
myClauses::iterator noMap() {return clauses.end();};
clause_t::iterator clause_iterator(std::map<id_t_, clause> m) {return m.begin();};
clause_t::iterator noClause(std::map<id_t_, clause> m) {return m.end();};
myNegations::iterator negation_iterator() {return negations.begin();};
myNegations::iterator no_more_negation() {return negations.end();};
myClaims::iterator claim_iterator() {return claims.begin();};
myClaims::iterator no_more_claim() {return claims.end();};
int size() {
clause_t::size_type count = 0;
for(auto e = map_iterator(); e!= noMap(); ++e){
count += e->second.size();
}
return count;
}
myNegations::size_type negates_count() const {
return negations.size();
}
myClaims::size_type claims_count() const {
return claims.size();
}
myfailedClauses::size_type fails_count() const {
return failedClauses.size();
}
myVariables::size_type var_count() const {
return variables.size();
}
void compute_failed_clauses() {
for(auto e = map_iterator(); e != noMap(); ++e){
for(auto l = e->second.begin(); l != e->second.end(); ++l){
if(!l->second.verified()){
failedClauses[e->first].insert(std::make_pair(l->first, l->second));
}
}
}
}
};
problem parse(const char* filename) {
problem prob;
std::string line;
std::ifstream fichier(filename);
getline(fichier, line);
while(std::getline(fichier, line)){
std::istringstream iss(line);
long int var1, var2;
iss >> var1 >> var2;
if(var1 < 0 and var2 > 0) {
variable variable1(abs(var1), false);
variable variable2(var2, true);
prob.create_clause(variable1, variable2, !variable1.isChecked() || variable2.isChecked());
prob.create_negation(abs(var1));
prob.create_claim(var2);
}
else if(var1 < 0 and var2 < 0) {
variable variable1(abs(var1), false);
variable variable2(abs(var2), false);
prob.create_clause(variable1, variable2, !variable1.isChecked() || !variable2.isChecked());
prob.create_negation(abs(var1));
prob.create_negation(abs(var2));
}
else if(var1 > 0 and var2 < 0) {
variable variable1(var1, true);
variable variable2(abs(var2), false);
prob.create_clause(variable1, variable2, variable1.isChecked() || !variable2.isChecked());
prob.create_claim(var1);
prob.create_negation(abs(var2));
}
else {
variable variable1(var1, true);
variable variable2(var2, true);
prob.create_clause(variable1, variable2, variable1.isChecked() || variable2.isChecked());
prob.create_claim(var1);
prob.create_claim(var2);
}
}
return prob;
}
int simplify_problem(problem& prob) {
int nb_deletions(0);
for(auto e = prob.claim_iterator(); e != prob.no_more_claim(); ++e){ // on parcourt les claims et on vérifie qu'elles sont dans les negations
if(!prob.contains_neg(e->first)){
prob.remove_clause_(e->first);
prob.remove_all_claim(e->first); //si c'est pas le cas // on supprime les clauses liées à la variable en question // on supprime la variable de la map claims
nb_deletions++;
}
}
for(auto e = prob.negation_iterator(); e != prob.no_more_negation(); ++e){ // on parcourt les negations et ...
if(!prob.contains_claim(e->first)){
prob.remove_clause_(e->first);
prob.remove_all_negation(e->first);
nb_deletions++;
}
}
return nb_deletions;
}
void initialize(problem& prob) {
// créer un assignment random
for(auto e = prob.map_iterator(); e != prob.noMap(); ++e){
int r(rand()%2);
if(r==0){
prob.get_variables()[e->first] = true;
}
else
prob.get_variables()[e->first] = false;
for(auto l = e->second.begin(); l != e->second.end(); ++l){
int r(rand()%2);
if(r==0){
prob.get_variables()[l->first] = true;
}
else
prob.get_variables()[l->first] = false;
}
}
}
void flip_value(problem& prob, clause& clause_) {
int r(rand()%2);
if(r==0){
bool update = clause_.getFirstVar().toggle();
prob.get_variables()[clause_.getFirstVar().id()] = update;
}
else{
clause_.getSecondVar().toggle();
bool update = clause_.getSecondVar().toggle();
prob.get_variables()[clause_.getFirstVar().id()] = update;
}
}
clause pick_random_clause(problem& prob){
int size = prob.fails_count();
int r = 2*rand()%size;
int k(0);
for(auto e = prob.map_iterator(); e != prob.noMap(); ++e){
for(auto l = e->second.begin(); l != e->second.end(); ++l){
k++;
if(k%r == 0){
return l->second;
}
}
}
}
bool papadimitriou(problem& prob) {
int nb_deletions(-1);
while(nb_deletions != 0){
nb_deletions = simplify_problem(prob);
}
for(auto e = prob.map_iterator(); e != prob.noMap(); ++e){ //pour remove les doublons
for(auto el = e->second.begin(); el != e->second.end(); ++el){
prob.rem_clause(el->first);
}
}
int steps(prob.var_count());
for(int k(0); k < log2(steps); k++){
initialize(prob);
for(int j(0); j < 2*pow(steps, 2); ++j){
prob.compute_failed_clauses();
if(prob.fails_count() > 0){
std::cout << prob.fails_count() << "cou" << std::endl;
clause c = pick_random_clause(prob);
flip_value(prob, c);
}
else {
std::cout << 1 << std::endl;
return true;
}
}
}
std::cout << 0 << std::endl;
return false;
}
int main(int argc, char* argv[]) {
problem prob = parse("2sat1.txt");
papadimitriou(prob);
problem prob2 = parse("2sat2.txt");
papadimitriou(prob2);
problem prob3 = parse("2sat3.txt");
papadimitriou(prob3);
problem prob4 = parse("2sat4.txt");
papadimitriou(prob4);
problem prob5 = parse("2sat5.txt");
papadimitriou(prob5);
problem prob6 = parse("2sat6.txt");
papadimitriou(prob6);
/* int k(0);
for(auto e = prob.map_iterator(); e!=prob.noMap(); ++e){
for(auto el = e->second.begin(); el!=e->second.end(); el++){
std::cout << el->second << std::endl;
k++;
}
}
std::cout << k << std::endl;*/
return 0;
} |