summaryrefslogtreecommitdiff
path: root/src/theory/arith/tableau.h
blob: 4e4088bb0be4473bbf35b5c36699b78b0f1b7c59 (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
/*********************                                                        */
/*! \file tableau.h
 ** \verbatim
 ** Original author: taking
 ** Major contributors: none
 ** Minor contributors (to current version): none
 ** This file is part of the CVC4 prototype.
 ** Copyright (c) 2009, 2010  The Analysis of Computer Systems Group (ACSys)
 ** Courant Institute of Mathematical Sciences
 ** New York University
 ** See the file COPYING in the top-level source directory for licensing
 ** information.\endverbatim
 **
 ** \brief [[ Add one-line brief description here ]]
 **
 ** [[ Add lengthier description here ]]
 ** \todo document this file
 **/


#include "expr/node.h"
#include "expr/attribute.h"

#include "theory/arith/basic.h"
#include "theory/arith/arith_activity.h"


#include <ext/hash_map>
#include <set>

#ifndef __CVC4__THEORY__ARITH__TABLEAU_H
#define __CVC4__THEORY__ARITH__TABLEAU_H

namespace CVC4 {
namespace theory {
namespace arith {


class Row {
  TNode d_x_i;

  typedef __gnu_cxx::hash_map<TNode, Rational, NodeHashFunction> CoefficientTable;

  std::set<TNode> d_nonbasic;
  CoefficientTable d_coeffs;

public:

  typedef std::set<TNode>::iterator iterator;

  /**
   * Construct a row equal to:
   *   basic = \sum_{x_i} c_i * x_i
   */
  Row(TNode basic, TNode sum):
    d_x_i(basic),
    d_nonbasic(),
    d_coeffs(){

    Assert(d_x_i.getMetaKind() == kind::metakind::VARIABLE);
    Assert(sum.getKind() == kind::PLUS);

    for(TNode::iterator iter=sum.begin(); iter != sum.end(); ++iter){
      TNode pair = *iter;
      Assert(pair.getKind() == kind::MULT);
      Assert(pair.getNumChildren() == 2);
      TNode coeff = pair[0];
      TNode var_i = pair[1];
      Assert(coeff.getKind() == kind::CONST_RATIONAL);
      Assert(var_i.getKind() == kind::VARIABLE);

      Assert(!has(var_i));
      d_nonbasic.insert(var_i);
      d_coeffs[var_i] = coeff.getConst<Rational>();
      Assert(coeff.getConst<Rational>() != Rational(0,1));
      Assert(d_coeffs[var_i] != Rational(0,1));
    }
  }

  iterator begin(){
    return d_nonbasic.begin();
  }

  iterator end(){
    return d_nonbasic.end();
  }

  TNode basicVar(){
    return d_x_i;
  }

  bool has(TNode x_j){
    CoefficientTable::iterator x_jPos = d_coeffs.find(x_j);
    return x_jPos != d_coeffs.end();
  }

  const Rational& lookup(TNode x_j){
    CoefficientTable::iterator x_jPos = d_coeffs.find(x_j);
    Assert(x_jPos !=  d_coeffs.end());
    return (*x_jPos).second;
  }

  void pivot(TNode x_j){
    Rational negInverseA_rs = -(lookup(x_j).inverse());
    d_coeffs[d_x_i] = Rational(Integer(-1));
    d_coeffs.erase(x_j);

    d_nonbasic.erase(x_j);
    d_nonbasic.insert(d_x_i);
    d_x_i = x_j;

    for(iterator nonbasicIter = d_nonbasic.begin();
        nonbasicIter != d_nonbasic.end();
        ++nonbasicIter){
      TNode nb = *nonbasicIter;
      d_coeffs[nb] *= negInverseA_rs;
    }

  }

  void subsitute(Row& row_s){
    TNode x_s = row_s.basicVar();

    Assert(has(x_s));
    Assert(d_nonbasic.find(x_s) != d_nonbasic.end());
    Assert(x_s != d_x_i);

    Rational zero(0,1);

    Rational a_rs = lookup(x_s);
    Assert(a_rs != zero);
    d_coeffs.erase(x_s);
    d_nonbasic.erase(x_s);

    for(iterator iter = row_s.d_nonbasic.begin();
        iter != row_s.d_nonbasic.end();
        ++iter){
      TNode x_j = *iter;
      Rational a_sj = row_s.lookup(x_j);
      a_sj *= a_rs;
      CoefficientTable::iterator coeff_iter = d_coeffs.find(x_j);

      if(coeff_iter != d_coeffs.end()){
        coeff_iter->second += a_sj;
        if(coeff_iter->second == zero){
          d_coeffs.erase(coeff_iter);
          d_nonbasic.erase(x_j);
        }
      }else{
        d_nonbasic.insert(x_j);
        d_coeffs.insert(make_pair(x_j,a_sj));
      }
    }
  }

  void printRow(){
    Debug("tableau") << d_x_i << " ";
    for(iterator nonbasicIter = d_nonbasic.begin();
        nonbasicIter != d_nonbasic.end();
        ++nonbasicIter){
      TNode nb = *nonbasicIter;
      Debug("tableau") << "{" << nb << "," << d_coeffs[nb] << "}";
    }
    Debug("tableau") << std::endl;
  }
};

class Tableau {
public:
  typedef std::set<TNode> VarSet;

private:
  typedef __gnu_cxx::hash_map<TNode, Row*, NodeHashFunction> RowsTable;

  VarSet d_activeBasicVars;
  RowsTable d_activeRows, d_inactiveRows;


public:
  /**
   * Constructs an empty tableau.
   */
  Tableau() : d_activeBasicVars(), d_activeRows(), d_inactiveRows() {}

  VarSet::iterator begin(){
    return d_activeBasicVars.begin();
  }

  VarSet::iterator end(){
    return d_activeBasicVars.end();
  }

  Row* lookup(TNode var){
    Assert(isActiveBasicVariable(var));
    return d_activeRows[var];
  }

private:
  Row* lookupEjected(TNode var){
    Assert(isEjected(var));
    return d_inactiveRows[var];
  }
public:

  void addRow(TNode eq){
    Assert(eq.getKind() == kind::EQUAL);
    Assert(eq.getNumChildren() == 2);

    TNode var = eq[0];
    TNode sum = eq[1];

    Assert(var.getAttribute(IsBasic()));

    //The new basic variable cannot already be a basic variable
    Assert(!isActiveBasicVariable(var));
    d_activeBasicVars.insert(var);
    Row* row_var = new Row(var,sum);
    d_activeRows[var] = row_var;

    //A variable in the row may have been made non-basic already.
    //If this is the case we fake pivoting this variable
    for(TNode::iterator sumIter = sum.begin(); sumIter!=sum.end(); ++sumIter){
      TNode child = *sumIter;
      Assert(child.getKind() == kind::MULT);
      Assert(child.getNumChildren() == 2);
      Assert(child[0].getKind() == kind::CONST_RATIONAL);
      TNode c = child[1];
      Assert(var.getMetaKind() == kind::metakind::VARIABLE);
      if(isActiveBasicVariable(c)){
        Row* row_c = lookup(c);
        row_var->subsitute(*row_c);
      }
    }
  }

  /**
   * preconditions:
   *   x_r is basic,
   *   x_s is non-basic, and
   *   a_rs != 0.
   */
  void pivot(TNode x_r, TNode x_s){
    RowsTable::iterator ptrRow_r = d_activeRows.find(x_r);
    Assert(ptrRow_r != d_activeRows.end());

    Row* row_s = (*ptrRow_r).second;

    Assert(row_s->has(x_s));
    row_s->pivot(x_s);

    d_activeRows.erase(ptrRow_r);
    d_activeBasicVars.erase(x_r);
    makeNonbasic(x_r);

    d_activeRows.insert(std::make_pair(x_s,row_s));
    d_activeBasicVars.insert(x_s);
    makeBasic(x_s);

    for(VarSet::iterator basicIter = begin(), endIter = end();
        basicIter != endIter; ++basicIter){
      TNode basic = *basicIter;
      Row* row_k = lookup(basic);
      if(row_k->basicVar() != x_s){
        if(row_k->has(x_s)){
          increaseActivity(basic, 30);

          row_k->subsitute(*row_s);
        }
      }
    }
  }
  void printTableau(){
    for(VarSet::iterator basicIter = begin(), endIter = end();
        basicIter != endIter; ++basicIter){
      TNode basic = *basicIter;
      Row* row_k = lookup(basic);
      row_k->printRow();
    }
  }

  bool isEjected(TNode var){
    return d_inactiveRows.find(var) != d_inactiveRows.end();
  }

  void ejectBasic(TNode basic){
    Assert(isActiveBasicVariable(basic));

    Row* row = lookup(basic);
    d_activeRows.erase(basic);
    d_activeBasicVars.erase(basic);

    d_inactiveRows.insert(make_pair(basic, row));
  }

  void reinjectBasic(TNode basic){
    Assert(isEjected(basic));

    Row* row = lookupEjected(basic);

    d_inactiveRows.erase(basic);
    d_activeBasicVars.insert(basic);
    d_activeRows.insert(make_pair(basic, row));

    updateRow(row);
  }
private:
  inline bool isActiveBasicVariable(TNode var){
    return d_activeBasicVars.find(var) != d_activeBasicVars.end();
  }

  void updateRow(Row* row){
    for(Row::iterator i=row->begin(), endIter = row->end(); i != endIter; ){
      TNode var = *i;
      ++i;
      if(isBasic(var)){
        Row* row_var = isActiveBasicVariable(var) ? lookup(var) : lookupEjected(var);

        Assert(row_var != row);
        row->subsitute(*row_var);

        i = row->begin();
        endIter = row->end();
      }
    }
  }
};

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

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