summaryrefslogtreecommitdiff
path: root/impl/FixpointAlgorithm.hpp
blob: bb08e7269bf13b702de39902eae3d9524d9965ee (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
#ifndef FIXPOINT_ALGORITHM_HPP
#define FIXPOINT_ALGORITHM_HPP

#include "IdSet.hpp"
#include "IdMap.hpp"
#include "VariableAssignment.hpp"
#include "EquationSystem.hpp"
#include "MaxStrategy.hpp"

template<typename Domain>
struct FixpointAlgorithm {
  virtual ~FixpointAlgorithm() { }
  virtual void maxFixpoint(
      const EquationSystem<Domain>& system,
      const MaxStrategy<Domain>& strat,
      StableVariableAssignment<Domain>& rho,
      IdSet<Variable<Domain> >& changedIn,
      IdSet<Variable<Domain> >& changedOut
  ) = 0;
};

template<typename Domain>
struct NaiveFixpointAlgorithm : public FixpointAlgorithm<Domain> {
  NaiveFixpointAlgorithm(const EquationSystem<Domain>& system)
    : _system(system) { }
  virtual void maxFixpoint(
      const EquationSystem<Domain>& system,
      const MaxStrategy<Domain>& strat,
      StableVariableAssignment<Domain>& rhoOut,
      IdSet<Variable<Domain> >&,
      IdSet<Variable<Domain> >&
  ) {
    VariableAssignment<Domain>* rho = NULL;
    VariableAssignment<Domain>* result = system.assignment(infinity<Domain>());
    do {
      delete rho;
      rho = result;
      result = _system.eval(*rho, strat);
    } while (!_system.equalAssignments(*rho, *result));
    delete rho;

    for (unsigned int i = 0, length = system.variableCount(); i < length; ++i) {
      Variable<Domain> var = system.variable(i);
      rhoOut[var] = (*result)[var];
    }
  }
  private:
  const EquationSystem<Domain>& _system;
};

template<typename Domain>
struct SmartFixpointAlgorithm : public FixpointAlgorithm<Domain> {
  SmartFixpointAlgorithm(const EquationSystem<Domain>& system)
    : _system(system),
      _influence(system.variableCount(), IdSet<Variable<Domain> >(system.variableCount())){
  }

  struct DynamicVariableAssignment : public VariableAssignment<Domain> {
    DynamicVariableAssignment(
      const EquationSystem<Domain>& system,
      const MaxStrategy<Domain>& strat,
      IdMap<Variable<Domain>,IdSet<Variable<Domain> > >& influence,
      StableVariableAssignment<Domain>& rho,
      IdSet<Variable<Domain> >& stable,
      IdSet<Variable<Domain> >& changed
    ) : _system(system),
        _strategy(strat),
        _values(rho),
        _stable(stable),
        _changed(changed),
        _influence(influence) { }

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

    private:

    void solve(const Variable<Domain>& x) const {
      if (!_stable.contains(x)) {
        _stable.insert(x);

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

        if (val != _values[x]) {
          IdSet<Variable<Domain> > oldInfluence = _influence[x];
          _influence[x].clear();
          _values[x] = val;
          _changed.insert(x);

          _stable.filter(oldInfluence);

          for (typename IdSet<Variable<Domain> >::iterator it = oldInfluence.begin();
               it != oldInfluence.end();
               ++it) {
            solve(_system.variable(*it));
          }
          _influence[x] = oldInfluence;
        }
      }
    }

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

    const EquationSystem<Domain>& _system;
    const MaxStrategy<Domain>& _strategy;
    StableVariableAssignment<Domain>& _values;
    IdSet<Variable<Domain> >& _stable;
    IdSet<Variable<Domain> >& _changed;
    IdMap<Variable<Domain>,IdSet<Variable<Domain> > >& _influence;
  };

  void invalidate(const IdSet<Variable<Domain> >& set, IdSet<Variable<Domain> >& changed, StableVariableAssignment<Domain>& rho) const {
    for (typename IdSet<Variable<Domain> >::iterator it = set.begin(),
                                                     end = set.end();
         it != end;
         ++it) {
      Variable<Domain>& var = _system.variable(*it);
      if (!changed.contains(var)) {
        rho[var] = infinity<Domain>();
        changed.insert(var);
        invalidate(_influence[var], changed, rho);
      }
    }
  }

  virtual void maxFixpoint(
      const EquationSystem<Domain>& system,
      const MaxStrategy<Domain>& strat,
      StableVariableAssignment<Domain>& rhoOut,
      IdSet<Variable<Domain> >& changedIn,
      IdSet<Variable<Domain> >& changedOut
  ) {
    for (typename IdSet<Variable<Domain> >::iterator it = changedIn.begin(),
                                                     end = changedIn.end();
         it != end;
         ++it) {
      //std::cout << *it << " ";
      Variable<Domain>& var = _system.variable(*it);
      rhoOut[var] = infinity<Domain>();
      invalidate(_influence[var], changedIn, rhoOut);
    }

    changedIn.invert();
    DynamicVariableAssignment rho(system, strat, _influence, rhoOut, changedIn, changedOut);
    for (unsigned int i = 0, length = system.variableCount(); i < length; ++i) {
      rho[system.variable(i)];
    }
    /*std::cout << _influence << std::endl;
    std::cout << std::endl;*/
  }
  private:
  const EquationSystem<Domain>& _system;
  IdMap<Variable<Domain>,IdSet<Variable<Domain> > > _influence;
};

#endif