Changeset 1530:d99c3c84f797 in lemon-0.x
- Timestamp:
- 07/04/05 15:08:31 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2021
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
demo/dijkstra_demo.cc
r1528 r1530 45 45 len.set(v5_t, 8); 46 46 47 std::cout << "This program is a simple demo of the LEMON Dijkstra class."<<std::endl; 48 std::cout << "We calculate the shortest path from node s to node t in a graph."<<std::endl; 49 std::cout <<std::endl; 50 51 47 52 std::cout << "The id of s is " << g.id(s)<< ", the id of t is " << g.id(t)<<"."<<std::endl; 48 53 -
demo/hello_lemon.cc
r1526 r1530 6 6 typedef lemon::ListGraph Graph; 7 7 typedef Graph::EdgeIt EdgeIt; 8 typedef Graph::Edge Edge; 8 9 typedef Graph::NodeIt NodeIt; 10 typedef Graph::Node Node; 11 typedef Graph::EdgeMap<int> LengthMap; 9 12 using lemon::INVALID; 10 13 11 14 Graph g; 12 15 13 for (int i = 0; i < 3; i++) 14 g.addNode(); 16 Node s=g.addNode(); 17 Node v2=g.addNode(); 18 Node v3=g.addNode(); 19 Node v4=g.addNode(); 20 Node v5=g.addNode(); 21 Node t=g.addNode(); 22 23 Edge s_v2=g.addEdge(s, v2); 24 Edge s_v3=g.addEdge(s, v3); 25 Edge v2_v4=g.addEdge(v2, v4); 26 Edge v2_v5=g.addEdge(v2, v5); 27 Edge v3_v5=g.addEdge(v3, v5); 28 Edge v4_t=g.addEdge(v4, t); 29 Edge v5_t=g.addEdge(v5, t); 15 30 16 for (NodeIt i(g); i!=INVALID; ++i) 17 for (NodeIt j(g); j!=INVALID; ++j) 18 if (i != j) g.addEdge(i, j); 31 LengthMap length(g); 32 33 length.set(s_v2, 10); 34 length.set(s_v3, 10); 35 length.set(v2_v4, 5); 36 length.set(v2_v5, 8); 37 length.set(v3_v5, 5); 38 length.set(v4_t, 8); 39 length.set(v5_t, 8); 19 40 20 41 std::cout << "Hello World!" << std::endl; … … 32 53 std::cout << " (" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")"; 33 54 std::cout << std::endl; 55 std::cout << std::endl; 56 57 std::cout << "There is a map on the edges (length)!" << std::endl; 58 std::cout << std::endl; 59 for (EdgeIt i(g); i!=INVALID; ++i) 60 std::cout << "length(" << g.id(g.source(i)) << "," << g.id(g.target(i)) << ")="<<length[i]<<std::endl; 61 62 std::cout << std::endl; 63 34 64 } -
demo/lp_demo.cc
r1513 r1530 24 24 //The following example is taken from the documentation of the GLPK library. 25 25 //See it in the GLPK reference manual and among the GLPK sample files (sample.c) 26 27 //A default solver is taken 26 28 LpDefault lp; 27 29 typedef LpDefault::Row Row; … … 37 39 Col x3 = lp.addCol(); 38 40 39 //One solution40 // Row p = lp.addRow();41 // Row q = lp.addRow();42 // Row r = lp.addRow();43 // lp.setRow(p,x1+x2+x3 <=100);44 // lp.setRow(q,10*x1+4*x2+5*x3<=600);45 // lp.setRow(r,2*x1+2*x2+6*x3<=300);46 47 //A more elegant one48 41 //Constraints 49 42 lp.addRow(x1+x2+x3 <=100); … … 70 63 } 71 64 65 //End of LEMON style code 72 66 73 67 //Here comes the same problem written in C using GLPK API routines -
demo/reader_writer_demo.cc
r1528 r1530 16 16 SmartGraph::EdgeMap<int> cap(graph); 17 17 reader.readEdgeMap("capacity",cap); 18 // reader.readNode("source",s).readNode("target",t)19 // .readEdgeMap("capacity",cap).run();20 18 reader.run(); 21 19 … … 31 29 writer.run(); 32 30 33 // LemonReader reader(std::cin);34 35 // NodeSetReader<SmartGraph> nodeset(reader, graph);36 // SmartGraph::NodeMap<int> cost(graph);37 // nodeset.readMap("cost", cost);38 // SmartGraph::NodeMap<char> mmap(graph);39 // nodeset.readMap("mmap", mmap);40 41 // EdgeSetReader<SmartGraph> edgeset(reader, graph, nodeset);42 // SmartGraph::EdgeMap<std::string> description(graph);43 // edgeset.readMap<QuotedStringReader>("description", description);44 45 // NodeReader<SmartGraph> nodes(reader, nodeset);46 // SmartGraph::Node source;47 // nodes.readNode("source", source);48 // SmartGraph::Node target;49 // nodes.readNode("target", target);50 51 // AttributeReader<> attribute(reader, "gui");52 // std::string author;53 // attribute.readAttribute<LineReader>("author", author);54 // int count;55 // attribute.readAttribute("count", count);56 57 // PrintReader print(reader);58 59 // reader.run();60 61 62 // for (SmartGraph::NodeIt it(graph); it != INVALID; ++it) {63 // std::cout << cost[it] << ' ' << mmap[it] << std::endl;64 // }65 66 // for (SmartGraph::EdgeIt it(graph); it != INVALID; ++it) {67 // std::cout << description[it] << std::endl;68 // }69 70 // std::cout << "author: " << author << std::endl;71 // std::cout << "count: " << count << std::endl;72 73 // std::cout << "source cost: " << cost[source] << std::endl;74 // std::cout << "target cost: " << cost[target] << std::endl;75 76 // std::cout.flush();77 31 } catch (DataFormatError& error) { 78 32 std::cerr << error.what() << std::endl; -
demo/route.lgf
r1435 r1530 12 12 0 189.239 92.5316 13 13 @edgeset 14 length 15 2 3 901.074 16 8 5 270.85 17 6 9 601.553 18 5 9 285.022 19 9 4 408.091 20 3 0 719.712 21 7 5 612.836 22 0 4 933.353 23 5 0 778.871 24 5 5 0 25 7 1 664.049 26 5 5 0 27 0 9 560.464 28 4 8 352.36 29 4 9 399.625 30 4 1 402.171 31 1 2 591.688 32 3 8 182.376 33 4 5 180.254 34 3 1 345.283 35 5 4 184.511 36 6 2 1112.45 37 0 1 556.624 14 length id 15 2 3 901.074 0 16 8 5 270.85 1 17 6 9 601.553 2 18 5 9 285.022 3 19 9 4 408.091 4 20 3 0 719.712 5 21 7 5 612.836 6 22 0 4 933.353 7 23 5 0 778.871 8 24 5 5 0 9 25 7 1 664.049 10 26 5 5 0 11 27 0 9 560.464 12 28 4 8 352.36 13 29 4 9 399.625 14 30 4 1 402.171 15 31 1 2 591.688 16 32 3 8 182.376 17 33 4 5 180.254 18 34 3 1 345.283 19 35 5 4 184.511 20 36 6 2 1112.45 21 37 0 1 556.624 22 38 38 @nodes 39 39 source 1 40 40 target 8 41 @edges 42 @attributes 41 43 @end -
demo/sample.lgf
r1528 r1530 1 @nodeset 2 id 3 5 4 4 5 3 6 2 7 1 8 0 9 @edgeset 10 id capacity 11 4 5 6 812 3 5 5 813 2 4 4 514 1 4 3 815 1 3 2 516 0 2 1 1017 0 1 0 1018 @nodes 1 @nodeset 2 id coordinates_x coordinates_y 3 5 796.398 208.035 4 4 573.002 63.002 5 3 568.549 401.748 6 2 277.889 68.476 7 1 288.248 397.327 8 0 102.239 257.532 9 @edgeset 10 id capacity 11 4 5 6 8 12 3 5 5 8 13 2 4 4 5 14 1 4 3 8 15 1 3 2 5 16 0 2 1 10 17 0 1 0 10 18 @nodes 19 19 source 0 20 20 target 5 -
doc/quicktour.dox
r1528 r1530 43 43 optimization. 44 44 45 <ol> <li>The following code fragment shows how to fill a graph with 46 data. It creates a complete graph on 4 nodes. The type Listgraph is one of the 47 LEMON graph types: the typedefs in the beginning are for convenience and we 48 will suppose them later as well. 45 <ol> <li>The following code shows how to build a graph from scratch 46 and iterate on its nodes and edges. This example also shows how to 47 give a map on the edges of the graph. The type Listgraph is one of 48 the LEMON graph types: the typedefs in the beginning are for 49 convenience and we will assume them later as well. 49 50 50 \dontinclude hello_lemon.cc 51 \skip ListGraph 52 \until addEdge 51 \include hello_lemon.cc 53 52 54 See the whole program in file \ref hello_lemon.cc in \c demo subdir of53 See the whole program in file \ref hello_lemon.cc in the \c demo subdir of 55 54 LEMON package. 56 55 57 56 If you want to read more on the LEMON graph structures and 58 57 concepts, read the page about \ref graphs "graphs". 58 59 60 <li>LEMON has an own file format for storing graphs, maps on edges/nodes and some other things. Instead of any explanation let us give a 61 short example file in this format: read the detailed description of the LEMON 62 graph file format and input-output routines here: \ref graph-io-page. 63 64 So here is a file describing a graph of 6 nodes (0 to 5), two nodemaps 65 (called \c coordinates_x and \c coordinates_y), several edges, an edge map 66 called \c capacity and two designated nodes (called \c source and \c target). 67 68 \include sample.lgf 69 70 Finally let us give a simple example that reads a graph from a file and writes 71 it to the standard output. 72 73 \include reader_writer_demo.cc 74 75 See the whole program in file \ref reader_writer_demo.cc. 59 76 60 77 <li> The following code shows how to read a graph from a stream … … 74 91 the details about the output routines into files of the DIMACS format. 75 92 76 <li>DIMACS formats could not give us the flexibility we needed,77 so we worked out our own file format. Instead of any explanation let us give a78 short example file in this format: read the detailed description of the LEMON79 graph file format and input-output routines \ref graph-io-page here.80 81 So here is a file describing a graph of 10 nodes (0 to 9), two nodemaps82 (called \c coordinates_x and \c coordinates_y), several edges, an edge map83 called \c length and two designated nodes (called \c source and \c target).84 85 \todo Maybe a shorter example would be better here.86 87 \include route.lgf88 89 Finally let us give a simple example that reads a graph from a file and writes90 it to the standard output.91 92 \include reader_writer_demo.cc93 94 See the whole program in file \ref reader_writer_demo.cc.95 96 \todo This is still under construction!97 98 93 </ol> 99 94 <li> If you want to solve some transportation problems in a network then … … 102 97 that solves this is the \ref lemon::Dijkstra "LEMON Dijkstra class". 103 98 The following code is a simple program using the 104 \ref lemon::Dijkstra "LEMON Dijkstra class" and it also shows how to define a map on the edges (the length105 function): 99 \ref lemon::Dijkstra "LEMON Dijkstra class": it calculates the shortest path between node \c s and \c t in a graph \c g. 100 We omit the part reading the graph \c g and the length map \c len. 106 101 107 102 \dontinclude dijkstra_demo.cc 108 103 \skip ListGraph 104 \until Graph g 105 ... 106 \skip Dijkstra algorithm 109 107 \until std::cout << g.id(s) 110 108 111 109 See the whole program in \ref dijkstra_demo.cc. 112 110 113 The first part of the code is self-explanatory: we build the graph and set the 114 length values of the edges. Then we instantiate a member of the Dijkstra class 115 and run the Dijkstra algorithm from node \c s. After this we read some of the 116 results. 117 You can do much more with the Dijkstra class, for example you can run it step 118 by step and gain full control of the execution. For a detailed description, see the documentation of the\ref lemon::Dijkstra "LEMON Dijkstra class".111 Some explanation: after instantiating a member of the Dijkstra class 112 we run the Dijkstra algorithm from node \c s. After this we read some 113 of the results. You can do much more with the Dijkstra class, for 114 example you can run it step by step and gain full control of the 115 execution. For a detailed description, see the documentation of the 116 \ref lemon::Dijkstra "LEMON Dijkstra class". 119 117 120 118 … … 155 153 interface. The code together with the comments is self-explanatory. 156 154 157 \code 158 159 //A default solver is taken 160 LpDefault lp; 161 typedef LpDefault::Row Row; 162 typedef LpDefault::Col Col; 163 164 165 //This will be a maximization 166 lp.max(); 167 168 //We add coloumns (variables) to our problem 169 Col x1 = lp.addCol(); 170 Col x2 = lp.addCol(); 171 Col x3 = lp.addCol(); 172 173 //Constraints 174 lp.addRow(x1+x2+x3 <=100); 175 lp.addRow(10*x1+4*x2+5*x3<=600); 176 lp.addRow(2*x1+2*x2+6*x3<=300); 177 //Nonnegativity of the variables 178 lp.colLowerBound(x1, 0); 179 lp.colLowerBound(x2, 0); 180 lp.colLowerBound(x3, 0); 181 //Objective function 182 lp.setObj(10*x1+6*x2+4*x3); 183 184 //Call the routine of the underlying LP solver 185 lp.solve(); 186 187 //Print results 188 if (lp.primalStatus()==LpSolverBase::OPTIMAL){ 189 printf("Z = %g; x1 = %g; x2 = %g; x3 = %g\n", 190 lp.primalValue(), 191 lp.primal(x1), lp.primal(x2), lp.primal(x3)); 192 } 193 else{ 194 std::cout<<"Optimal solution not found!"<<std::endl; 195 } 196 197 198 \endcode 155 \dontinclude lp_demo.cc 156 \skip A default solver is taken 157 \until End of LEMON style code 199 158 200 159 See the whole code in \ref lp_demo.cc. … … 211 170 in the memory. We will also omit the typedefs. 212 171 213 \code 214 //Define a map on the edges for the variables of the LP problem 215 typename G::template EdgeMap<LpDefault::Col> x(g); 216 lp.addColSet(x); 217 218 //Nonnegativity and capacity constraints 219 for(EdgeIt e(g);e!=INVALID;++e) { 220 lp.colUpperBound(x[e],cap[e]); 221 lp.colLowerBound(x[e],0); 222 } 172 \dontinclude lp_maxflow_demo.cc 173 \skip Define a map on the edges for the variables of the LP problem 174 \until lp.max(); 175 \skip Solve with the underlying solver 176 \until lp.solve(); 223 177 224 225 //Flow conservation constraints for the nodes (except for 's' and 't')226 for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {227 LpDefault::Expr ex;228 for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];229 for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];230 lp.addRow(ex==0);231 }232 233 //Objective function: the flow value entering 't'234 {235 LpDefault::Expr ex;236 for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e];237 for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];238 lp.setObj(ex);239 }240 241 //Maximization242 lp.max();243 244 //Solve with the underlying solver245 lp.solve();246 247 \endcode248 178 249 179 The complete program can be found in file \ref lp_maxflow_demo.cc. After compiling run it in the form:
Note: See TracChangeset
for help on using the changeset viewer.