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
|
#ifndef IMPROVEMENT_OPERATOR_HPP
#define IMPROVEMENT_OPERATOR_HPP
#include "IdSet.hpp"
#include "MaxStrategy.hpp"
template<typename Domain>
struct ImprovementOperator {
virtual ~ImprovementOperator() { }
virtual bool improve(
EquationSystem<Domain>& system,
ConcreteMaxStrategy<Domain>& strat,
StableVariableAssignment<Domain>& rho,
IdSet<Variable<Domain> >& changedIn,
IdSet<Variable<Domain> >& changedOut
) const = 0;
};
template<typename Domain>
struct NaiveImprovementOperator : public ImprovementOperator<Domain> {
bool improve(
EquationSystem<Domain>& system,
ConcreteMaxStrategy<Domain>& strat,
StableVariableAssignment<Domain>& rho,
IdSet<Variable<Domain> >&,
IdSet<Variable<Domain> >&
) const {
bool changed = false;
for (unsigned int i = 0, length = system.maxExpressionCount();
i < length;
++i) {
MaxExpression<Domain>& expr = system.maxExpression(i);
// this relies on the fact that an expression will only be proessed after the expressions
// it depends on (which should always be true, as they form a DAG)
unsigned int index = expr.bestStrategy(rho, strat);
if (index != strat.get(expr)) {
changed = true;
strat.set(expr, index);
}
}
log::strategy << strat << std::endl;
return changed;
}
};
template<typename Domain>
struct RepeatedImprovementOperator : public ImprovementOperator<Domain> {
RepeatedImprovementOperator(const ImprovementOperator<Domain>& op)
: _subImprovement(op) { }
bool improve(
EquationSystem<Domain>& system,
ConcreteMaxStrategy<Domain>& strat,
StableVariableAssignment<Domain>& rho,
IdSet<Variable<Domain> >& changedIn,
IdSet<Variable<Domain> >& changedOut
) const {
if (_subImprovement.improve(system, strat, rho, changedIn, changedOut)) {
StableVariableAssignment<Domain>* rho2 = system.eval(rho, strat);
improve(system, strat, *rho2, changedIn, changedOut);
delete rho2;
return true;
}
return false;
}
private:
const ImprovementOperator<Domain>& _subImprovement;
};
<<<<<<< HEAD
#include "VariableAssignment.hpp"
template<typename Domain>
struct DynamicMaxStrategy : public MaxStrategy<Domain> {
DynamicMaxStrategy(
const EquationSystem<Domain>& system
) : _system(system),
_rho(NULL),
_values(system),
_stable(system.maxExpressionCount()),
_influence(system.maxExpressionCount(),
IdSet<MaxExpression<Domain>>(system.maxExpressionCount())),
_var_influence(system.variableCount(),
IdSet<MaxExpression<Domain>>(system.maxExpressionCount()))
{}
void setRho(DynamicVariableAssignment<Domain>& rho) {
_rho = ρ
}
unsigned int get(const MaxExpression<Domain>& e) const {
solve(e);
return _values.get(e);
}
void invalidate(const Variable<Domain>& v) const {
log::strategy << "invalidating " << v << " - " << *_system[v] << std::endl;
log::strategy << "\t" << _var_influence[v] << std::endl;
_stable.filter(_var_influence[v]);
for (auto it = _var_influence[v].begin();
it != _var_influence[v].end();
++it) {
solve(_system.maxExpression(*it));
}
}
struct DependencyAssignment : public VariableAssignment<Domain>{
DependencyAssignment(const DynamicMaxStrategy& strat,
VariableAssignment<Domain>& rho,
const MaxExpression<Domain>& expr)
: _strat(strat),
_rho(rho),
_expr(expr) {
}
const Domain& operator[](const Variable<Domain>& var) const {
_strat._var_influence[var].insert(_expr);
return _rho[var];
}
private:
const DynamicMaxStrategy& _strat;
VariableAssignment<Domain>& _rho;
const MaxExpression<Domain>& _expr;
};
struct DependencyStrategy : public MaxStrategy<Domain> {
DependencyStrategy(const DynamicMaxStrategy& strat, const MaxExpression<Domain>& expr)
: _strat(strat),
_expr(expr) {
}
unsigned int get(const MaxExpression<Domain>& e) const {
_strat.solve(e);
if (&_expr != &e) {
_strat._influence[e].insert(_expr);
}
return _strat._values.get(e);
}
private:
const DynamicMaxStrategy& _strat;
const MaxExpression<Domain>& _expr;
};
private:
const EquationSystem<Domain>& _system;
mutable DynamicVariableAssignment<Domain>* _rho;
mutable ConcreteMaxStrategy<Domain> _values;
mutable IdSet<MaxExpression<Domain>> _stable;
mutable IdMap<MaxExpression<Domain>,IdSet<MaxExpression<Domain>>> _influence;
mutable IdMap<Variable<Domain>,IdSet<MaxExpression<Domain>>> _var_influence;
};
=======
>>>>>>> bitbucket/master
#endif
|