summaryrefslogtreecommitdiff
path: root/impl/MaxStrategy.hpp
blob: 71ea4f435ce168695dd8977179f6b4f0261ec187 (about) (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
#ifndef MAX_EXPRESSION_HPP
#define MAX_EXPRESSION_HPP

#include <ostream>
#include "Expression.hpp"
#include "EquationSystem.hpp"
#include "IdSet.hpp"

template<typename Domain>
struct MaxStrategy {
  virtual ~MaxStrategy() { }
  virtual unsigned int get(const MaxExpression<Domain>& e) {
    return const_cast<const MaxStrategy<Domain>*>(this)->get(e);
  }
  virtual unsigned int get(const MaxExpression<Domain>& e) const = 0;
};

unsigned int stack_depth = 1;

std::string indent() {
  std::string result = "";
  for (unsigned int i = 0; i < stack_depth; ++i) {
    result += '\t';
  }
  return result;
}

#include "VariableAssignment.hpp"

template<typename Domain>
struct DynamicVariableAssignment;

template<typename Domain>
struct DynamicMaxStrategy : public MaxStrategy<Domain> {
  DynamicMaxStrategy(
    const EquationSystem<Domain>& system
  ) : _system(system),
      _rho(NULL),
      _values(system.maxExpressionCount(), 0),
      _stable(system.maxExpressionCount()),
      _influence(system.maxExpressionCount(),
                 IdSet<MaxExpression<Domain> >(system.maxExpressionCount())),
      _changed(false)
  {}

  void setRho(DynamicVariableAssignment<Domain>& rho) {
    _rho = &rho;
  }

  void hasChanged(bool c) {
    _changed = c;
  }

  bool hasChanged() const {
    return _changed;
  }

  unsigned int get(const MaxExpression<Domain>& e) const {
    log::strategy << indent() << "Look up " << e << std::endl;
    return _values[e];
  }

  unsigned int get(const MaxExpression<Domain>& e) {
    log::strategy << indent() << "Solve for " << e << std::endl;
    solve(e);
    return _values[e];
  }

  void invalidate(const Variable<Domain>& v) {
    log::strategy << indent() << "Invalidating " << v << " - " << *_system[v] << std::endl; 

    IdSet<MaxExpression<Domain> > infl = _influence[*_system[v]];
    for (typename IdSet<MaxExpression<Domain> >::iterator
           it = infl.begin(),
           end = infl.end();
         it != end;
         ++it) {
      invalidate(_system.maxExpression(*it));
    }
  }

  void invalidate(const MaxExpression<Domain>& v) {
    if (_stable.contains(v)) {
      log::strategy << indent() << "Invalidating " << v << std::endl; 
      //log::debug << indent() << "  influence sets " << _influence << std::endl;
      _stable.remove(v);
      
      IdSet<MaxExpression<Domain> > infl = _influence[v];
      for (typename IdSet<MaxExpression<Domain> >::iterator
             it = infl.begin(),
             end = infl.end();
           it != end;
           ++it) {
        invalidate(_system.maxExpression(*it));
      }
    }
  }

private:
  void solve(const MaxExpression<Domain>& x) {
    if (!_stable.contains(x)) {
      _stable.insert(x);
      log::strategy << indent() << "Stabilise " << x << std::endl;

      stack_depth++; 
      DependencyAssignment assignment(*this, *_rho, x);
      DependencyStrategy depStrat(*this, x);
      unsigned int val = x.bestStrategy(assignment, depStrat);
      stack_depth--;

      if (val != _values[x]) {
        log::strategy << x << " => " << *x.arguments()[val] << std::endl;

        IdSet<MaxExpression<Domain> > oldInfluence = _influence[x];
        //_influence[x].clear();
        _values[x] = val;
        _changed = true;

        _rho->invalidate(*_system.varFromExpr(x));
        
	//_rho->stabilise();

        _stable.filter(oldInfluence);

        for (typename IdSet<MaxExpression<Domain> >::iterator
               it = oldInfluence.begin(),
               end = oldInfluence.end();
             it != end;
             ++it) {
          solve(_system.maxExpression(*it));
        }
      } else {
        log::strategy << indent() << x << " did not change: "
		      << x << " => " << *x.arguments()[val] << std::endl;
      }
    } else {
      log::strategy << indent() << x << " is stable: "
		    << x << " => " << *x.arguments()[_values[x]] << std::endl;
    }
  }

  struct DependencyAssignment : public VariableAssignment<Domain>{
    DependencyAssignment(DynamicMaxStrategy& strat,
                         VariableAssignment<Domain>& rho,
                         const MaxExpression<Domain>& expr)
      : _strat(strat),
        _rho(rho),
        _expr(expr),
	_current(strat._system.variableCount()) {
    }
    const Domain operator[](const Variable<Domain>& var) {
      // solve the strategy for this variable, too
      _strat.solve(*_strat._system[var]);
      _strat._influence[*_strat._system[var]].insert(_expr);
      return _rho[var];
    }
  private:
    DynamicMaxStrategy& _strat;
    VariableAssignment<Domain>& _rho;
    const MaxExpression<Domain>& _expr;
    IdSet<Variable<Domain> > _current;
  };

  struct DependencyStrategy : public MaxStrategy<Domain> {
    DependencyStrategy(DynamicMaxStrategy& strat, const MaxExpression<Domain>& expr)
      : _strat(strat),
        _expr(expr) {
    }
    unsigned int get(const MaxExpression<Domain>& e) const {
      _strat._influence[e].insert(_expr);
      return _strat._values[e];
    }
    unsigned int get(const MaxExpression<Domain>& e) {
      _strat.solve(e);
      _strat._influence[e].insert(_expr);
      return _strat._values[e];
    }
  private:
    DynamicMaxStrategy& _strat;
    const MaxExpression<Domain>& _expr;
  };

private: 
  const EquationSystem<Domain>& _system;
  DynamicVariableAssignment<Domain>* _rho;
  IdMap<MaxExpression<Domain>,unsigned int> _values;
  IdSet<MaxExpression<Domain> > _stable;
  IdMap<MaxExpression<Domain>,IdSet<MaxExpression<Domain> > > _influence;
  bool _changed;
};


template<typename Domain>
struct Solver {
  Solver(const EquationSystem<Domain>& system)
    : _system(system),
      _strategy(system),
      _rho(_system, _strategy, -infinity<Domain>()) {
    _strategy.setRho(_rho);
  }

  Domain solve(Variable<Domain>& var) {
    MaxExpression<Domain>& rhs = *_system[var];

    // _rho automatically keeps up to date with changes made to the
    // strategy, so you don't need to stabilise it

    _strategy.get(rhs);


    /*
    do {
      _strategy.hasChanged(false);
      
      log::debug << "Stabilise assignment (toplevel)" << std::endl;
      _rho.stabilise();
      
      log::debug << "Improve strategy (toplevel)" << std::endl;
      // improve strategy
      _strategy.get(rhs);
      log::debug << (_strategy.hasChanged() ? "Strategy has changed - loop again" : "Strategy has not changed - terminate") << std::endl;
    } while (_strategy.hasChanged());
    */

    return _rho[var];
  }
private:
  const EquationSystem<Domain>& _system;
  DynamicMaxStrategy<Domain> _strategy;
  DynamicVariableAssignment<Domain> _rho;
};


/*template<typename Domain>
std::ostream& operator<<(std::ostream& cout, const MaxStrategy<Domain>& strat) {
  strat.print(cout);
  return cout;
}*/

#endif