1 /*****************************************************************************/
2 /*!
3  *\file minisat_varorder.h
4  *\brief MiniSat decision heuristics
5  *
6  * Author: Alexander Fuchs
7  *
8  * Created: Fri Sep 08 11:04:00 2006
9  *
10  * <hr>
11  *
12  * License to use, copy, modify, sell and/or distribute this software
13  * and its documentation for any purpose is hereby granted without
14  * royalty, subject to the terms and conditions defined in the \ref
15  * LICENSE file provided with this distribution.
16  *
17  * <hr>
18  */
19 /*****************************************************************************/
20 
21 /**************************************************************************************[VarOrder.h]
22 MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
23 
24 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
25 associated documentation files (the "Software"), to deal in the Software without restriction,
26 including without limitation the rights to use, copy, modify, merge, publish, distribute,
27 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
28 furnished to do so, subject to the following conditions:
29 
30 The above copyright notice and this permission notice shall be included in all copies or
31 substantial portions of the Software.
32 
33 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
34 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
36 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
37 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 **************************************************************************************************/
39 
40 #ifndef _cvc3__minisat__varorder_h_
41 #define _cvc3__minisat__varorder_h_
42 
43 //=================================================================================================
44 
45 #include "minisat_types.h"
46 #include "minisat_heap.h"
47 #include <iostream>
48 #include <vector>
49 
50 // implements the decision heuristics by using a heap over variable ids (which are ints)
51 
52 namespace MiniSat {
53 
54 struct VarOrder_lt {
55   const std::vector<double>&  activity;
operatorVarOrder_lt56   bool operator () (Var x, Var y) { return activity[x] > activity[y]; }
VarOrder_ltVarOrder_lt57   VarOrder_lt(const std::vector<double>&  act) : activity(act) { }
58 };
59 
60 class VarOrder {
61   const std::vector<signed char>&    assigns;     // var->val. Pointer to external assignment table.
62   const std::vector<double>&  activity;    // var->act. Pointer to external activity table.
63   Heap<VarOrder_lt>   heap;
64   double              random_seed; // For the internal random number generator
65 
66 public:
VarOrder(const std::vector<signed char> & ass,const std::vector<double> & act)67   VarOrder(const std::vector<signed char>& ass, const std::vector<double>& act) :
68     assigns(ass), activity(act), heap(VarOrder_lt(act)), random_seed(91648253)
69   { }
70 
71     inline void newVar(void);
72     inline void newVar(int varIndex);
73     inline void update(Var x);                  // Called when variable increased in activity.
74     inline void undo(Var x);                    // Called when variable is unassigned and may be selected again.
75     inline Var  select(double random_freq =.0); // Selects a new, unassigned variable (or 'var_Undef' if none exists).
76 };
77 
78 
newVar(void)79 void VarOrder::newVar(void)
80 {
81     heap.setBounds(assigns.size());
82     heap.insert(assigns.size()-1);
83 }
84 
newVar(int varIndex)85 void VarOrder::newVar(int varIndex)
86 {
87     heap.setBounds(assigns.size());
88     heap.insert(varIndex);
89 }
90 
update(Var x)91 void VarOrder::update(Var x)
92 {
93     if (heap.inHeap(x))
94         heap.increase(x);
95 }
96 
undo(Var x)97 void VarOrder::undo(Var x)
98 {
99     if (!heap.inHeap(x))
100         heap.insert(x);
101 }
102 
103 
select(double random_var_freq)104 Var VarOrder::select(double random_var_freq)
105 {
106   // Random decision:
107   /*
108     if (drand(random_seed) < random_var_freq && !heap.empty()){
109     Var next = irand(random_seed,assigns.size());
110 
111     // find var which is not undefined or in the heap
112     while (toLbool(assigns[next]) == l_Undef && !heap.inHeap(next)) {
113     next = irand(random_seed,assigns.size());
114     }
115     if (toLbool(assigns[next]) == l_Undef) {
116     return next;
117     }
118 
119     // cvc does not necessarily use all variable ids without gaps,
120     // so need to check if the picked id is a valid variable.
121     //if (toLbool(assigns[next]) == l_Undef && heap.inHeap(next)) {
122     //    return next;
123     //}
124     }
125   */
126 
127   // Activity based decision:
128   while (!heap.empty()){
129     Var next = heap.getMin();
130     if (toLbool(assigns[next]) == l_Undef)
131       return next;
132   }
133 
134   return var_Undef;
135 }
136 
137 }
138 
139 //=================================================================================================
140 #endif
141