File: switch_table.h

package info (click to toggle)
polymake 4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,528 kB
  • sloc: cpp: 152,204; perl: 43,222; javascript: 30,700; ansic: 2,937; java: 2,654; python: 641; sh: 244; xml: 117; makefile: 62
file content (421 lines) | stat: -rw-r--r-- 13,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* Copyright (c) 1997-2018
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#include "polymake/client.h"
#include "polymake/Map.h"
#include "polymake/group/orbit.h"
#include "polymake/linalg.h"
#include "polymake/Set.h"
#include <stack>
#include <sstream>


// These declarations are necessary, so we friend this class/struct later for
// accessing private members in serialization.
namespace polymake { namespace group {
class SwitchTable;
}}
namespace pm {
   template<> struct spec_object_traits< pm::Serialized< polymake::group::SwitchTable > >;
}


namespace polymake { namespace group {

namespace switchtable {

inline bool fixes(Int i, const Array<Int>& g){
   return g[i] == i;
}

// Check whether a permutation (non-)fixes entry n(=i).
struct non_fixed {
   Int i;
   non_fixed(Int n): i(n){}
   bool operator() (const Array<Int>& g) { return !fixes(i, g); }
};

class Core {
public:
   private:
      Array<Int> identity;
      Map<Int, Map<Int, Array<Int>>> switch_table;
      Map<Int, pm::Set<Int>> supports;
      Int bound;

      friend class polymake::group::SwitchTable;
      friend struct pm::spec_object_traits< pm::Serialized< polymake::group::SwitchTable > >;

      void extract_supports() {
         for(const auto& level : switch_table){
            for(const auto& g:level.second){
               supports[level.first] += g.first;
            }
         }
      }

      Int nFixedPts(const Array<Int>& g){
         Int result = 0;
         while(fixes(result, g)){
            result++;
         }
         return result;
      }

      void extract_switches(const Array<Array<Int>>& all){
         Map<Int, std::list<Array<Int>> > fixed_pt_filter;
         for(const auto& elem : all){
            if(elem != identity){
               fixed_pt_filter[0].push_back(elem);
            }
         }
         Int i = 0;
         // Filter by fixed pts
         while(fixed_pt_filter[i].size() != 0){
            fixed_pt_filter[i+1] = fixed_pt_filter[i];
            fixed_pt_filter[i+1].remove_if(non_fixed(i));
            i++;
         }
         bound = i-1;
         // Populate actual switch table
         for(Int j=0; j<=bound; j++){
            for(const auto& g : fixed_pt_filter[j]){
               i = j;
               while(g[i] != j){
                  i++;
               }
               if(i != j){
                  if(!switch_table[j].exists(i)){
                     (switch_table[j])[i] = g;
                  }
               }
            }
         }
         bound++;
      }

   public:
      Core(const Array<Array<Int>>& all) {
         identity = Array<Int>(all[0].size());
         for(Int j=0; j<identity.size(); j++){
            identity[j] = j;
         }
         extract_switches(all);
         extract_supports();
      }

      Core() {}

      std::string to_string() const {
         std::ostringstream bos;
         wrap(bos) << "  Supports: (size, content)"<<endl;
         for(const auto& level : switch_table){
            wrap(bos) << "Level " << level.first << ": " << level.second.size() << " " << supports[level.first] << endl;
         }
         wrap(bos) << "  Entries:" << endl;
         for(const auto& level : switch_table){
            for(const auto& sublevel : level.second) {
               Int i = level.first;
               Int j = sublevel.first;
               wrap(bos) << "[" << i << "," << j << "]: " << sublevel.second << endl;
            }
         }
         return bos.str();
      }
      
      void extract_switches(Int fixed, const pm::Set<Int>& desired, std::list<const Array<Int>*>& switches) const {
         for(const auto& j : desired){
            switches.push_back(&(switch_table[fixed][j]));
         }
      }

      Int get_bound() const {
         return bound;
      }

      const Array<Int>& get_identity() const {
         return identity;
      }

      bool support_exists(Int fixed) const {
         return supports.exists(fixed);
      }

      const pm::Set<Int>& get_support(Int fixed) const {
         return supports[fixed];
      }

};

template<typename CoreType, typename ActedOn>
class Optimizer {
   private:
      const CoreType& core;
      ActedOn currentOptimal;
      Array<Int> optimalSwitch;
      // We need four stacks for our DFS
      // 1. switchStack collects the switches we have at every level
      // 2. iteratorStack contains iterators pointing in the switchStack
      // 3. currentSwitchStack is the switch we applied previously
      // 4. vStack contains the object we are optimizing after applying currentSwitchStack.top()
      std::stack<std::list<const Array<Int>*>> switchStack;
      std::stack<std::list<const Array<Int>*>::const_iterator> iteratorStack;
      std::stack<Array<Int>> currentSwitchStack;
      std::stack<ActedOn> vStack;
      // Records level we are at in DFS
      Int fixed;

      // Check whether there are any switches that potentially improve a
      // vector. If there are any elements, they are stored in the list
      // 'switches'.
      // First we check whether there are any switches at level 'fixed' at all.
      // Then we ask the element v, which switches would be convenient for it.
      inline void find_next_switches(std::list<const Array<Int>*>& switches, const ActedOn& v, bool& applyIdentity){
         if(core.support_exists(fixed)){
            const pm::Set<Int> goodSupport = v.get_support(fixed, core.get_support(fixed), applyIdentity);
            core.extract_switches(fixed, goodSupport, switches);
         } else {
            applyIdentity = true;
         }
      }

      inline void update_optimal(const ActedOn& ao, const Array<Int>& currentSwitch){
         if(ao > currentOptimal){
            currentOptimal = ao;
            optimalSwitch = Array<Int>(currentSwitch);
         }
      }
      
      inline void init() {
         fixed = 0;
         currentSwitchStack.push(core.get_identity());
         vStack.push(ActedOn(currentOptimal));
      }

      inline bool atLeaf() {
         return fixed >= core.get_bound();
      }

      inline void compute_next_switches(bool& applyIdentity){
         std::list<const Array<Int>*> switches;
         find_next_switches(switches, vStack.top(), applyIdentity);
         switchStack.push(std::move(switches));
         iteratorStack.push(switchStack.top().begin());
      }

      // Backtrack in DFS tree. If we were at a leaf then not all stacks need
      // to be pruned.
      inline void backtrack() {
         iteratorStack.pop();
         switchStack.pop();
         vStack.pop();
         currentSwitchStack.pop();
         fixed--;
      }
      inline void backtrack_leaf() {
         update_optimal(vStack.top(), currentSwitchStack.top());
         vStack.pop();
         currentSwitchStack.pop();
         fixed--;
      }
      
      // Descend in DFS tree. Avoid multiplication in case we are applying the
      // identity.
      inline void descend() {
         const Array<Int>* g = *(iteratorStack.top());
         // std::cout << "Applying " << g << std::endl;
         vStack.push(std::move(vStack.top().mutate(g)));
         currentSwitchStack.push(action<on_container>(currentSwitchStack.top(),*g));
         ++iteratorStack.top();
         fixed++;
      }
      inline void descend_identity() {
         vStack.push(vStack.top());
         currentSwitchStack.push(currentSwitchStack.top());
         fixed++;
      }


   public:
      Optimizer(const CoreType& c, const ActedOn& ao_in):
         core(c), currentOptimal(ao_in), optimalSwitch(core.get_identity()) {}
      
      void optimize(){
         // The following is a depth first search with three different stacks.
         // At every level we have certain switches we can apply that improve
         // the element we act on. We only update the optimal at the leaves.
         init();
         while(vStack.size()>0){
            if(atLeaf()){
               backtrack_leaf();
            } else {
               // Are the switches already computed?
               if(vStack.size() > switchStack.size()){
                  bool applyIdentity = false;
                  compute_next_switches(applyIdentity);
                  if(applyIdentity){
                     descend_identity();
                  } else {
                     descend();
                  }
               } else {
                  // Are we at the end?
                  if(iteratorStack.top() == switchStack.top().end()){
                     backtrack();
                  } else {
                     descend();
                  }
               }
            }
         }
      }

      std::pair<const ActedOn&, const Array<Int>&> get_optimal() const {
         return std::pair<const ActedOn&, const Array<Int>&>(currentOptimal, optimalSwitch);
      }
};

template<typename Scalar>
class PackagedVector {
   
   private:
      Vector<Scalar> v;
      using SupportsMapType = Map<Scalar, Set<Int>>;
      SupportsMapType supports;
      
      PackagedVector(const Vector<Scalar>& w, const Map<Scalar, Set<Int>>& s): v(w), supports(s){}

   public:

      PackagedVector(const Vector<Scalar>& w): v(w){
         for(Int i=0; i<v.dim(); i++){
            supports[v[i]] += i;
         }
      }

      // We are given the support at a certain level of the switch table. Now
      // we run through the levels of our vector to find one that intersects
      // the support non-trivially. This will always terminate, since the
      // identity always satisfies the condition imposed and the identity is
      // contained in the set 'in'.
      const pm::Set<Int> get_support(const Int& fixed, const pm::Set<Int>& in, bool& applyIdentity) const {
         pm::Set<Int> intersection;
         for(const auto& desired : supports){
            if(desired.first > v[fixed]) { break; }
            intersection = desired.second * in;
            if(!intersection.empty()){
               if(desired.first == v[fixed]){
                  applyIdentity = true;
               }
               return intersection;
            }
         }
         applyIdentity = true;
         return pm::Set<Int>();
      }

      const Vector<Scalar>& inner() const {
         return v;
      }
      
      bool operator >(const PackagedVector& other) const {
         return -1 == lex_compare(v, other.v);
      }

      PackagedVector mutate(const Array<Int>* g) const {
         Vector<Scalar> newV(action_inv<on_container>(*g, v));
         return PackagedVector(newV);
      }
};

} // end namespace switchtable


class SwitchTable {
   private:
      switchtable::Core core;
      friend struct pm::spec_object_traits< pm::Serialized< polymake::group::SwitchTable > >;

   public:
      SwitchTable(const Array<Array<Int>>& all) : core(all) {}

      SwitchTable() {}

      template<typename Scalar>
         std::pair<Vector<Scalar>, Array<Int>> lex_minimize_vector(const Vector<Scalar>& v) const {
            switchtable::PackagedVector<Scalar> pv(v);
            switchtable::Optimizer<switchtable::Core, switchtable::PackagedVector<Scalar>> sto(core, pv);
            sto.optimize();
            auto result = sto.get_optimal();
            return std::pair<Vector<Scalar>, Array<Int>>(result.first.inner(), result.second);
         }

      template<typename Scalar>
         std::pair<Vector<Scalar>, Array<Int>> lex_maximize_vector(const Vector<Scalar>& v) const {
            auto result = lex_minimize_vector<Scalar>(-v);
            return std::pair<Vector<Scalar>, Array<Int>>(-result.first, result.second);
         }

      bool operator==(const SwitchTable& other) const {
         return core.switch_table == other.core.switch_table;
      }

      template <typename Output> friend
      Output& operator<< (GenericOutput<Output>& out, const SwitchTable& me)
      {
         out.top() << me.core.to_string();
         return out.top();
      }

};



} // end namespace group
} // end namespace polymake

namespace pm{
   template<>
      struct spec_object_traits< Serialized< polymake::group::SwitchTable > > :
      spec_object_traits<is_composite> {

         typedef polymake::group::SwitchTable masquerade_for;

         typedef Map<Int, Map<Int, Array<Int>>> elements;

         template <typename Me, typename Visitor>
            static void visit_elements(Me& me, Visitor& v) //for data_load
            {
               v << me.core.switch_table;
               me.core.extract_supports();
            }

         template <typename Visitor>
            static void visit_elements(const pm::Serialized<masquerade_for>& me, Visitor& v) //for data_save
            {
               v << me.core.switch_table;
            }
      };
      
}


// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: