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

chinese_postman.cc

00001 #include<scil/scil.h>
00002 #include<scil/constraints/tjoin.h>
00003 #include<boost/graph/iteration_macros.hpp>
00004 #include<boost/graph/adjacency_list.hpp>
00005 #include<map>
00006 
00007 using namespace std;
00008 using namespace boost;
00009 using namespace SCIL;
00010 
00011    typedef adjacency_list<vecS, vecS, undirectedS> Graph;
00012    typedef boost::graph_traits<Graph> GraphTraits;
00013    typedef GraphTraits::vertex_descriptor vertex_descriptor;
00014    typedef GraphTraits::edge_descriptor edge_descriptor;
00015 
00016 void read_instance(char* file_name, Graph& G, var_map<edge_descriptor>& VM, ILP_Problem& IP)
00017 {
00018    int n, m;
00019    vertex_descriptor a, b;
00020    double c;
00021    char buf[1024];
00022    char d;
00023    string s;
00024 
00025    string path(file_name);
00026    ifstream file(path.c_str());
00027    if(file.fail()) {
00028       cerr<<"no file with name "<< file_name<<" found!"<< endl;
00029       exit(1);
00030    }
00031    file >> n >> m;
00032    for(int i = 0; i < n; i++)
00033       add_vertex(G);
00034 
00035    file.getline(buf, 1024);
00036 
00037    for(int j = 0; j < m; j++){
00038       file >> a >> b >> c;
00039       VM[add_edge(a,b,G).first] = IP.add_binary_variable(c);
00040    }
00041 
00042 }
00043 
00044 int main(int argc, char** argv){
00045 
00046    vertex_descriptor v, w;
00047    edge_descriptor e;
00048    var_map<edge_descriptor> VM;
00049    map<vertex_descriptor, bool> T;
00050    Graph G;
00051 
00052    ILP_Problem IP(Optsense_Min);
00053 
00054    read_instance(argv[1], G, VM, IP);
00055 
00056    vector<int> components(num_vertices(G));
00057    if(connected_components(G, &components[0]) != 1){
00058       cerr<<"Graph is not connected!"<<endl;
00059       exit(1);
00060    }
00061 
00062    //find vertices with odd degree
00063    BGL_FORALL_VERTICES(v, G, Graph)
00064       if(out_degree(v, G) % 2 == 1){
00065          cout<<v<<endl;
00066          T[v] = true;
00067       }
00068       else{
00069          cout<<v<<" even"<<endl;
00070          T[v] = false;
00071       }
00072    
00073    IP.add_sym_constraint(new TJOIN<Graph>(G, VM, T));
00074 
00075    IP.optimize();
00076 
00077    //print solution
00078    
00079    solution sol = IP.get_solution();
00080    cout<<"Additional edges (Minimum T-Join):"<<endl;
00081    double sum = 0;
00082    BGL_FORALL_EDGES(e, G, Graph){
00083      cout<<e<<": "<<sol.value(VM[e])<<endl;
00084      sum += IP.get_obj_coefficient(VM[e]) * (1 + sol.value(VM[e]));
00085    }
00086    cout<<"Weight of Postman Tour: "<<sum<<endl; 
00087    
00088    
00089    return 0;
00090 }
00091 
Generated on Mon Mar 28 22:03:47 2011 for SCIL by  doxygen 1.6.3