flatzinc.cpp
9.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 2007
*
* Last modified:
* $Date: 2010-07-02 19:18:43 +1000 (Fri, 02 Jul 2010) $ by $Author: tack $
* $Revision: 11149 $
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "flatzinc.hh"
#include "registry.hh"
#include <vector>
#include <string>
using namespace std;
namespace FlatZinc {
FlatZincModel::FlatZincModel(void)
: intVarCount(-1), boolVarCount(-1), setVarCount(-1), _optVar(-1),
_solveAnnotations(NULL) {}
void
FlatZincModel::init(int intVars, int boolVars, int setVars) {
intVarCount = 0;
iv = std::vector<IntVar>(intVars);
iv_introduced = std::vector<bool>(intVars);
iv_boolalias = std::vector<int>(intVars);
boolVarCount = 0;
bv = std::vector<BoolVar>(boolVars);
bv_introduced = std::vector<bool>(boolVars);
setVarCount = 0;
sv = std::vector<SetVar>(setVars);
sv_introduced = std::vector<bool>(setVars);
}
void
FlatZincModel::newIntVar(IntVarSpec* vs) {
if (vs->alias) {
iv[intVarCount++] = iv[vs->i];
} else {
std::cerr << "create new IntVar " << intVarCount << "\n";
/// TODO: create actual integer variable from vs
iv[intVarCount++] = IntVar();
}
iv_introduced[intVarCount-1] = vs->introduced;
iv_boolalias[intVarCount-1] = -1;
}
void
FlatZincModel::aliasBool2Int(int iv, int bv) {
iv_boolalias[iv] = bv;
}
int
FlatZincModel::aliasBool2Int(int iv) {
return iv_boolalias[iv];
}
void
FlatZincModel::newBoolVar(BoolVarSpec* vs) {
if (vs->alias) {
bv[boolVarCount++] = bv[vs->i];
} else {
std::cerr << "create new BoolVar " << boolVarCount << "\n";
/// TODO: create actual Boolean variable from vs
bv[boolVarCount++] = BoolVar();
}
bv_introduced[boolVarCount-1] = vs->introduced;
}
void
FlatZincModel::newSetVar(SetVarSpec* vs) {
if (vs->alias) {
sv[setVarCount++] = sv[vs->i];
} else {
std::cerr << "create new SetVar " << setVarCount << "\n";
/// TODO: create actual set variable from vs
sv[setVarCount++] = SetVar();
}
sv_introduced[setVarCount-1] = vs->introduced;
}
void
FlatZincModel::postConstraint(const ConExpr& ce, AST::Node* ann) {
try {
registry().post(*this, ce, ann);
} catch (AST::TypeError& e) {
throw FlatZinc::Error("Type error", e.what());
}
}
void flattenAnnotations(AST::Array* ann, std::vector<AST::Node*>& out) {
for (unsigned int i=0; i<ann->a.size(); i++) {
if (ann->a[i]->isCall("seq_search")) {
AST::Call* c = ann->a[i]->getCall();
if (c->args->isArray())
flattenAnnotations(c->args->getArray(), out);
else
out.push_back(c->args);
} else {
out.push_back(ann->a[i]);
}
}
}
void
FlatZincModel::createBranchers(AST::Node* ann, bool ignoreUnknown,
std::ostream& err) {
if (ann) {
std::vector<AST::Node*> flatAnn;
if (ann->isArray()) {
flattenAnnotations(ann->getArray() , flatAnn);
} else {
flatAnn.push_back(ann);
}
for (unsigned int i=0; i<flatAnn.size(); i++) {
try {
AST::Call *call = flatAnn[i]->getCall("int_search");
AST::Array *args = call->getArgs(4);
AST::Array *vars = args->a[0]->getArray();
std::cerr << "int_search\n";
// TODO: install search
} catch (AST::TypeError& e) {
(void) e;
try {
AST::Call *call = flatAnn[i]->getCall("bool_search");
AST::Array *args = call->getArgs(4);
AST::Array *vars = args->a[0]->getArray();
std::cerr << "bool_search\n";
// TODO: install search
} catch (AST::TypeError& e) {
(void) e;
try {
AST::Call *call = flatAnn[i]->getCall("set_search");
AST::Array *args = call->getArgs(4);
AST::Array *vars = args->a[0]->getArray();
std::cerr << "set_search\n";
// TODO: install search
} catch (AST::TypeError& e) {
(void) e;
if (!ignoreUnknown) {
err << "Warning, ignored search annotation: ";
flatAnn[i]->print(err);
err << std::endl;
}
}
}
}
}
}
// TODO: install search for all remaining variables
}
AST::Array*
FlatZincModel::solveAnnotations(void) const {
return _solveAnnotations;
}
void
FlatZincModel::solve(AST::Array* ann) {
_method = SAT;
_solveAnnotations = ann;
}
void
FlatZincModel::minimize(int var, AST::Array* ann) {
_method = MIN;
_optVar = var;
_solveAnnotations = ann;
// Branch on optimization variable to ensure that it is given a value.
AST::Array* args = new AST::Array(4);
args->a[0] = new AST::Array(new AST::IntVar(_optVar));
args->a[1] = new AST::Atom("input_order");
args->a[2] = new AST::Atom("indomain_min");
args->a[3] = new AST::Atom("complete");
AST::Call* c = new AST::Call("int_search", args);
if (!ann)
ann = new AST::Array(c);
else
ann->a.push_back(c);
}
void
FlatZincModel::maximize(int var, AST::Array* ann) {
_method = MAX;
_optVar = var;
_solveAnnotations = ann;
// Branch on optimization variable to ensure that it is given a value.
AST::Array* args = new AST::Array(4);
args->a[0] = new AST::Array(new AST::IntVar(_optVar));
args->a[1] = new AST::Atom("input_order");
args->a[2] = new AST::Atom("indomain_min");
args->a[3] = new AST::Atom("complete");
AST::Call* c = new AST::Call("int_search", args);
if (!ann)
ann = new AST::Array(c);
else
ann->a.push_back(c);
}
FlatZincModel::~FlatZincModel(void) {
delete _solveAnnotations;
}
void
FlatZincModel::run(std::ostream& out, const Printer& p) {
switch (_method) {
case MIN:
case MAX:
std::cerr << "start optimization search\n";
// TODO: perform actual search
break;
case SAT:
std::cerr << "start satisfiability search\n";
// TODO: perform actual search
break;
}
}
FlatZincModel::Meth
FlatZincModel::method(void) const {
return _method;
}
int
FlatZincModel::optVar(void) const {
return _optVar;
}
void
FlatZincModel::print(std::ostream& out, const Printer& p) const {
p.print(out, *this);
}
void
Printer::init(AST::Array* output) {
_output = output;
}
void
Printer::printElem(std::ostream& out, AST::Node* ai,
const FlatZincModel& m) const {
int k;
if (ai->isInt(k)) {
out << k;
} else if (ai->isIntVar()) {
// TODO: output actual variable
out << ai->getIntVar();
} else if (ai->isBoolVar()) {
// TODO: output actual variable
out << ai->getBoolVar();
} else if (ai->isSetVar()) {
// TODO: output actual variable
out << ai->getSetVar();
} else if (ai->isBool()) {
out << (ai->getBool() ? "true" : "false");
} else if (ai->isSet()) {
AST::SetLit* s = ai->getSet();
if (s->interval) {
out << s->min << ".." << s->max;
} else {
out << "{";
for (unsigned int i=0; i<s->s.size(); i++) {
out << s->s[i] << (i < s->s.size()-1 ? ", " : "}");
}
}
} else if (ai->isString()) {
std::string s = ai->getString();
for (unsigned int i=0; i<s.size(); i++) {
if (s[i] == '\\' && i<s.size()-1) {
switch (s[i+1]) {
case 'n': out << "\n"; break;
case '\\': out << "\\"; break;
case 't': out << "\t"; break;
default: out << "\\" << s[i+1];
}
i++;
} else {
out << s[i];
}
}
}
}
void
Printer::print(std::ostream& out, const FlatZincModel& m) const {
if (_output == NULL)
return;
for (unsigned int i=0; i< _output->a.size(); i++) {
AST::Node* ai = _output->a[i];
if (ai->isArray()) {
AST::Array* aia = ai->getArray();
int size = aia->a.size();
out << "[";
for (int j=0; j<size; j++) {
printElem(out,aia->a[j],m);
if (j<size-1)
out << ", ";
}
out << "]";
} else {
printElem(out,ai,m);
}
}
}
Printer::~Printer(void) {
delete _output;
}
}
// STATISTICS: flatzinc-any