PbSolver_convertSort.C 12.5 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 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
/**************************************************************************[PbSolver_convertSort.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 "PbSolver.h"
#include "OptimalBase.h"
#include "Hardware.h"
#include "Debug.h"

//#define pf(format, args...) (reportf(format, ## args), fflush(stdout))
#define pf(format, args...) nothing()

void nothing(void) {}


//=================================================================================================


#define lit2fml(p) id(var(var(p)),sign(p))


static
void buildSorter(vec<Formula>& ps, vec<int>& Cs, vec<Formula>& out_sorter)
{
    out_sorter.clear();
    for (int i = 0; i < ps.size(); i++)
        for (int j = 0; j < Cs[i]; j++)
            out_sorter.push(ps[i]);
    oddEvenSort(out_sorter); // (overwrites inputs)
}

static
void buildSorter(vec<Formula>& ps, vec<Int>& Cs, vec<Formula>& out_sorter)
{
    vec<int>    Cs_copy;
    for (int i = 0; i < Cs.size(); i++)
        Cs_copy.push(toint(Cs[i]));
    buildSorter(ps, Cs_copy, out_sorter);
}


class Exception_TooBig {};

static
void buildConstraint(vec<Formula>& ps, vec<Int>& Cs, vec<Formula>& carry, vec<int>& base, int digit_no, vec<vec<Formula> >& out_digits, int max_cost)
{
    assert(ps.size() == Cs.size());

    if (FEnv::topSize() > max_cost) throw Exception_TooBig();

    //**/reportf("buildConstraint("); for (int i = 0; i < ps.size(); i++) reportf("%d*%d ", toint(Cs[i]), (*debug_names)[index(ps[i])]); reportf("+ %d carry)\n", carry.size());    index==-1 --- true

    if (digit_no == base.size()){
        // Final digit, build sorter for rest:
        // -- add carry bits:
        for (int i = 0; i < carry.size(); i++)
            ps.push(carry[i]),
            Cs.push(1);
        out_digits.push();
        buildSorter(ps, Cs, out_digits.last());

    }else{
        vec<Formula>    ps_rem;
        vec<int>        Cs_rem;
        vec<Formula>    ps_div;
        vec<Int>        Cs_div;

        // Split sum according to base:
        int B = base[digit_no];
        for (int i = 0; i < Cs.size(); i++){
            Int div = Cs[i] / Int(B);
            int rem = toint(Cs[i] % Int(B));
            if (div > 0){
                ps_div.push(ps[i]);
                Cs_div.push(div);
            }
            if (rem > 0){
                ps_rem.push(ps[i]);
                Cs_rem.push(rem);
            }
        }

        // Add carry bits:
        for (int i = 0; i < carry.size(); i++)
            ps_rem.push(carry[i]),
            Cs_rem.push(1);

        // Build sorting network:
        vec<Formula> result;
        buildSorter(ps_rem, Cs_rem, result);

        // Get carry bits:
        carry.clear();
        for (int i = B-1; i < result.size(); i += B)
            carry.push(result[i]);

        out_digits.push();
        for (int i = 0; i < B-1; i++){
            Formula out = _0_;
            for (int j = 0; j < result.size(); j += B){
                int n = j+B-1;
                if (j + i < result.size())
                    out |= result[j + i] & ((n >= result.size()) ? _1_ : ~result[n]);
            }
            out_digits.last().push(out);
        }

        buildConstraint(ps_div, Cs_div, carry, base, digit_no+1, out_digits, max_cost); // <<== change to normal loop
    }
}

/*
Naming:
  - a 'base' is a vector of integers, stating how far you count at that position before you wrap to the next digit (generalize base).
  - A 'dig' is an integer representing a digit in a number of some base.
  - A 'digit' is a vector of formulas, where the number of 1:s represents a digit in a number of some base.
*/


static
void convert(Int num, vec<int>& base, vec<int>& out_digs)
{
    for (int i = 0; i < base.size(); i++){
        out_digs.push(toint(num % Int(base[i])));
        num /= Int(base[i]);
    }
    out_digs.push(toint(num));
}


// Compare number lexicographically to output digits from sorter networks.
// Formula is TRUE when 'sorter-digits >= num'.
//
static
Formula lexComp(int sz, vec<int>& num, vec<vec<Formula> >& digits)
{
    if (sz == 0)
        return _1_;
    else{
      //**/reportf("num    :"); for (int i = 0; i < sz; i++) reportf(" %d", num[i]); reportf("\n");
      //**/reportf("#digits:"); for (int i = 0; i < sz; i++) reportf(" %d", digits[i].size()); reportf("\n");

        sz--;
        vec<Formula>& digit = digits[sz];
        int           dig   = num[sz];

        Formula gt = (digit.size() > dig) ? digit[dig] : _0_;       // This digit is greater than the "dig" of 'num'.
        Formula ge = (dig == 0) ? _1_ :
                     (digit.size() > dig-1) ? digit[dig-1] : _0_;   // This digit is greater than or equal to the "dig" of 'num'.

        /**/if (sz == 0) return ge;
        return gt | (ge & lexComp(sz, num, digits));
    }
}
static
Formula lexComp(vec<int>& num, vec<vec<Formula> >& digits) {
    assert(num.size() == digits.size());
    return lexComp(num.size(), num, digits); }


static
Formula buildConstraint(vec<Formula>& ps, vec<Int>& Cs, vec<int>& base, Int lo, Int hi, int max_cost)
{
    vec<Formula> carry;
    vec<vec<Formula> > digits;
    buildConstraint(ps, Cs, carry, base, 0, digits, max_cost);
    if (FEnv::topSize() > max_cost) throw Exception_TooBig();

    vec<int> lo_digs;
    vec<int> hi_digs;
    if (lo != Int_MIN)
        convert(lo, base, lo_digs);
    if (hi != Int_MAX)
        convert(hi+1, base, hi_digs);   // (+1 because we will change '<= x' to '!(... >= x+1)'


    /*DEBUG
    pf("Networks:");
    for (int i = 0; i < digits.size(); i++)
        pf(" %d", digits[i].size());
    pf("\n");

    if (lo != Int_MIN){
        pf("lo=%d :", lo); for (int i = 0; i < lo_digs.size(); i++) pf(" %d", lo_digs[i]); pf("\n"); }
    if (hi != Int_MAX){
        pf("hi+1=%d :", hi+1); for (int i = 0; i < hi_dgis.size(); i++) pf(" %d", hi_digs[i]); pf("\n"); }
    END*/

/*
Base:  (1)    8    24   480
       aaa bbbbbb ccc ddddddd
Num:    2    0     5     6
*/

    if (hi == Int_MAX) stats_std_form++; else stats_band_form++;
    stats_sort_constraints++;
    Formula ret = ((lo == Int_MIN) ? _1_ :  lexComp(lo_digs, digits))
                & ((hi == Int_MAX) ? _1_ : ~lexComp(hi_digs, digits));
    if (FEnv::topSize() > max_cost) throw Exception_TooBig();
    return ret;
}


static
Formula buildConstraintGpw(vec<Formula>& ps, vec<Int>& Cs, vec<int>& base, Int lo, int max_cost)
{

  //##
  Int B = 1;
  for (int i=0; i < base.size(); i++)
    B *= Int(base[i]);

  Int rem    = lo % B ;
  int lo_val;
  Int c;

  if(rem==0) { lo_val=toint(lo/B);   c=0;    }
  else       { lo_val=toint(lo/B)+1; c=B-rem; }

  /**/ if (opt_verbosity >= 3){reportf("lo=%d, B=%d, rem=%d, c=%d, lo_val=%d\n", toint(lo), toint(B), toint(rem), toint(c), lo_val); }

  ps.push(_1_); Cs.push(c);

  vec<Formula> carry;
  vec<vec<Formula> > digits;
  buildConstraint(ps, Cs, carry, base, 0, digits, max_cost);

  /**/ if (opt_verbosity >= 3){reportf("Networks:"); for (int i = 0; i < digits.size(); i++) reportf(" %d", digits[i].size()); reportf("\n");}

  assert(base.size()+1 ==digits.size());
  stats_std_form ++;
  stats_sort_constraints++;
  Formula ret = (digits[base.size()])[lo_val-1];
  return ret;
}


// Will return '_undef_' if 'cost_limit' is exceeded.
//
Masahiko Sakai's avatar
Masahiko Sakai committed
255
Formula buildConstraint(const Linear& c, vec<int>& base, int max_cost)
Masahiko Sakai's avatar
Masahiko Sakai committed
256 257 258 259 260 261 262 263 264 265
{
  vec<Formula>    ps;
  vec<Int>        Cs;
  Int csum = 0; 

  for (int j = 0; j < c.size; j++)
    ps.push(lit2fml(c[j])),
      Cs.push(c(j)),
      csum += c(j);

Masahiko Sakai's avatar
Masahiko Sakai committed
266 267 268 269 270 271 272
  if(base.size() == 0) {
    vec<Int> dummy;
    int      cost;
    //vec<int> base;
    optimizeBase(Cs, dummy, cost, base, obt_comb);  // the last arugument declares carry_important
  }
  
Masahiko Sakai's avatar
Masahiko Sakai committed
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
  FEnv::push();

  /**/if (opt_verbosity >= 3){reportf("SORTER: "); dump(c); reportf(" csum=%d", toint(csum)); reportf(" lo="),dump(c.lo),reportf(" hi="),dump(c.hi),reportf("\n");}

  Formula ret = _1_ ;

  if(c.lo >  0) {
    if(opt_convert_gpw==gt_none) { // original minisatp conversion
      //      reportf("non-GPW\n");
      try {
        if(opt_avoid_band_constraint==0 ||
           (opt_avoid_band_constraint==1 && c.lo != c.hi)){
          ret = buildConstraint(ps, Cs, base, c.lo, c.hi, max_cost);
	}else{
          ret = buildConstraint(ps, Cs, base, c.lo, Int_MAX, max_cost);
	  if(c.hi != Int_MAX) {
	    ps.clear(); Cs.clear();
	    for(int i=0; i<c.size; i++)
	      ps.push(lit2fml(~c[i])), Cs.push(c(i));
	    ret &= buildConstraint(ps, Cs, base, csum - c.hi, Int_MAX, max_cost);
	  }
	}
      }catch (Exception_TooBig){
	FEnv::pop();
	return _undef_;
      }
      
      stats_sort_cost = max(stats_sort_cost, FEnv::topSize());
      
      if (opt_verbosity >= 2){
	reportf("Sorter-cost:%5d     ", FEnv::topSize());
	reportf("Base:"); for (int i = 0; i < base.size(); i++) reportf(" %d", base[i]); reportf("\n");
      }
      FEnv::keep();
      
    } else {   // GPW
      //      reportf("GPW\n");
      try {
	ret = _1_ ;

	bool lowp= c.lo <= (csum - c.lo + 1) ;
	if(opt_convert_gpw==gt_positive 
	   || opt_convert_gpw==gt_both
	   || (opt_convert_gpw==gt_low && lowp)
	   || (opt_convert_gpw==gt_high && !lowp) ) {
	  ps.clear(); Cs.clear();
	  for(int i=0; i<c.size; i++)
	    ps.push(lit2fml(c[i])), Cs.push(c(i));
	  //**/reportf("GPW: lo=%d\n", toint(c.lo)); 
	  ret &= buildConstraintGpw(ps, Cs, base, c.lo, max_cost); //positive
	}
      
	if(opt_convert_gpw==gt_negative
	   || opt_convert_gpw==gt_both
	   || (opt_convert_gpw==gt_low && !lowp)
	   || (opt_convert_gpw==gt_high && lowp) ) {
	  ps.clear(); Cs.clear();
	  for(int i=0; i<c.size; i++)
	    ps.push(lit2fml(~c[i])), Cs.push(c(i));
	  //**/reportf("GPW: hi+1=%d\n", toint(csum - c.lo + 1)); 
	  ret &= ~buildConstraintGpw(ps, Cs, base, csum - c.lo + 1, max_cost);
	                                                            //negative
	}

	if (c.hi != Int_MAX) { // a x b y <= d to a+b-d <= a ~x b ~y
	  //	  assert(c.dlt==lit_Undef);

	  bool lowp= csum - c.hi < c.hi + 1;
	  if(opt_convert_gpw==gt_positive 
	     || opt_convert_gpw==gt_both
	     || (opt_convert_gpw==gt_low && lowp)
	     || (opt_convert_gpw==gt_high && !lowp) ) {
	    ps.clear(); Cs.clear();
	    for(int i=0; i<c.size; i++)
	      ps.push(lit2fml(~c[i])), Cs.push(c(i));
	    //**/reportf("GPW: lo=%d\n", toint(csum - c.hi)); 
	    ret &= buildConstraintGpw(ps, Cs, base, csum - c.hi, max_cost);
	                                                          // positive
	  }

	  if(opt_convert_gpw==gt_negative
	     || opt_convert_gpw==gt_both
	     || (opt_convert_gpw==gt_low && !lowp)
	     || (opt_convert_gpw==gt_high && lowp) ) {
	    ps.clear(); Cs.clear();
	    for(int i=0; i<c.size; i++)
	      ps.push(lit2fml(c[i])), Cs.push(c(i));
	    //**/reportf("GPW: hi+1=%d\n", toint(c.hi + 1)); 
	    ret &= ~buildConstraintGpw(ps, Cs, base, c.hi+1, max_cost);
                                                                   //negative
	  }
	}
      }catch (Exception_TooBig){
	FEnv::pop();
	return _undef_;
      }
      
      stats_sort_cost = max(stats_sort_cost, FEnv::topSize());
      if (opt_verbosity >= 2){
	reportf("Sorter-cost:%5d     ", FEnv::topSize());
	reportf("Base:"); for (int i = 0; i < base.size(); i++) reportf(" %d", base[i]); reportf("\n");
      }
      FEnv::keep();
    }
  }

  assert(ret != _undef_);
  /*
    if(c.dlt!=lit_Undef) {
    Formula d= id(var(var(c.dlt)),sign(c.dlt));
    ret ^= ~d;  
    }
  */
  if(c.llt==lit_Undef) {
    return ret;
  } else {
    Formula f = _1_ ;
    if(c.llt!=lit_Undef)
      f &= ~lit2fml(c.llt) | ret ;   // llt => ret
/*
    if(c.rlt!=lit_Undef)
      f &= lit2fml(c.rlt) | ~ret ;   // rlt <= ret
*/
    return f;
  }
}