-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsims.cpp
More file actions
1893 lines (1504 loc) · 60.9 KB
/
Copy pathsims.cpp
File metadata and controls
1893 lines (1504 loc) · 60.9 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// libsemigroups_pybind11
// Copyright (C) 2024 Reinis Cirpons
//
// 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 3 of the License, or
// (at your option) any later version.
//
// 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.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// C++ stl headers....
#include <vector> // for vector
// libsemigroups....
#include <libsemigroups/presentation.hpp> // for Presentation
#include <libsemigroups/rx/ranges.hpp> // for rx::begin, rx::end, rx::transform
#include <libsemigroups/sims.hpp> // for Sims1, Sims2, ....
#include <libsemigroups/types.hpp> // for word_type
#include <libsemigroups/word-graph.hpp> // for WordGraph
// pybind11....
#include <pybind11/functional.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
// libsemigroups_pybind11....
#include "main.hpp" // for init_sims
namespace libsemigroups {
namespace py = pybind11;
using node_type = uint32_t;
using word_graph_type = WordGraph<node_type>;
using size_type = typename word_graph_type::size_type;
//////////////////////////////////////////////////////////////////////////////
// SimsSettings
//////////////////////////////////////////////////////////////////////////////
template <typename Subclass>
void bind_sims_settings(py::class_<SimsSettings<Subclass>>& ss,
std::string_view doc_type) {
using SimsSettings_ = SimsSettings<Subclass>;
ss.def(
"number_of_threads",
// TODO(0): add -> Subclass& here and everywhere else where we return a
// subclass
[](SimsSettings_& self, size_t val) -> Subclass& {
return self.number_of_threads(val);
},
py::arg("val"),
fmt::format(R"pbdoc(
:sig=(self: {0}, val: int) -> {0}:
Set the number of threads.
This function sets the number of threads to be used by :any:`Sims1` or
:any:`Sims2`. The default value is ``1``.
:param val: the maximum number of threads to use.
:type val: int
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError: if the argument *val* is 0.
)pbdoc",
doc_type)
.c_str());
ss.def(
"number_of_threads",
[](SimsSettings_ const& self) { return self.number_of_threads(); },
R"pbdoc(
Get the number of threads.
:returns: The current maximum number of threads.
:rtype: int
)pbdoc");
ss.def(
"presentation",
[](SimsSettings_& self, Presentation<word_type> const& p) -> Subclass& {
return self.presentation(p);
},
py::arg("p"),
fmt::format(R"pbdoc(
:sig=(self: {0}, p: Presentation) -> {0}:
Set the presentation over which the congruences produced by an instance are
defined.
This function sets the presentation over which the congruences produced by an
instance are defined. These are the rules used at every node in the depth first
search conducted by objects of this type. The parameter *p* is always first
converted to a :any:`Presentation` of ``list[int]`` and
it is this converted value that is used.
:param p: the presentation.
:type p: Presentation
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError:
if :any:`Presentation.throw_if_bad_alphabet_or_rules` raises an exception.
:raises LibsemigroupsError: if *p* has 0-generators and 0-relations.
)pbdoc",
doc_type)
.c_str());
ss.def(
"presentation",
[](SimsSettings_ const& self) -> auto const& {
return self.presentation();
},
fmt::format(R"pbdoc(
:sig=(self: {0}) -> Presentation:
Get the presentation over which the congruences produced by an instance are
defined.
This function returns the defining presentation of a :any:`Sims1` or
:any:`Sims2` instance. The congruences computed by :any:`Sims1.iterator` of the
appropriate subclass are defined over the semigroup or monoid defined by this
presentation.
:returns: The presentation.
:rtype: Presentation
)pbdoc",
doc_type)
.c_str(),
py::return_value_policy::reference_internal);
ss.def(
"first_long_rule_position",
[](SimsSettings_& self, size_t pos) -> Subclass& {
return self.cbegin_long_rules(pos);
},
py::arg("pos"),
fmt::format(R"pbdoc(
:sig=(self: {0}, pos: int) -> {0}:
Set the beginning of the long rules (position).
This function sets the beginning of the long rules using a position in
``self.presentation().rules``. The "long rules" are the rules used after a
complete deterministic word graph has been found in the search. If such a word
graph is compatible with the long rules specified by this function, then this
word graph is accepted, and if not it is rejected.
The purpose of this is to improve the backtrack search by reducing the time
spent processing "long" rules in each node of the search tree, and to only
check them at the leaves.
:param pos: position of the left-hand side of the first long rule.
:type pos: int
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError:
if *pos* is not a valid position in ``self.presentation().rules``.
:raises LibsemigroupsError:
if the rule at position *pos* is not the left-hand side of a rule (i.e. if
*pos* is odd).
)pbdoc",
doc_type)
.c_str());
ss.def(
"long_rules",
[](SimsSettings_ const& self) {
return py::make_iterator(self.cbegin_long_rules(),
self.presentation().rules.cend());
},
fmt::format(R"pbdoc(
:sig=(self: {0}) -> collections.abc.Iterator[tuple[list[int], list[int]]]:
Get the long rules.
Returns an iterator of long rules.
:returns: An iterator.
:rtype: collections.abc.Iterator[tuple[list[int], list[int]]]
)pbdoc",
doc_type)
.c_str());
ss.def(
"clear_long_rules",
[](SimsSettings_& self) -> Subclass& {
return self.clear_long_rules();
},
fmt::format(R"pbdoc(
Clear the set of long rules.
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
ss.def("number_of_long_rules",
&SimsSettings_::number_of_long_rules,
R"pbdoc(
Returns the number of rules marked as long rules.
:returns: The number of long rules.
:rtype: int
)pbdoc");
ss.def(
"long_rule_length",
[](SimsSettings_& self, size_t val) -> Subclass& {
return self.long_rule_length(val);
},
py::arg("val"),
fmt::format(R"pbdoc(
:sig=(self: {0}, val: int) -> {0}:
Set the length of a long rule.
Define the length of a "long" rule. This function modifies
:py:meth:`~Sims1.presentation()` so that the rules whose length (sum of the
lengths of both sides) is at least *val* (if any) occur at the end of
``presentation().rules`` and so that :py:meth:`~Sims1.long_rules` returns all
such rules. The relative orders of the rules within
:py:meth:`~Sims1.presentation` may not be preserved.
:param val: the long rule length.
:type val: int
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
ss.def(
"pruners",
[](SimsSettings_ const& s) -> auto const& { return s.pruners(); },
R"pbdoc(
Get all active pruners of the search tree.
This function returns a copy of the list of pruners. A pruner is any function that takes
as input a word graph and returns a boolean. We require that if a pruner
returns ``False`` for a word graph ``wg``, then it returns ``False`` for all
word graphs that are descended from ``wg`` in the Sims word graph search tree.
The pruners are used to refine the congruence search tree during the execution
of the Sims algorithm. As such, the congruences computed by this instance are
only taken among those whose word graphs are accepted by all pruners returned
by :py:meth:`~Sims1.pruners`.
:returns: A list of boolean functions on word graphs, the set of all pruners.
:rtype: list[collections.abc.Callable[[WordGraph], bool]]
)pbdoc", // The next line seemingly does nothing
py::return_value_policy::reference_internal);
ss.def(
"add_pruner",
[](SimsSettings_& self, SimsRefinerIdeals const& func) -> Subclass& {
if (self.presentation().alphabet().empty()
&& self.presentation().rules.empty()) {
throw pybind11::value_error(
"the 1st argument (Sims1/2) must be initialised with "
"a non-empty presentation before calling this function");
}
// Can't add this check to libsemigroups itself because it is a
// template.
if (func.presentation() != self.presentation()) {
throw pybind11::value_error(
"the 2nd argument (SimsRefinerIdeals) must be initialised with "
"the same presentation as the 1st argument (Sims1/2)");
}
return self.add_pruner(func);
},
py::arg("pruner"),
fmt::format(R"pbdoc(
:sig=(self: {0}, pruner: collections.abc.Callable[[WordGraph], bool]) -> {0}:
:only-document-once:
Add a pruner to the search tree.
:param pruner: a pruner function.
:type pruner: collections.abc.Callable[[WordGraph], bool]
:returns: The first argument *self*.
:rtype: {0}
.. warning::
When running the Sims low-index backtrack with multiple threads, each added
pruner must be guaranteed thread safe. Failing to do so could cause bad
things to happen.
)pbdoc",
doc_type)
.c_str());
ss.def(
"add_pruner",
[](SimsSettings_& self, SimsRefinerFaithful const& func) -> Subclass& {
return self.add_pruner(func);
},
py::arg("pruner"),
fmt::format(R"pbdoc(
:sig=(self: {0}, pruner: collections.abc.Callable[[WordGraph], bool]) -> {0}:
:only-document-once:
Add a pruner to the search tree.
:param pruner: a pruner function.
:type pruner: collections.abc.Callable[[WordGraph], bool]
:returns: The first argument *self*.
:rtype: {0}
.. warning::
When running the Sims low-index backtrack with multiple threads, each added
pruner must be guaranteed thread safe. Failing to do so could cause bad
things to happen.
)pbdoc",
doc_type)
.c_str());
ss.def(
"add_pruner",
[](SimsSettings_& self,
std::function<bool(word_graph_type const&)> func) -> Subclass& {
return self.add_pruner(func);
},
py::arg("pruner"),
fmt::format(R"pbdoc(
:sig=(self: {0}, pruner: collections.abc.Callable[[WordGraph], bool]) -> {0}:
:only-document-once:
Add a pruner to the search tree.
:param pruner: a pruner function.
:type pruner: collections.abc.Callable[[WordGraph], bool]
:returns: The first argument *self*.
:rtype: {0}
.. warning::
When running the Sims low-index backtrack with multiple threads, each added
pruner must be guaranteed thread safe. Failing to do so could cause bad
things to happen.
)pbdoc",
doc_type)
.c_str());
ss.def(
"clear_pruners",
[](SimsSettings_& self) -> Subclass& { return self.clear_pruners(); },
fmt::format(R"pbdoc(
Clear the set of pruners.
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
ss.def(
"included_pairs",
[](SimsSettings_ const& self) -> auto const& {
return self.included_pairs();
},
fmt::format(R"pbdoc(
:sig=(self: {0}) -> list[list[int]]:
Returns the set of pairs that must be included in every congruence.
This function returns the list of included pairs. The congruences computed by a
:any:`Sims1` or :any:`Sims2` instance always contain the relations of this
list. In other words, the congruences computed by this instance are only taken
among those that contain the pairs of elements of the underlying semigroup
(defined by the presentation returned by :py:meth:`~Sims1.presentation()` and
:py:meth:`~Sims1.long_rules()`) represented by the relations of the list of
words returned by :py:meth:`~Sims1.included_pairs()`.
:returns:
A list of words ``result`` such that ``(result[2*i], result[2*i+1])`` is
the ``i``-th included pair.
:rtype: list[list[int]]
)pbdoc",
doc_type)
.c_str());
ss.def(
"add_included_pair",
[](SimsSettings_& self, word_type const& lhs, word_type const& rhs)
-> Subclass& { return sims::add_included_pair(self, lhs, rhs); },
py::arg("lhs"),
py::arg("rhs"),
fmt::format(R"pbdoc(
:sig=(self: {0}, lhs: list[int], rhs: list[int]) -> {0}:
Add a pair that should be included in every congruence.
:param lhs: the left-hand side of the pair being added.
:type lhs: list[int]
:param rhs: the right-hand side of the pair being added.
:type rhs: list[int]
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError:
if :any:`Presentation.throw_if_letter_not_in_alphabet` raises an exception
on *lhs* or *rhs*.
)pbdoc",
doc_type)
.c_str());
ss.def(
"clear_included_pairs",
[](SimsSettings_& self) -> Subclass& {
return self.clear_included_pairs();
},
fmt::format(R"pbdoc(
Clear the set of included pairs.
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
ss.def(
"excluded_pairs",
[](SimsSettings_ const& self) { return self.excluded_pairs(); },
fmt::format(R"pbdoc(
:sig=(self: {0}) -> list[list[int]]:
Returns the set of pairs that must be excluded from every congruence.
This function returns the list of excluded pairs. The congruences computed by a
:any:`Sims1` or :any:`Sims2` instance will never contain the relations of this
list. In other words, the congruences computed by this instance are
only taken among those that do not contain any of the pairs of elements of the
underlying semigroup (defined by the presentation returned by
:py:meth:`~Sims1.presentation()` and :py:meth:`~Sims1.long_rules()`)
represented by the relations of the list of words returned by
:py:meth:`~Sims1.excluded_pairs()`.
:returns:
A list of words ``result`` such that ``(result[2*i], result[2*i+1])`` is
the ``i``-th excluded pair.
:rtype: list[list[int]]
)pbdoc",
doc_type)
.c_str());
ss.def(
"add_excluded_pair",
[](SimsSettings_& self, word_type const& lhs, word_type const& rhs)
-> Subclass& { return sims::add_excluded_pair(self, lhs, rhs); },
py::arg("lhs"),
py::arg("rhs"),
fmt::format(R"pbdoc(
:sig=(self: {0}, lhs: list[int], rhs: list[int]) -> {0}:
Add a pair that must be excluded from every congruence.
:param lhs: the left-hand side of the pair being added.
:type lhs: list[int]
:param rhs: the right-hand side of the pair being added.
:type rhs: list[int]
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError:
if :any:`Presentation.throw_if_letter_not_in_alphabet` raises
an exception on *lhs* or *rhs*.
)pbdoc",
doc_type)
.c_str());
ss.def(
"clear_excluded_pairs",
[](SimsSettings_& self) -> Subclass& {
return self.clear_excluded_pairs();
},
fmt::format(R"pbdoc(
Clear the set of excluded pairs.
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
ss.def("stats",
&SimsSettings_::stats,
R"pbdoc(
Get the current stats object.
This function returns the current stats object. The value returned by this
function is a :any:`SimsStats` object which contains some statistics related to
the current :any:`Sims1` or :any:`Sims2` instance and any part of the depth
first search already conducted.
:returns: The :any:`SimsStats` object containing the current stats.
:rtype: SimsStats
)pbdoc");
ss.def(
"idle_thread_restarts",
[](SimsSettings_ const& self) { return self.idle_thread_restarts(); },
R"pbdoc(
Get the idle thread restart attempt count.
This function returns the number of times an idle thread will attempt to
restart before yielding during execution.
:returns: The number of idle thread restarts.
:rtype: int
)pbdoc");
ss.def(
"idle_thread_restarts",
[](SimsSettings_& self, size_t val) -> Subclass& {
return self.idle_thread_restarts(val);
},
py::arg("val"),
fmt::format(R"pbdoc(
:sig=(self: {0}, val: int) -> {0}:
Set the idle thread restart attempt count.
This function sets the idle thread restart attempt count. The default value is
``64``.
:param val:
the maximum number of times an idle thread will attempt to restart before
yielding.
:type val: int
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError: if the argument *val* is 0.
)pbdoc",
doc_type)
.c_str());
}
//////////////////////////////////////////////////////////////////////////////
// Sims1, Sims2, RepOrc and MinimalRepOrc common functions
//////////////////////////////////////////////////////////////////////////////
// Some of these have been moved out of the base SimsSettings class. See
// https://github.com/libsemigroups/libsemigroups_pybind11/issues/305
template <typename Thing, typename ThingBase>
void def_sims_reporc_common(py::class_<Thing, ThingBase>& thing,
std::string_view doc_type) {
thing.def("__repr__",
[](Thing const& self) { return to_human_readable_repr(self); });
thing.def(py::init<>(),
fmt::format(R"pbdoc(
:sig=(self: {0}, word: type) -> None:
This function returns an uninitialized :any:`{0}` object that uses
words of the type specified by *word*.
:Keyword Arguments:
* **word** (*type*) -- the type of words to use, must be ``list[int]``.
)pbdoc",
doc_type)
.c_str());
// TODO(0): Uncomment or remove
// thing.def(py::init<Thing const&>(),
// fmt::format(R"pbdoc(
// Construct from a {0} object.
// )pbdoc",
// doc_type)
// .c_str());
thing.def(
"init",
[](Thing& self) -> Thing& { return self.init(); },
fmt::format(R"pbdoc(
Reinitialize an existing :any:`{0}` object.
This function puts a :any:`{0}` object back into the same state as if it had
been newly default constructed.
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
thing.def(
"init",
[](ThingBase& self, Thing const& that) -> Thing& {
return self.init(that);
},
py::arg("that"),
fmt ::format(R"pbdoc(
Reinitialize an existing :any:`{0}` object.
This function re-initializes a :any:`{0}` instance to be in the same state as
*that*.
:param that: The instance used for reinitialization.
:type that: {0}
:returns: The re-initialized object.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
}
//////////////////////////////////////////////////////////////////////////////
// Sims1 and Sims2 common functions
//////////////////////////////////////////////////////////////////////////////
template <typename Thing, typename ThingBase>
void def_sims_common(py::class_<Thing, ThingBase>& thing,
std::string_view doc_type) {
def_sims_reporc_common(thing, doc_type);
thing.def(py::init<Presentation<word_type> const&>(),
py::arg("p"),
fmt::format(R"pbdoc(
:sig=(self: {0}, p: Presentation) -> None:
Construct from a presentation.
The rules of the presentation *p* are used at every node in the depth first
search conducted by an object of this type.
:param p: the presentation to construct from.
:type p: Presentation
:raises LibsemigroupsError:
if :any:`Presentation.throw_if_bad_alphabet_or_rules` raises an exception.
:raises LibsemigroupsError:
if *p* has 0-generators and 0-relations.
.. seealso:: :any:`{0}.presentation`, :any:`{0}.init`
)pbdoc",
doc_type)
.c_str());
thing.def("__copy__", [](Thing const& self) { return Thing(self); });
thing.def(
"copy",
[](Thing const& self) { return Thing(self); },
fmt::format(R"pbdoc(
Copy a :any:`{0}` object.
:returns: A copy.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
thing.def(
"init",
[](Thing& self, Presentation<word_type> const& p) -> Thing& {
return self.init(p);
},
py::arg("p"),
fmt::format(R"pbdoc(
:sig=(self: {0}, p: Presentation) -> {0}:
Reinitialize an existing :any:`{0}` object from a presentation.
This function puts an object back into the same state as if it had been newly
constructed from the presentation *p*.
:param p: the presentation.
:type p: Presentation
:returns: The first argument *self*.
:rtype: {0}
:raises LibsemigroupsError:
if :any:`Presentation.throw_if_bad_alphabet_or_rules` raises an exception.
:raises LibsemigroupsError: if *p* has 0-generators and 0-relations.
)pbdoc",
doc_type)
.c_str());
thing.def("number_of_congruences",
&Thing::number_of_congruences,
py::arg("n"),
fmt::format(R"pbdoc(
:sig=(self: {0}, n: int) -> int:
Returns the number of one-sided congruences with up to a given number of
classes.
This function exists to:
* provide some feedback on the progress of the computation if it runs for more
than 1 second.
* allow for the computation of the number of congruences to be performed using
:py:meth:`~{0}.number_of_threads` in parallel.
:param n: the maximum number of congruence classes.
:type n: int
:returns:
The number of one-sided congruences with at most *n* congruence classes.
:rtype: int
:raises LibsemigroupsError: if *n* is ``0``.
:raises LibsemigroupsError:
if :py:meth:`~{0}.presentation()` has 0-generators and 0-relations (i.e.
it has not been initialised).
)pbdoc",
doc_type)
.c_str());
thing.def("for_each",
&Thing::for_each,
py::arg("n"),
py::arg("pred"),
fmt::format(R"pbdoc(
:sig=(self: {0}, n: int, pred: collections.abc.Callable[[WordGraph], None]) -> None:
Apply a unary predicate to every one-sided congruence with at most a given
number of classes.
This function applies the function *pred* to every one-sided congruence with at
most *n* classes. This function exists to:
* provide some feedback on the progress of the computation if it runs for more
than 1 second.
* allow for a function to be applied to all found word graphs using
:py:meth:`~{0}.number_of_threads` in parallel.
:param n: the maximum number of congruence classes.
:type n: int
:param pred: the predicate applied to every congruence found.
:type pred: collections.abc.Callable[[WordGraph], None]
:raises LibsemigroupsError: if *n* is ``0``.
:raises LibsemigroupsError:
if :py:meth:`~{0}.presentation()` has 0-generators and 0-relations (i.e.
it has not been initialised).
.. seealso:: :py:meth:`~{0}.iterator`, :py:meth:`~{0}.find_if`
)pbdoc",
doc_type)
.c_str());
thing.def("find_if",
&Thing::find_if,
py::arg("n"),
py::arg("pred"),
fmt::format(R"pbdoc(
:sig=(self: {0}, n: int, pred: collections.abc.Callable[[WordGraph], bool]) -> WordGraph:
Apply a unary predicate to one-sided congruences with at most a given number of
classes, until it returns ``True``.
This function applies the predicate *pred* to every congruence with at most *n*
classes, until a congruence satisfying the predicate is found. This function
exists to:
* provide some feedback on the progress of the computation if it runs for more
than 1 second.
* allow for searching for a congruence satisfying certain conditions using
:py:meth:`~{0}.number_of_threads` in parallel.
:param n: the maximum number of congruence classes.
:type n: int
:param pred: the predicate applied to every congruence found.
:type pred: collections.abc.Callable[[WordGraph], bool]
:returns:
The first :any:`WordGraph` for which *pred* returns ``True``, or the empty
word graph if no such word graph exists.
:rtype: WordGraph
:raises LibsemigroupsError: if *n* is ``0``.
:raises LibsemigroupsError:
if :py:meth:`~{0}.presentation()` has 0-generators and 0-relations (i.e.
it has not been initialised).
.. seealso:: :py:meth:`~{0}.iterator`, :py:meth:`~{0}.for_each`
)pbdoc",
doc_type)
.c_str());
thing.def(
"iterator",
[](Thing const& self, size_type n) {
return py::make_iterator(self.cbegin(n), self.cend(n));
},
py::arg("n"),
fmt::format(R"pbdoc(
:sig=(self: {0}, n: int) -> collections.abc.Iterator[WordGraph]:
Returns an iterator yielding all congruences of index at most *n*.
This function returns an iterator yielding instances of :any:`WordGraph` that
represent the congruences with at most *n* classes. The order in which the
congruences are yielded by the iterator is implementation specific. The meaning
of the :any:`WordGraph` objects yielded by the iterator depends on whether the
input is a monoid presentation (i.e.
:py:meth:`~Presentation.contains_empty_word()` returns ``True``) or a
semigroup presentation.
If the input is a monoid presentation for a monoid :math:`M`, then the
:any:`WordGraph` pointed to by an iterator of this type has at most *n* nodes,
and the right action of :math:`M` on the nodes of the word graph is isomorphic
to the action of :math:`M` on the classes of a right congruence.
If the input is a semigroup presentation for a semigroup :math:`S`, then the
:any:`WordGraph` has at most *n* + 1 nodes, and the right action of :math:`S`
on the nodes :math:`\{{1, \ldots, n\}}` of the :any:`WordGraph` is isomorphic to
the action of :math:`S` on the classes of a right congruence. It'd probably be
better in this case if node :math:`0` were not included in the output
:any:`WordGraph`, but it is required in the implementation of the low-index
congruence algorithm, and to avoid unnecessary copies, we've left it in for the
time being.
:param n: the maximum number of classes in a congruence.
:type n: int
:returns:
An iterator ``it`` yielding :any:`WordGraph` objects with at most *n* or
*n* + 1 nodes depending on the presentation, see above.
:rtype: collections.abc.Iterator[WordGraph]
:raises LibsemigroupsError: if *n* is ``0``.
:raises LibsemigroupsError:
if :py:meth:`~{0}.presentation()` has 0-generators and 0-relations (i.e.
it has not been initialised).
)pbdoc",
doc_type)
.c_str());
}
//////////////////////////////////////////////////////////////////////////////
// RepOrc and MinimalRepOrc common functions
//////////////////////////////////////////////////////////////////////////////
template <typename Thing, typename ThingBase>
void def_reporc_common(py::class_<Thing, ThingBase>& thing,
std::string_view doc_type) {
def_sims_reporc_common(thing, doc_type);
thing.def(
"target_size",
[](Thing const& self) { return self.target_size(); },
fmt::format(R"pbdoc(
Get the current target size.
This function returns the current value for the target size, i.e. the desired
size of the transformation semigroup corresponding to the :any:`WordGraph`
returned by the function :py:meth:`~{0}.word_graph`.
:returns: A value of type ``int``.
:rtype: int
)pbdoc",
doc_type)
.c_str());
thing.def(
"target_size",
[](Thing& self, size_t val) -> Thing& { return self.target_size(val); },
py::arg("val"),
fmt::format(R"pbdoc(
:sig=(self: {0}, val: int) -> {0}:
Set the target size.
This function sets the target size, i.e. the desired size of the transformation
semigroup corresponding to the :any:`WordGraph` returned by the function
:py:meth:`~{0}.word_graph`.
:param val: the target size.
:type val: int
:returns: The first argument *self*.
:rtype: {0}
)pbdoc",
doc_type)
.c_str());
}
void init_sims(py::module& m) {
////////////////////////////////////////////////////////////////////////////
// SimsStats
////////////////////////////////////////////////////////////////////////////
py::class_<SimsStats> st(m,
"SimsStats",
R"pbdoc(
For keeping track of various statistics arising during the runtime of the low
index algorithm.
The purpose of this class is to collect some statistics related to :any:`Sims1`
or :any:`Sims2`.
.. seealso:: :any:`Sims1`, :any:`Sims2`
)pbdoc");
st.def("__repr__",
[](SimsStats const& st) { return to_human_readable_repr(st); });
st.def(
"count_last",
[](SimsStats const& d) { return d.count_last.load(); },
R"pbdoc(
Get the number of congruences found at time of last report.
This function returns the member that holds the number of congruences found by the
:any:`Sims1` or :any:`Sims2` algorithm at the time of the last call to
:py:meth:`~SimsStats.stats_check_point`.
:returns: The number of congruences.
:rtype: int
.. seealso::
:py:meth:`~SimsStats.stats_check_point`, :py:meth:`~SimsStats.count_now`
)pbdoc");
st.def(
"count_now",
[](SimsStats const& d) { return d.count_now.load(); },
R"pbdoc(
Get the number of congruences found up to this point.
This function returns the total number of congruences found during the running
of the :any:`Sims1` or :any:`Sims2` algorithm.
:returns: The number of congruences.
:rtype: int
.. seealso:: :py:meth:`~SimsStats.count_last`
)pbdoc");
st.def(
"max_pending",
[](SimsStats const& d) { return d.max_pending.load(); },
R"pbdoc(
Get the maximum number of pending definitions.
A "pending definition" is just an edge that will be defined at some point in
the future in the :any:`WordGraph` represented by a :any:`Sims1` or
:any:`Sims2` instance at any given moment. This function returns the maximum
number of such pending definitions that occur during the running of the
algorithms in :any:`Sims1` or :any:`Sims2`.
:returns: The maximum number of definitions.
:rtype: int
)pbdoc");
st.def(
"total_pending_last",
[](SimsStats const& d) { return d.total_pending_last.load(); },
R"pbdoc(
Get the total number of pending definitions at time of last report.
A "pending definition" is just an edge that will be defined at some point in
the future in the :any:`WordGraph` represented by a :any:`Sims1` or
:any:`Sims2` instance at any given moment. This function returns the total
number of pending definitions that occur at the time of the last call to
:py:meth:`~SimsStats.stats_check_point`. This is the same as the number of
nodes in the search tree encountered during the running of :any:`Sims1` or
:any:`Sims2`.
:returns: The number of pending definitions.
:rtype: int
.. seealso::
:py:meth:`~SimsStats.stats_check_point`,
:py:meth:`~SimsStats.total_pending_now`
)pbdoc");
st.def(
"total_pending_now",
[](SimsStats const& d) { return d.total_pending_now.load(); },
R"pbdoc(