this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
5 *
6 * Copyright:
7 * Vincent Barichard, 2013
8 *
9 * Last modified:
10 * $Date$ by $Author$
11 * $Revision$
12 *
13 * This file is part of Quacode:
14 * http://quacode.barichard.com
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining
17 * a copy of this software and associated documentation files (the
18 * "Software"), to deal in the Software without restriction, including
19 * without limitation the rights to use, copy, modify, merge, publish,
20 * distribute, sublicense, and/or sell copies of the Software, and to
21 * permit persons to whom the Software is furnished to do so, subject to
22 * the following conditions:
23 *
24 * The above copyright notice and this permission notice shall be
25 * included in all copies or substantial portions of the Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36
37#include <iostream>
38#include <vector>
39
40#include <quacode/qspaceinfo.hh>
41#include <gecode/minimodel.hh>
42#include <gecode/driver.hh>
43
44using namespace Gecode;
45
46#ifdef GECODE_HAS_GIST
47namespace Gecode { namespace Driver {
48 /// Specialization for QDFS
49 template<typename S>
50 class GistEngine<QDFS<S> > {
51 public:
52 static void explore(S* root, const Gist::Options& opt) {
53 (void) Gist::explore(root, false, opt);
54 }
55 };
56}}
57#endif
58
59
60/**
61 * \brief Options taking one additional parameter
62 */
63class RndQCSPOptions : public Options {
64 public:
65 /// Print strategy or not
66 Gecode::Driver::BoolOption _printStrategy;
67 /// File name of bench
68 Gecode::Driver::StringValueOption _file;
69 /// Initialize options for example with name \a s
70 RndQCSPOptions(const char* s)
71 : Options(s),
72 _printStrategy("-printStrategy","Print strategy",false),
73 _file("-file","File name of benchmark file")
74 {
75 add(_printStrategy);
76 add(_file);
77 }
78 /// Return true if the strategy must be printed
79 bool printStrategy(void) const {
80 return _printStrategy.value();
81 }
82 /// Return file name
83 const char *file(void) const {
84 return _file.value();
85 }
86};
87
88class RndQCSP : public Script, public QSpaceInfo {
89 IntVarArray X;
90
91 public:
92 RndQCSP(const RndQCSPOptions& opt) : Script(opt), QSpaceInfo()
93 {
94 std::cout << "Loading problem" << std::endl;
95 using namespace Int;
96
97 if (!opt.printStrategy()) strategyMethod(0); // disable build and print strategy
98 if (!opt.file()) {
99 throw Gecode::Exception("rndQCSP","Unable to open file");
100 }
101
102 IntVarArgs vaX;
103 int nbVars=0;
104
105 static const int READ_DESC = 0;
106 static const int READ_VAR = 1;
107 static const int READ_CST = 2;
108 std::ifstream f(opt.file());
109 std::string line;
110 if (f.good()) {
111 int state = READ_DESC;
112 while ( !f.eof() ) {
113 getline(f,line);
114 if ( line.empty() ) break;
115 std::istringstream iLine(line);
116 int val;
117 switch (state) {
118 case READ_DESC:
119 iLine >> nbVars; // Read number of variables
120 iLine >> val; // Read number of existential variables
121 iLine >> val; // Read number of universal variables
122 state = READ_VAR;
123 break;
124 case READ_VAR:
125 {
126 char quant;
127 int domMin, domMax;
128 iLine >> val; // Read idx variable
129 iLine >> quant; // Read quantifier
130 iLine >> domMin; // Read minimal value of domain
131 iLine >> domMax; // Read maximal value of domain
132 IntVar v(*this,domMin,domMax);
133 if (quant == 'F') setForAll(*this,v);
134 vaX << v;
135 nbVars--;
136 if (nbVars == 0) {
137 X = IntVarArray(*this, vaX);
138 state = READ_CST;
139 }
140 }
141 break;
142 case READ_CST:
143 {
144 int idx, coeff;
145 bool firstElt=true;
146 LinIntExpr liExp;
147 while (iLine >> coeff >> idx) {
148 if (firstElt)
149 liExp = LinIntExpr(X[idx],coeff);
150 else
151 liExp = liExp + LinIntExpr(X[idx],coeff);
152 firstElt=false;
153 }
154 rel(*this, liExp == 0);
155 }
156 break;
157 }
158 }
159 }
160
161 f.close();
162
163 //branch(*this, X, INT_VAR_NONE(), INT_VALUES_MIN());
164 branch(*this, X, INT_VAR_NONE(), INT_VAL_MIN());
165 }
166
167 RndQCSP(bool share, RndQCSP& p) : Script(share,p), QSpaceInfo(*this,share,p)
168 {
169 X.update(*this,share,p.X);
170 }
171
172 virtual Space* copy(bool share) { return new RndQCSP(share,*this); }
173
174
175 void print(std::ostream& os) const {
176 strategyPrint(os);
177 }
178};
179
180int main(int argc, char* argv[])
181{
182
183 RndQCSPOptions opt("RndQCSP Problem");
184 opt.parse(argc,argv);
185 Script::run<RndQCSP,QDFS,RndQCSPOptions>(opt);
186
187 return 0;
188}