Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bijectionist/improve init #39313

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions src/sage/combinat/bijectionist.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@
sage: A = B = [permutation for n in range(4) for permutation in Permutations(n)]
sage: tau = Permutation.longest_increasing_subsequence_length
sage: bij = Bijectionist(A, B, tau, value_restrictions=((Permutation([1, 2]), [4, 5]),))
sage: next(bij.solutions_iterator())
Traceback (most recent call last):
...
ValueError: no possible values found for singleton block [[1, 2]]
Expand Down Expand Up @@ -494,7 +495,7 @@
Check that large input sets are handled well::

sage: A = B = range(20000)
sage: bij = Bijectionist(A, B) # long time
sage: bij = Bijectionist(A, B)
"""
# glossary of standard letters:
# A, B, Z, W ... finite sets
Expand Down Expand Up @@ -612,7 +613,7 @@
for a in p:
self._P.union(p[0], a)

self._compute_possible_block_values()
self._possible_block_values = None

def constant_blocks(self, singletons=False, optimal=False):
r"""
Expand Down Expand Up @@ -1093,8 +1094,7 @@
if not self._possible_block_values[p]:
if len(block) == 1:
raise ValueError(f"no possible values found for singleton block {block}")
else:
raise ValueError(f"no possible values found for block {block}")
raise ValueError(f"no possible values found for block {block}")

def set_distributions(self, *elements_distributions):
r"""
Expand Down Expand Up @@ -1789,13 +1789,18 @@
# veto new value and try again
tmp_constraints.append(bmilp._x[p, solution[p]] == 0)

# create dictionary to return
possible_values = {}
for p in blocks:
for a in self._P.root_to_elements_dict()[p]:
if optimal:
# create dictionary to return
possible_values = {}
for p in blocks:
for a in self._P.root_to_elements_dict()[p]:
possible_values[a] = solutions[p]
else:
else:
# create dictionary to return
if self._possible_block_values is None:
self._compute_possible_block_values()
possible_values = {}
for p in blocks:
for a in self._P.root_to_elements_dict()[p]:
possible_values[a] = self._possible_block_values[p]

return possible_values
Expand Down Expand Up @@ -2124,11 +2129,6 @@
In particular, if `P` consists only if singletons, this
method has no effect.

.. TODO::

create one test with one and one test with two
intertwining relations

.. TODO::

it is not clear whether this method makes sense
Expand All @@ -2144,6 +2144,17 @@
sage: bij._P
{{'a'}, {'b'}, {'c'}, {'d'}}

However, adding that `'a'` and `'c'` are in the same block,
we can infer that also `'b'` and `'d'` are in the same
block::

sage: bij.set_constant_blocks([['a', 'c']])
sage: bij._P
{{'a', 'c'}, {'b'}, {'d'}}
sage: bij._preprocess_intertwining_relations()
sage: bij._P
{{'a', 'c'}, {'b', 'd'}}

Let a group act on permutations::

sage: A = B = Permutations(3)
Expand All @@ -2152,6 +2163,11 @@
sage: bij._preprocess_intertwining_relations()
sage: bij._P
{{[1, 2, 3]}, {[1, 3, 2]}, {[2, 1, 3]}, {[2, 3, 1]}, {[3, 1, 2]}, {[3, 2, 1]}}

Thus, in this case we do not detect the constant blocks::

sage: bij.constant_blocks(optimal=True)
{{[1, 2, 3], [3, 2, 1]}, {[1, 3, 2], [2, 3, 1]}, {[2, 1, 3], [3, 1, 2]}}
"""
A = self._A
P = self._P
Expand Down Expand Up @@ -3119,7 +3135,7 @@
sage: bij = Bijectionist(sum(As, []), sum(Bs, []))
sage: bij.set_statistics((lambda x: x[0], lambda x: x[0]))
sage: bij.set_intertwining_relations((2, c1, c1), (1, c2, c2))
sage: l = list(bij.solutions_iterator()); len(l) # long time -- (2.7 seconds with SCIP on AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx)

Check warning on line 3138 in src/sage/combinat/bijectionist.py

View workflow job for this annotation

GitHub Actions / test-long (src/sage/[a-f]*)

Warning: slow doctest:

slow doctest:
64

A brute force check would be difficult::
Expand Down Expand Up @@ -3147,7 +3163,7 @@
sage: A = sum(As, [])
sage: respects_c1 = lambda s: all(c1(a1, a2) not in A or s[c1(a1, a2)] == c1(s[a1], s[a2]) for a1 in A for a2 in A)
sage: respects_c2 = lambda s: all(c2(a1) not in A or s[c2(a1)] == c2(s[a1]) for a1 in A)
sage: l2 = [s for s in it if respects_c1(s) and respects_c2(s)] # long time -- (17 seconds on AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx)

Check warning on line 3166 in src/sage/combinat/bijectionist.py

View workflow job for this annotation

GitHub Actions / test-long (src/sage/[a-f]*)

Warning: slow doctest:

slow doctest:
sage: sorted(l1, key=lambda s: tuple(s.items())) == l2 # long time
True

Expand Down
Loading