permlib  0.2.9
Library for permutation computations
matrix_refinement2.h
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 MATRIXREFINEMENT2_H_
34 #define MATRIXREFINEMENT2_H_
35 
36 #include <permlib/predicate/pointwise_stabilizer_predicate.h>
37 #include <permlib/search/partition/refinement.h>
38 
39 #include <map>
40 
41 namespace permlib {
42 namespace partition {
43 
45 
48 template<class PERM,class MATRIX>
49 class MatrixRefinement2 : public Refinement<PERM> {
50 public:
52  explicit MatrixRefinement2(unsigned long n, const MATRIX& matrix);
53 
54  virtual unsigned int apply(Partition& pi) const;
55 
56  virtual bool init(Partition& pi);
57 
58 private:
59  const MATRIX& m_matrix;
60 
62  class Fingerprint {
63  public:
64  Fingerprint(unsigned long k) : m_fingerprint(k) {}
65 
67  bool operator<(const Fingerprint& f) const {
68  BOOST_ASSERT(f.m_fingerprint.size() == m_fingerprint.size());
69  for (unsigned int i=0; i<m_fingerprint.size(); ++i) {
70  if (m_fingerprint[i] < f.m_fingerprint[i])
71  return true;
72  if (m_fingerprint[i] > f.m_fingerprint[i])
73  return false;
74  }
75  return false;
76  }
77  bool operator==(const Fingerprint& f) const {
78  BOOST_ASSERT(f.m_fingerprint.size() == m_fingerprint.size());
79  for (unsigned int i=0; i<m_fingerprint.size(); ++i) {
80  if (m_fingerprint[i] != f.m_fingerprint[i])
81  return false;
82  }
83  return true;
84  }
85  unsigned long& operator[](unsigned long i) {
86  BOOST_ASSERT(i < m_fingerprint.size());
87  return m_fingerprint[i];
88  }
89  const unsigned long& operator[](unsigned long i) const {
90  BOOST_ASSERT(i < m_fingerprint.size());
91  return m_fingerprint[i];
92  }
93  private:
94  std::vector<unsigned long> m_fingerprint;
95  };
96 
97  unsigned int splitCell(Partition& pi, unsigned long i) const;
98  void computeFingerprint(const Partition& pi, unsigned long i, unsigned long j, std::map<Fingerprint,std::list<unsigned long> >& map) const;
99 };
100 
101 template<class PERM,class MATRIX>
102 MatrixRefinement2<PERM,MATRIX>::MatrixRefinement2(unsigned long n, const MATRIX& matrix)
103  : Refinement<PERM>(n, Default), m_matrix(matrix)
104 {
105 }
106 
107 template<class PERM,class MATRIX>
109  BOOST_ASSERT( this->initialized() );
110 
111  unsigned int ret = 0;
112  std::list<int>::const_iterator cellPairIt = Refinement<PERM>::m_cellPairs.begin();
113  while (cellPairIt != Refinement<PERM>::m_cellPairs.end()) {
114  unsigned long i = *cellPairIt++;
115  ret += splitCell(pi, static_cast<unsigned long>(i));
116  }
117  return ret;
118 }
119 
120 
121 template<class PERM,class MATRIX>
123  for (unsigned long i = 0; i < pi.cells(); ++i) {
124  if (splitCell(pi, i))
125  Refinement<PERM>::m_cellPairs.push_back(i);
126  }
127 
128  if (!Refinement<PERM>::m_cellPairs.empty()) {
129  typename Refinement<PERM>::RefinementPtr ref(new MatrixRefinement2<PERM,MATRIX>(*this));
131  return true;
132  }
133  return false;
134 }
135 
136 template<class PERM,class MATRIX>
137 unsigned int MatrixRefinement2<PERM,MATRIX>::splitCell(Partition& pi, unsigned long i) const {
138  unsigned long ret = 0;
139  if (pi.cellSize(i) < 2)
140  return ret;
141  for (unsigned long j = 0; j < pi.cells(); ++j) {
142  std::map<Fingerprint,std::list<unsigned long> > map;
143  computeFingerprint(pi, i, j, map);
144  if (map.size() > 1) {
145  PERMLIB_DEBUG(std::cout << "split " << i << " because of " << j << " in " << pi << std::endl;)
146  typename std::map<Fingerprint,std::list<unsigned long> >::const_iterator fit;
147  for (fit = map.begin(); fit != map.end(); ++fit) {
148  std::pair<Fingerprint, std::list<unsigned long> > splitCellPair = *fit;
149  /*std::cout << "FOO ";
150  BOOST_FOREACH(unsigned long a, splitCellPair.second) {
151  std::cout << (a+1) << " ";
152  }
153  std::cout << std::endl;
154  std::cout << "GOO ";
155  BOOST_FOREACH(unsigned long a, splitCellPair.first.m_fingerprint) {
156  std::cout << (a) << " ";
157  }
158  std::cout << std::endl;*/
159  if (pi.intersect(splitCellPair.second.begin(), splitCellPair.second.end(), i)) {
160  ++ret;
161  }
162  }
163  break;
164  }
165  }
166  return ret;
167 }
168 
169 template<class PERM,class MATRIX>
170 void MatrixRefinement2<PERM,MATRIX>::computeFingerprint(const Partition& pi, unsigned long i, unsigned long j, std::map<Fingerprint,std::list<unsigned long> >& map) const {
171  for (Partition::CellIt cellI = pi.cellBegin(i); cellI != pi.cellEnd(i); ++cellI) {
172  Fingerprint f(m_matrix.k());
173  for (Partition::CellIt cellJ = pi.cellBegin(j); cellJ != pi.cellEnd(j); ++cellJ) {
174  ++f[m_matrix.at(*cellI, *cellJ)];
175  }
176  std::pair<typename std::map<Fingerprint,std::list<unsigned long> >::iterator, bool> p =
177  map.insert(std::pair<Fingerprint, std::list<unsigned long> >(f, std::list<unsigned long>()));
178  std::list<unsigned long>& l = p.first->second;
179  l.push_back(*cellI);
180  }
181 }
182 
183 }
184 }
185 
186 #endif // -- MATRIXREFINEMENT2_H_
virtual bool init(Partition &pi)
initializes refinement
Definition: matrix_refinement2.h:122
base class for a -refinement which is used in an R-base and bound to an initial partition ...
Definition: refinement.h:53
partition
Definition: partition.h:48
unsigned long cells() const
number of cells in this partition
Definition: partition.h:157
bool intersect(ForwardIterator begin, ForwardIterator end, unsigned int j)
intersects the j-th cell of this partition with a given set
Definition: partition.h:186
MatrixRefinement2(unsigned long n, const MATRIX &matrix)
constructor
Definition: matrix_refinement2.h:102
virtual unsigned int apply(Partition &pi) const
applies (left-)refinement to pi which is the original partition this refinement was initialized to ...
Definition: matrix_refinement2.h:108
concrete -refinement for symmetric matrix automorphisms
Definition: matrix_refinement2.h:49
Definition: abstract_bsgs.h:49
unsigned long cellSize(unsigned int c) const
size of the c-th cell
Definition: partition.h:170