00001 #include "gecode/minimodel.hh"
00002 #include "gecode/search.hh"
00003 #include "Implicative.hh"
00004 #include "qsolver.hh"
00005 #include <iostream>
00006
00007 using namespace std;
00008 using namespace Gecode;
00009 using namespace Gecode::Int;
00010
00011 void printStr(Strategy s,int depth) {
00012 StrategyNode plop = s.getTag();
00013 for (int glou=0;glou<depth;glou++) cout<<" ";
00014 if (s.isTrue()) cout<<"TRUE"<<endl;
00015 else if (s.isFalse()) cout<<"FALSE"<<endl;
00016 else cout<<"type "<<plop.type<<" qt "<<plop.quantifier<<" vmin "<<plop.Vmin<<" vmax "<<plop.Vmax<<" scope "<<plop.scope<<" - ";
00017 for (int i=0;i<s.getTag().valeurs.size();i++) cout<<s.getTag().valeurs[i]<<" ";
00018 cout<<endl;
00019 for (int glou=0;glou<depth;glou++) cout<<" ";
00020 cout<<s.degree()<<" child(ren)"<<endl;
00021 for (int i=0;i<s.degree();i++) {
00022 for (int glou=0;glou<depth;glou++) cout<<" ";
00023 cout<<"Child "<<i<<" : "<<endl;
00024 printStr(s.getChild(i),depth+1);
00025 }
00026 }
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00046
00047 int main() {
00048 int NCustomer = 9;
00049 int NArc = 5;
00050 int*c = new int[NCustomer*NArc];
00051 int* d = new int[NCustomer];
00052 int* u = new int[NCustomer];
00053 int max = 19;
00054
00055 c[0*NArc+0]=5; c[0*NArc+1]=1; c[0*NArc+2]=8; c[0*NArc+3]=6; c[0*NArc+4]=6;
00056 c[1*NArc+0]=2; c[1*NArc+1]=8; c[1*NArc+2]=6; c[1*NArc+3]=3; c[1*NArc+4]=1;
00057 c[2*NArc+0]=0; c[2*NArc+1]=8; c[2*NArc+2]=0; c[2*NArc+3]=8; c[2*NArc+4]=6;
00058 c[3*NArc+0]=2; c[3*NArc+1]=4; c[3*NArc+2]=7; c[3*NArc+3]=5; c[3*NArc+4]=8;
00059 c[4*NArc+0]=8; c[4*NArc+1]=2; c[4*NArc+2]=4; c[4*NArc+3]=3; c[4*NArc+4]=4;
00060 c[5*NArc+0]=5; c[5*NArc+1]=4; c[5*NArc+2]=6; c[5*NArc+3]=4; c[5*NArc+4]=1;
00061 c[6*NArc+0]=5; c[6*NArc+1]=6; c[6*NArc+2]=7; c[6*NArc+3]=4; c[6*NArc+4]=8;
00062 c[7*NArc+0]=5; c[7*NArc+1]=8; c[7*NArc+2]=1; c[7*NArc+3]=3; c[7*NArc+4]=6;
00063 c[8*NArc+0]=7; c[8*NArc+1]=8; c[8*NArc+2]=8; c[8*NArc+3]=3; c[8*NArc+4]=5;
00064
00065 d[0]=7; d[1]=16; d[2]=16; d[3]=19; d[4]=18; d[5]=13; d[6]=8; d[7]=11; d[8]=18;
00066 u[0]=122; u[1]=190; u[2]=113; u[3]=285; u[4]=247; u[5]=255; u[6]=143; u[7]=121; u[8]=139;
00067 IntArgs carg(NCustomer*NArc,c);
00068 IntArgs darg(NCustomer,d);
00069 IntArgs uarg(NCustomer,u);
00070
00071 bool q[] = {false,true,false};
00072 int* nv = new int[3];
00073 nv[0]=NArc;
00074 nv[1]=1;
00075 nv[2]=9;
00076
00077 Implicative problem(3,q,nv);
00078 int postarfs[] = {3,7,11,15,19};
00079 IntSet thePossibleTariffs(postarfs,5);
00080 for (int i=0;i<NArc;i++)
00081 problem.QIntVar(i,thePossibleTariffs);
00082 IntVarArgs branch1(NArc);
00083 for (int i=0;i<NArc;i++)
00084 branch1[i] = problem.var(i);
00085 branch(problem.space(),branch1,INT_VAR_SIZE_MIN,INT_VAL_MIN);
00086 problem.nextScope();
00087
00088 problem.QIntVar(NArc,0,NCustomer-1);
00089 IntVarArgs branch2(NArc+1);
00090 for (int i=0;i<NArc+1;i++)
00091 branch2[i] = problem.var(i);
00092 branch(problem.space(),branch2,INT_VAR_SIZE_MIN,INT_VAL_MIN);
00093 problem.nextScope();
00094
00095 problem.QIntVar(NArc+1,0,NArc-1);
00096 problem.QIntVar(NArc+2,0,1000000);
00097 problem.QIntVar(NArc+3,0,1000000);
00098 IntVar a(problem.var(NArc+1));
00099 IntVar cost(problem.var(NArc+2));
00100 IntVar income(problem.var(NArc+3));
00101 problem.QIntVar(NArc+4,0,1000000);
00102 problem.QIntVar(NArc+5,0,1000000);
00103 problem.QIntVar(NArc+6,0,1000000);
00104 problem.QIntVar(NArc+7,0,1000000);
00105 problem.QIntVar(NArc+8,0,1000000);
00106 problem.QIntVar(NArc+9,0,1000000);
00107 IntVar aux1(problem.var(NArc+4));
00108 IntVar aux2(problem.var(NArc+5));
00109 IntVar aux3(problem.var(NArc+6));
00110 IntVar aux4(problem.var(NArc+7));
00111 IntVar aux5(problem.var(NArc+8));
00112 IntVar aux6(problem.var(NArc+9));
00113 IntVar k(problem.var(NArc));
00114 post(problem.space(), aux1 == ( NArc * k + a) );
00115 element(problem.space(),carg,aux1,aux2);
00116 IntVarArgs t(NArc);
00117 for (int i=0;i<NArc;i++) t[i]=problem.var(i);
00118 element(problem.space(),t,a,aux3);
00119 element(problem.space(),darg,k,aux4);
00120 post(problem.space(), aux5 == aux2 + aux3);
00121 mult(problem.space(),aux5,aux4,cost);
00122 mult(problem.space(),aux3,aux4,income);
00123 element(problem.space(),uarg,k,aux6);
00124 post(problem.space(),cost <= aux6);
00125
00126 IntVarArgs branch3(NArc+10);
00127 for (int i=0;i<NArc+10;i++)
00128 branch3[i] = problem.var(i);
00129 branch(problem.space(),branch3,INT_VAR_SIZE_MIN,INT_VAL_MIN);
00130
00131 OptVar* costopt = problem.getExistential(NArc+2);
00132 OptVar* incomeopt = problem.getExistential(NArc+3);
00133 problem.optimize(2,1,costopt);
00134 AggregatorSum somme;
00135 OptVar* sumvar = problem.getAggregate(1,incomeopt,&somme);
00136 problem.optimize(0,2,sumvar);
00137
00138
00139 QSolver sol(&problem);
00140 unsigned long int nodes=0;
00141
00142 Strategy hihihi = sol.solve(nodes);
00143 cout<<nodes<<" nodes encountered."<<endl;
00144 printStr(hihihi,0);
00145 return 0;
00146 }