MinLCA algorithms
mip_read_graph.cc
Go to the documentation of this file.
1 #include <lemon/smart_graph.h>
2 #include <utils/graph_utils.hh>
3 #include <minlca/greedy.hh>
4 #include <minlca/mip-model.hh>
5 #include <utils/timer.hh>
6 #include <minlca/profiler.hh>
8 using namespace std;
9 using namespace lemon;
10 using namespace minlca;
11 using namespace minlca::utils;
12 
13 typedef GrbMip Mip;
14 
20 
29 int main(int argc, char **argv)
30 {
31  GRAPH_TYPEDEFS(SmartGraph);
32  SmartGraph g;
33  ifstream graphFile(argv[1]);
34  GraphFormat format = RMF;
35 
36  if (string(argv[2]) == "GRA") {
37  format = GRA;
38  }
39 
40  Timer timer;
41  readGraph(g, format, graphFile);
42 
43  graphFile.close();
44 
45  cout << "Graph reading time: " << timer.elapsed() << endl;
46  cout << endl;
47 
48  int n = countNodes(g);
49  cout << "Total nodes: " << n << endl;
50  cout << "Total edges: " << countEdges(g) << endl;
51 
52  cout << endl;
53  cout << "Using greedy nearest:" << endl;
54 
56  cout << "Creation elapsed time: " << greedyNearest.creationTime() << endl;
57  greedyNearest.init();
58  cout << "Initialisation elapsed time: " << greedyNearest.initTime() << endl;
59 
60  if (greedyNearest.run() == SOLUTION_FOUND) {
61  cout << "Algorithm running time: " << greedyNearest.runTime() << endl;
62  cout << "Max colours: " << greedyNearest.maxK() << endl;
63  cout << "Colours used: " << greedyNearest.totalColours() << endl;
64  cout << "LCA value: " << greedyNearest.lcaValue() << endl;
65  }
66  else {
67  cout << "Could not find a solution!" << endl;
68  }
69 
70  cout << endl;
71 
72  cout << "Using greedy least cost:" << endl;
73 
75  cout << "Creation elapsed time: " << greedyLeastCost.creationTime() << endl;
76  greedyLeastCost.init();
77  cout << "Initialisation elapsed time: " << greedyLeastCost.initTime() << endl;
78 
79  if (greedyLeastCost.run() == SOLUTION_FOUND) {
80  cout << "Algorithm running time: " << greedyLeastCost.runTime() << endl;
81  cout << "Max colours: " << greedyLeastCost.maxK() << endl;
82  cout << "Colours used: " << greedyLeastCost.totalColours() << endl;
83  cout << "LCA value: " << greedyLeastCost.lcaValue() << endl;
84  }
85  else {
86  cout << "Could not find a solution!" << endl;
87  }
88 
89  cout << endl;
90 
91  int k;
92 
93  if (greedyLeastCost.lcaValue() < greedyNearest.lcaValue()) {
94  k = greedyLeastCost.totalColours();
95  }
96  else {
97  k = greedyNearest.totalColours();
98  }
99 
100  cout << "Using MIP model:" << endl;
102  mipModel.init();
103 
104  if (argc > 4) {
105  mipModel.timeLimit(atof(argv[4]));
106  }
107 
108  if (argc > 5) {
109  mipModel.model().getEnv().set(GRB_StringParam_LogFile, argv[5]);
110  }
111 
112  mipModel.model().getEnv().set(GRB_IntParam_Method, GRB_METHOD_CONCURRENT);
113  cout << "Initialisation elapsed time: " << mipModel.initTime() << endl;
114 
115 
116  if (greedyLeastCost.lcaValue() < greedyNearest.lcaValue()) {
117  mipModel.initialSolution(greedyLeastCost.colouring());
118  }
119  else {
120  mipModel.initialSolution(greedyNearest.colouring());
121  }
122 
123  cout << "Initial solution elapsed time: " << mipModel.initialSolutionTime() <<
124  endl;
125 
126  if (mipModel.run() == SOLUTION_FOUND) {
127  cout << "Algorithm running time: " << mipModel.runTime() << endl;
128  cout << "Max colours: " << mipModel.maxK() << endl;
129  cout << "Colours used: " << mipModel.totalColours() << endl;
130  cout << "LCA value: " << mipModel.lcaValue() << endl;
131  cout << "Optimal?: ";
132 
133  if (mipModel.model().getModel().get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
134  cout << "YES";
135  }
136  else {
137  cout << "NO";
138  }
139 
140  cout << endl;
141 
142  if (argc > 3) {
143  ofstream outfile(argv[3], ofstream::out | ofstream::trunc);
144 
145  for (int i = 0; i < n - 1; ++i) {
146  Node v = g.nodeFromId(i);
147  outfile << mipModel.colouring()[v] << ' ';
148  }
149 
150  outfile << mipModel.colouring()[g.nodeFromId(n - 1)] << endl;
151  outfile.close();
152  }
153  }
154  else {
155  cout << "Could not find a solution!" << endl;
156  }
157 }
158 
GraphFormat
Available graph formats.
Definition: graph_utils.hh:20
double initialSolutionTime()
Retrieve the duration of setting the initial solution.
void readGraph(Graph &g, GraphFormat f=RMF, IS &in=std::cin)
Read a graph.
Definition: graph_utils.hh:160
double runTime()
Retrieve the duration of running the algorithm.
Definition: profiler.hh:62
virtual MinLCAStatus run()
Run the algorithm.
Definition: profiler.hh:50
int main(int argc, char **argv)
Main function.
Solution has been found after running.
Definition: base.hh:22
LEMON namespace.
Definition: grb.cc:29
Default namespace Default namespace for MinLCA algorithms.
Definition: base.hh:15
Class for timing MinLCA algorithms.
Definition: profiler.hh:16
Functions for reading graphs and getting graph properties.
double initTime()
Retrieve the duration of initialising the algorithm.
Definition: profiler.hh:70
Description of Timer.
Simple timing class.
Definition: timer.hh:14
double creationTime()
Retrieve the duration of instantiating the algorithm class.
Definition: profiler.hh:78
Interface for the Gurobi MIP solver.
Definition: grb.h:307
Contains the definitions for timing MinLCA algorithms.
double elapsed() const
Retrieve elapsed time.
Definition: timer.cc:20
Class for timing MinLCA optimisation algorithms.
void init(Args &&...args)
Reset the data structures of the algorithm.
Definition: profiler.hh:39
Defines the integer linear programming model for MinLCA.
Contains the definitions for timing MinLCA optimisation algorithms.
Namespace for util functions and classes.
Definition: binomial.hh:14