1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * PowerSetGenerator.cpp
|
---|
10 | *
|
---|
11 | * Created on: Oct 18, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include "PowerSetGenerator.hpp"
|
---|
23 |
|
---|
24 | #include "CodePatterns/Log.hpp"
|
---|
25 |
|
---|
26 | #include "atom.hpp"
|
---|
27 | #include "Bond/bond.hpp"
|
---|
28 | #include "Fragmentation/KeySet.hpp"
|
---|
29 | #include "Fragmentation/UniqueFragments.hpp"
|
---|
30 |
|
---|
31 | /** Constructor of class PowerSetGenerator.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | PowerSetGenerator::PowerSetGenerator(class UniqueFragments *_FragmentSearch, int Order) :
|
---|
35 | BondsPerSPList(Order),
|
---|
36 | FragmentSearch(_FragmentSearch)
|
---|
37 | {}
|
---|
38 |
|
---|
39 | /** Destructor of class PowerSetGenerator.
|
---|
40 | *
|
---|
41 | */
|
---|
42 | PowerSetGenerator::~PowerSetGenerator()
|
---|
43 | {
|
---|
44 | FragmentSearch = NULL;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /** Clears the touched list
|
---|
48 | * \param verbosity verbosity level
|
---|
49 | * \param *&TouchedList touched list
|
---|
50 | * \param SubOrder current suborder
|
---|
51 | * \param TouchedIndex currently touched
|
---|
52 | */
|
---|
53 | void PowerSetGenerator::ClearingTouched(int verbosity, int *&TouchedList, int SubOrder, int &TouchedIndex)
|
---|
54 | {
|
---|
55 | Log() << Verbose(1+verbosity) << "Clearing touched list." << endl;
|
---|
56 | for (TouchedIndex=SubOrder+1;TouchedIndex--;) // empty touched list
|
---|
57 | TouchedList[TouchedIndex] = -1;
|
---|
58 | TouchedIndex = 0;
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Adds the current combination of the power set to the snake stack.
|
---|
63 | * \param verbosity verbosity level
|
---|
64 | * \param CurrentCombination
|
---|
65 | * \param SetDimension maximum number of bits in power set
|
---|
66 | * \param *FragmentSet snake stack to remove from
|
---|
67 | * \param &BondsSet set of bonds
|
---|
68 | * \param *&TouchedList touched list
|
---|
69 | * \param TouchedIndex currently touched
|
---|
70 | * \return number of set bits
|
---|
71 | */
|
---|
72 | int PowerSetGenerator::AddPowersetToSnakeStack(int verbosity, int CurrentCombination, int SetDimension, KeySet *FragmentSet, std::vector<bond *> &BondsSet, int *&TouchedList, int &TouchedIndex)
|
---|
73 | {
|
---|
74 | atom *OtherWalker = NULL;
|
---|
75 | bool bit = false;
|
---|
76 | KeySetTestPair TestKeySetInsert;
|
---|
77 |
|
---|
78 | int Added = 0;
|
---|
79 | for (int j=0;j<SetDimension;j++) { // pull out every bit by shifting
|
---|
80 | bit = ((CurrentCombination & (1 << j)) != 0); // mask the bit for the j-th bond
|
---|
81 | if (bit) { // if bit is set, we add this bond partner
|
---|
82 | OtherWalker = BondsSet[j]->rightatom; // rightatom is always the one more distant, i.e. the one to add
|
---|
83 | //Log() << Verbose(1+verbosity) << "Current Bond is " << BondsSet[j] << ", checking on " << *OtherWalker << "." << endl;
|
---|
84 | Log() << Verbose(2+verbosity) << "Adding " << *OtherWalker << " with nr " << OtherWalker->getNr() << "." << endl;
|
---|
85 | TestKeySetInsert = FragmentSet->insert(OtherWalker->getNr());
|
---|
86 | if (TestKeySetInsert.second) {
|
---|
87 | TouchedList[TouchedIndex++] = OtherWalker->getNr(); // note as added
|
---|
88 | Added++;
|
---|
89 | } else {
|
---|
90 | Log() << Verbose(2+verbosity) << "This was item was already present in the keyset." << endl;
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | Log() << Verbose(2+verbosity) << "Not adding." << endl;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | return Added;
|
---|
97 | };
|
---|
98 |
|
---|
99 | /** Counts the number of elements in a power set.
|
---|
100 | * \param SetFirst begin iterator first bond
|
---|
101 | * \param SetLast end iterator
|
---|
102 | * \param *&TouchedList touched list
|
---|
103 | * \param TouchedIndex currently touched
|
---|
104 | * \return number of elements
|
---|
105 | */
|
---|
106 | int PowerSetGenerator::CountSetMembers(std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex)
|
---|
107 | {
|
---|
108 | int SetDimension = 0;
|
---|
109 | for( std::list<bond *>::const_iterator Binder = SetFirst;
|
---|
110 | Binder != SetLast;
|
---|
111 | ++Binder) {
|
---|
112 | for (int k=TouchedIndex;k--;) {
|
---|
113 | if ((*Binder)->Contains(TouchedList[k])) // if we added this very endpiece
|
---|
114 | SetDimension++;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return SetDimension;
|
---|
118 | };
|
---|
119 |
|
---|
120 | /** Fills a list of bonds from another
|
---|
121 | * \param *BondsList bonds array/vector to fill
|
---|
122 | * \param SetFirst begin iterator first bond
|
---|
123 | * \param SetLast end iterator
|
---|
124 | * \param *&TouchedList touched list
|
---|
125 | * \param TouchedIndex currently touched
|
---|
126 | * \return number of elements
|
---|
127 | */
|
---|
128 | int PowerSetGenerator::FillBondsList(std::vector<bond *> &BondsList, std::list<bond *>::const_iterator SetFirst, std::list<bond *>::const_iterator SetLast, int *&TouchedList, int TouchedIndex)
|
---|
129 | {
|
---|
130 | int SetDimension = 0;
|
---|
131 | for( std::list<bond *>::const_iterator Binder = SetFirst;
|
---|
132 | Binder != SetLast;
|
---|
133 | ++Binder) {
|
---|
134 | for (int k=0;k<TouchedIndex;k++) {
|
---|
135 | if ((*Binder)->leftatom->getNr() == TouchedList[k]) // leftatom is always the closer one
|
---|
136 | BondsList[SetDimension++] = (*Binder);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | return SetDimension;
|
---|
140 | };
|
---|
141 |
|
---|
142 | /** Remove all items that were added on this SP level.
|
---|
143 | * \param verbosity verbosity level
|
---|
144 | * \param *FragmentSet snake stack to remove from
|
---|
145 | * \param *&TouchedList touched list
|
---|
146 | * \param TouchedIndex currently touched
|
---|
147 | */
|
---|
148 | void PowerSetGenerator::RemoveAllTouchedFromSnakeStack(int verbosity, KeySet *FragmentSet, int *&TouchedList, int &TouchedIndex)
|
---|
149 | {
|
---|
150 | int Removal = 0;
|
---|
151 | for(int j=0;j<TouchedIndex;j++) {
|
---|
152 | Removal = TouchedList[j];
|
---|
153 | Log() << Verbose(2+verbosity) << "Removing item nr. " << Removal << " from snake stack." << endl;
|
---|
154 | FragmentSet->erase(Removal);
|
---|
155 | TouchedList[j] = -1;
|
---|
156 | }
|
---|
157 | DoLog(2) && (Log() << Verbose(2) << "Remaining local nr.s on snake stack are: ");
|
---|
158 | for(KeySet::iterator runner = FragmentSet->begin(); runner != FragmentSet->end(); runner++)
|
---|
159 | DoLog(0) && (Log() << Verbose(0) << (*runner) << " ");
|
---|
160 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
161 | TouchedIndex = 0; // set Index to 0 for list of atoms added on this level
|
---|
162 | };
|
---|
163 |
|
---|
164 |
|
---|
165 | /** Creates a list of all unique fragments of certain vertex size from a given graph \a Fragment for a given root vertex in the context of \a this molecule.
|
---|
166 | * -# initialises UniqueFragments structure
|
---|
167 | * -# fills edge list via BFS
|
---|
168 | * -# creates the fragment by calling recursive function SPFragmentGenerator with UniqueFragments structure, 0 as
|
---|
169 | root distance, the edge set, its dimension and the current suborder
|
---|
170 | * -# Free'ing structure
|
---|
171 | * Note that we may use the fact that the atoms are SP-ordered on the atomstack. I.e. when popping always the last, we first get all
|
---|
172 | * with SP of 2, then those with SP of 3, then those with SP of 4 and so on.
|
---|
173 | * \param RestrictedKeySet Restricted vertex set to use in context of molecule
|
---|
174 | * \param saturation whether to treat hydrogen special or not
|
---|
175 | * \return number of inserted fragments
|
---|
176 | * \note ShortestPathList in FragmentSearch structure is probably due to NumberOfAtomsSPLevel and SP not needed anymore
|
---|
177 | */
|
---|
178 | int PowerSetGenerator::operator()(KeySet &RestrictedKeySet, const enum HydrogenSaturation saturation)
|
---|
179 | {
|
---|
180 | int Counter = FragmentSearch->FragmentCounter; // mark current value of counter
|
---|
181 |
|
---|
182 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
183 | DoLog(0) && (Log() << Verbose(0) << "Begin of PowerSetGenerator with order " << BondsPerSPList.getOrder() << " at Root " << *FragmentSearch->getRoot() << "." << endl);
|
---|
184 |
|
---|
185 | BondsPerSPList.SetSPList(FragmentSearch->getRoot());
|
---|
186 |
|
---|
187 | // do a BFS search to fill the SP lists and label the found vertices
|
---|
188 | BondsPerSPList.FillSPListandLabelVertices(FragmentSearch->getRoot()->GetTrueFather()->getNr(), RestrictedKeySet, saturation);
|
---|
189 |
|
---|
190 | // outputting all list for debugging
|
---|
191 | BondsPerSPList.OutputSPList();
|
---|
192 |
|
---|
193 | // creating fragments with the found edge sets (may be done in reverse order, faster)
|
---|
194 | int SP = BondsPerSPList.CountNumbersInBondsList();
|
---|
195 | DoLog(0) && (Log() << Verbose(0) << "Total number of edges is " << SP << "." << endl);
|
---|
196 | if (SP >= (BondsPerSPList.getOrder()-1)) {
|
---|
197 | // start with root (push on fragment stack)
|
---|
198 | DoLog(0) && (Log() << Verbose(0) << "Starting fragment generation with " << *FragmentSearch->getRoot() << ", local nr is " << FragmentSearch->getRoot()->getNr() << "." << endl);
|
---|
199 | FragmentSearch->FragmentSet->clear();
|
---|
200 | DoLog(0) && (Log() << Verbose(0) << "Preparing subset for this root and calling generator." << endl);
|
---|
201 |
|
---|
202 | // prepare the subset and call the generator
|
---|
203 | std::vector<bond*> BondsList;
|
---|
204 | BondsList.resize(BondsPerSPList.BondsPerSPCount[0]);
|
---|
205 | ASSERT(BondsPerSPList.BondsPerSPList[0].size() != 0,
|
---|
206 | "molecule::PowerSetGenerator() - BondsPerSPList.BondsPerSPList[0] contains no root bond.");
|
---|
207 | BondsList[0] = (*BondsPerSPList.BondsPerSPList[0].begin()); // on SP level 0 there's only the root bond
|
---|
208 |
|
---|
209 | SPFragmentGenerator(0, BondsList, BondsPerSPList.BondsPerSPCount[0], BondsPerSPList.getOrder());
|
---|
210 | } else {
|
---|
211 | DoLog(0) && (Log() << Verbose(0) << "Not enough total number of edges to build " << BondsPerSPList.getOrder() << "-body fragments." << endl);
|
---|
212 | }
|
---|
213 |
|
---|
214 | // as FragmentSearch structure is used only once, we don't have to clean it anymore
|
---|
215 | // remove root from stack
|
---|
216 | DoLog(0) && (Log() << Verbose(0) << "Removing root again from stack." << endl);
|
---|
217 | FragmentSearch->FragmentSet->erase(FragmentSearch->getRoot()->getNr());
|
---|
218 |
|
---|
219 | // free'ing the bonds lists
|
---|
220 | BondsPerSPList.ResetSPList();
|
---|
221 |
|
---|
222 | // return list
|
---|
223 | DoLog(0) && (Log() << Verbose(0) << "End of PowerSetGenerator." << endl);
|
---|
224 | return (FragmentSearch->FragmentCounter - Counter);
|
---|
225 | };
|
---|
226 |
|
---|
227 | /** From a given set of Bond sorted by Shortest Path distance, create all possible fragments of size \a SetDimension.
|
---|
228 | * -# loops over every possible combination (2^dimension of edge set)
|
---|
229 | * -# inserts current set, if there's still space left
|
---|
230 | * -# yes: calls SPFragmentGenerator with structure, created new edge list and size respective to root dist
|
---|
231 | ance+1
|
---|
232 | * -# no: stores fragment into keyset list by calling InsertFragmentIntoGraph
|
---|
233 | * -# removes all items added into the snake stack (in UniqueFragments structure) added during level (root
|
---|
234 | distance) and current set
|
---|
235 | * \param RootDistance current shortest path level, whose set of edges is represented by **BondsSet
|
---|
236 | * \param BondsSet array of bonds to check
|
---|
237 | * \param SetDimension Number of possible bonds on this level (i.e. size of the array BondsSet[])
|
---|
238 | * \param SubOrder remaining number of allowed vertices to add
|
---|
239 | */
|
---|
240 | void PowerSetGenerator::SPFragmentGenerator(int RootDistance, std::vector<bond *> &BondsSet, int SetDimension, int SubOrder)
|
---|
241 | {
|
---|
242 | int verbosity = 0; //FragmentSearch->ANOVAOrder-SubOrder;
|
---|
243 | int NumCombinations;
|
---|
244 | int bits, TouchedIndex, SubSetDimension, SP, Added;
|
---|
245 | int SpaceLeft;
|
---|
246 | int *TouchedList = new int[SubOrder + 1];
|
---|
247 | KeySetTestPair TestKeySetInsert;
|
---|
248 |
|
---|
249 | NumCombinations = 1 << SetDimension;
|
---|
250 |
|
---|
251 | // here for all bonds of Walker all combinations of end pieces (from the bonds)
|
---|
252 | // have to be added and for the remaining ANOVA order GraphCrawler be called
|
---|
253 | // recursively for the next level
|
---|
254 |
|
---|
255 | Log() << Verbose(1+verbosity) << "Begin of SPFragmentGenerator." << endl;
|
---|
256 | Log() << Verbose(1+verbosity) << "We are " << RootDistance << " away from Root, which is " << *FragmentSearch->getRoot() << ", SubOrder is " << SubOrder << ", SetDimension is " << SetDimension << " and this means " << NumCombinations-1 << " combination(s)." << endl;
|
---|
257 |
|
---|
258 | // initialised touched list (stores added atoms on this level)
|
---|
259 | ClearingTouched(verbosity, TouchedList, SubOrder, TouchedIndex);
|
---|
260 |
|
---|
261 | // create every possible combination of the endpieces
|
---|
262 | Log() << Verbose(1+verbosity) << "Going through all combinations of the power set." << endl;
|
---|
263 | for (int i=1;i<NumCombinations;i++) { // sweep through all power set combinations (skip empty set!)
|
---|
264 | // count the set bit of i
|
---|
265 | bits = 0;
|
---|
266 | for (int j=SetDimension;j--;)
|
---|
267 | bits += (i & (1 << j)) >> j;
|
---|
268 |
|
---|
269 | Log() << Verbose(1+verbosity) << "Current set is " << Binary(i | (1 << SetDimension)) << ", number of bits is " << bits << "." << endl;
|
---|
270 | if (bits <= SubOrder) { // if not greater than additional atoms allowed on stack, continue
|
---|
271 | // --1-- add this set of the power set of bond partners to the snake stack
|
---|
272 | Added = AddPowersetToSnakeStack(verbosity, i, SetDimension, FragmentSearch->FragmentSet, BondsSet, TouchedList, TouchedIndex);
|
---|
273 |
|
---|
274 | SpaceLeft = SubOrder - Added ;// SubOrder - bits; // due to item's maybe being already present, this does not work anymore
|
---|
275 | if (SpaceLeft > 0) {
|
---|
276 | Log() << Verbose(1+verbosity) << "There's still some space left on stack: " << SpaceLeft << "." << endl;
|
---|
277 | if (SubOrder > 1) { // Due to Added above we have to check extra whether we're not already reaching beyond the desired Order
|
---|
278 | // --2-- look at all added end pieces of this combination, construct bond subsets and sweep through a power set of these by recursion
|
---|
279 | SP = RootDistance+1; // this is the next level
|
---|
280 |
|
---|
281 | // first count the members in the subset
|
---|
282 | SubSetDimension = CountSetMembers(BondsPerSPList.BondsPerSPList[SP].begin(), BondsPerSPList.BondsPerSPList[SP].end(), TouchedList, TouchedIndex);
|
---|
283 |
|
---|
284 | // then allocate and fill the list
|
---|
285 | std::vector<bond *> BondsList;
|
---|
286 | BondsList.resize(SubSetDimension);
|
---|
287 | SubSetDimension = FillBondsList(BondsList, BondsPerSPList.BondsPerSPList[SP].begin(), BondsPerSPList.BondsPerSPList[SP].end(), TouchedList, TouchedIndex);
|
---|
288 |
|
---|
289 | // then iterate
|
---|
290 | Log() << Verbose(2+verbosity) << "Calling subset generator " << SP << " away from root " << *FragmentSearch->getRoot() << " with sub set dimension " << SubSetDimension << "." << endl;
|
---|
291 | SPFragmentGenerator(SP, BondsList, SubSetDimension, SubOrder-bits);
|
---|
292 | }
|
---|
293 | } else {
|
---|
294 | // --2-- otherwise store the complete fragment
|
---|
295 | Log() << Verbose(1+verbosity) << "Enough items on stack for a fragment!" << endl;
|
---|
296 | // store fragment as a KeySet
|
---|
297 | DoLog(2) && (Log() << Verbose(2) << "Found a new fragment[" << FragmentSearch->FragmentCounter << "], local nr.s are: ");
|
---|
298 | for(KeySet::iterator runner = FragmentSearch->FragmentSet->begin(); runner != FragmentSearch->FragmentSet->end(); runner++)
|
---|
299 | DoLog(0) && (Log() << Verbose(0) << (*runner) << " ");
|
---|
300 | DoLog(0) && (Log() << Verbose(0) << endl);
|
---|
301 | FragmentSearch->InsertFragmentIntoGraph();
|
---|
302 | }
|
---|
303 |
|
---|
304 | // --3-- remove all added items in this level from snake stack
|
---|
305 | Log() << Verbose(1+verbosity) << "Removing all items that were added on this SP level " << RootDistance << "." << endl;
|
---|
306 | RemoveAllTouchedFromSnakeStack(verbosity, FragmentSearch->FragmentSet, TouchedList, TouchedIndex);
|
---|
307 | } else {
|
---|
308 | Log() << Verbose(2+verbosity) << "More atoms to add for this set (" << bits << ") than space left on stack " << SubOrder << ", skipping this set." << endl;
|
---|
309 | }
|
---|
310 | }
|
---|
311 | delete[](TouchedList);
|
---|
312 | Log() << Verbose(1+verbosity) << "End of SPFragmentGenerator, " << RootDistance << " away from Root " << *FragmentSearch->getRoot() << " and SubOrder is " << SubOrder << "." << endl;
|
---|
313 | };
|
---|