Main Page   Class Hierarchy   Compound List   File List   Contact   Download   Symbolic Constraints   Examples  

tpp2.c

00001 #line 2 "tpp2.lw"
00002 #include<LEDA/graph.h>
00003 #include<scil/scil.h>
00004 
00005 using namespace LEDA;
00006 using namespace SCIL;
00007 using namespace std;
00008 
00009 //#include"cp_cuts.h"
00010 
00011 int** D;
00012 
00013 class trip {
00014   public:
00015   int* C;
00016   int a,h;
00017   int H;
00018   int s;
00019   int id;
00020   int l;
00021 
00022   public:
00023   trip() {};
00024 
00025   trip(int H_, int h_, int s_, list<int>& c, int id_) {
00026     H=H_;
00027     s=s_;
00028     a=c.size();
00029     C=new int[a];
00030     int i, j=0;
00031     forall(i, c) {
00032       C[j]=i;
00033       j++;
00034     };
00035     h=h_;
00036     id=id_;
00037     l=0; j=H;
00038     if(c.size()!=0) {
00039       forall(i,c) {
00040         l+=D[j][i];
00041         j=i;
00042       };
00043       l+=D[j][H];
00044     };
00045   }
00046 
00047   bool home(int i) {
00048     return (i>=a+s) && (i<a+h+s);
00049   };
00050 
00051   int number() {
00052     return id;
00053   };
00054 
00055   int length() {
00056     return l;
00057   };
00058 
00059   int city(int i) {
00060     if(i<s) return -1;
00061     i-=s;
00062     if(i>=a+h) return -1;
00063     if(i>=a) return H;
00064     return C[i];
00065   };
00066 
00067   void print() const {
00068     cout<<s<<" start : "<<H<<" home :";
00069     for(int i=0; i<a; i++) cout<<C[i]<<" ";
00070     for(int i=0; i<h; i++) cout<<H<<" ";
00071   };
00072 };
00073 
00074 list<int>** L1, **L2, **L3, **L4;
00075 
00076 void list_append(list<trip>& L, trip t) {
00077   L.append(t);
00078   cout<<L.size()<<" : ";
00079   t.print();cout<<endl;
00080   for(int i=t.s; i<t.s+t.a+t.h; i++) {
00081     L1[t.H][i].append(t.number());
00082   };
00083   for(int i=t.s+t.a; i<t.s+t.a+t.h; i++) {
00084     L2[t.H][i].append(t.number());
00085   };
00086   for(int i=t.s; i<t.s+t.a; i++) {
00087     L3[t.city(i)][i].append(t.number());
00088     L4[t.H][t.city(i)].append(t.number());
00089   };
00090 };
00091 
00092 void generate_trip(list<trip>& L, list<int>& T, bool* B, int U, int n, int H) {
00093   for(int i=0; i<n; i++) if(B[i]==false) {
00094     T.append(i); B[i]=true;
00095     for(int j=1; j<=U; j++) {
00096       for(int s=0; s<2*(n-1)-T.size()-j+1; s++) {
00097         list_append(L, trip(H, j, s, T, L.size()));
00098       };
00099     }
00100     list_append(L, trip(H, 0, 2*(n-1)-T.size(), T, L.size()));
00101     if(T.size()<U) generate_trip(L, T, B, U, n, H);
00102     B[i]=false; T.pop_back();
00103   };
00104 };
00105 
00106 list<trip> L;
00107 var_map<int> VM;
00108 int n, U;
00109 
00110 class sc : public sym_constraint {
00111  public:
00112   status separate(subproblem& s) {
00113     /*
00114     trip t; double d;
00115     double D[n][2*n-2][n];
00116 
00117     for(int i=0; i<n; i++) for(int j=0; j<n; j++) for(int l=0; l<2*n-2; l++)
00118       D[i][l][j]=0;
00119 
00120     forall(t, L) {
00121       d=s.value(VM[t.number()]);
00122       if(d>0) {
00123         for(int i=t.s; i<t.s+t.a+t.h; i++) {
00124           D[t.H][i][t.city(i)]+=d;
00125         };
00126       };
00127     };
00128 
00129     for(int i=0; i<n; i++) {
00130       cout<<"\n"<<i<<" city\n";
00131       for(int j=0; j<n; j++) {
00132         cout<<"\n"<<j<<": ";
00133         for(int l=0; l<2*n-2; l++) {
00134           cout<<D[i][l][j]<<" ";
00135         };
00136       };
00137     };
00138     cout<<endl;
00139     */
00140     return no_constraint_found;
00141   };
00142 };
00143       
00144 
00145 int main() {
00146   
00147   ifstream is("data.txt");
00148 
00149   is>>n;
00150   is>>U;
00151   cout<<n<<" cities\n";
00152   cout<<U<<" succesive\n";
00153 
00154   D=new int*[n];
00155   for(int i=0; i<n; i++) D[i]=new int[n];
00156 
00157   L1=new list<int>*[n];
00158   L2=new list<int>*[n];
00159   L3=new list<int>*[n];
00160   L4=new list<int>*[n];
00161 
00162   for(int i=0; i<n; i++) {
00163     L1[i]=new list<int>[2*n-2];
00164     L2[i]=new list<int>[2*n-2];
00165     L3[i]=new list<int>[2*n-2];
00166     L4[i]=new list<int>[n];
00167   };
00168 
00169   for(int i=0; i<n; i++) {
00170     for(int j=0; j<n; j++) {
00171       is>>D[i][j];
00172       cout<<D[i][j]<<" ";
00173     }
00174     cout<<endl;
00175   }
00176 
00177   bool B[n];
00178   for(int i=0; i<n; i++) B[i]=false;
00179   list<int> T;
00180   
00181   for(int i=0; i<n; i++) {
00182     B[i]=true;
00183     for(int j=1; j<=U; j++) {
00184       list_append(L, trip(i, j, 0, T, L.size()));
00185     };
00186     generate_trip(L, T, B, U, n, i);
00187     cout<<L.size()<<" variables\n";
00188     B[i]=false;
00189   };
00190 
00191   cout<<L.size()<<" L.size()\n";
00192 
00193   ILP_Problem IP(Optsense_Min);
00194 
00195   //cp_cuts* CPC=new cp_cuts;
00196 
00197   cons c; 
00198 
00199   //var_map<int> VM;
00200   trip t;
00201   forall(t, L) {
00202     VM[t.number()]=IP.add_variable(t.length(), 0,1, Vartype_Integer);
00203   };
00204 
00205   cout<<L.size()<<" L.size()\n";
00206   int k;
00207   for(int i=0; i<n; i++) {
00208     for(int t=0; t<2*n-2; t++) {
00209       row r1;
00210       forall(k, L1[i][t]) r1+=(row) VM[k];
00211       c=IP.add_basic_constraint(r1==1);
00212       //CPC->add_sym_constraint(new cp_basic_constraint(c));
00213       row r2;
00214       forall(k, L2[i][t]) r2+=(row) VM[k];
00215       forall(k, L3[i][t]) r2-=(row) VM[k];
00216       c=IP.add_basic_constraint(r2==0);
00217       //CPC->add_sym_constraint(new cp_basic_constraint(c));
00218     }
00219     for(int j=0; j<n; j++) if(j!=i) {
00220       row r3;
00221       forall(k, L4[i][j]) r3+=(row) VM[k];
00222       c=IP.add_basic_constraint(r3 == 1);
00223       //CPC->add_sym_constraint(new cp_basic_constraint(c));
00224     };
00225   }
00226 
00227   for(int i=0; i<n; i++) {
00228     for(int k=0; k<n; k++) if(k!=i) {
00229       for(int l=0; l<2*n-4; l++) {
00230         row r;
00231         forall(t, L) {
00232           if((t.H==i) && (t.city(l)!=i) && (t.city(l+1)!=i)
00233              && (t.city(l)!=-1) && (t.city(l+1)!=-1)) 
00234             r+=(row) VM[t.number()];
00235           if((t.H==k) && ((t.city(l)==i) || (t.city(l+1)==i))
00236              && (t.city(l+2)!=k))
00237             r+=(row) VM[t.number()];
00238           if((t.H!=i) && (t.H!=k) && ((t.city(l)==i) || (t.city(l+1)==i))
00239              && (t.city(l+2)==k))
00240             r+=(row) VM[t.number()];
00241         };
00242         c=IP.add_basic_constraint(r<=1);
00243         //CPC->add_sym_constraint(new cp_basic_constraint(c));
00244       };
00245     };
00246   };
00247  
00248   row sr;
00249   forall(t, L) {
00250     if((t.H==0) && (t.city(0)==0)) sr+=(row) VM[t.number()];
00251     if((t.H==0) && (t.city(2*n-3)==0)) sr-=(row) VM[t.number()];
00252   };
00253   c=IP.add_basic_constraint(sr<=0);
00254   //CPC->add_sym_constraint(new cp_basic_constraint(c));
00255 
00256   //IP.add_sym_constraint(CPC);
00257   //IP.add_sym_constraint(new sc());
00258 
00259   IP.optimize();
00260 
00261   int D[n][2*n-2];
00262   //trip t;
00263   forall(t, L) {
00264     if(IP.get_solution(VM[t.number()])>0.01) {
00265       for(int i=t.s; i<t.s+t.a+t.h; i++) {
00266         D[t.H][i]=t.city(i);
00267       }
00268       t.print();
00269       cout<<"\n Solution: "<<IP.get_solution(VM[t.number()])<<endl;
00270       cout<<t.number()<<" number\n";
00271     }
00272   };
00273 
00274   for(int i=0; i<n; i++) {
00275     for(int j=0; j<2*n-2; j++) {
00276       cout<<D[i][j]<<" ";
00277     };
00278     cout<<endl;
00279   };
00280 
00281 }

Generated on Tue Nov 16 15:18:22 2004 for SCIL by doxygen1.2.16