Hardware_clausify.C 12.6 KB
Newer Older
Masahiko Sakai's avatar
Masahiko Sakai committed
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
/*****************************************************************************[Hardware_clausify.C]
Copyright (c) 2005-2010, Niklas Een, Niklas Sorensson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************************************/

#include "Hardware.h"
#include "Debug.h"

struct Clausifier
{
    Solver&      s;
    vec<Lit>     tmp_clause;
    vec<Formula> tmp_marked;

    Clausifier(Solver& _s) : s(_s) {}

    static /*WARNING*/ FEnv::CompMap<int>      occ;
    static /*WARNING*/ FEnv::CompMap<Var>      vmap;
    static /*WARNING*/ FEnv::CompMap<Lit,true> vmapp;
    FMap<bool>   seen;

    inline void clause(Lit a, Lit b) {
        tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); s.addClause(tmp_clause); }
    inline void clause(Lit a, Lit b, Lit c) {
        tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); tmp_clause.push(c); s.addClause(tmp_clause); }
    inline void clause(Lit a, Lit b, Lit c, Lit d) {
        tmp_clause.clear(); tmp_clause.push(a); tmp_clause.push(b); tmp_clause.push(c); tmp_clause.push(d); s.addClause(tmp_clause); }


    void  usage  (Formula f);
    void _collect(Formula f, vec<Formula>& out);
    void  collect(Formula f, vec<Formula>& out);

    Lit   basicClausify   (Formula f);
    Lit   polarityClausify(Formula f);
};

FEnv::CompMap<int>      Clausifier::occ  (0);
FEnv::CompMap<Var>      Clausifier::vmap (var_Undef);
FEnv::CompMap<Lit,true> Clausifier::vmapp(lit_Undef);

void Clausifier::usage(Formula f)
{
    if (Atom_p(f))
        return;

    occ.set(f,occ.at(f)+1);

    if (occ.at(f) == 1){
        if (Bin_p(f)){
            usage(left(f)); usage(right(f));
        }else if (ITE_p(f) || ITEp_p(f) || ITEn_p(f)){
            usage(cond(f)); usage(tt(f)); usage(ff(f));
        }else{
            assert(FA_p(f));
            usage(FA_x(f)); usage(FA_y(f)); usage(FA_c(f));
        }
    }
}

void Clausifier::collect(Formula f, vec<Formula>& out)
{
    tmp_marked.clear();
    _collect(left(f), out);
    _collect(right(f),out);
    for (int i = 0; i < tmp_marked.size(); i++)
        seen.set(tmp_marked[i],false);
}

void Clausifier::_collect(Formula f, vec<Formula>& out)
{
    if (!seen.at(f)){
        seen.set(f,true);
        tmp_marked.push(f);
        if (Bin_p(f) && op(f) == op_And && !sign(f) && occ.at(f) == 1){
            _collect(left(f) ,out);
            _collect(right(f),out);
        }
        else
            out.push(f);
    }
}

Lit Clausifier::polarityClausify(Formula f)
{
    Lit result = lit_Undef;

    if (Atom_p(f)){
      #if 0
        assert(!Const_p(f));
      #else
        if (Const_p(f)){
	  Var x = s.newVar(); //reportf("pol 1: newVar = %d\n", x); 
            s.addUnit(Lit(x, (f == _0_)));
            result = Lit(x); 
        }else
      #endif
        result = Lit(index(f),sign(f));
Masahiko Sakai's avatar
Masahiko Sakai committed
113 114 115 116 117 118 119
    }else if (vmapp.at(f) != lit_Undef &&
              ( !opt_simp_solver || !s.varElimed(var(vmapp.at(f))) ) ){
        result = vmapp.at(f);
    }else{
        result = (vmapp.at(~f) != lit_Undef &&
	          (!opt_simp_solver || !s.varElimed(var(vmapp.at(~f)))) ) ?
	         Lit(var(vmapp.at(~f))) : Lit(s.newVar(!opt_branch_pbvars));
Masahiko Sakai's avatar
Masahiko Sakai committed
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
        if (Bin_p(f)){
            if (op(f) == op_And){
                vec<Formula> conj;
                collect(f, conj);
                assert(conj.size() > 1);
                if (!sign(f)){
                    for (int i = 0; i < conj.size(); i++)
                        clause(~result,polarityClausify(conj[i]));
                }else{
                    vec<Lit> ls;
                    ls.push(result);
                    for (int i = 0; i < conj.size(); i++)
                        ls.push(polarityClausify(~conj[i]));
                    s.addClause(ls);
                }
                //printf("and: %d = ", var(result));
                //for (int i = 0; i < conj.size(); i++)
                //    printf("%c%d ", sign(polarityClausify(conj[i])) ? '-' : ' ',
                //           var(polarityClausify(conj[i])));
                //printf("\n");
            }else{
                Lit l  = polarityClausify( left (f));
                Lit r  = polarityClausify( right(f));
                Lit nl = polarityClausify(~left (f));
                Lit nr = polarityClausify(~right(f));

                //printf("equiv:\n");
                assert(op(f) == op_Equiv);
                if (!sign(f)){
                    clause(~result, nl,  r);
                    clause(~result,  l, nr);
                }else{
                    clause( result, nl, nr);
                    clause( result,  l,  r);
                }
            }
        }else if (ITE_p(f)){
          stats_non_monotonic_coding++;
	  Lit  c = polarityClausify( cond(f));
	  Lit nc = polarityClausify(~cond(f));
	  if (!sign(f)){
	    Lit  a = polarityClausify( tt  (f));
	    Lit  b = polarityClausify( ff  (f));
	    clause(~result, nc,  a);
	    clause(~result,  c,  b);
	    clause( a,  b, ~result);
	  }else{
	    Lit na = polarityClausify(~tt  (f));
	    Lit nb = polarityClausify(~ff  (f));
	    clause( result, nc, na);
	    clause( result,  c, nb);
	    clause(na, nb,  result);
	  }
        }else if (ITEn_p(f)){
          stats_monotonic_coding++;
	  if (!sign(f)){
	    Lit  c = polarityClausify( cond(f));
	    Lit  a = polarityClausify( tt  (f));
	    Lit  b = polarityClausify( ff  (f));
	    clause(~result,  a);
	    clause(~result,  c,  b);
	  }else{
	    Lit nc = polarityClausify(~cond(f));
	    Lit na = polarityClausify(~tt  (f));
	    Lit nb = polarityClausify(~ff  (f));
	    clause( result, nb);
	    clause( result, nc, na);
	  }
        }else if (ITEp_p(f)){
          stats_monotonic_coding++;
	  if (!sign(f)){
	    Lit nc = polarityClausify(~cond(f));
	    Lit  a = polarityClausify( tt  (f));
	    Lit  b = polarityClausify( ff  (f));
	    clause(~result,  b);
	    clause(~result,  nc,  a);
	  }else{
	    Lit  c = polarityClausify( cond(f));
	    Lit na = polarityClausify(~tt  (f));
	    Lit nb = polarityClausify(~ff  (f));
	    clause( result, na);
	    clause( result, c, nb);
	  }
        }else{
            assert(FA_p(f));

            if (isCarry(f)){
                //printf("carry:\n");
                if (!sign(f)){
                    Lit  a = polarityClausify( FA_x(f));
                    Lit  b = polarityClausify( FA_y(f));
                    Lit  c = polarityClausify( FA_c(f));
                    clause(~result,  a,  b);
                    clause(~result,  c,  a);
                    clause(~result,  c,  b);
                }else{
                    Lit na = polarityClausify(~FA_x(f));
                    Lit nb = polarityClausify(~FA_y(f));
                    Lit nc = polarityClausify(~FA_c(f));
                    clause( result, na, nb);
                    clause( result, nc, na);
                    clause( result, nc, nb);
                }
            }else{
                Lit  a = polarityClausify( FA_x(f));
                Lit  b = polarityClausify( FA_y(f));
                Lit  c = polarityClausify( FA_c(f));
                Lit na = polarityClausify(~FA_x(f));
                Lit nb = polarityClausify(~FA_y(f));
                Lit nc = polarityClausify(~FA_c(f));

                //printf("sum:\n");
                if (!sign(f)){
                    clause(~result, nc,  na,  b);
                    clause(~result, nc,   a, nb);
                    clause(~result,  c,  na, nb);
                    clause(~result,  c,   a,  b);
                }else{
                    clause( result, nc,  na, nb);
                    clause( result, nc,   a,  b);
                    clause( result,  c,  na,  b);
                    clause( result,  c,   a, nb);
                }
            }
        }
        result = Lit(var(result),sign(f));
        vmapp.set(f,result);
    }

    assert(result != lit_Undef);

    return result;
}

Lit Clausifier::basicClausify(Formula f)
{
    Var result = var_Undef;

    //**/ reportf("basicClausify("); dump(f); reportf(")\n");
    if (Atom_p(f)){
        assert(!Const_p(f));
        result = index(f);
    }else if (vmap.at(f) != var_Undef && !s.varElimed(vmap.at(f))){
        result = vmap.at(f);
    }else{
        result = s.newVar(!opt_branch_pbvars); reportf("bas 1: newVar = %d\n", result);
        Lit p  = Lit(result);
        if (Bin_p(f)){

            if (op(f) == op_And){
                vec<Formula> conj;
                collect(f, conj);
                assert(conj.size() > 1);
                for (int i = 0; i < conj.size(); i++)
                    clause(~p,basicClausify(conj[i]));

                tmp_clause.clear();
                tmp_clause.push(p);
                for (int i = 0; i < conj.size(); i++)
                    tmp_clause.push(~basicClausify(conj[i]));
                s.addClause(tmp_clause);
            }else{

                Lit l = basicClausify(left (f));
                Lit r = basicClausify(right(f));

                //printf("equiv:\n");
                assert(op(f) == op_Equiv);
                clause(~p, ~l,  r);
                clause(~p,  l, ~r);
                clause( p, ~l, ~r);
                clause( p,  l,  r);
            }
        }else if (ITE_p(f)){
            stats_non_monotonic_coding++;
            Lit c = basicClausify(cond(f));
            Lit a = basicClausify(tt  (f));
            Lit b = basicClausify(ff  (f));
	    /*
	    if(opt_convert_bdd_monotonic) {
	      clause(~p,  a);
	      clause(~p,  c,  b);
	      clause( p, ~b);
	      clause( p, ~c, ~a);
	    } else {
	    */
	    clause(~p, ~c,  a);
	    clause(~p,  c,  b);
	    clause( p, ~c, ~a);
	    clause( p,  c, ~b);

	    // not neccessary !!
	    clause(~a, ~b,  p);
	    clause( a,  b, ~p);
        }else if (ITEn_p(f)){
            stats_monotonic_coding++;
            Lit c = basicClausify(cond(f));
            Lit a = basicClausify(tt  (f));
            Lit b = basicClausify(ff  (f));
	    clause(~p,  a);
	    clause(~p,  c,  b);
	    clause( p, ~b);
	    clause( p, ~c, ~a);
        }else if (ITEp_p(f)){
            stats_monotonic_coding++;
            Lit c = basicClausify(cond(f));
            Lit a = basicClausify(tt  (f));
            Lit b = basicClausify(ff  (f));
	    clause(~p,  b);
	    clause(~p, ~c,  a);
	    clause( p, ~a);
	    clause( p,  c, ~b);
        }else{
            assert(FA_p(f));

            Lit a = basicClausify(FA_x(f));
            Lit b = basicClausify(FA_y(f));
            Lit c = basicClausify(FA_c(f));

            if (isCarry(f)){
                //printf("carry:\n");
                clause(~p,  a,  b);
                clause(~p,  c,  a);
                clause(~p,  c,  b);
                clause( p, ~c, ~a);
                clause( p, ~c, ~b);
                clause( p, ~a, ~b);
            }else{
                //printf("sum:\n");
                clause(~p, ~c,  ~a,  b);
                clause(~p, ~c,   a, ~b);
                clause(~p,  c,  ~a, ~b);
                clause(~p,  c,   a,  b);
                clause( p, ~c,  ~a, ~b);
                clause( p, ~c,   a,  b);
                clause( p,  c,  ~a,  b);
                clause( p,  c,   a, ~b);
            }
        }
        vmap.set(f,result);
    }

    assert(result != var_Undef);

    return Lit(result,sign(f));
}


void clausify(Solver& s, const vec<Formula>& fs, vec<Lit>& out)
{
    Clausifier c(s);

    for (int i = 0; i < fs.size(); i++)
        c.usage(fs[i]);

    if (opt_convert_weak)
        for (int i = 0; i < fs.size(); i++)
            out.push(c.polarityClausify(fs[i]));
    else
        for (int i = 0; i < fs.size(); i++)
            out.push(c.basicClausify(fs[i]));
}


void clausify(Solver& s, const vec<Formula>& fs)
{
    vec<Lit>  out;
    clausify(s, fs, out);
    for (int i = 0; i < out.size(); i++)
        s.addUnit(out[i]);
}