1 /******************************************
2 Copyright (c) 2019, Mate Soos
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 ***********************************************/
22 
23 
24 #include "vardistgen.h"
25 #include "solver.h"
26 #include "sqlstats.h"
27 
28 using namespace CMSat;
29 
VarDistGen(Solver * _solver)30 VarDistGen::VarDistGen(Solver* _solver) :
31     solver(_solver)
32 {}
33 
compute_tot_act_vsids(Clause * cl) const34 double VarDistGen::compute_tot_act_vsids(Clause* cl) const
35 {
36     double tot_var_acts = 0.0;
37     for(Lit l: *cl) {
38         tot_var_acts += solver->var_act_vsids[l.var()].combine();
39     }
40     tot_var_acts += 10e-300;
41     //NOTE Kuldeep wants to re-visit
42     tot_var_acts = std::log2(tot_var_acts)/std::log2(solver->max_vsids_act+10e-300);
43     return tot_var_acts;
44 }
45 
calc()46 void VarDistGen::calc()
47 {
48     double myTime = cpuTime();
49     data.clear();
50     data.resize(solver->nVars());
51 
52     for(auto& off: solver->longIrredCls) {
53         Clause* cl = solver->cl_alloc.ptr(off);
54         double tot_var_acts = compute_tot_act_vsids(cl);
55 
56         for(Lit l: *cl) {
57             data[l.var()].irred.num_times_in_long_clause++;
58             data[l.var()].irred.tot_num_lit_of_long_cls_it_appears_in+=cl->size();
59             if (solver->varData[l.var()].polarity ^ !l.sign()) {
60                 data[l.var()].irred.satisfies_cl++;
61             } else {
62                 data[l.var()].irred.falsifies_cl++;
63             }
64             data[l.var()].irred.sum_var_act_of_cls += tot_var_acts;
65         }
66     }
67 
68     for(auto& x: solver->longRedCls) {
69         for(auto& off: x) {
70             Clause* cl = solver->cl_alloc.ptr(off);
71             double tot_var_acts = compute_tot_act_vsids(cl);
72             for(Lit l: *cl) {
73                 data[l.var()].red.num_times_in_long_clause++;
74                 data[l.var()].red.tot_num_lit_of_long_cls_it_appears_in+=cl->size();
75                 if (std::log2(solver->max_cl_act+10e-300) != 0) {
76                     data[l.var()].tot_act_long_red_cls +=
77                         std::log2((double)cl->stats.activity+10e-300)
78                             /std::log2(solver->max_cl_act+10e-300);
79                 }
80 
81                 if (solver->varData[l.var()].polarity ^ !l.sign()) {
82                     data[l.var()].red.satisfies_cl++;
83                 } else {
84                     data[l.var()].red.falsifies_cl++;
85                 }
86                 data[l.var()].red.sum_var_act_of_cls += tot_var_acts;
87             }
88         }
89     }
90 
91     for(uint32_t i = 0; i < solver->nVars()*2; i++) {
92         Lit l = Lit::toLit(i);
93         for(Watched& w: solver->watches[l]) {
94             if (w.isBin() && l < w.lit2()) {
95                 if (w.red()) {
96                     data[l.var()].red.num_times_in_bin_clause++;
97                     data[l.var()].red.tot_num_lit_of_bin_it_appears_in+=2;
98                     if (solver->varData[l.var()].polarity ^ !l.sign()) {
99                         data[l.var()].red.satisfies_cl++;
100                     } else {
101                         data[l.var()].red.falsifies_cl++;
102                     }
103                 } else {
104                     data[l.var()].irred.num_times_in_bin_clause++;
105                     data[l.var()].irred.tot_num_lit_of_bin_it_appears_in+=2;
106                     if (solver->varData[l.var()].polarity ^ !l.sign()) {
107                         data[l.var()].irred.satisfies_cl++;
108                     } else {
109                         data[l.var()].irred.falsifies_cl++;
110                     }
111                 }
112             }
113         }
114     }
115 
116     double time_used = cpuTime() - myTime;
117     if (solver->conf.verbosity) {
118         cout << "c [vardistgen] generated var distribution data "
119         << solver->conf.print_times(time_used)
120         << endl;
121     }
122 
123     if (solver->sqlStats) {
124         solver->sqlStats->time_passed_min(
125             solver
126             , "var-dist-gen"
127             , time_used
128         );
129     }
130 }
131 
132 #ifdef STATS_NEEDED_BRANCH
dump()133 void VarDistGen::dump()
134 {
135     for(uint32_t i = 0; i < solver->nVars(); i++) {
136         uint32_t outer_var = solver->map_inter_to_outer(i);
137         solver->sqlStats->var_dist(
138             outer_var, data[i], solver);
139     }
140 }
141 #endif
142