Generated on for Gecode by doxygen 1.17.0
flatzinc.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.dev>
5 *
6 * Copyright:
7 * Guido Tack, 2014
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include "test/flatzinc.hh"
35
36#include <memory>
37
38namespace Test { namespace FlatZinc {
39
40 namespace {
41
43 class TupleSetAutoRepresentation : public Base {
44 public:
45 TupleSetAutoRepresentation(void)
46 : Base("FlatZinc::TupleSet::AutoRepresentation") {}
47
48 virtual bool run(void) {
49 using namespace Gecode;
50
51 const int n = 4096;
52 IntArgs tuples(2*n);
53 for (int i=0; i<n; i++) {
54 tuples[2*i] = i;
55 tuples[2*i+1] = n+i;
56 }
57
59 TupleSet ts = space.arg2tupleset(tuples,2);
60 return ts.finalized() &&
61 (ts.representation() == EPK_DENSE_COMPRESSED) &&
62 (ts.tuples() == n);
63 }
64 };
65
66 TupleSetAutoRepresentation tuple_set_auto_representation;
67
68#ifdef GECODE_HAS_FLOAT_VARS
71 class FloatCutExcludesIncumbent : public Base {
72 private:
75 static bool cutBound(const std::string& source,
76 Gecode::FloatNum& bound) {
79 std::stringstream ss(source);
80 std::unique_ptr<FlatZincSpace>
81 fg(Gecode::FlatZinc::parse(ss, p, olog));
82 if (!fg || (fg->status() == Gecode::SS_FAILED))
83 return false;
84 std::unique_ptr<FlatZincSpace>
85 best(static_cast<FlatZincSpace*>(fg->clone()));
86 Gecode::rel(*best, best->fv[0], Gecode::FRT_EQ, 0.5);
87 if (best->status() == Gecode::SS_FAILED)
88 return false;
89 std::unique_ptr<FlatZincSpace>
90 s(static_cast<FlatZincSpace*>(fg->clone()));
91 s->constrain(*best);
92 if ((s->status() == Gecode::SS_FAILED) || s->fv[0].assigned())
93 return false;
94 bound = (source.find("minimize") != std::string::npos) ?
95 s->fv[0].max() : s->fv[0].min();
96 return true;
97 }
98 public:
99 FloatCutExcludesIncumbent(void)
100 : Base("FlatZinc::Float::CutExcludesIncumbent") {}
101
102 virtual bool run(void) {
103 Gecode::FloatNum minBound, maxBound;
104 return
105 cutBound("var 0.0..1.0: x :: output_var;\nsolve minimize x;\n",
106 minBound) &&
107 cutBound("var 0.0..1.0: x :: output_var;\nsolve maximize x;\n",
108 maxBound) &&
109 (minBound < 0.5) && (maxBound > 0.5);
110 }
111 };
112
113 FloatCutExcludesIncumbent float_cut_excludes_incumbent;
114#endif
115
117 class GistStatisticsMode : public Base {
118 private:
119 static Gecode::ScriptMode mode(std::vector<std::string> args) {
120 Gecode::FlatZinc::FlatZincOptions opt("Gecode/FlatZinc");
121 std::string cmd("fzn-gecode");
122 int argc = static_cast<int>(args.size()) + 1;
123 std::vector<char*> argv(argc);
124 argv[0] = const_cast<char*>(cmd.data());
125 for (int i=1; i<argc; i++)
126 argv[i] = const_cast<char*>(args[i-1].data());
127 opt.parse(argc,argv.data());
128 return opt.mode();
129 }
130 public:
131 GistStatisticsMode(void)
132 : Base("FlatZinc::Options::GistStatisticsMode",
133 TestTags::check()) {}
134
135 virtual bool run(void) {
136 return
137 (mode({"-s"}) == Gecode::SM_STAT) &&
138 (mode({"-mode", "gist", "-s"}) == Gecode::SM_GIST) &&
139 (mode({"-s", "-mode", "gist"}) == Gecode::SM_GIST);
140 }
141 };
142
143 GistStatisticsMode gist_statistics_mode;
144
145#ifndef GECODE_HAS_GIST
147 FlatZincErrorTest gist_unavailable(
149 "Options::GistUnavailable",
150 "var 1..1: x :: output_var;\nsolve satisfy;\n",
151 {"-mode", "gist", "-s"},
152 "Gist mode is unavailable in this build");
153#endif
154
155 }
156
157 FlatZincTest::FlatZincTest(const std::string& name, const std::string& source,
158 const std::string& expected, bool allSolutions,
159 std::vector<std::string> cmdlineOpt,
161 : FlatZincTest(TestTags::standard(), name, source, expected, allSolutions,
162 cmdlineOpt, check, before) {}
163
165 const std::string& source,
166 const std::string& expected, bool allSolutions,
167 std::vector<std::string> cmdlineOpt,
169 : Base("FlatZinc::"+name, tags), _name(name), _source(source),
170 _expected(expected),
171 _allSolutions(allSolutions), _cmdlineOpt(cmdlineOpt),
172 _check(check), _before(before) {}
173
175 const std::string& source,
176 std::vector<std::string> cmdlineOpt,
177 std::string expectedMessage)
178 : FlatZincErrorTest(TestTags::standard(), name, source, cmdlineOpt,
179 expectedMessage) {}
180
182 const std::string& source,
183 std::vector<std::string> cmdlineOpt,
184 std::string expectedMessage)
185 : FlatZincTest(tags, name, source, "", false, cmdlineOpt),
186 _expectedMessage(expectedMessage) {}
187
188 bool
190 using namespace Gecode;
191 Support::Timer t_total;
192 t_total.start();
193 Gecode::FlatZinc::FlatZincOptions fznopt("Gecode/FlatZinc");
194 if (!_cmdlineOpt.empty()) {
195 std::string cmd("fzn-gecode");
196 int argc = static_cast<int>(_cmdlineOpt.size()) + 1;
197 std::vector<char*> argv(argc);
198 argv[0] = const_cast<char*>(cmd.data());
199 for (int i = 1; i < argc; ++i) {
200 argv[i] = const_cast<char*>(_cmdlineOpt[i-1].data());
201 }
202 fznopt.parse(argc, argv.data());
203 }
206 try {
207 if (_before) {
208 _before();
209 }
210 std::stringstream ss(_source);
211 std::unique_ptr<Gecode::FlatZinc::FlatZincSpace> fg(
213
214 if (fg) {
215 fg->createBranchers(p, fg->solveAnnotations(), fznopt,
216 false, olog);
217 fg->shrinkArrays(p);
218 std::ostringstream os;
219 fg->run(os, p, fznopt, t_total);
220
221 const std::string output = os.str();
222 fg.reset();
223 if (_check ? _check(output) : (_expected == output)) {
224 return true;
225 }
226 if (opt.log)
227 olog << "FlatZinc produced the following output:\n" << output << "\n";
228 return false;
229 } else {
230 if (opt.log)
231 olog << "Could not parse input\n";
232 return false;
233 }
234 } catch (Gecode::FlatZinc::Error& e) {
235 if (opt.log)
236 olog << ind(2) << "FlatZinc error : " << e.toString() << std::endl;
237 return false;
238 }
239 return true;
240 }
241
242 bool
244 using namespace Gecode;
245 Support::Timer t_total;
246 t_total.start();
247 Gecode::FlatZinc::FlatZincOptions fznopt("Gecode/FlatZinc");
248 if (!_cmdlineOpt.empty()) {
249 std::string cmd("fzn-gecode");
250 int argc = static_cast<int>(_cmdlineOpt.size()) + 1;
251 std::vector<char*> argv(argc);
252 argv[0] = const_cast<char*>(cmd.data());
253 for (int i = 1; i < argc; ++i) {
254 argv[i] = const_cast<char*>(_cmdlineOpt[i-1].data());
255 }
256 fznopt.parse(argc, argv.data());
257 }
259 std::ostringstream os;
260 try {
261 std::stringstream ss(_source);
262 std::unique_ptr<Gecode::FlatZinc::FlatZincSpace> fg(
264 if (fg) {
265 fg->createBranchers(p, fg->solveAnnotations(), fznopt,
266 false, olog);
267 fg->shrinkArrays(p);
268 fg->run(os, p, fznopt, t_total);
269 }
270 return false;
271 } catch (Gecode::FlatZinc::Error& e) {
272 const std::string message = e.toString();
273 if (opt.log)
274 olog << ind(2) << "Expected FlatZinc error : "
275 << message << std::endl;
276 return (os.str().find("----------") == std::string::npos) &&
277 (_expectedMessage.empty() ||
278 (message.find(_expectedMessage) != std::string::npos));
279 } catch (Gecode::Exception& e) {
280 const std::string message = e.what();
281 if (opt.log)
282 olog << ind(2) << "Expected Gecode exception : "
283 << message << std::endl;
284 return (os.str().find("----------") == std::string::npos) &&
285 (_expectedMessage.empty() ||
286 (message.find(_expectedMessage) != std::string::npos));
287 }
288 return false;
289 }
290
291}}
292
293// STATISTICS: test-flatzinc
Exception: Base-class for exceptions
Definition exception.hpp:42
Exception class for FlatZinc errors
Definition flatzinc.hh:727
const std::string & toString(void) const
Definition flatzinc.hh:735
Options for running FlatZinc models
Definition flatzinc.hh:226
bool allSolutions(void) const
Script mode to run.
Definition flatzinc.hh:344
void parse(int &argc, char *argv[])
Script mode to run.
Definition flatzinc.hh:324
A space that can be initialized with a FlatZinc model.
Definition flatzinc.hh:424
TupleSet arg2tupleset(const IntArgs &a, int noOfVars)
Convert a to TupleSet.
Output support class for FlatZinc interpreter.
Definition flatzinc.hh:107
Passing integer arguments.
Definition int.hh:653
void start(void)
Start timer.
Definition timer.hpp:58
Class representing a set of tuples.
Definition int.hh:2821
int tuples(void) const
Number of tuples.
bool finalized(void) const
Is tuple set successfully finalized.
ExtensionalPropKind representation(void) const
Return materialized tuple-set representation.
Base class for all tests to be run
Definition test.hh:186
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
const std::string & name(void) const
Return name of test.
Definition test.hpp:91
static std::string str(bool b)
Map bool to string.
Definition test.hpp:121
TestTags tags(void) const
Return tags for test.
Definition test.hpp:95
virtual bool run(void)
Perform test.
Definition flatzinc.cpp:243
FlatZincErrorTest(const std::string &name, const std::string &source, std::vector< std::string > cmdlineOpt={}, std::string expectedMessage="")
Construct and register test.
Definition flatzinc.cpp:174
virtual bool run(void)
Perform test.
Definition flatzinc.cpp:189
FlatZincTest(const std::string &name, const std::string &source, const std::string &expected, bool allSolutions=false, std::vector< std::string > cmdlineOpt={}, OutputCheck check=OutputCheck(), BeforeRun before=BeforeRun())
Construct and register test.
Definition flatzinc.cpp:157
std::function< void(void)> BeforeRun
Definition flatzinc.hh:58
std::vector< std::string > _cmdlineOpt
Definition flatzinc.hh:63
std::function< bool(const std::string &)> OutputCheck
Definition flatzinc.hh:57
Set of test tags.
Definition test.hh:105
static TestTags check(void)
Integrity tests, also part of the standard suite.
Definition test.hpp:51
Simple class for describing indentation.
Definition test.hh:82
ScriptMode
Different modes for executing scripts.
Definition driver.hh:97
@ SM_STAT
Print statistics for script.
Definition driver.hh:100
@ SM_GIST
Run script in Gist.
Definition driver.hh:101
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
double FloatNum
Floating point number base type.
Definition float.hh:106
@ FRT_EQ
Equality ( ).
Definition float.hh:1071
@ SS_FAILED
Space is failed
Definition core.hpp:1727
GECODE_FLATZINC_EXPORT FlatZincSpace * parse(const std::string &fileName, Printer &p, std::ostream &err=std::cerr, FlatZincSpace *fzs=nullptr, Rnd &rnd=defrnd)
Parse FlatZinc file fileName into fzs and return it.
Gecode toplevel namespace
Tests for FlatZinc.
Definition flatzinc.cpp:38
General test support.
Definition afc.cpp:41
std::ostringstream olog
Stream used for logging.
Definition test.cpp:54
@ check
Basic integrity tests.
Definition test.hh:99
@ standard
Standard test suite.
Definition test.hh:100
Options opt
The options.
Definition test.cpp:163