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
| #include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <algorithm>
#include <map>
#include <list>
#include <ctime>
class variable {
private:
long int var;
bool checked;
public:
long int get_variable() {return var;};
bool isChecked() {return checked;};
void setChecked(bool boolean) {checked = boolean;};
variable(long int var_, bool checked_) : var(var_), checked(checked_) {}
};
class clause {
private:
variable var_1;
variable var_2;
bool expression;
public:
bool verified() {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, clause& clause_){ // pour pouvoir afficher une clause
if(clause_.getFirstVar().isChecked() and clause_.getSecondVar().isChecked())
flux << clause_.getFirstVar().get_variable() << " ou " << clause_.getSecondVar().get_variable() << " : " << clause_.verified() ;
else if(clause_.getFirstVar().isChecked() and !clause_.getSecondVar().isChecked())
flux << clause_.getFirstVar().get_variable() << " ou non " << clause_.getSecondVar().get_variable() << " : " << clause_.verified() ;
else if(!clause_.getFirstVar().isChecked() and clause_.getSecondVar().isChecked())
flux << "non " << clause_.getFirstVar().get_variable() << " ou " << clause_.getSecondVar().get_variable() << " : " << clause_.verified() ;
else
flux << "non " << clause_.getFirstVar().get_variable() << " ou non " << clause_.getSecondVar().get_variable() << " : " << clause_.verified() ;
return flux;
}
struct clause_compare {
bool operator() (clause const& c1, clause const& c2){
return c1.getFirstVar().get_variable() < c2.getFirstVar().get_variable() ;
}
};
class problem {
private:
typedef std::map<long int, std::map<long int, clause>> myClauses;
myClauses clauses;
typedef std::map<long int, int> myNegations;
myNegations negations;
typedef std::map<long int, int> myClaims;
myClaims claims;
typedef std::set<clause, clause_compare> myfailedClauses;
myfailedClauses failedClauses;
public:
void create_clause(variable var_1, variable var_2, bool clause_) {
clauses[var_1.get_variable()].insert(std::make_pair(var_2.get_variable(), clause(var_1, var_2, clause_))); // on ajoute chaque contrainte deux fois
clauses[var_2.get_variable()].insert(std::make_pair(var_1.get_variable(), clause(var_2, var_1, clause_))); // sous la forme clauses[x1][x2] et clauses[x2][x1]
}
void create_negation(long int neg) {
if(negations.find(neg) == no_more_negation()){
negations[neg] = 1;
}
else
negations[neg]++;
}
void create_claim(long int 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(long int 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(long int claim) {
claims.erase(claim);
}
void remove_all_negation(long int neg) {
negations.erase(neg);
}
void remove_negation(long int neg) {
if(negations.find(neg) != no_more_negation()){
if(negations[neg] == 1)
negations.erase(neg);
else
negations[neg]--;
}
}
void remove_clause_(long int variable) {
for(auto e = clauses[variable].begin(); e!=clauses[variable].end(); ++e){
if(e->second.getSecondVar().isChecked()){
remove_claim(e->second.getSecondVar().get_variable());
}
else{ //on décrémente le nbr d'apparations dans claims ou négations de la variation impliquée
remove_negation(e->second.getSecondVar().get_variable());
} // dans la contrainte avec la variable "variable"
clauses[e->second.getSecondVar().get_variable()].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(long int 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;};
bool contains_claim(long int var) {if(claims.find(var) != no_more_claim()) return true; else return false;};
bool contains_neg(long int neg) {if(negations.find(neg) != no_more_negation()) return true; else return false;};
std::map<long int, std::map<long int, clause>>::iterator map_iterator() {return clauses.begin();};
std::map<long int, std::map<long int, clause>>::iterator noMap() {return clauses.end();};
std::map<long int, clause>::iterator clause_iterator(std::map<long int, clause> m) {return m.begin();};
std::map<long int, clause>::iterator noClause(std::map<long int, clause> m) {return m.end();};
std::map<long int, int>::iterator negation_iterator() {return negations.begin();};
std::map<long int, int>::iterator no_more_negation() {return negations.end();};
std::map<long int, int>::iterator claim_iterator() {return claims.begin();};
std::map<long int, int>::iterator no_more_claim() {return claims.end();};
};
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 flip_value(clause& clause_) {
int r(rand()%2);
if(r==0)
clause_.getFirstVar().setChecked(!clause_.getFirstVar().isChecked());
else
clause_.getSecondVar().setChecked(!clause_.getSecondVar().isChecked());
}
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);
if(!el->second.verified())
prob.get_failed_clauses().insert(el->second);
}
}
return true;
} |
Partager