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

atsp.c

00001 #define OUR_FILE
00002 
00003 #include<scil/scil.h>
00004 #include<scil/constraints/atour.h>
00005 #include<scil/constraints/path.h>
00006 //#include<scil/constraints/disjunctive.h>
00007 #include<LEDA/tuple.h>
00008 #include<LEDA/stream.h>
00009 #include<LEDA/string.h>
00010 #include<fstream.h>
00011 
00012 using namespace SCIL;
00013 using namespace LEDA;
00014 
00015 typedef two_tuple<node, node> prec;
00016 
00017 LEDA::string read_next_line(istream& is) {
00018   LEDA::string s="#";
00019   while((!is.eof()) && ((s.head(1)=="#") || (s.length()==0) || (s.head(1)==" "))) {
00020     s.read_line(is);
00021   };
00022   if(is.eof()) s="";
00023   return s;
00024 };
00025 
00026 
00027 int main(int argc, char** argv) {
00028 
00029   // READ_GRAPH
00030 
00031   ifstream is(argv[1]);
00032 
00033   GRAPH<int,int> G;
00034 
00035   LEDA::string_istream s(read_next_line(is));
00036   int n,i,j,k;
00037 
00038   s>>n;
00039   node N[n+1];
00040   LEDA::string Names[n];
00041 
00042   cout<<n<<" nodes in the graph\n";
00043 
00044 #ifdef OUR_FILE
00045   for(int i=0; i<n; i++) Names[i]=read_next_line(is);
00046 #endif
00047 
00048   for(int i=0; i<n; i++) {
00049     N[i]=G.new_node(i);
00050   };
00051 
00052 #ifdef OUR_FILE
00053   bool start=false;
00054   while(!is.eof()) {
00055     string_istream s(read_next_line(is));
00056     s>>i;
00057     if(!s.eof()) {
00058       if(i==-1) {
00059         i=n;
00060         if(start==false) {
00061           start=true;
00062           N[n]=G.new_node(n);
00063         }
00064       }
00065       s>>j; s>>k;
00066       //double d; s>>d; k=(int) d;
00067       cout<<i<<" "<<j<<" "<<k<<" edge\n";
00068       G.new_edge(N[i],N[j],k);
00069     }
00070   };
00071   if(start) {
00072     for(int i=0; i<n; i++) {
00073       G.new_edge(N[i],N[n],0);
00074     }
00075   };
00076 #endif
00077 
00078 #ifndef OUR_FILE
00079   for(i=0; i<n; i++) for(j=0; j<n; j++) {
00080     is>>k;
00081     if((k<10000000) && (i!=j))
00082       G.new_edge(N[i],N[j],k);
00083   }
00084 #endif
00085 
00086   // END_READ_GRAPH
00087 
00088   ILP_Problem IP(Optsense_Min);
00089 
00090   edge e;
00091   row_map<edge> VM;
00092   forall_edges(e, G) {
00093     VM[e]=IP.add_variable(G[e],0,1, Vartype_Integer);
00094   };
00095 
00096   IP.add_sym_constraint(new ATOUR(G, VM));
00097 
00098   IP.optimize();
00099 
00100   int d=0;
00101   forall_edges(e, G) if(IP.get_solution(VM[e])>=0.5) {
00102     cout<<G[source(e)]<<" "<<G[target(e)]<<" "<<G[e]<<" edge\n";
00103     d+=G[e];
00104   } else G.del_edge(e);
00105 
00106   cerr<<argv[1]<<" "<<d<<endl;
00107 
00108   node u=N[0];
00109 #ifdef OUR_FILE
00110   if(start==true) u=N[n];
00111 #endif
00112   node v=u;
00113   do {
00114     if(G[u]!=n) cout<<G[u]<<" "<<Names[G[u]]<<endl;
00115     u=G.opposite(u,G.first_adj_edge(u));
00116   } while(u!=v);
00117   cout<<"\n\n";
00118 
00119   exit(0);
00120 };
00121 

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