[1033] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2010 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[1031] | 19 | #ifndef LEMON_OPT2_TSP_H |
---|
| 20 | #define LEMON_OPT2_TSP_H |
---|
| 21 | |
---|
[1033] | 22 | /// \ingroup tsp |
---|
| 23 | /// \file |
---|
[1034] | 24 | /// \brief 2-opt algorithm for symmetric TSP. |
---|
[1033] | 25 | |
---|
[1031] | 26 | #include <vector> |
---|
| 27 | #include <lemon/full_graph.h> |
---|
| 28 | |
---|
| 29 | namespace lemon { |
---|
[1033] | 30 | |
---|
[1034] | 31 | /// \ingroup tsp |
---|
| 32 | /// |
---|
[1033] | 33 | /// \brief 2-opt algorithm for symmetric TSP. |
---|
| 34 | /// |
---|
| 35 | /// Opt2Tsp implements the 2-opt heuristic for solving |
---|
| 36 | /// symmetric \ref tsp "TSP". |
---|
| 37 | /// |
---|
| 38 | /// This algorithm starts with an initial tour and iteratively improves it. |
---|
| 39 | /// At each step, it removes two edges and the reconnects the created two |
---|
| 40 | /// paths in the other way if the resulting tour is shorter. |
---|
| 41 | /// The algorithm finishes when no such 2-opt move can be applied, and so |
---|
| 42 | /// the tour is 2-optimal. |
---|
| 43 | /// |
---|
| 44 | /// If no starting tour is given to the \ref run() function, then the |
---|
| 45 | /// algorithm uses the node sequence determined by the node IDs. |
---|
| 46 | /// Oherwise, it starts with the given tour. |
---|
| 47 | /// |
---|
[1036] | 48 | /// This is a rather slow but effective method. |
---|
| 49 | /// Its typical usage is the improvement of the result of a fast tour |
---|
| 50 | /// construction heuristic (e.g. the InsertionTsp algorithm). |
---|
[1033] | 51 | /// |
---|
| 52 | /// \tparam CM Type of the cost map. |
---|
[1031] | 53 | template <typename CM> |
---|
[1033] | 54 | class Opt2Tsp |
---|
| 55 | { |
---|
| 56 | public: |
---|
| 57 | |
---|
| 58 | /// Type of the cost map |
---|
| 59 | typedef CM CostMap; |
---|
| 60 | /// Type of the edge costs |
---|
| 61 | typedef typename CM::Value Cost; |
---|
| 62 | |
---|
[1031] | 63 | private: |
---|
[1033] | 64 | |
---|
[1031] | 65 | GRAPH_TYPEDEFS(FullGraph); |
---|
| 66 | |
---|
[1033] | 67 | const FullGraph &_gr; |
---|
| 68 | const CostMap &_cost; |
---|
| 69 | Cost _sum; |
---|
| 70 | std::vector<int> _plist; |
---|
| 71 | std::vector<Node> _path; |
---|
| 72 | |
---|
[1031] | 73 | public: |
---|
[1033] | 74 | |
---|
| 75 | /// \brief Constructor |
---|
| 76 | /// |
---|
| 77 | /// Constructor. |
---|
| 78 | /// \param gr The \ref FullGraph "full graph" the algorithm runs on. |
---|
| 79 | /// \param cost The cost map. |
---|
| 80 | Opt2Tsp(const FullGraph &gr, const CostMap &cost) |
---|
| 81 | : _gr(gr), _cost(cost) {} |
---|
| 82 | |
---|
| 83 | /// \name Execution Control |
---|
| 84 | /// @{ |
---|
| 85 | |
---|
| 86 | /// \brief Runs the algorithm from scratch. |
---|
| 87 | /// |
---|
| 88 | /// This function runs the algorithm starting from the tour that is |
---|
| 89 | /// determined by the node ID sequence. |
---|
| 90 | /// |
---|
| 91 | /// \return The total cost of the found tour. |
---|
| 92 | Cost run() { |
---|
| 93 | _path.clear(); |
---|
| 94 | |
---|
| 95 | if (_gr.nodeNum() == 0) return _sum = 0; |
---|
| 96 | else if (_gr.nodeNum() == 1) { |
---|
| 97 | _path.push_back(_gr(0)); |
---|
| 98 | return _sum = 0; |
---|
[1031] | 99 | } |
---|
[1033] | 100 | else if (_gr.nodeNum() == 2) { |
---|
| 101 | _path.push_back(_gr(0)); |
---|
| 102 | _path.push_back(_gr(1)); |
---|
| 103 | return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))]; |
---|
| 104 | } |
---|
[1031] | 105 | |
---|
[1033] | 106 | _plist.resize(2*_gr.nodeNum()); |
---|
| 107 | for (int i = 1; i < _gr.nodeNum()-1; ++i) { |
---|
| 108 | _plist[2*i] = i-1; |
---|
| 109 | _plist[2*i+1] = i+1; |
---|
[1031] | 110 | } |
---|
[1033] | 111 | _plist[0] = _gr.nodeNum()-1; |
---|
| 112 | _plist[1] = 1; |
---|
| 113 | _plist[2*_gr.nodeNum()-2] = _gr.nodeNum()-2; |
---|
| 114 | _plist[2*_gr.nodeNum()-1] = 0; |
---|
| 115 | |
---|
| 116 | return start(); |
---|
[1031] | 117 | } |
---|
| 118 | |
---|
[1034] | 119 | /// \brief Runs the algorithm starting from the given tour. |
---|
[1033] | 120 | /// |
---|
| 121 | /// This function runs the algorithm starting from the given tour. |
---|
| 122 | /// |
---|
| 123 | /// \param tour The tour as a path structure. It must be a |
---|
| 124 | /// \ref checkPath() "valid path" containing excactly n arcs. |
---|
| 125 | /// |
---|
| 126 | /// \return The total cost of the found tour. |
---|
| 127 | template <typename Path> |
---|
| 128 | Cost run(const Path& tour) { |
---|
| 129 | _path.clear(); |
---|
| 130 | |
---|
| 131 | if (_gr.nodeNum() == 0) return _sum = 0; |
---|
| 132 | else if (_gr.nodeNum() == 1) { |
---|
| 133 | _path.push_back(_gr(0)); |
---|
| 134 | return _sum = 0; |
---|
| 135 | } |
---|
| 136 | else if (_gr.nodeNum() == 2) { |
---|
| 137 | _path.push_back(_gr(0)); |
---|
| 138 | _path.push_back(_gr(1)); |
---|
| 139 | return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))]; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | _plist.resize(2*_gr.nodeNum()); |
---|
| 143 | typename Path::ArcIt it(tour); |
---|
| 144 | int first = _gr.id(_gr.source(it)), |
---|
| 145 | prev = first, |
---|
| 146 | curr = _gr.id(_gr.target(it)), |
---|
| 147 | next = -1; |
---|
| 148 | _plist[2*first+1] = curr; |
---|
| 149 | for (++it; it != INVALID; ++it) { |
---|
| 150 | next = _gr.id(_gr.target(it)); |
---|
| 151 | _plist[2*curr] = prev; |
---|
| 152 | _plist[2*curr+1] = next; |
---|
| 153 | prev = curr; |
---|
| 154 | curr = next; |
---|
| 155 | } |
---|
| 156 | _plist[2*first] = prev; |
---|
| 157 | |
---|
| 158 | return start(); |
---|
| 159 | } |
---|
| 160 | |
---|
[1034] | 161 | /// \brief Runs the algorithm starting from the given tour. |
---|
[1033] | 162 | /// |
---|
[1034] | 163 | /// This function runs the algorithm starting from the given tour |
---|
| 164 | /// (node sequence). |
---|
[1033] | 165 | /// |
---|
[1034] | 166 | /// \param tour A vector that stores all <tt>Node</tt>s of the graph |
---|
| 167 | /// in the desired order. |
---|
[1033] | 168 | /// |
---|
| 169 | /// \return The total cost of the found tour. |
---|
[1034] | 170 | Cost run(const std::vector<Node>& tour) { |
---|
[1033] | 171 | _path.clear(); |
---|
| 172 | |
---|
| 173 | if (_gr.nodeNum() == 0) return _sum = 0; |
---|
| 174 | else if (_gr.nodeNum() == 1) { |
---|
| 175 | _path.push_back(_gr(0)); |
---|
| 176 | return _sum = 0; |
---|
| 177 | } |
---|
| 178 | else if (_gr.nodeNum() == 2) { |
---|
| 179 | _path.push_back(_gr(0)); |
---|
| 180 | _path.push_back(_gr(1)); |
---|
| 181 | return _sum = 2 * _cost[_gr.edge(_gr(0), _gr(1))]; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | _plist.resize(2*_gr.nodeNum()); |
---|
[1034] | 185 | typename std::vector<Node>::const_iterator it = tour.begin(); |
---|
[1033] | 186 | int first = _gr.id(*it), |
---|
| 187 | prev = first, |
---|
| 188 | curr = _gr.id(*(++it)), |
---|
| 189 | next = -1; |
---|
| 190 | _plist[2*first+1] = curr; |
---|
| 191 | for (++it; it != tour.end(); ++it) { |
---|
| 192 | next = _gr.id(*it); |
---|
| 193 | _plist[2*curr] = prev; |
---|
| 194 | _plist[2*curr+1] = next; |
---|
| 195 | prev = curr; |
---|
| 196 | curr = next; |
---|
| 197 | } |
---|
| 198 | _plist[2*first] = curr; |
---|
| 199 | _plist[2*curr] = prev; |
---|
| 200 | _plist[2*curr+1] = first; |
---|
| 201 | |
---|
| 202 | return start(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /// @} |
---|
| 206 | |
---|
| 207 | /// \name Query Functions |
---|
| 208 | /// @{ |
---|
| 209 | |
---|
| 210 | /// \brief The total cost of the found tour. |
---|
| 211 | /// |
---|
| 212 | /// This function returns the total cost of the found tour. |
---|
| 213 | /// |
---|
| 214 | /// \pre run() must be called before using this function. |
---|
| 215 | Cost tourCost() const { |
---|
| 216 | return _sum; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /// \brief Returns a const reference to the node sequence of the |
---|
| 220 | /// found tour. |
---|
| 221 | /// |
---|
[1034] | 222 | /// This function returns a const reference to a vector |
---|
[1033] | 223 | /// that stores the node sequence of the found tour. |
---|
| 224 | /// |
---|
| 225 | /// \pre run() must be called before using this function. |
---|
| 226 | const std::vector<Node>& tourNodes() const { |
---|
| 227 | return _path; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /// \brief Gives back the node sequence of the found tour. |
---|
| 231 | /// |
---|
| 232 | /// This function copies the node sequence of the found tour into |
---|
[1037] | 233 | /// an STL container through the given output iterator. The |
---|
| 234 | /// <tt>value_type</tt> of the container must be <tt>FullGraph::Node</tt>. |
---|
| 235 | /// For example, |
---|
| 236 | /// \code |
---|
| 237 | /// std::vector<FullGraph::Node> nodes(countNodes(graph)); |
---|
| 238 | /// tsp.tourNodes(nodes.begin()); |
---|
| 239 | /// \endcode |
---|
| 240 | /// or |
---|
| 241 | /// \code |
---|
| 242 | /// std::list<FullGraph::Node> nodes; |
---|
| 243 | /// tsp.tourNodes(std::back_inserter(nodes)); |
---|
| 244 | /// \endcode |
---|
[1033] | 245 | /// |
---|
| 246 | /// \pre run() must be called before using this function. |
---|
[1037] | 247 | template <typename Iterator> |
---|
| 248 | void tourNodes(Iterator out) const { |
---|
| 249 | std::copy(_path.begin(), _path.end(), out); |
---|
[1033] | 250 | } |
---|
| 251 | |
---|
| 252 | /// \brief Gives back the found tour as a path. |
---|
| 253 | /// |
---|
| 254 | /// This function copies the found tour as a list of arcs/edges into |
---|
| 255 | /// the given \ref concept::Path "path structure". |
---|
| 256 | /// |
---|
| 257 | /// \pre run() must be called before using this function. |
---|
| 258 | template <typename Path> |
---|
| 259 | void tour(Path &path) const { |
---|
| 260 | path.clear(); |
---|
| 261 | for (int i = 0; i < int(_path.size()) - 1; ++i) { |
---|
| 262 | path.addBack(_gr.arc(_path[i], _path[i+1])); |
---|
| 263 | } |
---|
| 264 | if (int(_path.size()) >= 2) { |
---|
| 265 | path.addBack(_gr.arc(_path.back(), _path.front())); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | /// @} |
---|
| 270 | |
---|
[1031] | 271 | private: |
---|
[1033] | 272 | |
---|
| 273 | // Iterator class for the linked list storage of the tour |
---|
| 274 | class PathListIt { |
---|
[1031] | 275 | public: |
---|
[1033] | 276 | PathListIt(const std::vector<int> &pl, int i=0) |
---|
| 277 | : plist(&pl), act(i), last(pl[2*act]) {} |
---|
| 278 | PathListIt(const std::vector<int> &pl, int i, int l) |
---|
| 279 | : plist(&pl), act(i), last(l) {} |
---|
[1031] | 280 | |
---|
[1033] | 281 | int nextIndex() const { |
---|
| 282 | return (*plist)[2*act] == last ? 2*act+1 : 2*act; |
---|
[1031] | 283 | } |
---|
[1033] | 284 | |
---|
| 285 | int prevIndex() const { |
---|
| 286 | return (*plist)[2*act] == last ? 2*act : 2*act+1; |
---|
[1031] | 287 | } |
---|
[1033] | 288 | |
---|
[1031] | 289 | int next() const { |
---|
[1033] | 290 | int x = (*plist)[2*act]; |
---|
| 291 | return x == last ? (*plist)[2*act+1] : x; |
---|
[1031] | 292 | } |
---|
| 293 | |
---|
| 294 | int prev() const { |
---|
[1033] | 295 | return last; |
---|
[1031] | 296 | } |
---|
[1033] | 297 | |
---|
| 298 | PathListIt& operator++() { |
---|
[1031] | 299 | int tmp = act; |
---|
| 300 | act = next(); |
---|
| 301 | last = tmp; |
---|
| 302 | return *this; |
---|
| 303 | } |
---|
[1033] | 304 | |
---|
[1031] | 305 | operator int() const { |
---|
| 306 | return act; |
---|
| 307 | } |
---|
[1033] | 308 | |
---|
[1031] | 309 | private: |
---|
[1033] | 310 | const std::vector<int> *plist; |
---|
[1031] | 311 | int act; |
---|
| 312 | int last; |
---|
| 313 | }; |
---|
| 314 | |
---|
[1033] | 315 | // Checks and applies 2-opt move (if it improves the tour) |
---|
| 316 | bool checkOpt2(const PathListIt& i, const PathListIt& j) { |
---|
| 317 | Node u = _gr.nodeFromId(i), |
---|
| 318 | un = _gr.nodeFromId(i.next()), |
---|
| 319 | v = _gr.nodeFromId(j), |
---|
| 320 | vn = _gr.nodeFromId(j.next()); |
---|
[1031] | 321 | |
---|
[1033] | 322 | if (_cost[_gr.edge(u, un)] + _cost[_gr.edge(v, vn)] > |
---|
| 323 | _cost[_gr.edge(u, v)] + _cost[_gr.edge(un, vn)]) |
---|
| 324 | { |
---|
| 325 | _plist[PathListIt(_plist, i.next(), i).prevIndex()] = j.next(); |
---|
| 326 | _plist[PathListIt(_plist, j.next(), j).prevIndex()] = i.next(); |
---|
[1031] | 327 | |
---|
[1033] | 328 | _plist[i.nextIndex()] = j; |
---|
| 329 | _plist[j.nextIndex()] = i; |
---|
[1031] | 330 | |
---|
[1033] | 331 | return true; |
---|
[1031] | 332 | } |
---|
[1033] | 333 | |
---|
[1031] | 334 | return false; |
---|
[1033] | 335 | } |
---|
[1031] | 336 | |
---|
[1033] | 337 | // Executes the algorithm from the initial tour |
---|
| 338 | Cost start() { |
---|
[1031] | 339 | |
---|
[1033] | 340 | restart_search: |
---|
| 341 | for (PathListIt i(_plist); true; ++i) { |
---|
| 342 | PathListIt j = i; |
---|
| 343 | if (++j == 0 || ++j == 0) break; |
---|
| 344 | for (; j != 0 && j != i.prev(); ++j) { |
---|
| 345 | if (checkOpt2(i, j)) |
---|
| 346 | goto restart_search; |
---|
[1031] | 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
[1033] | 350 | PathListIt i(_plist); |
---|
| 351 | _path.push_back(_gr.nodeFromId(i)); |
---|
| 352 | for (++i; i != 0; ++i) |
---|
| 353 | _path.push_back(_gr.nodeFromId(i)); |
---|
[1031] | 354 | |
---|
[1033] | 355 | _sum = _cost[_gr.edge(_path.back(), _path.front())]; |
---|
| 356 | for (int i = 0; i < int(_path.size())-1; ++i) { |
---|
| 357 | _sum += _cost[_gr.edge(_path[i], _path[i+1])]; |
---|
| 358 | } |
---|
[1031] | 359 | |
---|
| 360 | return _sum; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | }; |
---|
| 364 | |
---|
| 365 | }; // namespace lemon |
---|
| 366 | |
---|
| 367 | #endif |
---|