| 1 | #ifndef BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H |
| 2 | #define BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H |
| 3 | |
| 4 | #ifndef BALL_COMMON_GLOBAL_H |
| 5 | # include <BALL/COMMON/global.h> |
| 6 | #endif |
| 7 | |
| 8 | #ifndef BALL_MATHS_COMMON_H |
| 9 | # include <BALL/MATHS/common.h> |
| 10 | #endif |
| 11 | |
| 12 | #ifndef BALL_KERNEL_ATOMCONTAINER_H |
| 13 | # include <BALL/KERNEL/atomContainer.h> |
| 14 | #endif |
| 15 | |
| 16 | #ifndef BALL_KERNEL_BOND_H |
| 17 | # include <BALL/KERNEL/bond.h> |
| 18 | #endif |
| 19 | |
| 20 | #ifndef BALL_DATATYPE_HASHMAP_H |
| 21 | # include <BALL/DATATYPE/hashMap.h> |
| 22 | #endif |
| 23 | |
| 24 | #ifndef BALL_DATATYPE_GRAPH_H |
| 25 | # include <BALL/DATATYPE/GRAPH/molecularGraph.h> |
| 26 | #endif |
| 27 | |
| 28 | #ifndef BALL_DATATYPE_GRAPH_GRAPHALGORITHMS_H |
| 29 | # include <BALL/DATATYPE/GRAPH/graphAlgorithms.h> |
| 30 | #endif |
| 31 | |
| 32 | #ifndef BALL_DATATYPE_GRAPH_TREEWIDTH_H |
| 33 | # include <BALL/DATATYPE/GRAPH/treeWidth.h> |
| 34 | #endif |
| 35 | |
| 36 | #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENTSTRATEGY_H |
| 37 | # include <BALL/STRUCTURE/BONDORDERS/bondOrderAssignmentStrategy.h> |
| 38 | #endif |
| 39 | |
| 40 | #ifndef BALL_STRUCTURE_BONDORDERS_BONDORDERASSIGNMENT_H |
| 41 | # include <BALL/STRUCTURE/BONDORDERS/bondOrderAssignment.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <algorithm> |
| 45 | #include <map> |
| 46 | #include <set> |
| 47 | #include <vector> |
| 48 | #include <stack> |
| 49 | #include <iterator> |
| 50 | #include <queue> |
| 51 | |
| 52 | #include <boost/shared_ptr.hpp> |
| 53 | #include <boost/ref.hpp> |
| 54 | |
| 55 | namespace BALL |
| 56 | { |
| 57 | //TODO: documentation is obsolete! |
| 58 | /** FPT algorithm for bond order assignment. |
| 59 | * |
| 60 | * This class implements a fixed parameter tractability approach |
| 61 | * for the bond order assignment problem that can be used by |
| 62 | * the \link AssignBondOrderProcessor AssignBondOrderProcessor \endlink. |
| 63 | * |
| 64 | * It handles the creation of the nice tree decomposition, the bond assignment computing and the backtracking. |
| 65 | * This class is the only one in the dynamic programming algorithm which expects a molecule. |
| 66 | * All other classes works with graphs (which doesn't have atoms or bonds, but just vertices and edges). |
| 67 | * So this class close the gap between the algorithmic bond order problem and it's practical meaning. |
| 68 | * The most classes of the bond order algorithm doesn't manage their dependencies, so it's important to delete |
| 69 | * them in the correct order. This class wraps about them and manages the memory of their instances itself. |
| 70 | * So it's the best to forget, that their are other classes than this: Just use this class if you want to assign |
| 71 | * bond orders by the dynamic programming algorithm. |
| 72 | * |
| 73 | * The class has a default constructor and can be copied and assigned. It saves it's dynamic programming table and the |
| 74 | * nice tree decomposition in an extra data structure which is accessed by shared pointers. So you can copy instances |
| 75 | * of this class without copying all the algorithm data! Nevertheless you have to copy the backtracking combiner and all |
| 76 | * it's remembered solutions. |
| 77 | * |
| 78 | * After setting the atom container and the penalty map, you have to call the #start method to start the computing. |
| 79 | * After that, the instance of this class behaves like an Ball iterator (although it doesn't inherited from it and |
| 80 | * doesn't provide all it's functions). You can iterate about each solution by calling ++, and you can check for |
| 81 | * further solutions by calling +. |
| 82 | * |
| 83 | * @code |
| 84 | * FPTBondOrderStrategy algorithm; |
| 85 | * algorithm.setMolecule(someAtomContainer); |
| 86 | * algorithm.setPenaltyMap(somePenaltyMap); |
| 87 | * algorithm.start(); |
| 88 | * for (; +algorithm; ++algorithm) |
| 89 | * { |
| 90 | * cout << algorithm->penalty; |
| 91 | * } |
| 92 | * @endcode |
| 93 | * |
| 94 | * Dereferencing an algorithm instance means: return the result of the last computed solution. The result is |
| 95 | * an Assignment instance. So it doesn't give information about the concrete bonds, if you don't know their indizes. |
| 96 | * There are two ways to handle this: You cann call #getBonds to get a vector with pointers to the bonds. The order |
| 97 | * of the bonds in the vector is the same as the order of the bond values on the assignments. |
| 98 | * Another way is to call #getBondAssignmentHashMap. This returns a HashMap which maps each bond to it's bond value. |
| 99 | * This structure is nearly the same as AssignBondOrderProcessor is using, just with the different that the pointers |
| 100 | * references constant bonds. |
| 101 | * |
| 102 | * An important difference to the AssignBondOrderProcessor is that a bond value of 0 means "single bond", a bond |
| 103 | * value of 1 means "double bond" and a bond value of 2 "tripple bound". So you have to increase the bond values |
| 104 | * by 1 if you want to use them in AssignBondOrderProcessor or in the Bond class itself. |
| 105 | * |
| 106 | * Beside the iterator-like functions you can also call the same functions you know from the DPBackTracking and |
| 107 | * DPBackTrackingCombiner class. But other than DPBackTracking this class computes the best solution after |
| 108 | * calling #start without the need of calling #nextSolution. |
| 109 | * |
| 110 | * An DPBondAssignmentStrategy instance which hasn't started behaves like an iterator which isn't bound to a container. |
| 111 | * You can return the instance into this state by calling #reset. Also changing the molecule or penalty map also |
| 112 | * reset the instance. Calling other methods than #start, #reset, #setMolecule, #getMolecule, #setPenaltyMap, |
| 113 | * #getPenaltyMap, #setUpperbound, #getUpperbound, #setNumberOfSolutions, #getNumberOfSolutions will throw an |
| 114 | * InvalidIterator exception if you forget to call #start before. |
| 115 | * |
| 116 | * The DPBondAssignmentStrategy expect that the atom container and the penalty map aren't deleted during the |
| 117 | * instances' lifetime. If you want do delete one of them, reset the instance and set their atom container and penalty |
| 118 | * map to NULL. |
| 119 | * |
| 120 | */ |
| 121 | class BALL_EXPORT FPTBondOrderStrategy |
| 122 | : public BondOrderAssignmentStrategy |
| 123 | { |
| 124 | public: |
| 125 | typedef GRAPH::GraphTraits<MolecularGraph>::EdgeType Edge; |
| 126 | typedef GRAPH::GraphTraits<MolecularGraph>::VertexType VertexType; |
| 127 | |
| 128 | typedef TreeWidth<MolecularGraph>::TreeDecomposition TreeDecomposition; |
| 129 | typedef TreeWidth<MolecularGraph>::TreeDecompositionBag TreeDecompositionBag; |
| 130 | |
| 131 | friend class FPTBondOrderAssignment_; |
| 132 | |
| 133 | /** |
| 134 | * Penalties are represented as floats |
| 135 | */ |
| 136 | typedef float Penalty; |
| 137 | |
| 138 | /** |
| 139 | * The valence-type for atoms is represented as integer, although it shouldn't exceed a small |
| 140 | * constant value (typical 8) |
| 141 | */ |
| 142 | typedef int Valence; |
| 143 | |
| 144 | /** |
| 145 | * The BondOrder type is represented as integer, although it shouldn't exceed a small constant |
| 146 | * value (typical 3) |
| 147 | */ |
| 148 | typedef int BondOrder; |
| 149 | |
| 150 | /** @name Constant Definitions |
| 151 | */ |
| 152 | static const Penalty infinite_penalty; |
| 153 | static const Valence max_valence; |
| 154 | |
| 155 | //@{ |
| 156 | /// Option names |
| 157 | struct BALL_EXPORT Option |
| 158 | { |
| 159 | /** The maximum penalty for which this FPT strategy will compute solutions. |
| 160 | * Setting this to a value different from infinite_penalty can significantly |
| 161 | * speed up the computation. |
| 162 | */ |
| 163 | static String UPPER_PENALTY_BOUND; |
| 164 | }; |
| 165 | |
| 166 | /// Default option values |
| 167 | struct BALL_EXPORT Default |
| 168 | { |
| 169 | /** The maximum penalty for which this FPT strategy will compute solutions. |
| 170 | * Setting this to a value different from infinite_penalty can significantly |
| 171 | * speed up the computation. |
| 172 | */ |
| 173 | static Penalty UPPER_PENALTY_BOUND; |
| 174 | }; |
| 175 | |
| 176 | //@} |
| 177 | |
| 178 | FPTBondOrderStrategy(AssignBondOrderProcessor* parent); |
| 179 | |
| 180 | /** |
| 181 | * Destructor. Deletes the backtracking. The nice tree decomposition and the dynamic programming tables are |
| 182 | * deleted if there are no other FPTBondOrderStrategy instances which access them. |
| 183 | */ |
| 184 | virtual ~FPTBondOrderStrategy(); |
| 185 | |
| 186 | /** |
| 187 | * delete all previous computings and frees the nice tree decomposition and penalty map, if they are not |
| 188 | * referenced by another FPTBondOrderStrategy. |
| 189 | * After this method you can not access the solutions. Call #start again to compute the bond order. |
| 190 | */ |
| 191 | virtual void clear(); |
| 192 | |
| 193 | /** |
| 194 | * Start the bond assignment computing. Computes the nice tree decomposition and the dynamic programming table. |
| 195 | * After calling this method, you can access the solutions |
| 196 | * @throw BALL::Exception::NullPointer if the atom container or penalty map aren't set |
| 197 | */ |
| 198 | virtual void init(); |
| 199 | |
| 200 | virtual bool readOptions(const Options& options); |
| 201 | virtual void setDefaultOptions(); |
| 202 | |
| 203 | /** |
| 204 | * Backtracks the next best solution. Call #hasNextSolution first to avoid an OutOfRange exception |
| 205 | * @throw BALL::Exception::OutOfRange if there are no more solutions to backtrack |
| 206 | * @throw BALL::Exception::InvalidIterator if #start is not called |
| 207 | */ |
| 208 | virtual boost::shared_ptr<BondOrderAssignment> computeNextSolution(); |
| 209 | |
| 210 | protected: |
| 211 | /** |
| 212 | * A DPConfig_ is an entry in a dynamic programming table. It holds the current bond-order assignments, |
| 213 | * and the valences, which were consumed after forgetting some bonds in forget-nodes. |
| 214 | * Each DPConfig belongs to just one node in the nice tree decomposition. So it contains just the valences |
| 215 | * and bondvalues of the atoms and bonds in the bag. |
| 216 | * DPConfigs are computed in Leaf-nodes or by one (or two) ancestor tables by |
| 217 | * forgetting, introducing or merging their entries. So even if a DPConfig is empty (as in the root table), you can |
| 218 | * get the assignment of all ancestors by backtracking the computing of this DPConfig. |
| 219 | */ |
| 220 | class DPConfig_ |
| 221 | { |
| 222 | public: |
| 223 | /** |
| 224 | * Default constructor |
| 225 | */ |
| 226 | DPConfig_(); |
| 227 | |
| 228 | /** |
| 229 | * Builds a new DPConfig with the given number of atoms and bonds. The valence and bond values |
| 230 | * are filled with 0. |
| 231 | */ |
| 232 | DPConfig_(Size atoms, Size bonds); |
| 233 | |
| 234 | /** |
| 235 | * Builds a new DPConfig with the given atom-valences and bond-values |
| 236 | */ |
| 237 | DPConfig_(std::vector<Valence> const& v, std::vector<BondOrder> const& bo); |
| 238 | |
| 239 | /** |
| 240 | * Assignment operator |
| 241 | */ |
| 242 | DPConfig_& operator=(DPConfig_ const& copy); |
| 243 | |
| 244 | /** |
| 245 | * Builds a new DPConfig and initialize its valences and bond-values with the values which are iterated by the given iterators |
| 246 | */ |
| 247 | template<typename ValenceIterator, typename BondIterator> |
| 248 | DPConfig_(ValenceIterator vit, ValenceIterator vend, BondIterator boit, BondIterator boend) |
| 249 | : consumed_valences(vit, vend), |
| 250 | bond_assignments(boit, boend) |
| 251 | { |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @see #compare |
| 256 | */ |
| 257 | bool operator < (DPConfig_ const& conf) const; |
| 258 | |
| 259 | /** |
| 260 | * @see #compare |
| 261 | */ |
| 262 | bool operator > (DPConfig_ const& conf) const; |
| 263 | |
| 264 | /** |
| 265 | * @see #compare |
| 266 | */ |
| 267 | bool operator <= (DPConfig_ const& conf) const; |
| 268 | |
| 269 | /** |
| 270 | * @see #compare |
| 271 | */ |
| 272 | bool operator >= (DPConfig_ const& conf) const; |
| 273 | |
| 274 | /** |
| 275 | * @see #compare |
| 276 | */ |
| 277 | bool operator == (DPConfig_ const& conf) const; |
| 278 | |
| 279 | /** |
| 280 | * Comparison is needed to insert a DPConfig into a tree-map. A DPConfig is |
| 281 | * equal to another DPConfig if it's valences and bond-values are equal. |
| 282 | * Other comparisons are done by comparing each valence and bond value seperately. |
| 283 | * You can compare only entries from the same table or from tables which belong to the same |
| 284 | * nice tree decomposition bag. The number of valences and bounds have to be equal. |
| 285 | * @param other another entry of the same table |
| 286 | * @return 1, if this is greather than other, 0, if both entries are equal, otherwise -1. |
| 287 | * @throws BALL::Exception::InvalidArgument if two entries of different tables are compared |
| 288 | */ |
| 289 | int compare(DPConfig_ const& other) const; |
| 290 | |
| 291 | /** |
| 292 | * returns the number of atoms of the bag this entry belongs to. |
| 293 | */ |
| 294 | Size numberOfAtoms() const; |
| 295 | |
| 296 | /** |
| 297 | * returns the number of bonds of the bag this entry belongs to. |
| 298 | */ |
| 299 | Size numberOfBonds() const; |
| 300 | |
| 301 | /** |
| 302 | * Every time a bond is forgotten, the consumed valences of its incident vertices are increased by its bond value + 1. |
| 303 | * If a atom is forgotten, it's consumed valences holds the final valence assignment of this atom. |
| 304 | */ |
| 305 | std::vector<Valence> consumed_valences; |
| 306 | |
| 307 | /** |
| 308 | * Holds the current assigned bond values. |
| 309 | * Every time a new bond is visited in an introduce node, entries for each possible bond value of this bond |
| 310 | * are inserted into the table. |
| 311 | * Single bonds habe a bond value of 0, double bonds have the bond value 1 and tripple bonds have the bond value 2. |
| 312 | * So increasing the valences means adding the bond value + 1 (!) to the valence value. |
| 313 | */ |
| 314 | std::vector<BondOrder> bond_assignments; |
| 315 | |
| 316 | }; |
| 317 | |
| 318 | /** |
| 319 | * A single row in a DPTable, which consists of the DPConfig (valences and bond values) and the penalty, which was |
| 320 | * computed for the DPConfig and it's ancestors. |
| 321 | */ |
| 322 | typedef std::pair<boost::reference_wrapper<DPConfig_>, Penalty> DPRow_; |
| 323 | |
| 324 | /** |
| 325 | * After computing a DPTable, we don't modify it's entries (because we need them for backtracking). So usually we |
| 326 | * work with const references to the table entries |
| 327 | */ |
| 328 | typedef std::pair<boost::reference_wrapper<DPConfig_ const>, Penalty> DPConstRow_; |
| 329 | |
| 330 | /** |
| 331 | * Is used to save a reference to a const DPConfig in an object (which isn't |
| 332 | * possible with references, because they are constant and would prevent it's |
| 333 | * adding into collections) |
| 334 | */ |
| 335 | typedef std::pair<DPConfig_*, Penalty> DPPointerRow_; |
| 336 | |
| 337 | /** |
| 338 | * A map which gives fast access to the penalty of a given DPConfig. Is used to compare the penalty of two DPConfigs or to |
| 339 | * iterate above all table entries. |
| 340 | */ |
| 341 | typedef std::map<DPConfig_, Penalty> DPMap_; |
| 342 | |
| 343 | /** |
| 344 | * The dynamic programming table. |
| 345 | * Each nice tree decomposition bag has it's own dynamic programming table. They are computed either from leaf-nodes, by |
| 346 | * forgetting or introducing entries from ancestor-tables in forget/introduce nodes or by merging two tables into one in |
| 347 | * join-nodes. The rows in this table are different possible bond assignments for the molecule graph. |
| 348 | * It's columns are the consumed valences of the bag's atoms and bond values of the bag's bonds. |
| 349 | */ |
| 350 | class DPTable_ |
| 351 | { |
| 352 | protected: |
| 353 | /** |
| 354 | * the table data is represented as a tree map, because this allow fast inserting of DPConfigs and removing |
| 355 | * duplicates with greater penalty. |
| 356 | */ |
| 357 | DPMap_ table; |
| 358 | |
| 359 | public: |
| 360 | /** |
| 361 | * Default constructor |
| 362 | */ |
| 363 | DPTable_(); |
| 364 | |
| 365 | /** |
| 366 | * Copy constructor |
| 367 | */ |
| 368 | DPTable_(DPTable_ const& table); |
| 369 | |
| 370 | /** |
| 371 | * iterator above the table entries |
| 372 | */ |
| 373 | typedef DPMap_::iterator iterator; |
| 374 | |
| 375 | /** |
| 376 | * constant iterator above the table entries |
| 377 | */ |
| 378 | typedef DPMap_::const_iterator const_iterator; |
| 379 | |
| 380 | /** |
| 381 | * returns the penalty of a given DPConfig |
| 382 | */ |
| 383 | Penalty operator[](DPConfig_ const& config) const; |
| 384 | |
| 385 | /** |
| 386 | * insert a new DPConfig with the given penalty. If the table already contains an equal DPConfig |
| 387 | * with a lower penalty, this function does nothing. |
| 388 | */ |
| 389 | bool insert(DPConfig_ const& config, Penalty penalty); |
| 390 | |
| 391 | /** |
| 392 | * returns the number of rows in this table |
| 393 | */ |
| 394 | Size size() const; |
| 395 | |
| 396 | /** |
| 397 | * returns the best penalty in this table or infinite_penalty, if the table is empty |
| 398 | */ |
| 399 | Penalty bestPenalty() const; |
| 400 | |
| 401 | /** |
| 402 | * returns the entry with best penalty. Because this function throws an exception if the table |
| 403 | * is empty, you should check this by calling #size() before calling this function. |
| 404 | * @throw BALL::Exception::IndexOverflow if table is empty |
| 405 | */ |
| 406 | DPConstRow_ bestEntry() const; |
| 407 | |
| 408 | /** |
| 409 | * returns an iterator to the first entry. |
| 410 | */ |
| 411 | iterator begin(); |
| 412 | |
| 413 | /** |
| 414 | * returns an iterator to the end of the table |
| 415 | */ |
| 416 | iterator end(); |
| 417 | |
| 418 | /** |
| 419 | * returns a constant iterator to the first entry |
| 420 | */ |
| 421 | const_iterator begin() const; |
| 422 | |
| 423 | /** |
| 424 | * returns a constant iterator to the end of the table |
| 425 | */ |
| 426 | const_iterator end() const; |
| 427 | }; |
| 428 | |
| 429 | /** |
| 430 | * Contains informations about a bag which are computed during the |
| 431 | * bond assignment algorithm. Later, this informations are used by the |
| 432 | * backtracking. |
| 433 | */ |
| 434 | class AdditionalBagProperties_ |
| 435 | { |
| 436 | public: |
| 437 | /** |
| 438 | * Default constructor |
| 439 | */ |
| 440 | AdditionalBagProperties_(); |
| 441 | |
| 442 | /** |
| 443 | * Copy constructor |
| 444 | */ |
| 445 | AdditionalBagProperties_(AdditionalBagProperties_ const& copy); |
| 446 | |
| 447 | /** |
| 448 | * Assignment operator |
| 449 | */ |
| 450 | AdditionalBagProperties_& operator=(AdditionalBagProperties_ const& copy); |
| 451 | |
| 452 | /** |
| 453 | * Destructor |
| 454 | */ |
| 455 | ~AdditionalBagProperties_(); |
| 456 | |
| 457 | /** |
| 458 | * bonds in this bag |
| 459 | */ |
| 460 | std::vector<MolecularGraphTraits::EdgeType> bonds; |
| 461 | |
| 462 | /** |
| 463 | * The table of this bag. Is computed during the bond assignment |
| 464 | */ |
| 465 | DPTable_ * table; |
| 466 | }; |
| 467 | |
| 468 | class DPBackTracking_; |
| 469 | class DPBackTrackingCombiner_; |
| 470 | |
| 471 | /** |
| 472 | * bond assignment algorithm. |
| 473 | * Traverse in post-order the nice tree decomposition and computes the dynamic programming table for each vertex |
| 474 | * This class uses a pointer to a graph, to a penalty map and to a nice tree decomposition. So you should take care that |
| 475 | * all three objects aren't deleted during this algorithm, because this class neither uses shared pointers, nor make |
| 476 | * copies of the objects. |
| 477 | */ |
| 478 | class FPTBondOrderAssignment_ |
| 479 | { |
| 480 | public: |
| 481 | friend class DPBackTracking_; |
| 482 | friend class DPBackTrackingCombiner_; |
| 483 | friend class GRAPH::PostOrderFolding<TreeDecomposition, TreeDecompositionBag, DPTable_*, FPTBondOrderAssignment_>; |
| 484 | |
| 485 | /** |
| 486 | * Construct a new FPTBondOrder with the given molecule, a PenaltyMap and a built nice tree decomposition |
| 487 | * @param parent the bond order assignment algorithm |
| 488 | * @param ntd a nice tree decomposition of the graph |
| 489 | * @param upperbound the algorithm will only compute solutions which are better than the upperbound. By default |
| 490 | * the upperbound is infinity. |
| 491 | */ |
| 492 | FPTBondOrderAssignment_(FPTBondOrderStrategy& parent, boost::shared_ptr<TreeDecomposition>& ntd, |
| 493 | Penalty upper_bound = infinite_penalty); |
| 494 | |
| 495 | /** |
| 496 | * Destructor. Just delete the AdditionalBagProperties_, which were computed during this algorithm. So don't |
| 497 | * delete this instance before backtracking. Furthermore this destructor doesn't delete the nice tree decomposition |
| 498 | * and the penalty map. You have to take care for this yourself. |
| 499 | */ |
| 500 | ~FPTBondOrderAssignment_(); |
| 501 | |
| 502 | /** |
| 503 | * Call this to start the bond assignment computing. This function returns the optimal penalty for the |
| 504 | * graph. If you want to know the bond order assignment or the penalties of other solutions, you have to |
| 505 | * start the backtracking by DPBackTracking. |
| 506 | */ |
| 507 | Penalty compute(); |
| 508 | |
| 509 | protected: |
| 510 | /** |
| 511 | * A pointer to our parent |
| 512 | */ |
| 513 | FPTBondOrderStrategy* parent_; |
| 514 | |
| 515 | /** |
| 516 | * the graph with the bonds, which were assigned to a bond value during this algorithm |
| 517 | */ |
| 518 | MolecularGraph * molecule_; |
| 519 | |
| 520 | /** |
| 521 | * The nice tree decomposition which is built from the graph |
| 522 | */ |
| 523 | boost::shared_ptr<TreeDecomposition> ntd_; |
| 524 | |
| 525 | /** |
| 526 | * A vector of AdditionalBagProperties_. Contains the bonds and the dynamic programming tables for each vertex |
| 527 | * in the nice tree decomposition |
| 528 | */ |
| 529 | vector<AdditionalBagProperties_> properties_; |
| 530 | |
| 531 | /** |
| 532 | * The algorithm will just compute solutions which are better than the upperbound (they have to be BETTER, |
| 533 | * not equal, so "upperbound" is maybe not the best word for it). |
| 534 | * The upperbound is infinite_penalty by default, but you can change this if you know that your molecule |
| 535 | * has a better solution. This can improve the performance - but usually the algorithm is fast enough to |
| 536 | * compute without an upperbound. |
| 537 | */ |
| 538 | Penalty upper_bound_; |
| 539 | |
| 540 | /** |
| 541 | * The maximum value which can be assigned to a bond |
| 542 | */ |
| 543 | BondOrder max_bond_order_; |
| 544 | |
| 545 | /** |
| 546 | * The maximum valence which can be assigned to an atom |
| 547 | */ |
| 548 | Valence max_valence_; |
| 549 | |
| 550 | /** |
| 551 | * Compute the dynamic programming table for a given bag. On this time, the dynamic programming tables of all |
| 552 | * vertices of the subtree, rooted by this bag, should are computed. |
| 553 | * This function shouldn't be called directly. Instead use the #compute function. |
| 554 | * @param bag the bag which is visited during the tree traversal |
| 555 | * @param type the type of this bag. Isn't used because the bag gives also access to it's bag type |
| 556 | * @param begin an iterator to the first computed child-table of this bag. For join-nodes there are two childs, |
| 557 | * leaf nodes have no childs. All other vertices have one child. |
| 558 | * @param end an iterator the end of the computed child-tables of this bag |
| 559 | * @throws BALL::Exception::IllegalTreeOperation if the nice tree decomposition is damaged |
| 560 | * @return the dynamic programming table for this bag |
| 561 | */ |
| 562 | DPTable_* operator() (TreeDecompositionBag& bag, |
| 563 | std::vector<DPTable_*>::const_iterator begin, std::vector<DPTable_*>::const_iterator end); |
| 564 | |
| 565 | /** |
| 566 | * Remember which bond belonging to the given bag. |
| 567 | * Because this information is reused in backtracking, the bonds |
| 568 | * are saved in the AdditionalBagProperties_ object. |
| 569 | * @param bag the bag of the nice tree decomposition |
| 570 | * @return a vector of edges which are only incident to vertices in this bag |
| 571 | */ |
| 572 | std::vector<MolecularGraphTraits::EdgeType> getBondsInBag(TreeDecompositionBag& bag); |
| 573 | |
| 574 | /** |
| 575 | * computes the dynamic programming table for a given leaf node. |
| 576 | * This is done by filling an entry with just one valence-entry (with value 0) into the table. |
| 577 | * So the complexity of this operation is constant. |
| 578 | * @param bagProperties contains the dynamic programming table |
| 579 | */ |
| 580 | void computeLeafIntroduceBag(AdditionalBagProperties_& bag_properties); |
| 581 | |
| 582 | /** |
| 583 | * computes the dynamic programming table for a given introduce node. |
| 584 | * This is done by insert a new column for the introduced vertex in each entry, filled with the value 0. |
| 585 | * If the bag introduces new bonds, each entry is duplicated for each possible bond value. Then new bond order |
| 586 | * columns are inserted into the entries, filled with the different possible combinations of bond values. |
| 587 | * So at the end of this function, the bag contains each possible bond order combination of the introduced bonds. This |
| 588 | * could be very heavy, but usually we have never more than two or three bonds in a bag. So the complexity of this |
| 589 | * operation is O([number of bond values]^[number of introduced bonds] * [number of table entries] * insertion-costs), |
| 590 | * where insertion-costs are logarithmic, because the table is implemented as search tree. |
| 591 | */ |
| 592 | void computeIntroduceBag(TreeDecompositionBag& bag, |
| 593 | DPTable_& child, AdditionalBagProperties_& bag_properties); |
| 594 | |
| 595 | /** |
| 596 | * computes the dynamic programming table for a given forget node. |
| 597 | * This is done by removing the columns of each bond which is incident to the forget node. |
| 598 | * It's bond value (+1) is added to the consumed valence of the both incident atoms. After this, |
| 599 | * the forget-atom is removed itself and the penalty for its valences is added to the penalty of the table entry. |
| 600 | * If multiple entries have the same bond and valence values, just the entry with the lowest penalty is kept. |
| 601 | * So this operation removes many entries from the table. This is important, because introduce nodes insert many |
| 602 | * entries in the table. So during the algorithm the tables doesn't grow very much, because there is a sucession |
| 603 | * of forgetting table entries and introducing new table entries. |
| 604 | * The complexity of forgetting a vertex is linear in O(number of bonds + number of atoms + PenaltyMap lookup). |
| 605 | * This is done for each table entry. Furthermore there comes a lookup cost for finding duplicate entries in the table |
| 606 | * which is done in O(log(number of entries) * (number of bonds + number of atoms)), |
| 607 | * because the table is implemented as search tree. |
| 608 | */ |
| 609 | void computeForgetBag(TreeDecompositionBag& bag, |
| 610 | DPTable_& child, AdditionalBagProperties_& property); |
| 611 | |
| 612 | /** |
| 613 | * computes the dynamic programming table for a given join node. |
| 614 | * The child bags of a join node contains the same vertices and bonds as the join bag itself. So the table |
| 615 | * of the join bag is computed by merging the tables of the both childs. |
| 616 | * This is done by finding each pair of table entries from first and second child, which have the same |
| 617 | * bond values but - maybe - different penalties and valence values. The valence values and penalties of this pairs |
| 618 | * are added and the merged entry is inserted in the table of the join node. Again, entries with the same |
| 619 | * values but greater penalty than other one are removed from the table. |
| 620 | * To avoid quadratic complexity by finding pairs, the table entries are inserted into a search tree which |
| 621 | * compares just the bond values of its keys. So just the entries with the same bond values are combined, which is |
| 622 | * quadratic in worst case, too. But usually the number of entries in the table is not so huge, because the forget bags |
| 623 | * remove many entries which can not have better solutions than the kept one. |
| 624 | */ |
| 625 | void computeJoinBag(TreeDecompositionBag& bag, |
| 626 | DPTable_& leftChild, DPTable_& rightChild, AdditionalBagProperties_& bag_properties); |
| 627 | |
| 628 | /** |
| 629 | * computes the dynamic programming table for a given root node. |
| 630 | * A root bag is nothing more than a forget bag, so this operations does the same as the #computeForgetBag |
| 631 | * function. |
| 632 | */ |
| 633 | void computeRootBag(TreeDecompositionBag& bag, DPTable_& child, AdditionalBagProperties_& bag_properties); |
| 634 | |
| 635 | /** |
| 636 | * delete the column of the forgotten vertex and it's incident bonds in a given table entry. Sum the bond values of |
| 637 | * the forgotten bonds to the valence values of their incident non-forgotten vertices. |
| 638 | * This function is reused in DPBackTracking and is called in the #computeForgetBag function. |
| 639 | */ |
| 640 | Penalty forgetInnerVertexIn(TreeDecompositionBag& bag, DPConstRow_ child_row, DPConfig_& entry, |
| 641 | std::vector<MolecularGraphTraits::EdgeType>& child_bonds, Size forgotten_index); |
| 642 | |
| 643 | }; |
| 644 | |
| 645 | /** |
| 646 | * This data structure contains all algorithm data which is generated during the #start function |
| 647 | * and which can be shared above more than one FPTBondOrderStrategy "iterator", if they compute all |
| 648 | * the same molecule with the same penalty table. |
| 649 | */ |
| 650 | class ComputingData_ |
| 651 | { |
| 652 | public: |
| 653 | /** |
| 654 | * Default constructor |
| 655 | */ |
| 656 | ComputingData_(); |
| 657 | |
| 658 | /** |
| 659 | * Deletes the dynamic programming table, the molecule graph and the nice tree decomposition |
| 660 | */ |
| 661 | ~ComputingData_(); |
| 662 | |
| 663 | /** |
| 664 | * The bond assignments for each connection component of this molecule |
| 665 | */ |
| 666 | vector<FPTBondOrderAssignment_*> bond_assignments; |
| 667 | |
| 668 | /** |
| 669 | * the molecule graph |
| 670 | */ |
| 671 | MolecularGraph *molecule_graph; |
| 672 | |
| 673 | /** |
| 674 | * The nice tree decompositions for each connection component |
| 675 | */ |
| 676 | boost::shared_ptr<TreeWidth<MolecularGraph> > tw; |
| 677 | |
| 678 | /** |
| 679 | * a vector with pointers to the bonds of the atom container. The order of this bonds in the vector |
| 680 | * is the same as the order of the bond values in the assignments. |
| 681 | */ |
| 682 | vector<Bond const *> bonds; |
| 683 | }; |
| 684 | |
| 685 | /** |
| 686 | * An assignment is the solution of the bond assignment problem. It consists of the penalty of this solution, |
| 687 | * and a vector of bond values for each bond. The bond order is just a vector of bond values. If you want a HashMap which |
| 688 | * maps the concrete bonds to their bond value, you should use the FPTBondOrderStrategy class instead, which can |
| 689 | * construct such a HashMap from an assignment. |
| 690 | * Nevertheless the index of the bonds has a strict order: Each bond which points to atoms with indizes u and v can be |
| 691 | * represented as edge (u < v). An edge is less than another edge, is either it's u is less, or if it's u is |
| 692 | * equal to the other's u and it's v is less than the other's v. The indizes of the atoms is given by the graph structure. |
| 693 | * The first atom has index 0. |
| 694 | * We call an assignment valid to a given UndirectedGraph and PenaltyMap, if it's penalty is equal to the sum of penalties |
| 695 | * of it's atoms in the given penalty map. Assignments which was computed by the FPTBondOrder algorithm should |
| 696 | * be valid to the same graph and penalty map which was used by the algorithm. |
| 697 | */ |
| 698 | class Assignment_ |
| 699 | { |
| 700 | public: |
| 701 | /** |
| 702 | * build a new, empty assignment |
| 703 | */ |
| 704 | Assignment_(); |
| 705 | |
| 706 | /** |
| 707 | * build an assignment with the given number of bonds. The bond values are filled with 0, the penalty |
| 708 | * is initialized with infinite_penalty. Such a bond assignment don't have to be valid! |
| 709 | */ |
| 710 | Assignment_(Size num_bonds); |
| 711 | |
| 712 | /** |
| 713 | * Copy constructor |
| 714 | */ |
| 715 | Assignment_(Assignment_ const& copy); |
| 716 | |
| 717 | /** |
| 718 | * Build an assignment from a given bond value vector and penalty. Such a bond assignment don't have to |
| 719 | * be valid! |
| 720 | */ |
| 721 | Assignment_(std::vector<BondOrder> const& bonds, Penalty penalty); |
| 722 | |
| 723 | /** |
| 724 | * Assignment operator |
| 725 | */ |
| 726 | Assignment_& operator=(Assignment_ const& copy); |
| 727 | |
| 728 | /** |
| 729 | * Returns the value for the bond with the given index. |
| 730 | * @throw BALL::Exception::IndexOverflow unless index is less than the number of bonds in this assignment |
| 731 | */ |
| 732 | BondOrder operator [](Size index) const; |
| 733 | |
| 734 | /** |
| 735 | * Gives access to the bond value of the bond with the given index. |
| 736 | * @throw BALL::Exception::IndexOverflow unless index is less than the number of bonds in this assignment |
| 737 | */ |
| 738 | BondOrder& operator [](Size index); |
| 739 | |
| 740 | /** |
| 741 | * Combines two disjoint assignments. Two assignments are disjoint, if for each bond index i there is a 0 value in |
| 742 | * at least one of the both assignments. Combining such disjoint assignments means: Insert for each index i the |
| 743 | * not-zero bond value of the both assignments. |
| 744 | * Such disjoint assignments exist in separated computing of connection components of the same graph. Each computing |
| 745 | * just assign the bond values of the bonds, which were connected to the connection component. The other bond values |
| 746 | * are 0. The combining of assignments of all connection components of a graph creates an assignment of the whole |
| 747 | * graph. This approach is important, because the FPTBondOrder algorithm is just defined for connected graphs. |
| 748 | * @throw BALL::Exception::InvalidArgument if the two assignments have a different number of bonds |
| 749 | */ |
| 750 | void combine(Assignment_ const& other); |
| 751 | |
| 752 | /** |
| 753 | * returns a const reference to the bond values |
| 754 | */ |
| 755 | std::vector<BondOrder> const& getBondOrders() const; |
| 756 | |
| 757 | /** |
| 758 | * Comparison is needed to insert such an assignment into a priority queue or a search tree. |
| 759 | * An assignment x is less than an assignment y, if the penalty of x is less than the penalty of y. |
| 760 | * If two assignments have the same penalty, the comparison is done by comparing each bond value separately. |
| 761 | */ |
| 762 | int compare(Assignment_ const& a) const; |
| 763 | |
| 764 | /** |
| 765 | * @see #compare |
| 766 | */ |
| 767 | bool operator < (Assignment_ const& a) const; |
| 768 | |
| 769 | /** |
| 770 | * @see #compare |
| 771 | */ |
| 772 | bool operator > (Assignment_ const& a) const; |
| 773 | |
| 774 | /** |
| 775 | * @see #compare |
| 776 | */ |
| 777 | bool operator <= (Assignment_ const& a) const; |
| 778 | |
| 779 | /** |
| 780 | * @see #compare |
| 781 | */ |
| 782 | bool operator >= (Assignment_ const& a) const; |
| 783 | |
| 784 | /** |
| 785 | * @see #compare |
| 786 | */ |
| 787 | bool operator == (Assignment_ const& a) const; |
| 788 | |
| 789 | /** |
| 790 | * Returns true if the assignment is valid to the given UndirectedGraph and PenaltyMap. This happens if the |
| 791 | * sum of penalties of each atom with the valence as sum of the bond values of each incident bond is equal to the |
| 792 | * penalty of this assignment. |
| 793 | * @param molecule should be the same graph which was used to compute this assignment |
| 794 | * @param PenaltyMap should be the same penalty map which was used to compute this assignment |
| 795 | * @return true if the assignment is valid, false otherwise |
| 796 | */ |
| 797 | bool isValid(MolecularGraph& molecule, FPTBondOrderStrategy& parent); |
| 798 | |
| 799 | /** |
| 800 | * The penalty of this assignment |
| 801 | */ |
| 802 | Penalty penalty; |
| 803 | |
| 804 | protected: |
| 805 | /** |
| 806 | * A vector with bond values. 0 is single bond, 1 is double bond, 2 is tripple bond |
| 807 | */ |
| 808 | std::vector<BondOrder> bonds_; |
| 809 | |
| 810 | }; |
| 811 | |
| 812 | /** |
| 813 | * A comparator, which searches DPConfigs in a DPJoinMap. Two DPConfigs are |
| 814 | * equal, if they have the same bond values, even if they have different |
| 815 | * consumed valences. |
| 816 | */ |
| 817 | struct DPJoinMapComparator_ |
| 818 | { |
| 819 | /** |
| 820 | * Less-operator. Returns true if leftp is smaller than rightp. |
| 821 | * @throws BALL::Exception::InvalidArgument if the both DPConfig entries can not be compared |
| 822 | */ |
| 823 | bool operator() (DPConfig_ const* leftp, DPConfig_ const* rightp) const; |
| 824 | |
| 825 | /** |
| 826 | * returns 1, if leftp is greather than rightp, 0 if both are equal and -1 otherwise. |
| 827 | * You can compare only childs from the same join bag. |
| 828 | * @throws BALL::Exception::InvalidArgument if the both DPConfig entries can not be compared |
| 829 | */ |
| 830 | int compare(DPConfig_ const* leftp, DPConfig_ const* rightp) const; |
| 831 | }; |
| 832 | |
| 833 | /** A comparator for edges in the molecular graph. |
| 834 | */ |
| 835 | class EdgeComparator_ |
| 836 | { |
| 837 | public: |
| 838 | typedef GRAPH::GraphTraits<MolecularGraph>::EdgeType Edge; |
| 839 | |
| 840 | EdgeComparator_(MolecularGraph* graph) |
| 841 | : graph_(graph) |
| 842 | { } |
| 843 | |
| 844 | bool operator() (Edge const& e1, Edge const& e2); |
| 845 | |
| 846 | protected: |
| 847 | MolecularGraph* graph_; |
| 848 | }; |
| 849 | |
| 850 | /** |
| 851 | * A BackTrackingState_ is a position in the tree during the backtracking. |
| 852 | * It contains an assignment, which |
| 853 | * contains all bond values of the bonds which were forgotten during the backtracking. |
| 854 | * Furthermore it contains the table entry of the dynamic programming table |
| 855 | */ |
| 856 | class BackTrackingState_ |
| 857 | { |
| 858 | public: |
| 859 | /** |
| 860 | * Default constructor |
| 861 | */ |
| 862 | BackTrackingState_(); |
| 863 | |
| 864 | /** |
| 865 | * build a BackTrackingState for a bag with the given number of bonds |
| 866 | */ |
| 867 | BackTrackingState_(Size bonds); |
| 868 | |
| 869 | /** |
| 870 | * Copy constructor |
| 871 | */ |
| 872 | BackTrackingState_(BackTrackingState_ const& other); |
| 873 | |
| 874 | /** |
| 875 | * Assignment operator |
| 876 | */ |
| 877 | BackTrackingState_& operator=(BackTrackingState_ const& other); |
| 878 | |
| 879 | /** |
| 880 | * Comparison is needed to insert BackTrackingState_ entries into a search tree. |
| 881 | * Comparison is done by compare the assignments of the states. |
| 882 | */ |
| 883 | int compare(BackTrackingState_ const& other) const; |
| 884 | |
| 885 | /** |
| 886 | * @see #compare |
| 887 | */ |
| 888 | bool operator < (BackTrackingState_ const&) const; |
| 889 | |
| 890 | /** |
| 891 | * @see #compare |
| 892 | */ |
| 893 | bool operator > (BackTrackingState_ const&) const; |
| 894 | |
| 895 | /** |
| 896 | * @see #compare |
| 897 | */ |
| 898 | bool operator <= (BackTrackingState_ const&) const; |
| 899 | |
| 900 | /** |
| 901 | * @see #compare |
| 902 | */ |
| 903 | bool operator >= (BackTrackingState_ const&) const; |
| 904 | |
| 905 | /** |
| 906 | * @see #compare |
| 907 | */ |
| 908 | bool operator == (BackTrackingState_ const&) const; |
| 909 | |
| 910 | /** |
| 911 | * Assignment which contains all bond values of the bonds which were forgotten during the backtracking. |
| 912 | * Such an assignment is valid, if the BackTrackingState is pointing to the end of the backtracking |
| 913 | */ |
| 914 | Assignment_ assignment; |
| 915 | |
| 916 | /** |
| 917 | * the table entry, which this backtracking state follows. This is the entry of the bag, this BackTrackingState is |
| 918 | * pointing to. |
| 919 | */ |
| 920 | DPConfig_ config; |
| 921 | |
| 922 | /** |
| 923 | * If the BackTrackingState reachs a join node, it can just follow one of it's both children. So it have to remember |
| 924 | * which entry of the table of the second child it choosed. This is done in this stack. It contains the table entry |
| 925 | * of the right child and the index of this child in pre-order. |
| 926 | */ |
| 927 | std::stack<std::pair<DPConfig_, Size> > join_branches; |
| 928 | |
| 929 | /** |
| 930 | * pre-order index of this bag. If you traverse the tree in pre-order and give each vertex a number from 0 to n-1, |
| 931 | * this number is the pre-order index. |
| 932 | */ |
| 933 | Size index; |
| 934 | |
| 935 | |
| 936 | }; |
| 937 | |
| 938 | /** |
| 939 | * is used to remember the pair of table entries of the children of a join node without |
| 940 | * copying their configuration |
| 941 | */ |
| 942 | typedef std::pair<DPTable_::const_iterator, DPTable_::const_iterator> DPPairIt_; |
| 943 | |
| 944 | /** |
| 945 | * compare two join-table antecessor pairs by comparing their penalties |
| 946 | */ |
| 947 | static bool compareJoinTablePairs_(DPPairIt_ const& left, DPPairIt_ const& right); |
| 948 | |
| 949 | /** |
| 950 | * Compare pointers of entries of introduce or forget table antecessors by comparing their penalties |
| 951 | */ |
| 952 | static bool compareTablePointerEntries_(DPPointerRow_ const& left, DPPointerRow_ const& right); |
| 953 | |
| 954 | /** |
| 955 | * A map which remember pointers to DPConfigs of a child of a join-node. It uses a DPJoinMapComparator to find |
| 956 | * entries with equal bond-values very fast. |
| 957 | */ |
| 958 | typedef std::multimap<DPConfig_ const*, Penalty, DPJoinMapComparator_> DPJoinMap_; |
| 959 | |
| 960 | /** |
| 961 | * The backtracking algorithm. It traverses the nice tree decomposition in pre-order and chooses from the next table |
| 962 | * the entry, which was used to computed the previous one. |
| 963 | * We call a table entry successor, if we choosed it in the previous backtracking step. And we call a table entry |
| 964 | * antecessor, if it was used in the bond assignment algorithm to compute the successor. |
| 965 | * The backtracking starts in the root vertex which has just one table entry which becomes the first successor. |
| 966 | * The antecessor entry is gotten by finding the entry of the root's child bag, which can be used to compute the table |
| 967 | * entry in the root bag. This entry is used as successor for the next table. Often more than one table entry can be used |
| 968 | * to compute the successor entry. If we don't look at the penalty, there are even more possible antecessor entries. |
| 969 | * In each step the algorithm uses any antecessor, which computes the successor with the correct penalty. The other possible |
| 970 | * antecessors are inserted into a priority queue. After finding the successor of the last vertex in the tree, |
| 971 | * we can get the assignment by insert all bond values of the forgotten bonds in the choosed antecessor entries |
| 972 | * of visited forgot bags. Furthermore we can backtrack the next best solution by picking up the best |
| 973 | * antecessor in our priority queue and continue the backtracking with this entry as new successor. |
| 974 | * Remark that it's easy to get the penalty of the next solution (because we just have to look into the penalty of the |
| 975 | * antecessor entry). Just the computing of the bond order requires to traverse the whole tree. |
| 976 | * Furthermore if we don't specify an upperbound, this backtracking algorithm can iterate about EACH possible solution |
| 977 | * of this bond order problem. |
| 978 | */ |
| 979 | class DPBackTracking_ |
| 980 | { |
| 981 | public: |
| 982 | typedef TreeWidth<MolecularGraph>::TreeDecomposition TreeDecomposition; |
| 983 | typedef TreeWidth<MolecularGraph>::TreeDecompositionBag TreeDecompositionBag; |
| 984 | typedef TreeWidth<MolecularGraph>::TreeDecompositionContent TreeDecompositionContent; |
| 985 | |
| 986 | /** |
| 987 | * Construct a new DPBackTracking_ for a given FPTBondOrder algorithm, which backtracks not more than |
| 988 | * maxNumberOfSolutions. By default, the backtracking backtracks only the optimal solution. |
| 989 | * You have to call the FPTBondOrder#compute method before constructing the DPBackTracking_. |
| 990 | * Furthermore you should take care to delete the DPBackTracking_ before the FPTBondOrder, because this |
| 991 | * class operates on a pointer to the bond assignment algorithm, not on a copy. |
| 992 | * @param bondAssignment a reference to a FPTBondOrder which is already computed |
| 993 | * @param maxNumberOfSolutions the number of solutions you want to backtrack. Is by default 1. The size of the |
| 994 | * priority queue can never be greater than maxNumberOfSolutions |
| 995 | */ |
| 996 | DPBackTracking_(FPTBondOrderAssignment_& bond_assignment, Size max_number_of_solutions, |
| 997 | std::vector<MolecularGraphTraits::EdgeType> const& bonds, Penalty upperbound = infinite_penalty); |
| 998 | |
| 999 | /** |
| 1000 | * Copy constructor |
| 1001 | */ |
| 1002 | DPBackTracking_(DPBackTracking_ const& copy); |
| 1003 | |
| 1004 | /** |
| 1005 | * Destructor. Removes just the BackTrackingStates, not the bond assignment algorithm instance. |
| 1006 | */ |
| 1007 | ~DPBackTracking_(); |
| 1008 | |
| 1009 | /** |
| 1010 | * Assignment operator |
| 1011 | */ |
| 1012 | DPBackTracking_& operator= (DPBackTracking_ const& copy); |
| 1013 | |
| 1014 | /** |
| 1015 | * returns the current solution. Remark that after constructing the backtracking, there is no solution computed. So |
| 1016 | * you have to call #nextSolution first. |
| 1017 | * @throw BALL::Exception::NullPointer if you forgot to call #nextSolution |
| 1018 | */ |
| 1019 | Assignment_& getSolution(); |
| 1020 | |
| 1021 | /** |
| 1022 | * returns the current solution, const version. |
| 1023 | * Remark that after constructing the backtracking, there is no solution computed. So |
| 1024 | * you have to call #nextSolution first. |
| 1025 | * @throw BALL::Exception::NullPointer if you forgot to call #nextSolution |
| 1026 | */ |
| 1027 | Assignment_ const& getSolution() const; |
| 1028 | |
| 1029 | /** |
| 1030 | * returns true if there is another solution. Call this method before calling |
| 1031 | * #nextSolution. |
| 1032 | */ |
| 1033 | bool hasMoreSolutions() const; |
| 1034 | |
| 1035 | /** |
| 1036 | * Computes the next best solution. You can access it by calling #getSolution. |
| 1037 | * @throw BALL::Exception::OutOfRange if there are no more solutions |
| 1038 | */ |
| 1039 | void nextSolution(); |
| 1040 | |
| 1041 | /** |
| 1042 | * returns the penalty of the next best solution. If there are no more solutions, |
| 1043 | * this function returns infinite_penalty. |
| 1044 | */ |
| 1045 | Penalty penaltyOfNextSolution() const; |
| 1046 | |
| 1047 | void clear(); |
| 1048 | |
| 1049 | void preorder(TreeDecompositionBag node, TreeDecomposition&) |
| 1050 | { |
| 1051 | bags_->push_back(node); |
| 1052 | } |
| 1053 | |
| 1054 | void inorder(TreeDecompositionBag, TreeDecomposition&) |
| 1055 | { |
| 1056 | } |
| 1057 | |
| 1058 | void postorder(TreeDecompositionBag, TreeDecomposition&) |
| 1059 | { |
| 1060 | } |
| 1061 | |
| 1062 | protected: |
| 1063 | |
| 1064 | /** |
| 1065 | * Defines the Min-order for BackTrackingState-pointer instances in the queue |
| 1066 | */ |
| 1067 | struct StateComparator_ |
| 1068 | { |
| 1069 | /** |
| 1070 | * Comparison is done by dereferencing the pointer |
| 1071 | */ |
| 1072 | bool operator () (BackTrackingState_ const * left, BackTrackingState_ const * right) const; |
| 1073 | }; |
| 1074 | |
| 1075 | /** |
| 1076 | * The instance of the FPTBondOrder algorithm, which gives access to the computed tables and |
| 1077 | * the nice tree decomposition. This class doesn't make a copy of the bondAssignment, so take care |
| 1078 | * that the bondAssignment isn't deleted before the instance of this class. |
| 1079 | */ |
| 1080 | FPTBondOrderAssignment_* bond_assignment_; |
| 1081 | |
| 1082 | /** |
| 1083 | * The current state of the backtracking. Each other state is in the priority queue |
| 1084 | */ |
| 1085 | BackTrackingState_* current_state_; |
| 1086 | |
| 1087 | /** |
| 1088 | * priority queue for backtracking states. It is implemented as search tree, because we need also |
| 1089 | * access to the worst element (to limit the queues size). |
| 1090 | */ |
| 1091 | std::multiset<BackTrackingState_*, StateComparator_> queue_; |
| 1092 | |
| 1093 | /** |
| 1094 | * the maximum number of solutions we want do backtrack. |
| 1095 | */ |
| 1096 | Size max_num_solutions_; |
| 1097 | |
| 1098 | /** |
| 1099 | * A sorted vector of the edges of the graph. The bond values in the assignments are in the same order |
| 1100 | * as the edges in this vector. |
| 1101 | */ |
| 1102 | std::vector<MolecularGraphTraits::EdgeType> const* bonds_; |
| 1103 | |
| 1104 | /** |
| 1105 | * The nice tree decomposition bags in pre-order |
| 1106 | */ |
| 1107 | boost::shared_ptr<std::vector<TreeDecompositionBag> > bags_; |
| 1108 | |
| 1109 | /** |
| 1110 | * maxHeapSize is the maxNumberOfSolutions - the number of backtracked solutions. So this attribute |
| 1111 | * contains the current number of solutions we want to backtrack. |
| 1112 | */ |
| 1113 | Size max_heap_size_; |
| 1114 | |
| 1115 | /** |
| 1116 | * The number of solutions produced so far. |
| 1117 | */ |
| 1118 | Size num_computed_solutions_; |
| 1119 | |
| 1120 | /** |
| 1121 | * current upperbound. This algorithm will just iterate solutions which are better than this upperbound; |
| 1122 | */ |
| 1123 | Penalty upper_bound_; |
| 1124 | |
| 1125 | typedef vector<TreeDecompositionBag> BagVector; |
| 1126 | |
| 1127 | /** |
| 1128 | * returns the dynamic programming table of the bag with the given pre-order index |
| 1129 | */ |
| 1130 | DPTable_& getTable(Size order); |
| 1131 | |
| 1132 | /** |
| 1133 | * returns the bag properties of the bag with the given pre-order index |
| 1134 | */ |
| 1135 | AdditionalBagProperties_& getProperties(Size order); |
| 1136 | |
| 1137 | /** |
| 1138 | * Leaf-Nodes have no antecessors. So either the computing is finished, |
| 1139 | * or there is an unfinished join node in join branch stack which has to |
| 1140 | * be computed next. |
| 1141 | */ |
| 1142 | void visitLeaf(BackTrackingState_& state); |
| 1143 | |
| 1144 | /** |
| 1145 | * search the both antecessors of this join node. This is the pair of entries, |
| 1146 | * which bond values are the same as the successor's bond values and which |
| 1147 | * sum of consumed valences are the same as the successor's consumed valences. |
| 1148 | * The best left entry becomes the new current state. The right entry is pushed |
| 1149 | * on top of the state's join branch stack. Other possible pairs of antecessors |
| 1150 | * are inserted into the priority queue |
| 1151 | * @param state the current backtracking state |
| 1152 | * @param bag the join bag with the successor entry |
| 1153 | * @param leftTable the table of the first child |
| 1154 | * @param rightTable the table of the second child |
| 1155 | */ |
| 1156 | void visitJoin(BackTrackingState_& state, TreeDecompositionBag& bag, |
| 1157 | DPTable_& leftTable, DPTable_& rightTable); |
| 1158 | |
| 1159 | /** |
| 1160 | * search the antecessor of this forget node. This is the entry which is equal to |
| 1161 | * the successor after forgetting the forget-vertex and it's incident bonds. |
| 1162 | * @param state the current backtracking state |
| 1163 | * @param bag the forget bag with the successor entry |
| 1164 | * @param table the child's table with the antecessor entry |
| 1165 | */ |
| 1166 | void visitForget(BackTrackingState_& state, TreeDecompositionBag& bag, DPTable_& table); |
| 1167 | |
| 1168 | /** |
| 1169 | * search antecessor of this introduce node. This is the same entry |
| 1170 | * as the successor, but without the introduced columns |
| 1171 | * @param state the current backtracking state |
| 1172 | * @param bag the introduce bag with the successor entry |
| 1173 | * @param table the child's table with the antecessor entry |
| 1174 | */ |
| 1175 | void visitIntroduce(BackTrackingState_& state, TreeDecompositionBag& bag, DPTable_& table); |
| 1176 | |
| 1177 | /** |
| 1178 | * searchs the index of this bond in the assignment array. |
| 1179 | * Because there is a strict ordering of bonds, this search |
| 1180 | * is computed as binary search in logarithmic time. |
| 1181 | */ |
| 1182 | Size bondIndexFor(MolecularGraphTraits::EdgeType bond) const; |
| 1183 | |
| 1184 | /** |
| 1185 | * remembers the given state as another possible solution with higher penalty. |
| 1186 | * The state is inserted in the queue. |
| 1187 | * If the algorithm found enough solutions, it updates the upperbound to the worst solution in the |
| 1188 | * queue. So just solutions with better penalty are inserted into the queue. |
| 1189 | * @param state an alternative antecessor which has a greater or equal penalty than the choosed one |
| 1190 | */ |
| 1191 | void remember(BackTrackingState_& state); |
| 1192 | |
| 1193 | /** |
| 1194 | * Checks if the penalty of this solution is good enough for backtracking. |
| 1195 | * This happens if the penalty is better than the upperbound |
| 1196 | */ |
| 1197 | bool isSolutionNeeded(Penalty penalty); |
| 1198 | |
| 1199 | /** |
| 1200 | * Is called by visitForget. It writes the values of all forgotten bonds into |
| 1201 | * the state's assignment. |
| 1202 | * @param state the current state |
| 1203 | * @param bag the forget bag |
| 1204 | * @param antecessor the choosed entry in the bag's child bag |
| 1205 | * @param forgottenVertex the vertex which is forgotten in the forget bag |
| 1206 | */ |
| 1207 | void setStateAssignment(BackTrackingState_& state, TreeDecompositionBag& bag, |
| 1208 | DPConfig_& antecessor, MolecularGraphTraits::VertexType forgotten_vertex); |
| 1209 | |
| 1210 | /** |
| 1211 | * Make the antecessor entry to the new successor entry of the given state and |
| 1212 | * adding the penalty |
| 1213 | * @param state the backtracking state |
| 1214 | * @param antecessor the choosed entry which becomes the new successor |
| 1215 | * @param additionalPenalty the penalty which is added to the best previous solution for choosing this antecessor |
| 1216 | */ |
| 1217 | void extendState(BackTrackingState_& state, DPConfig_ const& antecessor, Penalty additional_penalty); |
| 1218 | |
| 1219 | /** |
| 1220 | * Remember the choosed entry of the right child's table by adding it into the joinBranch stack. |
| 1221 | * @param state the backtracking state |
| 1222 | * @param child the right child of the join bag |
| 1223 | * @param antecessor the choosed entry in the right child's table |
| 1224 | */ |
| 1225 | void branchState(BackTrackingState_& state, TreeDecompositionBag const& child, DPConfig_ const& antecessor); |
| 1226 | |
| 1227 | }; |
| 1228 | |
| 1229 | /** |
| 1230 | * Combines backtracked solutions from other DPBackTrackers. |
| 1231 | * Is used to combine the solutions of computed connection components |
| 1232 | * of a graph to a solution for the whole graph. Because the bond orders of the |
| 1233 | * connection components are disjoint, the solutions of the backtrackers can be combined |
| 1234 | * independently. |
| 1235 | * This class provides the same public functions as DPBackTracking. |
| 1236 | * Remarks that this class will start the backtracking of the best solution after constructing, |
| 1237 | * while DPBackTracking will start only after calling DPBackTracking#nextSolution. Nevertheless |
| 1238 | * you have to call #nextSolution before you can access the optimal solution. |
| 1239 | */ |
| 1240 | class DPBackTrackingCombiner_ |
| 1241 | { |
| 1242 | public: |
| 1243 | /** |
| 1244 | * Construct a DPBackTrackingCombiner with the given FPTBondOrder and the number of solutions |
| 1245 | * @param bondAssignments vector with pointers to the bond assignments. Call #compute before constructing |
| 1246 | * @param solutionNumber the maximum number of solutions you want to backtrack |
| 1247 | */ |
| 1248 | DPBackTrackingCombiner_(std::vector<FPTBondOrderAssignment_*>& bond_assignments, |
| 1249 | Size solution_number, Penalty upper_bound = infinite_penalty); |
| 1250 | |
| 1251 | /** |
| 1252 | * Copy constructor |
| 1253 | */ |
| 1254 | DPBackTrackingCombiner_(DPBackTrackingCombiner_ const& copy); |
| 1255 | |
| 1256 | /** |
| 1257 | * Destructor. Deletes all backtrackers but not the bond assignment algorithms |
| 1258 | */ |
| 1259 | ~DPBackTrackingCombiner_(); |
| 1260 | |
| 1261 | void clear(); |
| 1262 | |
| 1263 | /** |
| 1264 | * Assignment operator |
| 1265 | */ |
| 1266 | DPBackTrackingCombiner_& operator = (DPBackTrackingCombiner_ const& copy); |
| 1267 | |
| 1268 | /** |
| 1269 | * return true if there are more solutions to backtrack |
| 1270 | */ |
| 1271 | bool hasMoreSolutions() const; |
| 1272 | |
| 1273 | /** |
| 1274 | * computes the next solution. Call #hasMoreSolutions before to avoid an OutOfRange exception. |
| 1275 | * @throw BALL::Exception::OutOfRange if there is no more solution to backtrack |
| 1276 | */ |
| 1277 | void nextSolution(); |
| 1278 | |
| 1279 | /** |
| 1280 | * returns the last computed solution |
| 1281 | */ |
| 1282 | Assignment_& getSolution(); |
| 1283 | |
| 1284 | /** |
| 1285 | * returns the last computed solution, const version |
| 1286 | */ |
| 1287 | Assignment_ const& getSolution() const; |
| 1288 | |
| 1289 | /** |
| 1290 | * returns the penalty of the solution which can be backtracked next or INFINITE_PENALTY, if there |
| 1291 | * is no more solution. |
| 1292 | */ |
| 1293 | Penalty penaltyOfNextSolution() const; |
| 1294 | |
| 1295 | /** |
| 1296 | * A sorted vector of the edges of the graph. The bond values in the assignments are in the same order |
| 1297 | * as the edges in this vector. |
| 1298 | */ |
| 1299 | std::vector<MolecularGraphTraits::EdgeType> sorted_edges; |
| 1300 | |
| 1301 | protected: |
| 1302 | /** |
| 1303 | * The backtrackers. They are managed by this class, so you don't have to care about deleting them. |
| 1304 | */ |
| 1305 | std::vector<DPBackTracking_*> backtrackers_; |
| 1306 | |
| 1307 | /** |
| 1308 | * The priority queue for the assignments. Because each new backtracked assignment of a connection |
| 1309 | * component can be combined with each other found assignment of the other connection components, |
| 1310 | * you get many new solutions in each backtracking step. They are combined inserted into this queue. |
| 1311 | */ |
| 1312 | std::priority_queue<Assignment_, std::vector<Assignment_>, std::greater<Assignment_> > priority_queue_; |
| 1313 | |
| 1314 | /** |
| 1315 | * The backtracked solutions of the connection components. They can be combined to build the |
| 1316 | * solution of the whole graph. |
| 1317 | */ |
| 1318 | std::vector<std::vector<Assignment_> > component_solutions_; |
| 1319 | |
| 1320 | /** |
| 1321 | * the last backtracked and combined solution |
| 1322 | */ |
| 1323 | Assignment_ assignment_; |
| 1324 | |
| 1325 | /** |
| 1326 | * maximum number of solutions you want to backtrack |
| 1327 | */ |
| 1328 | Size solution_number_; |
| 1329 | |
| 1330 | /** |
| 1331 | * The penalty of the best solution |
| 1332 | */ |
| 1333 | Penalty optimum_; |
| 1334 | |
| 1335 | /** |
| 1336 | * This backtracker returns only solutions which have a better penalty than the given upperbound |
| 1337 | */ |
| 1338 | Penalty upper_bound_; |
| 1339 | |
| 1340 | /** |
| 1341 | * Searches for the backtracker which would compute the best next solution (after combining). |
| 1342 | * Returns it index and the penalty of it's solution. |
| 1343 | */ |
| 1344 | std::pair<Size, Penalty> getNextMinimumBackTracker_() const; |
| 1345 | |
| 1346 | /** |
| 1347 | * Combines the component assignment with the whole graph assignment |
| 1348 | * @param backtracker_index index of the backtracker |
| 1349 | * @param solutionIndex the number of the assignment of this backtracker, which will be combined |
| 1350 | */ |
| 1351 | void applyAssignment_(Size backtracker_index, Size solution_index); |
| 1352 | |
| 1353 | /** |
| 1354 | * Lets each backtracker backtrack a solution and initialize the combiner. |
| 1355 | */ |
| 1356 | void initialize_(); |
| 1357 | |
| 1358 | /** |
| 1359 | * Combines the given new assignment with each previous found assignments |
| 1360 | * @param mindex index of the backtracker which found the next best assignment |
| 1361 | */ |
| 1362 | void combineEachSolution_(Size mindex); |
| 1363 | |
| 1364 | /** |
| 1365 | * copy each DPBackTracking |
| 1366 | */ |
| 1367 | std::vector<DPBackTracking_*> deepCopyOfBacktrackers_() const; |
| 1368 | |
| 1369 | }; |
| 1370 | |
| 1371 | |
| 1372 | /// Initialize pointers to penalty data |
| 1373 | void initPenaltyData_(); |
| 1374 | |
| 1375 | /// Return penalty value for given vertex and valence |
| 1376 | Penalty getPenaltyFor_(MolecularGraphTraits::VertexType vertex, Valence valence) const; |
| 1377 | |
| 1378 | /** |
| 1379 | * contains for each block-index the position in the penalties_ array where this block starts |
| 1380 | */ |
| 1381 | std::vector<int> const* penalties_; |
| 1382 | |
| 1383 | /** |
| 1384 | * contains for each block_index the number of saved penalties for the atoms in the block |
| 1385 | */ |
| 1386 | std::vector<Position> const * block_to_start_idx_; |
| 1387 | |
| 1388 | /** |
| 1389 | * contains for each block_index the number of saved penalties for the atoms in the block |
| 1390 | */ |
| 1391 | std::vector<Size> const * block_to_length_; |
| 1392 | |
| 1393 | /** |
| 1394 | * contains the valence of the first entry of the given block |
| 1395 | */ |
| 1396 | std::vector<int> const * block_to_start_valence_; |
| 1397 | |
| 1398 | /** |
| 1399 | * contains the block index for the given atom |
| 1400 | */ |
| 1401 | std::vector<std::vector<int> > const* atom_to_block_; |
| 1402 | |
| 1403 | /** |
| 1404 | * A shared pointer to the computing data, so that you can copy this instance without copying |
| 1405 | * all the computing data |
| 1406 | */ |
| 1407 | boost::shared_ptr<ComputingData_> computing_data_; |
| 1408 | |
| 1409 | /** |
| 1410 | * The backtracking combiner_ |
| 1411 | */ |
| 1412 | boost::shared_ptr<DPBackTrackingCombiner_> combiner_; |
| 1413 | }; |
| 1414 | } |
| 1415 | #endif // BALL_STRUCTURE_BONDORDERS_FPTBONDORDERSTRATEGY_H |