summaryrefslogtreecommitdiff
path: root/src/decision/relevancy.h
blob: 115ef2e4948bc6a8c1a6da042e088ee8c12acbd7 (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*********************                                                        */
/*! \file relevancy.h
 ** \verbatim
 ** Original author: Kshitij Bansal <kshitij@cs.nyu.edu>
 ** Major contributors: none
 ** Minor contributors (to current version): Tim King <taking@cs.nyu.edu>, Dejan Jovanović <dejan@cs.nyu.edu>, Morgan Deters <mdeters@cs.nyu.edu>
 ** This file is part of the CVC4 project.
 ** Copyright (c) 2009-2013  New York University and The University of Iowa
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief Justification heuristic for decision making
 **
 ** A ATGP-inspired justification-based decision heuristic. See
 ** [insert reference] for more details. This code is, or not, based
 ** on the CVC3 implementation of the same heuristic.
 **
 ** It needs access to the simplified but non-clausal formula.
 **
 ** Relevancy
 ** ---------
 **
 ** This class also currently computes the may-relevancy of a node
 **
 ** A node is may-relevant if there is a path from the root of the
 ** formula to the node such that no node on the path is justified. As
 ** contrapositive, a node is not relevant (with the may-notion) if all
 ** path from the root to the node go through a justified node.
 **/

#include "cvc4_private.h"

#ifndef __CVC4__DECISION__RELEVANCY
#define __CVC4__DECISION__RELEVANCY

#include "decision_engine.h"
#include "decision_strategy.h"
#include "decision/options.h"

#include "context/cdhashset.h"
#include "context/cdhashmap.h"
#include "expr/node.h"
#include "prop/sat_solver_types.h"

namespace CVC4 {

namespace decision {

class RelGiveUpException : public Exception {
public:
  RelGiveUpException() : 
    Exception("relevancy: giving up") {
  }
};/* class GiveUpException */

class Relevancy : public RelevancyStrategy {
  typedef std::vector<TNode> IteList;
  typedef hash_map<TNode,IteList,TNodeHashFunction> IteCache;
  typedef hash_map<TNode,TNode,TNodeHashFunction> SkolemMap;
  typedef hash_map<TNode,SatValue,TNodeHashFunction> PolarityCache;

  // being 'justified' is monotonic with respect to decisions
  context::CDHashSet<Node, NodeHashFunction> d_justified;
  context::CDO<unsigned>  d_prvsIndex;

  IntStat d_helfulness;
  IntStat d_polqueries;
  IntStat d_polhelp;
  IntStat d_giveup;
  IntStat d_expense;          /* Total number of nodes considered over
                                 all calls to the findSplitter function */
  TimerStat d_timestat;

  /**
   * A copy of the assertions that need to be justified
   * directly. Doesn't have ones introduced during during ITE-removal.
   */
  std::vector<TNode> d_assertions;   
  //TNode is fine since decisionEngine has them too

  /** map from ite-rewrite skolem to a boolean-ite assertion */
  SkolemMap d_iteAssertions;
  // 'key' being TNode is fine since if a skolem didn't exist anywhere,
  // we won't look it up. as for 'value', same reason as d_assertions

  /** Cache for ITE skolems present in a atomic formula */
  IteCache d_iteCache;

  /**
   * This is used to prevent infinite loop when trying to find a
   * splitter. Can happen when exploring assertion corresponding to a
   * term-ITE.
   */
  hash_set<TNode,TNodeHashFunction> d_visited;

  /** 
   * May-relevancy of a node is an integer. For a node n, it becomes
   * irrelevant when d_maxRelevancy[n] = d_relevancy[n]
   */
  hash_map<TNode,int,TNodeHashFunction> d_maxRelevancy;
  context::CDHashMap<TNode,int,TNodeHashFunction> d_relevancy;
  PolarityCache d_polarityCache;

  /**  **** * * *** * * OPTIONS *** *  * ** * * **** */

  /**
   * do we do "multiple-backtrace" to try to justify a node
   */
  bool d_multipleBacktrace;
  //bool d_computeRelevancy;     // are we in a mode where we compute relevancy?

  static const double secondsPerDecision;
  static const double secondsPerExpense;
  static const double EPS;

  /** Maximum time this algorithm should spent as part of whole algorithm */
  double d_maxTimeAsPercentageOfTotalTime;

  /** current decision for the recursive call */
  SatLiteral* d_curDecision;
public:
  Relevancy(CVC4::DecisionEngine* de, context::Context *c):
    RelevancyStrategy(de, c),
    d_justified(c),
    d_prvsIndex(c, 0),
    d_helfulness("decision::rel::preventedDecisions", 0),
    d_polqueries("decision::rel::polarityQueries", 0),
    d_polhelp("decision::rel::polarityHelp", 0),
    d_giveup("decision::rel::giveup", 0),
    d_expense("decision::rel::expense", 0),
    d_timestat("decision::rel::time"),
    d_relevancy(c),
    d_multipleBacktrace(options::decisionComputeRelevancy()),
    //    d_computeRelevancy(decOpt.computeRelevancy),
    d_maxTimeAsPercentageOfTotalTime(options::decisionMaxRelTimeAsPermille()*1.0/10.0),
    d_curDecision(NULL)
  {
    Warning() << "There are known bugs in this Relevancy code which we know"
              << "how to fix (but haven't yet)." << std::endl
              << "Please bug kshitij if you wish to use." << std::endl;

    StatisticsRegistry::registerStat(&d_helfulness);
    StatisticsRegistry::registerStat(&d_polqueries);
    StatisticsRegistry::registerStat(&d_polhelp);
    StatisticsRegistry::registerStat(&d_giveup);
    StatisticsRegistry::registerStat(&d_expense);
    StatisticsRegistry::registerStat(&d_timestat);
    Trace("decision") << "relevancy enabled with" << std::endl;
    Trace("decision") << "  * computeRelevancy: " << (options::decisionComputeRelevancy() ? "on" : "off")
                      << std::endl;
    Trace("decision") << "  * relevancyLeaves: " << (options::decisionRelevancyLeaves() ? "on" : "off")
                      << std::endl;
    Trace("decision") << "  * mustRelevancy [unimplemented]: " << (options::decisionMustRelevancy() ? "on" : "off")
                      << std::endl;
  }
  ~Relevancy() {
    StatisticsRegistry::unregisterStat(&d_helfulness);
    StatisticsRegistry::unregisterStat(&d_polqueries);
    StatisticsRegistry::unregisterStat(&d_polhelp);
    StatisticsRegistry::unregisterStat(&d_giveup);
    StatisticsRegistry::unregisterStat(&d_expense);
    StatisticsRegistry::unregisterStat(&d_timestat);
  }
  prop::SatLiteral getNext(bool &stopSearch) {
    Debug("decision") << std::endl;
    Trace("decision") << "Relevancy::getNext()" << std::endl;
    TimerStat::CodeTimer codeTimer(d_timestat);

    if( d_maxTimeAsPercentageOfTotalTime < 100.0 - EPS &&
        double(d_polqueries.getData())*secondsPerDecision < 
          d_maxTimeAsPercentageOfTotalTime*double(d_expense.getData())*secondsPerExpense
      ) {
      ++d_giveup;
      return undefSatLiteral;
    }

    d_visited.clear();
    d_polarityCache.clear();

    for(unsigned i = d_prvsIndex; i < d_assertions.size(); ++i) {
      Trace("decision") << d_assertions[i] << std::endl;

      // Sanity check: if it was false, aren't we inconsistent?
      Assert( tryGetSatValue(d_assertions[i]) != SAT_VALUE_FALSE);

      SatValue desiredVal = SAT_VALUE_TRUE;
      SatLiteral litDecision = findSplitter(d_assertions[i], desiredVal);

      if(!options::decisionComputeRelevancy() && litDecision != undefSatLiteral) {
        d_prvsIndex = i;
        return litDecision;
      }
    }

    if(options::decisionComputeRelevancy()) return undefSatLiteral;

    Trace("decision") << "jh: Nothing to split on " << std::endl;

#if defined CVC4_ASSERTIONS || defined CVC4_DEBUG
    bool alljustified = true;
    for(unsigned i = 0 ; i < d_assertions.size() && alljustified ; ++i) {
      TNode curass = d_assertions[i];
      while(curass.getKind() == kind::NOT)
        curass = curass[0];
      alljustified &= checkJustified(curass);

      if(Debug.isOn("decision")) {
	if(!checkJustified(curass))
	  Debug("decision") << "****** Not justified [i="<<i<<"]: " 
                            << d_assertions[i] << std::endl;
      }
    }
    Assert(alljustified);
#endif

    // SAT solver can stop...
    stopSearch = true;
    d_decisionEngine->setResult(SAT_VALUE_TRUE);
    return prop::undefSatLiteral;
  }

  void addAssertions(const std::vector<Node> &assertions,
                     unsigned assertionsEnd,
                     IteSkolemMap iteSkolemMap) {
    Trace("decision")
      << "Relevancy::addAssertions()" 
      << " size = " << assertions.size()
      << " assertionsEnd = " << assertionsEnd
      << std::endl;

    // Save mapping between ite skolems and ite assertions
    for(IteSkolemMap::iterator i = iteSkolemMap.begin();
        i != iteSkolemMap.end(); ++i) {
      Assert(i->second >= assertionsEnd && i->second < assertions.size());
      Debug("jh-ite") << " jh-ite: " << (i->first) << " maps to "
                      << assertions[(i->second)] << std::endl;
      d_iteAssertions[i->first] = assertions[i->second];
    }

    // Save the 'real' assertions locally
    for(unsigned i = 0; i < assertionsEnd; ++i) {
      d_assertions.push_back(assertions[i]);
      d_visited.clear();
      if(options::decisionComputeRelevancy()) increaseMaxRelevancy(assertions[i]);
      d_visited.clear();
    }

  }

  bool isRelevant(TNode n) {
    Trace("decision") << "isRelevant(" << n << "): ";

    if(Debug.isOn("decision")) {
      if(d_maxRelevancy.find(n) == d_maxRelevancy.end()) {
        // we are becuase of not getting information about literals
        // created using newLiteral command... need to figure how to
        // handle that
        Message() << "isRelevant: WARNING: didn't find node when we should had" << std::endl;
      }      
    }

    if(d_maxRelevancy.find(n) == d_maxRelevancy.end()) {
      Trace("decision") << "yes [not found in db]" << std::endl;
      return true;
    }
    
    if(options::decisionRelevancyLeaves() && !isAtomicFormula(n)) {
      Trace("decision") << "no [not a leaf]" << std::endl;
      return false;
    }

    Trace("decision") << (d_maxRelevancy[n] == d_relevancy[n] ? "no":"yes")
                      << std::endl;

    Debug("decision") << " maxRel: " << (d_maxRelevancy[n] )
                      << " rel: " << d_relevancy[n].get() << std::endl;
    // Assert(d_maxRelevancy.find(n) != d_maxRelevancy.end());
    Assert(0 <= d_relevancy[n] && d_relevancy[n] <= d_maxRelevancy[n]);

    if(d_maxRelevancy[n] == d_relevancy[n]) {
      ++d_helfulness; // preventedDecisions
      return false;
    } else {
      return true;
    }
  }

  SatValue getPolarity(TNode n) {
    Trace("decision") << "getPolarity([" << n.getId() << "]" << n << "): ";
    Assert(n.getKind() != kind::NOT);
    ++d_polqueries;
    PolarityCache::iterator it = d_polarityCache.find(n);
    if(it == d_polarityCache.end()) {
      Trace("decision") << "don't know" << std::endl;
      return SAT_VALUE_UNKNOWN;
    } else {
      ++d_polhelp;
      Trace("decision") << it->second << std::endl;
      return it->second;
    }
  }

  /* Compute justified */
  /*bool computeJustified() {
    
  }*/
private:
  SatLiteral findSplitter(TNode node, SatValue desiredVal)
  {
    bool ret;
    SatLiteral litDecision;
    try {
      // init
      d_curDecision = &litDecision;
      
      // solve
      ret = findSplitterRec(node, desiredVal);

      // post
      d_curDecision = NULL;
    }catch(RelGiveUpException &e) {
      return prop::undefSatLiteral;
    }
    if(ret == true) {
      Trace("decision") << "Yippee!!" << std::endl;
      return litDecision;
    } else {
      return undefSatLiteral;
    }
  }
  
  /** 
   * Do all the hardwork. 
   * Return 'true' if the node cannot be justfied, 'false' it it can be.
   * Sets 'd_curDecision' if unable to justify (depending on the mode)
   * If 'd_computeRelevancy' is on does multiple backtrace
   */ 
  bool findSplitterRec(TNode node, SatValue value);
  // Functions to make findSplitterRec modular...
  bool handleOrFalse(TNode node, SatValue desiredVal);
  bool handleOrTrue(TNode node, SatValue desiredVal);
  bool handleITE(TNode node, SatValue desiredVal);

  /* Helper functions */
  void setJustified(TNode);
  bool checkJustified(TNode);
  
  /* If literal exists corresponding to the node return
     that. Otherwise an UNKNOWN */
  SatValue tryGetSatValue(Node n);

  /* Get list of all term-ITEs for the atomic formula v */
  const Relevancy::IteList& getITEs(TNode n);

  /* Compute all term-ITEs in a node recursively */
  void computeITEs(TNode n, IteList &l);

  /* Checks whether something is an atomic formula or not */
  bool isAtomicFormula(TNode n) {
    return theory::kindToTheoryId(n.getKind()) != theory::THEORY_BOOL;
  }
  
  /** Recursively increase relevancies */
  void updateRelevancy(TNode n) {
    if(d_visited.find(n) != d_visited.end()) return;

    Assert(d_relevancy[n] <= d_maxRelevancy[n]);

    if(d_relevancy[n] != d_maxRelevancy[n])
      return;

    d_visited.insert(n);
    if(isAtomicFormula(n)) {
      const IteList& l = getITEs(n);
      for(unsigned i = 0; i < l.size(); ++i) {
        if(d_visited.find(l[i]) == d_visited.end()) continue;
        d_relevancy[l[i]] = d_relevancy[l[i]] + 1;
        updateRelevancy(l[i]);
      }
    } else {
      for(unsigned i = 0; i < n.getNumChildren(); ++i) {
        if(d_visited.find(n[i]) == d_visited.end()) continue;
        d_relevancy[n[i]] = d_relevancy[n[i]] + 1;
        updateRelevancy(n[i]);
      }
    }
    d_visited.erase(n);
  }

  /* */
  void increaseMaxRelevancy(TNode n) {

    Debug("decision") 
      << "increaseMaxRelevancy([" << n.getId() << "]" << n << ")" 
      << std::endl;    

    d_maxRelevancy[n]++;

    // don't update children multiple times
    if(d_visited.find(n) != d_visited.end()) return;
    
    d_visited.insert(n);
    // Part to make the recursive call
    if(isAtomicFormula(n)) {
      const IteList& l = getITEs(n);
      for(unsigned i = 0; i < l.size(); ++i)
        if(d_visited.find(l[i]) == d_visited.end())
          increaseMaxRelevancy(l[i]);
    } else {
      for(unsigned i = 0; i < n.getNumChildren(); ++i)
        increaseMaxRelevancy(n[i]);
    } //else (...atomic formula...)
  }

};/* class Relevancy */

}/* namespace decision */

}/* namespace CVC4 */

#endif /* __CVC4__DECISION__RELEVANCY */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback