1
/**** , [ qsolver_qcop.cc ],
2
Copyright (c) 2010 Universite de Caen Basse Normandie - Jeremie Vautard
4
Permission is hereby granted, free of charge, to any person obtaining a copy
5
of this software and associated documentation files (the "Software"), to deal
6
in the Software without restriction, including without limitation the rights
7
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
copies of the Software, and to permit persons to whom the Software is
9
furnished to do so, subject to the following conditions:
11
The above copyright notice and this permission notice shall be included in
12
all copies or substantial portions of the Software.
14
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
*************************************************************************/
23
#include "qsolver_qcop.hh"
26
QCOP_solver::QCOP_solver(Qcop* sp) {
31
Strategy QCOP_solver::solve(unsigned long int& nodes) {
34
return rSolve(sp,0,plop,nodes);
37
Strategy QCOP_solver::rSolve(Qcop* qs,int scope, vector<int> assignments, unsigned long int& nodes) {
39
// cout<<"rSolve for scope "<<scope<<" with assignments ";
40
// for (int i=0;i<assignments.size();i++) cout<<assignments[i]<<" ";
42
//////////////////////////////////////////////////////////////////////////////////////////
43
// First case : we reached the goal, we just have to check it //
44
//////////////////////////////////////////////////////////////////////////////////////////
45
if (scope == qs->spaces()) {
46
// cout<<"First case"<<endl;
47
MySpace* g = qs->getGoal();
48
if (g == NULL) {/*cout<<"the goal was null"<<endl;*/return Strategy::SFalse();}
49
for (int i=0;i<g->nbVars();i++) {
50
switch (g->type_of_v[i]) {
52
rel(*g,*(static_cast<IntVar*>(g->v[i])) == assignments[i]);
55
rel(*g,*(static_cast<BoolVar*>(g->v[i])) == assignments[i]);
58
cout<<"1Unknown variable type"<<endl;
62
if (g->status() == SS_FAILED) {
63
// cout<<"goal failed after assignments"<<endl;
65
return Strategy::SFalse();
67
// cout<<"goal OK"<<endl;
69
return Strategy(StrategyNode::STrue());
72
/////////////////////////////////////////////////////////////////////////////////////////
73
// Second case : we are in the middle of the problem... //
74
/////////////////////////////////////////////////////////////////////////////////////////
76
// cout<<"second case "<<endl;
77
MySpace* espace = qs->getSpace(scope);
78
if (espace == NULL) cout<<"I caught a NULL for scope "<<scope<<". I will crash..."<<endl;
79
// cout<<"size of assignments "<<assignments.size()<<endl;
80
for (int i=0;i<assignments.size();i++) {
81
// cout<<"I assign variable "<<i<<" with value "<<assignments[i]<<endl; cout.flush();
82
switch (espace->type_of_v[i]) {
84
rel(*espace,*(static_cast<IntVar*>(espace->v[i])) == assignments[i]);
87
rel(*espace,*(static_cast<BoolVar*>(espace->v[i])) == assignments[i]);
90
cout<<"2Unknown variable type"<<endl;
95
// Second case, first subcase : current scope is universal
96
/////////////////////////////////////////////////////////
97
if (qs->quantification(scope)) {
98
// cout<<"universal"<<endl;
100
if (espace->status() == SS_FAILED) {
101
// cout<<"the scope is failed"<<endl;
103
return Strategy::STrue();
106
DFS<MySpace> solutions(espace);
107
MySpace* sol = solutions.next();
109
// cout<<"first sol is null"<<endl;
111
return Strategy::STrue();
114
Strategy retour = Strategy::Dummy();
115
while (sol != NULL) {
116
// cout<<"a solution"<<endl;
118
for (int i = 0; i<sol->nbVars();i++) {
119
switch (sol->type_of_v[i]) {
121
assign.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
124
assign.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
127
cout<<"3Unknown variable type"<<endl;
132
int vmin = ( (scope==0)? 0 : (qs->nbVarInScope(scope-1)) );
133
int vmax = (qs->nbVarInScope(scope))-1;
134
vector<int> zevalues;
135
for (int i = vmin; i<=vmax;i++) {
136
switch (sol->type_of_v[i]) {
138
zevalues.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
141
zevalues.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
144
cout<<"4Unknown variable type"<<endl;
148
Strategy toAttach(true,vmin,vmax,scope,zevalues);
150
Strategy son = rSolve(qs,scope+1,assign,nodes);
152
// cout<<"the son is false"<<endl;
155
return Strategy::SFalse();
157
// cout<<"the son is true"<<endl;
158
toAttach.attach(son);
159
retour.attach(toAttach);
161
sol = solutions.next();
165
} // end of if(universal)
167
// Second case, second subcase : current scope is existential
168
////////////////////////////////////////////////////////////
170
// cout<<"existential"<<endl;
171
if ((espace->status()) == SS_FAILED) {
172
// cout<<"the Espace is failed"<<endl;
174
return Strategy::SFalse();
177
DFS<MySpace> solutions(espace);
178
MySpace* sol =solutions.next();
180
// cout<<"the first sol is null"<<endl;
182
return Strategy::SFalse();
185
OptVar* opt = qs->getOptVar(scope);
186
int opttype = qs->getOptType(scope);
187
Strategy retour(StrategyNode::SFalse());
188
int score= ( (opttype == 1) ? INT_MAX : INT_MIN );
189
while (sol != NULL) {
190
// cout<<"une solution"<<endl;
192
for (int i = 0; i<sol->nbVars();i++) {
193
switch (sol->type_of_v[i]) {
195
assign.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
198
assign.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
201
cout<<"5Unknown variable type"<<endl;
206
int vmin = ( (scope==0)?0 : qs->nbVarInScope(scope-1) );
207
int vmax = qs->nbVarInScope(scope)-1;
208
vector<int> zevalues;
209
for (int i = vmin; i<=vmax;i++) {
210
switch (sol->type_of_v[i]) {
212
zevalues.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
215
zevalues.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
218
cout<<"6unknown Variable type"<<endl;
222
Strategy candidate(false,vmin,vmax,scope,zevalues);
224
Strategy son_of_candidate = rSolve(qs,scope+1,assign,nodes);
225
if (son_of_candidate.isFalse()) candidate = Strategy::SFalse();
226
else candidate.attach(son_of_candidate);
228
int score_of_candidate;
229
if (candidate.isFalse()) score_of_candidate = ( (opttype == 1) ? INT_MAX : INT_MIN );
230
else score_of_candidate = opt->getVal(candidate);
231
// cout<<"score of candidate is "<<score_of_candidate<<endl;
232
// cout<<"Opt type is";
235
// cout<<"ANY"<<endl;
236
if (!candidate.isFalse()) {
243
// cout<<"MIN"<<endl;
244
if (score_of_candidate < score) {
246
score=score_of_candidate;
250
// cout<<"MAX"<<endl;
251
if (score_of_candidate > score) {
253
score=score_of_candidate;
257
cout<<"Unknown opt type : "<<opttype<<endl;
262
sol=solutions.next();
266
} // end if..else (existential)
267
}// end "Second case"