/Users/jamian/Desktop/qecode/examples/network-pricing.cc

Go to the documentation of this file.
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 // This is an instance of the Network Pricing Problem, taken from :                    //
00029 //                                                                                     //
00030 // Bouhtou, M., Grigoriev, A., van Hoesel, S., van der Kraaij, A.F., Spieksma, F.C.,   //
00031 // Uetz, M.: Pricing bridges to cross a river. Naval Research Logistics 54(4) (2007)   //
00032 // 411Ð420                                                                             //
00033 //                                                                                     //
00034 // The problem is to set a tariff on some network links in a way that maximizes the    //
00035 // profit of the owner of the links.                                                   //
00036 // Customer i wishes to transmit di amount of data from a source xi to a target yi.    //
00037 // Each path from a source to a target has to cross a tolled arc aj . On the way from  //
00038 // si to aj , the cost of the links owned by other providers is cij . It is assumed    //
00039 // that each customer i wishes to minimize the cost to route her data and that they can//
00040 // always choose an other provider at a cost ui. The purpose of the problem is to      //
00041 // determine the cost tj to cross a tolled arc aj in order to maximize the revenue of  //
00042 // the telecom operator.                                                               //
00043 // This instance has 5 links and 9 customers. The network operator may choose among 5  //
00044 // tarifs for each link.                                                               //
00046 
00047 int main() {
00048     int NCustomer = 9;
00049     int NArc = 5;
00050     int*c = new int[NCustomer*NArc]; // [NArc*i+j] : Initial cost for client i to reach way j 
00051     int* d = new int[NCustomer]; // d[i] : amount of data client i wants to transmit
00052     int* u = new int[NCustomer]; // u[i] : maximum cost client i will accept
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); // Copy c in an IntArgs for further constraint posting 
00068     IntArgs darg(NCustomer,d); // Copy d in an IntArgs for further constraint posting 
00069     IntArgs uarg(NCustomer,u); // Copy u in an IntArgs for further constraint posting 
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); // tariff for way i
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); // k
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); // a
00096     problem.QIntVar(NArc+2,0,1000000); // cost
00097     problem.QIntVar(NArc+3,0,1000000); // Income
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)); // k* NArc + a
00108     IntVar aux2(problem.var(NArc+5)); // c[k*Narc+a]
00109     IntVar aux3(problem.var(NArc+6)); // t[a]
00110     IntVar aux4(problem.var(NArc+7)); // d[k]
00111     IntVar aux5(problem.var(NArc+8)); // c[]+t[]
00112     IntVar aux6(problem.var(NArc+9)); // u[k]
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); // cost = aux5 * aux4
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); // at scope 2, we minimize (1) the variable cost
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 }

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