/Users/jamian/Desktop/qecode/examples/NimFibo.cpp

Go to the documentation of this file.
00001 /****   , [ NimFibo.cpp ], 
00002 Copyright (c) 2007 Universite d'Orleans - Jeremie Vautard 
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021  *************************************************************************/
00022 
00023 #include "qsolver.hh"
00024 #include "implicative.hh"
00025 
00027 // This is a model of the nim-fibonacci game. We have a N matches set. First player may  //
00028 // take between 1 and N-1 matches. Then, each player may take at most twice the number of//
00029 // matches taken by the last player. Take the last match to win !                        //
00031 
00032 int main() {
00033     for (int N = 10; N<=22;N++)  // Initial number of matches
00034     {
00035     int* scopeSize = new int[N+2];
00036     bool* qtScope = new bool[N+2];
00037     for (int i=0;i<N+2;i++) {
00038         qtScope[i] = ( i%2 != 0);
00039         scopeSize[i] = 2;
00040     }
00041     Implicative p(N+2,qtScope,scopeSize);
00042     
00043     p.QIntVar(0,1,N-1);
00044     p.QIntVar(1,0,N);
00045     post(p.space(),p.var(0) == p.var(1));
00046     IntVarArgs b(2); b[0] = p.var(0) ; b[1] = p.var(1);
00047     branch(p.space(),b,INT_VAR_SIZE_MIN,INT_VAL_MIN);
00048     p.nextScope();
00049     
00050     for (int i=1;i<N+2;i++) {
00051         p.QIntVar(2*i , 1, N-1);
00052         p.QIntVar(2*i + 1, 0, N);
00053         post(p.space(), p.var(2*i) <= 2*p.var(2*(i-1)));
00054         post(p.space(), p.var(2*i + 1) == p.var(2*i) + p.var(2*i - 1));
00055         IntVarArgs bb(2*i + 2);
00056         for (int plop = 0;plop < (2*i + 2);plop++)
00057             bb[plop] = p.var(plop);
00058         branch(p.space(),bb,INT_VAR_SIZE_MIN,INT_VAL_MIN);
00059         p.nextScope();
00060     }
00061     
00062     p.makeStructure();
00063     QSolver s(&p);
00064     
00065     unsigned long int nodes=0;
00066     
00067     Strategy outcome  = s.solve(nodes);
00068     cout << "For " << N << " matches : "<<endl;
00069     cout << "  outcome: " << ( outcome.isFalse() ? "FALSE" : "TRUE") << endl;
00070     cout << "  nodes visited: " << nodes << endl;
00071     }
00072     return 0;
00073 }

Generated on Tue Jun 10 18:31:30 2008 for qecode by  doxygen 1.5.2