File: qsolver_qcop.cc

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (268 lines) | stat: -rw-r--r-- 11,404 bytes parent folder | download | duplicates (4)
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
/****   , [ qsolver_qcop.cc ],
Copyright (c) 2010 Universite de Caen Basse Normandie - Jeremie Vautard

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 "qsolver_qcop.hh"
#include <climits>

QCOP_solver::QCOP_solver(Qcop* sp) {
    this->sp = sp;
    nbRanges=new int;
}

Strategy QCOP_solver::solve(unsigned long int& nodes) {
    vector<int> plop;
    plop.clear();
    return rSolve(sp,0,plop,nodes);
}

Strategy QCOP_solver::rSolve(Qcop* qs,int scope, vector<int> assignments, unsigned long int& nodes) {
    nodes++;
//    cout<<"rSolve for scope "<<scope<<" with assignments ";
//    for (int i=0;i<assignments.size();i++) cout<<assignments[i]<<" ";
//    cout<<endl;
    //////////////////////////////////////////////////////////////////////////////////////////
    // First case : we reached the goal, we just have to check it                           //
    //////////////////////////////////////////////////////////////////////////////////////////
    if (scope == qs->spaces()) {
//        cout<<"First case"<<endl;
        MySpace* g = qs->getGoal();
        if (g == NULL) {/*cout<<"the goal was null"<<endl;*/return Strategy::SFalse();}
        for (int i=0;i<g->nbVars();i++) {
            switch (g->type_of_v[i]) {
                case VTYPE_INT :
                    rel(*g,*(static_cast<IntVar*>(g->v[i])) == assignments[i]);
                    break;
                case VTYPE_BOOL :
                    rel(*g,*(static_cast<BoolVar*>(g->v[i])) == assignments[i]);
                    break;
                default :
                    cout<<"1Unknown variable type"<<endl;
                    abort();
            }
        }
        if (g->status() == SS_FAILED) {
//            cout<<"goal failed after assignments"<<endl;
            delete g;
            return Strategy::SFalse();
        }
//        cout<<"goal OK"<<endl;
        delete g;
        return Strategy(StrategyNode::STrue());
    }

    /////////////////////////////////////////////////////////////////////////////////////////
    // Second case : we are in the middle of the problem...                                //
    /////////////////////////////////////////////////////////////////////////////////////////
    else {
//        cout<<"second case "<<endl;
        MySpace* espace = qs->getSpace(scope);
        if (espace == NULL) cout<<"I caught a NULL for scope "<<scope<<". I will crash..."<<endl;
//        cout<<"size of assignments "<<assignments.size()<<endl;
        for (int i=0;i<assignments.size();i++) {
//            cout<<"I assign variable "<<i<<" with value "<<assignments[i]<<endl; cout.flush();
            switch (espace->type_of_v[i]) {
                case VTYPE_INT :
                    rel(*espace,*(static_cast<IntVar*>(espace->v[i])) == assignments[i]);
                    break;
                case VTYPE_BOOL :
                    rel(*espace,*(static_cast<BoolVar*>(espace->v[i])) == assignments[i]);
                    break;
                default :
                    cout<<"2Unknown variable type"<<endl;
                    abort();
            }
        }

        // Second case, first subcase : current scope is universal
        /////////////////////////////////////////////////////////
        if (qs->quantification(scope)) {
//            cout<<"universal"<<endl;

            if (espace->status() == SS_FAILED) {
//                cout<<"the scope is failed"<<endl;
                delete espace;
                return Strategy::STrue();
            }

            DFS<MySpace> solutions(espace);
            MySpace* sol = solutions.next();
            if (sol == NULL) {
//                cout<<"first sol is null"<<endl;
                delete espace;
                return Strategy::STrue();
            }

            Strategy retour = Strategy::Dummy();
            while (sol != NULL) {
//                cout<<"a solution"<<endl;
                vector<int> assign;
                for (int i = 0; i<sol->nbVars();i++) {
                    switch (sol->type_of_v[i]) {
                        case VTYPE_INT :
                            assign.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
                            break;
                        case VTYPE_BOOL :
                            assign.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
                            break;
                        default :
                            cout<<"3Unknown variable type"<<endl;
                            abort();
                    }
                }

                int vmin = ( (scope==0)? 0 : (qs->nbVarInScope(scope-1)) );
                int vmax = (qs->nbVarInScope(scope))-1;
                vector<int> zevalues;
                for (int i = vmin; i<=vmax;i++) {
                    switch (sol->type_of_v[i]) {
                        case VTYPE_INT :
                            zevalues.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
                            break;
                        case VTYPE_BOOL :
                            zevalues.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
                            break;
                        default :
                            cout<<"4Unknown variable type"<<endl;
                            abort();
                    }
                }
                Strategy toAttach(true,vmin,vmax,scope,zevalues);

                Strategy son = rSolve(qs,scope+1,assign,nodes);
                if (son.isFalse()) {
//                    cout<<"the son is false"<<endl;
                    delete sol;
                    delete espace;
                    return Strategy::SFalse();
                }
//                cout<<"the son is true"<<endl;
                toAttach.attach(son);
                retour.attach(toAttach);
                delete sol;
                sol = solutions.next();
            } // end of while
            delete espace;
            return retour;
        } // end of if(universal)

        // Second case, second subcase : current scope is existential
        ////////////////////////////////////////////////////////////
        else {
//            cout<<"existential"<<endl;
            if ((espace->status()) == SS_FAILED) {
//                cout<<"the Espace is failed"<<endl;
                delete espace;
                return Strategy::SFalse();
            }

            DFS<MySpace> solutions(espace);
            MySpace* sol =solutions.next();
            if (sol == NULL) {
//                cout<<"the first sol is null"<<endl;
                delete espace;
                return Strategy::SFalse();
            }

            OptVar* opt = qs->getOptVar(scope);
            int opttype = qs->getOptType(scope);
            Strategy retour(StrategyNode::SFalse());
            int score= ( (opttype == 1) ? INT_MAX : INT_MIN );
            while (sol != NULL) {
//                cout<<"une solution"<<endl;
                vector<int> assign;
                for (int i = 0; i<sol->nbVars();i++) {
                    switch (sol->type_of_v[i]) {
                        case VTYPE_INT :
                            assign.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
                            break;
                        case VTYPE_BOOL :
                            assign.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
                            break;
                        default :
                            cout<<"5Unknown variable type"<<endl;
                            abort();
                    }
                } // end for

                int vmin = ( (scope==0)?0 : qs->nbVarInScope(scope-1) );
                int vmax = qs->nbVarInScope(scope)-1;
                vector<int> zevalues;
                for (int i = vmin; i<=vmax;i++) {
                    switch (sol->type_of_v[i]) {
                        case VTYPE_INT :
                            zevalues.push_back( (static_cast<IntVar*>(sol->v[i]))->val() );
                            break;
                        case VTYPE_BOOL :
                            zevalues.push_back( (static_cast<BoolVar*>(sol->v[i]))->val() );
                            break;
                        default :
                            cout<<"6unknown Variable type"<<endl;
                            abort();
                    }
                }
                Strategy candidate(false,vmin,vmax,scope,zevalues);

                Strategy son_of_candidate = rSolve(qs,scope+1,assign,nodes);
                if (son_of_candidate.isFalse()) candidate = Strategy::SFalse();
                else candidate.attach(son_of_candidate);

                int score_of_candidate;
                if (candidate.isFalse()) score_of_candidate = ( (opttype == 1) ? INT_MAX : INT_MIN );
                else score_of_candidate = opt->getVal(candidate);
//                cout<<"score of candidate is "<<score_of_candidate<<endl;
//                cout<<"Opt type is";
                switch (opttype) {
                    case 0 : // ANY
//                        cout<<"ANY"<<endl;
                        if (!candidate.isFalse()) {
                            delete sol;
                            delete espace;
                            return candidate;
                        }
                        break;
                    case 1 : // MIN
//                        cout<<"MIN"<<endl;
                        if (score_of_candidate < score) {
                            retour=candidate;
                            score=score_of_candidate;
                        }
                        break;
                    case 2 : // MAX
//                        cout<<"MAX"<<endl;
                        if (score_of_candidate > score) {
                            retour=candidate;
                            score=score_of_candidate;
                        }
                        break;
                    default :
                        cout<<"Unknown opt type : "<<opttype<<endl;
                        abort();
                        break;
                } // end switch
                delete sol;
                sol=solutions.next();
            } // end while
            delete espace;
            return retour;
        } // end if..else (existential)
    }// end "Second case"
}