summaryrefslogtreecommitdiff
path: root/libs/cassowary/ClTableau.cc
blob: 85ac8417257c32c9b9084b4f5941bdf6e6542d55 (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
// $Id$
//
// Cassowary Incremental Constraint Solver
// Original Smalltalk Implementation by Alan Borning
// This C++ Implementation by Greg J. Badros, <gjb@cs.washington.edu>
// http://www.cs.washington.edu/homes/gjb
// (C) 1998, 1999 Greg J. Badros and Alan Borning
// See ../LICENSE for legal details regarding this software
//
// ClTableau.cc

using namespace std;

#include <cassowary/ClTableau.h>
#include <cassowary/debug.h>

#ifdef HAVE_CONFIG_H
#include <config.h>
#define CONFIG_H_INCLUDED
#endif


// delete the linear expressions
// let ClSimplexSolver worry about deleting the variables
ClTableau::~ClTableau()
{
  ClTableauRowsMap::iterator it = _rows.begin();
  for (; it != _rows.end(); ++it)
    {
    // free the ClLinearExpression that we new-ed 
#ifdef CL_TRACE
    cerr << "Deleting row  delete@ " << ((*it).second) << endl;
#endif
    delete (*it).second;
    }
}

#ifndef CL_NO_IO
// Some extra debugging info
ostream &
ClTableau::PrintInternalInfo(ostream &xo) const
{
  xo << "ncns:" << _rows.size() -1
     << "; cols:" << _columns.size()
     << "; infrows:" << _infeasibleRows.size() 
     << "; ebvars:" << _externalRows.size()
     << "; epvars:" << _externalParametricVars.size();
  return xo;
}


ostream &
ClTableau::printExternalVariablesTo(ostream &xo) const
{
  xo << "Parametric: ";
  ClVarSet::iterator itParVars = _externalParametricVars.begin();
  for ( ; itParVars != _externalParametricVars.end(); ++itParVars ) {
    ClVariable v = *itParVars;
    xo << v << " ";
  }
  xo << "\nBasic: ";
  ClVarSet::iterator itRowVars = _externalRows.begin();
  for ( ; itRowVars != _externalRows.end() ; ++itRowVars ) {
    ClVariable v = *itRowVars;
    xo << v << " ";
  }
  return xo << endl;
}

#endif


// Add v, update column cross indices
// v becomes a basic variable
// expr is now owned by ClTableau class, 
// and ClTableauis responsible for deleting it
// (also, expr better be allocated on the heap!)
void 
ClTableau::addRow(ClVariable var, const ClLinearExpression &expr)
{
#ifdef CL_TRACE
  Tracer TRACER(__FUNCTION__);
  cerr << "(" << var << ", " << expr << ")" << endl;
#endif
  _rows[var] = const_cast<ClLinearExpression *>(&expr);
  ClVarToNumberMap::const_iterator it = expr.Terms().begin();
  // for each variable in expr, Add var to the set of rows which have that variable
  // in their Expression
  for (; it != expr.Terms().end(); ++it)
    {
    ClVariable v = (*it).first;
    _columns[v].insert(var);
    if (v.IsExternal() && !FIsBasicVar(v))
      {
      _externalParametricVars.insert(v);
      }
    }
  if (var.IsExternal())
    {
    _externalRows.insert(var);
    }
#ifdef CL_TRACE
  cerr << *this << endl;
#endif
}

// Remove var from the tableau -- remove the column cross indices for var
// and remove var from every Expression in rows in which v occurs
// Remove the parametric variable var, updating the appropriate column and row entries.
// (Renamed from Smalltalk implementation's `removeParametricVar')
ClVariable
ClTableau::RemoveColumn(ClVariable var)
{
#ifdef CL_TRACE
  Tracer TRACER(__FUNCTION__);
  cerr << "(" << var << ")" << endl;
#endif
  ClTableauColumnsMap::iterator it_var = _columns.find(var);
  if (it_var == _columns.end())
    return var;  // nothing to do

  ClVarSet &varset = (*it_var).second;
  // remove the rows with the variables in varset
  ClVarSet::iterator it = varset.begin();
  for (; it != varset.end(); ++it)
    {
    ClVariable v = (*it);
    ClVarToNumberMap &Terms = _rows[v]->Terms();
    Terms.erase(Terms.find(var));
    }
  if (var.IsExternal())
    {
    _externalRows.erase(var);
    _externalParametricVars.erase(var);
    }
  _columns.erase(it_var);
  return var;
}

// Remove the basic variable v from the tableau row v=expr
// Then update column cross indices
ClLinearExpression *
ClTableau::RemoveRow(ClVariable var)
{
#ifdef CL_TRACE
  Tracer TRACER(__FUNCTION__);
  cerr << "(" << var << ")" << endl;
#endif
  ClTableauRowsMap::iterator it = _rows.find(var);
  assert(it != _rows.end());
  ClLinearExpression *pexpr = (*it).second;
  ClVarToNumberMap &Terms = pexpr->Terms();
  ClVarToNumberMap::iterator it_term = Terms.begin();
  for (; it_term != Terms.end(); ++it_term)
    {
    ClVariable v = (*it_term).first;
    _columns[v].erase(var);
    if (_columns[v].size() == 0)
      {
      _columns.erase(v);
      _externalParametricVars.erase(v);
      }
    }

  _infeasibleRows.erase(var);

  if (var.IsExternal())
    {
    _externalRows.erase(var);
    _externalParametricVars.erase(var);
    }

  _rows.erase(it);
#ifdef CL_TRACE
  cerr << "- returning " << *pexpr << endl;
#endif
  return pexpr;
}

// Replace all occurrences of oldVar with expr, and update column cross indices
// oldVar should now be a basic variable
// Uses the Columns data structure and calls SubstituteOut on each
// row that has oldVar in it
// oldVar is leaving the basis, and becoming parametric
void 
ClTableau::SubstituteOut(ClVariable oldVar, const ClLinearExpression &expr)
{
#ifdef CL_TRACE
  cerr << "* ClTableau::";
  Tracer TRACER(__FUNCTION__);
  cerr << "(" << oldVar << ", " << expr << ")" << endl;
  cerr << (*this) << endl;
#endif

  ClTableauColumnsMap::iterator it_oldVar = _columns.find(oldVar);
  if (it_oldVar == _columns.end())
    return;

  ClVarSet &varset = (*it_oldVar).second;
  ClVarSet::iterator it = varset.begin();
  for (; it != varset.end(); ++it)
    {
    ClVariable v = (*it);
    ClLinearExpression *prow = _rows[v];
    prow->SubstituteOut(oldVar,expr,v,*this);
    if (v.IsRestricted() && prow->Constant() < 0.0)
      {
      _infeasibleRows.insert(v);
      }
    }
  _columns.erase(it_oldVar);
  if (oldVar.IsExternal())
    {
    if (_columns[oldVar].size() > 0) 
      {
      _externalRows.insert(oldVar);
      }
    _externalParametricVars.erase(oldVar);
    }
}


#ifndef CL_NO_IO

ostream &
PrintTo(ostream &xo, const ClVarSet & varset)
{
  ClVarSet::const_iterator it = varset.begin();
  xo << "{ ";
  if (it != varset.end())
    {
    xo << *it;
    ++it;
    }
  for (; it != varset.end(); ++it) 
    {
    xo << ", " << *it;
    }
  xo << " }";
  return xo;
}  

ostream &operator<<(ostream &xo, const ClVarSet & varset)
{ return PrintTo(xo,varset); }

ostream &
PrintTo(ostream &xo, const ClTableauColumnsMap & varmap)
{
  ClTableauColumnsMap::const_iterator it = varmap.begin();
  for (; it != varmap.end(); ++it) 
    {
    xo << (*it).first << " -> " << (*it).second << endl;
    }
  return xo;
}

ostream &operator<<(ostream &xo, const ClTableauColumnsMap & varmap)
{ return PrintTo(xo,varmap); }

ostream &
PrintTo(ostream &xo, const ClTableauRowsMap & rows)
{
  ClTableauRowsMap::const_iterator it = rows.begin();
  for (; it != rows.end(); ++it) 
    {
    ClVariable v = it->first;
    const ClLinearExpression *pe = it->second;
    xo << v << " <-=-> ";
    if (pe) xo << *pe; else xo << "NilExpr";
    xo << endl;
    }
  return xo;
}

ostream &operator<<(ostream &xo, const ClTableauRowsMap & rows)
{ return PrintTo(xo,rows); }

ostream &
ClTableau::PrintOn(ostream &xo) const
{
  xo << "Tableau:\n" 
     << _rows << endl;
  xo << "Columns:\n" 
     << _columns << endl;
  xo << "Infeasible rows: " 
     << _infeasibleRows << endl;
  xo << "External basic variables: "
     << _externalRows << endl;
  xo << "External parametric variables: "
     << _externalParametricVars << endl;
  return xo;
}

ostream &operator<<(ostream &xo, const ClTableau &clt)
{ return clt.PrintOn(xo); }

#endif