summaryrefslogtreecommitdiff
path: root/impl/VariableAssignment.hpp
blob: e074f6fd537687f5dd5bc684e5dcfdb49e8f273d (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
#ifndef VARIABLE_ASSIGNMENT_HPP
#define VARIABLE_ASSIGNMENT_HPP

#include "Expression.hpp"
#include "IdMap.hpp"

template<typename Domain>
struct VariableAssignment {
  virtual ~VariableAssignment() { }
  virtual const Domain operator[](const Variable<Domain>& x) = 0;
};

#include "EquationSystem.hpp"

template<typename Domain>
struct DynamicMaxStrategy;

template<typename Domain>
struct DynamicVariableAssignment : public VariableAssignment<Domain> {
  DynamicVariableAssignment(
    const EquationSystem<Domain>& system,
    DynamicMaxStrategy<Domain>& strat,
    const Domain& value=infinity<Domain>()
  ) : _system(system),
      _strategy(strat),
      _values(system.variableCount(), value),
      _unstable(system.variableCount()),
      _influence(system.variableCount(),
                 IdSet<Variable<Domain> >(system.variableCount())),
      _frozen(false),
      _changed(false)
  {
  }

  void freeze() {
    _frozen = true;
  }

  void thaw() {
    _frozen = false;
  }

  bool is_frozen() const {
    return _frozen;
  }

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

  bool has_changed() const {
    return _changed;
  }

  const Domain operator[](const Variable<Domain>& var) {
    if (!_frozen)
      solve(var);
    return _values[var];
  }

  void stabilise() {
    if (!_unstable.empty()) {
      Variable<Domain>& var = _system.variable(*_unstable.begin());
      solve(var);
    }
  }

  void invalidate(const Variable<Domain>& x) {
    log::fixpoint << indent() << "Invalidating " << x << std::endl;
    if (!_unstable.contains(x)) {
      _unstable.insert(x);
      _values[x] = infinity<Domain>();
      _changed = true;
     
      IdSet<Variable<Domain> > infl = _influence[x];
      _influence[x].clear();
      for (typename IdSet<Variable<Domain> >::iterator
	     it = infl.begin(),
	     ei = infl.end();
	   it != ei;
	   ++it) {
	invalidate(_system.variable(*it));
      }
    }
  }

private:

  void solve(const Variable<Domain>& x) {
    if (_unstable.contains(x)) {
      _unstable.remove(x);
      log::fixpoint << indent() << "Stabilise " << x << std::endl;

      stack_depth++;
      DependencyAssignment assignment(*this, x);
      Domain val = _system[x]->eval(assignment, _strategy);
      stack_depth--;

      if (val != _values[x]) {
        log::fixpoint << x << " = " << val << std::endl;

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

        _strategy.invalidate(x);

        _unstable.absorb(oldInfluence);

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

  struct DependencyAssignment : public VariableAssignment<Domain> {
    DependencyAssignment(DynamicVariableAssignment& assignment, const Variable<Domain>& var)
      : _assignment(assignment), _var(var) { }
    const Domain operator[](const Variable<Domain>& x) {
      const Domain result = _assignment[x];
      _assignment._influence[x].insert(_var);
      return result;
    }
  private:
    DynamicVariableAssignment& _assignment;
    const Variable<Domain>& _var;
  };

  const EquationSystem<Domain>& _system;
  DynamicMaxStrategy<Domain>& _strategy;
  IdMap<Variable<Domain>, Domain> _values;
  IdSet<Variable<Domain> > _unstable;
  IdMap<Variable<Domain>,IdSet<Variable<Domain> > > _influence;
  bool _frozen;
  bool _changed;
};

#endif