1/**************************************************************************************[VarOrder.h]
2MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18**************************************************************************************************/
19
20#ifndef VarOrder_h
21#define VarOrder_h
22
23#include "SolverTypes.h"
24#include "Heap.h"
25
26
27//=================================================================================================
28
29
30struct VarOrder_lt {
31    const vec<double>&  activity;
32    bool operator () (Var x, Var y) { return activity[x] > activity[y]; }
33    VarOrder_lt(const vec<double>&  act) : activity(act) { }
34};
35
36class VarOrder {
37    const vec<char>&    assigns;     // var->val. Pointer to external assignment table.
38    const vec<double>&  activity;    // var->act. Pointer to external activity table.
39    Heap<VarOrder_lt>   heap;
40    double              random_seed; // For the internal random number generator
41
42public:
43    VarOrder(const vec<char>& ass, const vec<double>& act) :
44        assigns(ass), activity(act), heap(VarOrder_lt(act)), random_seed(91648253)
45        { }
46
47    inline void newVar(void);
48    inline void update(Var x);                  // Called when variable increased in activity.
49    inline void undo(Var x);                    // Called when variable is unassigned and may be selected again.
50    inline Var  select(double random_freq =.0); // Selects a new, unassigned variable (or 'var_Undef' if none exists).
51};
52
53
54void VarOrder::newVar(void)
55{
56    heap.setBounds(assigns.size());
57    heap.insert(assigns.size()-1);
58}
59
60
61void VarOrder::update(Var x)
62{
63    if (heap.inHeap(x))
64        heap.increase(x);
65}
66
67
68void VarOrder::undo(Var x)
69{
70    if (!heap.inHeap(x))
71        heap.insert(x);
72}
73
74
75Var VarOrder::select(double random_var_freq)
76{
77    // Random decision:
78    if (drand(random_seed) < random_var_freq && !heap.empty()){
79        Var next = irand(random_seed,assigns.size());
80        if (toLbool(assigns[next]) == l_Undef)
81            return next;
82    }
83
84    // Activity based decision:
85    while (!heap.empty()){
86        Var next = heap.getmin();
87        if (toLbool(assigns[next]) == l_Undef)
88            return next;
89    }
90
91    return var_Undef;
92}
93
94
95//=================================================================================================
96#endif
97