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
|
#ifndef OPERATOR_HPP
#define OPERATOR_HPP
#include <cassert>
#include <vector>
template<typename Domain>
struct Operator {
virtual ~Operator() { }
virtual Domain eval(const std::vector<Domain>&) const = 0;
virtual void print(std::ostream&) const = 0;
};
template<typename Domain>
struct Maximum : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = -infinity<Domain>();
for (typename std::vector<Domain>::const_iterator it = arguments.begin();
it != arguments.end();
++it) {
result = (result < *it ? *it : result);
}
return result;
}
void print(std::ostream& cout) const {
cout << "max";
}
};
template<typename Domain>
struct Minimum : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = infinity<Domain>();
for (typename std::vector<Domain>::const_iterator it = arguments.begin();
it != arguments.end();
++it) {
result = (*it < result ? *it : result);
}
return result;
}
void print(std::ostream& cout) const {
cout << "min";
}
};
template<typename Domain>
struct Negation : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
assert(arguments.size() == 1);
return -arguments[0];
}
void print(std::ostream& cout) const {
cout << "-";
}
};
template<typename Domain>
struct Addition : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = 0;
for (typename std::vector<Domain>::const_iterator it = arguments.begin();
it != arguments.end();
++it) {
result += *it;
}
return result;
}
void print(std::ostream& cout) const {
cout << "add";
}
};
template<typename Domain>
struct Subtraction : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = 0;
for (typename std::vector<Domain>::const_iterator it = arguments.begin();
it != arguments.end();
++it) {
if (it == arguments.begin())
result = *it;
else
result -= *it;
}
return result;
}
void print(std::ostream& cout) const {
cout << "sub";
}
};
template<typename Domain>
struct Multiplication : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = 1;
for (typename std::vector<Domain>::const_iterator it = arguments.begin(),
end = arguments.end();
it != end;
++it) {
result *= *it;
}
return result;
}
void print(std::ostream& cout) const {
cout << "mult";
}
};
template<typename Domain>
struct Comma : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
if (arguments[0] == -infinity<Domain>()) {
return -infinity<Domain>();
}
return arguments[1];
}
void print(std::ostream& cout) const {
cout << "comma";
}
};
template<typename Domain>
struct Guard : public Operator<Domain> {
virtual Domain eval(const std::vector<Domain>& arguments) const {
Domain result = arguments[2];
if (arguments[0] < arguments[1]) {
result = -infinity<Domain>();
}
return result;
}
void print(std::ostream& cout) const {
cout << "guard";
}
};
#include "MCFSimplex.h"
template<typename Domain>
struct MinCostFlow : public Operator<Domain> {
MinCostFlow(const std::vector<Domain>& supplies, const std::vector<std::pair<int,int> > arcs)
: _supplies(supplies), _arcs(arcs), _solver(0,0) {
MCFClass::FNumber* deficits = new MCFClass::FNumber[supplies.size()];
MCFClass::Index* starts = new MCFClass::Index[arcs.size()];
MCFClass::Index* ends = new MCFClass::Index[arcs.size()];
for (int i = 0, size = supplies.size(); i < size; ++i) {
deficits[i] = -supplies[i].template as<MCFClass::FNumber>();
}
for (int i = 0, size = arcs.size(); i < size; ++i) {
starts[i] = arcs[i].first;
ends[i] = arcs[i].second;
}
_solver.LoadNet(supplies.size(), supplies.size(), // max nodes/arcs
supplies.size(), supplies.size(), // current nodes/arcs
NULL, NULL, // arcs have inf cap, zero cost (to begin)
deficits, // deficits for each node
starts, ends); // start/end of each edge
delete[] deficits;
delete[] starts;
delete[] ends;
}
Domain eval (const std::vector<Domain>& costs) const {
assert(costs.size() == _arcs.size());
if (costs.size() == 0)
return 0;
for (int i = 0, size = costs.size(); i < size; ++i) {
_solver.ChgCost(i, costs[i].template as<MCFClass::CNumber>());
}
_solver.SolveMCF();
if (_solver.MCFGetStatus() == MCFClass::kUnfeasible){
return -infinity<Domain>();
} else if (_solver.MCFGetFO() == MCFClass::Inf<MCFClass::FONumber>()) {
return infinity<Domain>();
} else if (_solver.MCFGetFO() == -MCFClass::Inf<MCFClass::FONumber>()) {
return -infinity<Domain>();
} else {
return _solver.MCFGetFO();
}
}
void print(std::ostream& cout) const {
std::string supplyString = "[";
for (int i = 0, size = _supplies.size(); i < size; ++i) {
if (i > 0)
supplyString += ",";
std::stringstream stream;
stream << _supplies[i];
supplyString += stream.str();
}
supplyString += ']';
std::string arcString = "[";
for (int i = 0, size = _arcs.size(); i < size; ++i) {
if (i > 0)
arcString += ",";
{
std::stringstream stream;
stream << _arcs[i].first;
arcString += stream.str() + ":";
}
{
std::stringstream stream;
stream << _arcs[i].second;
arcString += stream.str();
}
}
arcString += ']';
cout << "MCF<" << supplyString << "," << arcString << ">";
}
private:
std::vector<Domain> _supplies;
std::vector<std::pair<int,int> > _arcs;
mutable MCFSimplex _solver;
};
/*#include "TemplateConstraintMatrix.hpp"
template<typename Domain>
struct MinimumCostFlow : public Operator<Domain> {
MinimumCostFlow() {
}
Domain solve(const Domain& d) const {
}
virtual Domain eval(const std::vector<Domain>& arguments) const {
if (arguments.size() != 1)
throw "Incorrect number of arguments.";
return solve(arguments[0]);
}
void print(std::ostream& cout) const {
cout << "minCostFlow";
}
private:
TemplateConstraintMatrix& constraint; // T
std::vector<Domain> guard; // c
std::vector<std::vector<Domain>> multiplication; //A
unsigned int row;
};*/
template<typename T>
std::ostream& operator<<(std::ostream& cout, const Operator<T>& op) {
op.print(cout);
return cout;
}
#endif
|