summaryrefslogtreecommitdiff
path: root/src/theory/arith/partial_model.h
blob: a0f35f9db27b55307ff8f6bd3b5cba3ea958f5de (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

#include "context/cdlist.h"
#include "context/context.h"
#include "theory/arith/delta_rational.h"
#include "expr/node.h"


#ifndef __CVC4__THEORY__ARITH__PARTIAL_MODEL_H
#define __CVC4__THEORY__ARITH__PARTIAL_MODEL_H

namespace CVC4 {
namespace theory {
namespace arith {

typedef CVC4::context::CDList<TNode> BoundsList;

namespace partial_model {
struct DeltaRationalCleanupStrategy{
  static void cleanup(DeltaRational* dq){
    Debug("arithgc") << "cleaning up  " << dq << "\n";
    delete dq;
  }
};

struct AssignmentAttrID;
typedef expr::Attribute<AssignmentAttrID,DeltaRational*,DeltaRationalCleanupStrategy> Assignment;

// TODO should have a cleanup see bug #110
struct LowerBoundAttrID;
typedef expr::CDAttribute<LowerBoundAttrID,DeltaRational*> LowerBound;

// TODO should have a cleanup see bug #110
struct UpperBoundAttrID;
typedef expr::CDAttribute<UpperBoundAttrID,DeltaRational*> UpperBound;

struct LowerConstraintAttrID;
typedef expr::CDAttribute<LowerConstraintAttrID,TNode> LowerConstraint;

struct UpperConstraintAttrID;
typedef expr::CDAttribute<UpperConstraintAttrID,TNode> UpperConstraint;

typedef CVC4::context::CDList<TNode> BoundsList;

struct BoundsListCleanupStrategy{
  static void cleanup(BoundsList* bl){
    Debug("arithgc") << "cleaning up  " << bl << "\n";
    bl->deleteSelf();
  }
};


/** Unique name to use for constructing ECAttr. */
struct BoundsListID;

/**
 * BoundsListAttr is the attribute that maps a node to atoms .
 */
typedef expr::Attribute<BoundsListID,
                        BoundsList*,
                        BoundsListCleanupStrategy> BoundsListAttr;

}; /*namespace partial_model*/

struct TheoryArithPropagatedID;
typedef expr::CDAttribute<TheoryArithPropagatedID, bool> TheoryArithPropagated;

/**
 * Validates that a node constraint has the following form:
 *   constraint: x |><| c
 * where |><| is either <, <=, ==, >=, LT and c is a constant rational.
 */
bool validateConstraint(TNode constraint){
  using namespace CVC4::kind;
  switch(constraint.getKind()){
  case LT:case LEQ: case EQUAL: case GEQ: case GT: break;
  default: return false;
  }

  if(constraint[0].getMetaKind() != metakind::VARIABLE) return false;
  return constraint[1].getKind() == CONST_RATIONAL;
}

void addBound(TNode constraint){
  Assert(validateConstraint(constraint));
  TNode x = constraint[0];

  BoundsList* bl;
  if(!x.getAttribute(partial_model::BoundsListAttr(), bl)){
    //TODO
    context::Context* context = NULL;
    bl = new (true) BoundsList(context);
    x.setAttribute(partial_model::BoundsListAttr(), bl);
  }
  bl->push_back(constraint);
}

inline int deltaCoeff(Kind k){
  switch(k){
  case kind::LT:
    return -1;
  case kind::GT:
    return 1;
  default:
    return 0;
  }
}


inline bool negateBoundPropogation(CVC4::Kind k, bool isLowerBound){
  /* !isLowerBound
   * [x <= u && u < c] \=> x <  c
   * [x <= u && u < c] \=> x <= c
   * [x <= u && u < c] \=> !(x == c)
   * [x <= u && u < c] \=> !(x >= c)
   * [x <= u && u < c] \=> !(x >  c)
   */
  /* isLowerBound
   * [x >= l && l > c] \=> x > c
   * [x >= l && l > c] \=> x >= c
   * [x >= l && l > c] \=> !(x == c)
   * [x >= l && l > c] \=> !(x <= c)
   * [x >= l && l > c] \=> !(x < c)
   */
  using namespace CVC4::kind;
  switch(k){
  case LT:
  case LEQ:
    return isLowerBound;
  case EQUAL:
    return true;
  case GEQ:
  case GT:
    return !isLowerBound;
  default:
    Unreachable();
    return false;
  }
}

void propogateBound(TNode constraint, OutputChannel& oc, bool isLower){
  constraint.setAttribute(TheoryArithPropagated(),true);
  bool neg = negateBoundPropogation(constraint.getKind(), isLower);

  if(neg){
    oc.propagate(constraint.notNode(),false);
  }else{
    oc.propagate(constraint,false);
  }
}

void propagateBoundConstraints(TNode x, OutputChannel& oc){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  DeltaRational* l;
  DeltaRational* u;
  bool hasLowerBound = x.getAttribute(partial_model::LowerBound(), l);
  bool hasUpperBound = x.getAttribute(partial_model::UpperBound(), u);

  if(!(hasLowerBound || hasUpperBound)) return;
  BoundsList* bl;

  if(!x.getAttribute(partial_model::BoundsListAttr(), bl)) return;

  for(BoundsList::const_iterator iter = bl->begin(); iter != bl->end(); ++iter){
    TNode constraint = *iter;
    if(constraint.hasAttribute(TheoryArithPropagated())){
      continue;
    }
    //TODO improve efficiency Rational&
    Rational c = constraint[1].getConst<Rational>();
    Rational k(Integer(deltaCoeff(constraint.getKind())));
    DeltaRational ec(c, k);
    if(hasUpperBound && (*u) < ec ){
      propogateBound(constraint, oc, false);
    }
    if(hasLowerBound && (*l) > ec ){
      propogateBound(constraint, oc, true);
    }
  }
}

void setUpperBound(TNode x, DeltaRational& r){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);
  DeltaRational* c;
  if(x.getAttribute(partial_model::UpperBound(), c)){
    *c = r;
  }else{
    c = new DeltaRational(r);
    x.setAttribute(partial_model::UpperBound(), c);
  }
}

void setLowerBound(TNode x, DeltaRational& r){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);
  DeltaRational* c;
  if(x.getAttribute(partial_model::LowerBound(), c)){
    *c = r;
  }else{
    c = new DeltaRational(r);
    x.setAttribute(partial_model::LowerBound(), c);
  }
}
void setAssignment(TNode x, DeltaRational& r){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);
  DeltaRational* c;
  if(x.getAttribute(partial_model::Assignment(), c)){
    *c = r;
  }else{
    c = new DeltaRational(r);
    x.setAttribute(partial_model::Assignment(), c);
  }
}

/** Must know that the bound exists both calling this! */
DeltaRational getUpperBound(TNode x){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  DeltaRational* assign;
  AlwaysAssert(x.getAttribute(partial_model::UpperBound(),assign));
  return *assign;
}


DeltaRational getLowerBound(TNode x){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  DeltaRational* assign;
  AlwaysAssert(x.getAttribute(partial_model::LowerBound(),assign));
  return *assign;
}

DeltaRational getAssignment(TNode x){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  DeltaRational* assign;
  AlwaysAssert(x.getAttribute(partial_model::Assignment(),assign));
  return *assign;
}

void setLowerConstraint(TNode constraint){
  TNode x = constraint[0];
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  x.setAttribute(partial_model::LowerConstraint(),constraint);
}

void setUpperConstraint(TNode constraint){
  TNode x = constraint[0];
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  x.setAttribute(partial_model::UpperConstraint(),constraint);
}
TNode getLowerConstraint(TNode x){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  TNode ret;
  AlwaysAssert(x.getAttribute(partial_model::LowerConstraint(),ret));
  return ret;
}

TNode getUpperConstraint(TNode x){
  Assert(x.getMetaKind() == CVC4::kind::metakind::VARIABLE);

  TNode ret;
  AlwaysAssert(x.getAttribute(partial_model::UpperConstraint(),ret));
  return ret;
}

/**
 * x <= l
 * ? c < l
 */
bool belowLowerBound(TNode x, DeltaRational& c, bool strict){
  DeltaRational* l;
  if(!x.getAttribute(partial_model::LowerBound(), l)){
    // l = -\intfy
    // ? c < -\infty |-  _|_
    return false;
  }

  if(strict){
    return c < *l;
  }else{
    return c <= *l;
  }
}

/**
 * x <= u
 * ? c < u
 */
bool aboveUpperBound(TNode x, DeltaRational& c, bool strict){
  DeltaRational* u;
  if(!x.getAttribute(partial_model::UpperBound(), u)){
    // c = \intfy
    // ? c > \infty |-  _|_
    return false;
  }

  if(strict){
    return c > *u;
  }else{
    return c >= *u;
  }
}

bool strictlyBelowUpperBound(TNode x){
  DeltaRational* assign;
  AlwaysAssert(x.getAttribute(partial_model::Assignment(),assign));

  DeltaRational* u;
  if(!x.getAttribute(partial_model::UpperBound(), u)){ // u = \infty
    return true;
  }
  return *assign < *u;
}

bool strictlyAboveLowerBound(TNode x){
  DeltaRational* assign;
  AlwaysAssert(x.getAttribute(partial_model::Assignment(),assign));

  DeltaRational* l;
  if(!x.getAttribute(partial_model::LowerBound(), l)){ // l = -\infty
    return true;
  }
  return *l < *assign;
}

bool assignmentIsConsistent(TNode x){
  DeltaRational beta = getAssignment(x);

  //l_i <= beta(x_i) <= u_i
  return !aboveUpperBound(x,beta, true) && !belowLowerBound(x,beta,true);
}

}; /* namesapce arith */
}; /* namespace theory */
}; /* namespace CVC4 */



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