1 // ---------------------------------------------------------------------------
2 //
3 //  This file is part of PermLib.
4 //
5 // Copyright (c) 2009-2011 Thomas Rehn <thomas@carmen76.de>
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 // 1. Redistributions of source code must retain the above copyright
12 //    notice, this list of conditions and the following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright
14 //    notice, this list of conditions and the following disclaimer in the
15 //    documentation and/or other materials provided with the distribution.
16 // 3. The name of the author may not be used to endorse or promote products
17 //    derived from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // ---------------------------------------------------------------------------
31 
32 
33 #ifndef PARTITION_VECTOR_STABILIZER_SEARCH_H_
34 #define PARTITION_VECTOR_STABILIZER_SEARCH_H_
35 
36 #include <permlib/search/partition/r_base.h>
37 #include <permlib/predicate/vector_stabilizer_predicate.h>
38 #include <permlib/search/partition/set_stabilize_refinement.h>
39 #include <permlib/search/partition/refinement_family.h>
40 
41 namespace permlib {
42 namespace partition {
43 
44 /// subgroup search for a stabilizer of an integer vector based on partition backtracking
45 template<class BSGSIN,class TRANSRET>
46 class VectorStabilizerSearch : public RBase<BSGSIN,TRANSRET> {
47 public:
48 	typedef typename RBase<BSGSIN,TRANSRET>::PERM PERM;
49 
50 	/// constructor
51 	/**
52 	 * @param bsgs BSGS of group
53 	 * @param pruningLevelDCM level up to which expensive double coset minimality pruning is performed; zero to disable
54 	 */
55 	VectorStabilizerSearch(const BSGSIN& bsgs, unsigned int pruningLevelDCM);
56 
57 	/// initializes search
58 	/**
59 	 * assume that integer vector has entries 0, 1, ..., maxEntries-1
60 	 * @param begin iterator(unsigned long) begin of the integer vector to be stabilized
61 	 * @param end iterator(unsigned long) end of the integer vector to be stabilized
62 	 * @param maxEntries value of maximal entry of integer vector plus 1
63 	 */
64 	template<class InputIterator>
65 	void construct(InputIterator begin, InputIterator end, unsigned int maxEntries);
66 protected:
67 	virtual unsigned int processNewFixPoints(const Partition& pi, unsigned int backtrackCount);
68 private:
69 	std::vector<unsigned int> toStab;
70 	unsigned int m_maxEntries;
71 };
72 
73 template<class BSGSIN,class TRANSRET>
VectorStabilizerSearch(const BSGSIN & bsgs,unsigned int pruningLevelDCM)74 VectorStabilizerSearch<BSGSIN,TRANSRET>::VectorStabilizerSearch(const BSGSIN& bsgs, unsigned int pruningLevelDCM)
75 	: RBase<BSGSIN,TRANSRET>(bsgs, pruningLevelDCM)
76 { }
77 
78 template<class BSGSIN,class TRANSRET>
79 template<class InputIterator>
construct(InputIterator begin,InputIterator end,unsigned int maxEntries)80 void VectorStabilizerSearch<BSGSIN,TRANSRET>::construct(InputIterator begin, InputIterator end, unsigned int maxEntries) {
81 	VectorStabilizerPredicate<PERM>* stabPred = new VectorStabilizerPredicate<PERM>(begin, end);
82 	m_maxEntries = maxEntries;
83 	toStab.insert(toStab.begin(), begin, end);
84 	std::vector<unsigned int> stabC(toStab.size());
85 
86 #ifdef PERMLIB_DEBUGMODE
87 	BOOST_FOREACH(const unsigned int v, stabC) {
88 		BOOST_ASSERT( v < maxEntries );
89 	}
90 #endif
91 
92 	// we can ignore the highest entries because these are automatically stabilized
93 	// if all other entries are stabilized
94 	for (unsigned int c = 0; c < maxEntries - 1; ++c) {
95 		unsigned int i = 0;
96 		std::vector<unsigned int>::iterator stabIt = toStab.begin(), cIt = stabC.begin();
97 		for (; stabIt != toStab.end(); ++stabIt) {
98 			BOOST_ASSERT( cIt != stabC.end() );
99 			if (*stabIt == c)
100 				*cIt++ = i;
101 			++i;
102 		}
103 		SetStabilizeRefinement<PERM> ssr(this->m_bsgs.n, stabC.begin(), cIt);
104 		ssr.initializeAndApply(this->m_partition);
105 		PERM empty(this->m_bsgs.n);
106 		ssr.apply2(this->m_partition2, empty);
107 	}
108 	RBase<BSGSIN,TRANSRET>::construct(stabPred, 0);
109 }
110 
111 template<class BSGSIN,class TRANSRET>
processNewFixPoints(const Partition & pi,unsigned int level)112 unsigned int VectorStabilizerSearch<BSGSIN,TRANSRET>::processNewFixPoints(const Partition& pi, unsigned int level) {
113 	const unsigned int basePos = RBase<BSGSIN,TRANSRET>::processNewFixPoints(pi, level);
114 	if (!this->m_limitInitialized) {
115 		bool allFound = true;
116 		int pos = -1;
117 		BOOST_FOREACH(unsigned int alpha, toStab) {
118 			++pos;
119 			if (alpha == m_maxEntries - 1)
120 				continue;
121 			if (std::find(pi.fixPointsBegin(), pi.fixPointsEnd(), static_cast<unsigned int>(pos)) == pi.fixPointsEnd()) {
122 				allFound = false;
123 				break;
124 			}
125 		}
126 		if (allFound) {
127 			this->m_limitLevel = level;
128 			this->m_limitBase = basePos;
129 			this->m_limitInitialized = true;
130 		}
131 	}
132 	return basePos;
133 }
134 
135 }
136 }
137 
138 #endif // -- PARTITION_VECTOR_STABILIZER_SEARCH_H_
139