コード例 #1
0
ファイル: Solver.C プロジェクト: lokdlok/Numberjack
Lit Solver::pickBranchLit(int polarity_mode, double random_var_freq)
{
    Var next = var_Undef;

    // Random decision:
    if (drand(random_seed) < random_var_freq && !order_heap.empty()){
        next = order_heap[irand(random_seed,order_heap.size())];
        if (toLbool(assigns[next]) == l_Undef && decision_var[next])
            rnd_decisions++; }

    // Activity based decision:
    while (next == var_Undef || toLbool(assigns[next]) != l_Undef || !decision_var[next])
        if (order_heap.empty()){
            next = var_Undef;
            break;
        }else
            next = order_heap.removeMin();

    bool sign = false;
    switch (polarity_mode){
    case polarity_true:  sign = false; break;
    case polarity_false: sign = true;  break;
    case polarity_user:  sign = polarity[next]; break;
    case polarity_rnd:   sign = irand(random_seed, 2); break;
    default: assert(false); }

    return next == var_Undef ? lit_Undef : Lit(next, sign);
}
コード例 #2
0
ファイル: Solver.C プロジェクト: msoos/glucosetrack
Lit Solver::pickBranchLit(int polarity_mode, double random_var_freq)
{
    Var next = var_Undef;
    /*if (backup.stage == 0) {
        assert(order_heap.heapProperty());
        for(int i = 0; i < std::min(order_heap.size(), 10); i++) {
            printf("order_heap %d has var %d, has activity %lf\n", i, order_heap[i], activity[order_heap[i]]);
        }
    }*/

    // Random decision:
    if (drand(random_seed) < random_var_freq
        && !order_heap.empty()
    ){
        next = order_heap[irand(random_seed,order_heap.size())];
        if (toLbool(assigns[next]) == l_Undef && decision_var[next])
            rnd_decisions++;
    }

    // Activity based decision:
    while (next == var_Undef || toLbool(assigns[next]) != l_Undef || !decision_var[next])
      if (order_heap.empty()){
            next = var_Undef;
            break;
        }else
            next = order_heap.removeMin();

    bool sign = false;
    switch (polarity_mode){
    case polarity_true:  sign = false; break;
    case polarity_false: sign = true;  break;
    case polarity_user:  if(next!=var_Undef) sign = polarity[next]; break;
    case polarity_rnd:   sign = irand(random_seed, 2); break;
    default: assert(false); }

    #ifdef RESTORE
    if (backup.stage == 0) {
        printf("--> Picking decision lit "); printLit(Lit(next, sign)); printf(" at decision level: %d, sublevel: %d\n", decisionLevel(), trail.size());
    }
    #endif

    return next == var_Undef ? lit_Undef : Lit(next, sign);
}
コード例 #3
0
ファイル: Solver.C プロジェクト: richardw347/STRP_EXT_TOOLS
/*_________________________________________________________________________________________________
|
|  enqueue : (p : Lit) (from : Constr*)  ->  [bool]
|  
|  Description:
|    Puts a new fact on the propagation queue as well as immediately updating the variable's value.
|    Should a conflict arise, FALSE is returned.
|  
|  Input:
|    p    - The fact to enqueue
|    from - [Optional] Fact is propagated from this (currently) unit clause. Stored in 'reason[]'.
|           Default value is NULL (no reason).
|  
|  Output:
|    TRUE if fact was enqueued without conflict, FALSE otherwise.
|________________________________________________________________________________________________@*/
bool Solver::enqueue(Lit p, Constr* from, Deriv *deriv)
{
//printf("enqueuing: "L_LIT" at decisionlevel %d\n", L_lit(p), decisionLevel);
    if (value(p) != l_Undef){
        if (value(p) == l_False){
            // Conflicting enqueued assignment
            if (decisionLevel == 0)
                ok = false;
            return false;
        }else{
            // Existing consistent assignment -- don't enqueue
            return true;
        }
    }else{
        // New fact -- store it.
        //if (verbosity >= 2) printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(p));
//printf(L_IND"bind("L_LIT")\n", L_ind, L_lit(p));
        assigns[var(p)] = toLbool(!sign(p));
        level  [var(p)] = decisionLevel;
        reason [var(p)] = from;
        trail.push(p);
        propQ.insert(p);

		if (!doDeriv || decisionLevel != 0) {
			return true;
		}

		if (from != NULL) {
			assert(deriv == NULL);
			assert(var_deriv[var(p)]->empty());
			assert(!from->getDeriv()->empty());
			var_deriv[var(p)]->addDeriv(from->getDeriv());

			vec<Lit> p_reason;
			from->calcReason(*this, p, p_reason);
			for (int j = 0; j < p_reason.size(); j++){
				Lit q = p_reason[j];
				var_deriv[var(p)]->addDeriv(var_deriv[var(q)]);
			}
		}
		else {
			assert(deriv != NULL);
			assert(var_deriv[var(p)]->empty());
			assert(!deriv->empty());
			var_deriv[var(p)] = deriv;
		}

        return true;
    }
}
コード例 #4
0
ファイル: Solver.C プロジェクト: TatianaBatura/link-grammar
Lit Solver::pickBranchLit(int polarity_mode, double random_var_freq)
{
  Var next = var_Undef;

  // Random decision:
  /*  if (drand(random_seed) < random_var_freq && !order_heap.empty()){
    next = order_heap[irand(random_seed,order_heap.size())];
    if (toLbool(assigns[next]) == l_Undef && decision_var[next])
      rnd_decisions++; }
  */

  // Activity based decision:
  while (next == var_Undef || toLbool(assigns[next]) != l_Undef || !decision_var[next]) {
    if (order_heap.empty()){
      next = var_Undef;
      break;
    }else
      next = order_heap.removeMin();
  }

  if (next == var_Undef) return lit_Undef;

  bool sign = false;

  
  switch (polarity_mode){
  case polarity_true:  sign = false; break;
  case polarity_false: sign = true;  break;
  case polarity_user:  sign = polarity[next]; break;
  case polarity_rnd:   sign = irand(random_seed, 2); break;
  }

  sign = !polarity[next];

  //  do {
  //    int var;
  //    std::cin >> var;
  //    sign = !(var > 0);
  //    next = Var(abs(var) - 1);
  //  } while(toLbool(assigns[next]) != l_Undef);

  //  std::cout << "var: " << var << " sign: " << sign << std::endl;

  return next == var_Undef ? lit_Undef : Lit(next, sign);
}
コード例 #5
0
public:
    lbool()       : value(0) { }
    lbool(bool x) : value((int)x*2-1) { }
    int toInt(void) const { return value; }

    bool  operator == (const lbool& other) const { return value == other.value; }
    bool  operator != (const lbool& other) const { return value != other.value; }
    lbool operator ~  (void)               const { return lbool(-value); }

    friend int   toInt  (lbool l);
    friend lbool toLbool(int   v);
};
inline int   toInt  (lbool l) { return l.toInt(); }
inline lbool toLbool(int   v) { return lbool(v);  }

const lbool l_True  = toLbool( 1);
const lbool l_False = toLbool(-1);
const lbool l_Undef = toLbool( 0);


//=================================================================================================
// Relation operators -- extend definitions from '==' and '<'


#ifndef __SGI_STL_INTERNAL_RELOPS   // (be aware of SGI's STL implementation...)
#define __SGI_STL_INTERNAL_RELOPS
template <class T> static inline bool operator != (const T& x, const T& y) { return !(x == y); }
template <class T> static inline bool operator >  (const T& x, const T& y) { return y < x;     }
template <class T> static inline bool operator <= (const T& x, const T& y) { return !(y < x);  }
template <class T> static inline bool operator >= (const T& x, const T& y) { return !(x < y);  }
#endif