Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
6d7ab9c
initial commit
mantepse Oct 18, 2022
6149eb2
make the linter happier
mantepse Oct 18, 2022
9d4bfb9
remove useless assignment
mantepse Oct 18, 2022
6ee34e1
make initialisation more efficient
mantepse Oct 18, 2022
1a14bcf
consistently name variable
mantepse Oct 18, 2022
0901ec7
slightly improve documentation
mantepse Oct 19, 2022
c39d653
non-copying intersection, to save memory when there are almost no res…
mantepse Nov 2, 2022
2f0bb0e
Merge branch 'develop' of trac.sagemath.org:sage into t/33238/a_bijec…
mantepse Dec 5, 2022
321ba43
start to cache solutions
mantepse Dec 5, 2022
306395c
finish implementation of cache
mantepse Dec 21, 2022
1a8e564
Merge branch 'develop' of trac.sagemath.org:sage into t/33238/a_bijec…
mantepse Dec 21, 2022
1ac0978
add some documentation and doctests, slightly simplify code
mantepse Dec 21, 2022
47945ac
add missing documentation in table of contents
mantepse Dec 22, 2022
19c3d8f
mark doctests as long, slightly simplify logic
mantepse Dec 22, 2022
eca857e
slightly simplify, more doctests
mantepse Dec 22, 2022
a04d146
doctest _find_counter_example
mantepse Dec 22, 2022
3508426
doctest add_distribution_constraints and add_intertwing_relation_cons…
mantepse Dec 22, 2022
0ac618c
doctest _preprocess_intertwining_relations, _solution, _show_bmilp, _…
mantepse Dec 22, 2022
d50b62c
expand docstring of main class
mantepse Dec 23, 2022
288e391
copy milp instead of adding and removing constraints
mantepse Dec 23, 2022
d57c8e5
derandomize a test, mark example as random
mantepse Dec 23, 2022
7353fb5
correct typo, remove useless assignment
mantepse Dec 23, 2022
db850f0
add and remove constraints instead of copying the whole program
mantepse Dec 23, 2022
ec3271c
fix problem with SCIP, add timings
mantepse Dec 23, 2022
dfdc6ba
use convert for MixedIntegerLinearProgram.get_values
mantepse Dec 24, 2022
d0836cb
include information on MILP also in public documentation
mantepse Dec 25, 2022
563af98
remove unnecessary copy
mantepse Dec 25, 2022
590bae2
add possibility to constrain to involutions
mantepse Dec 26, 2022
da0655b
correct indentation
mantepse Dec 26, 2022
03b1fb8
better handling of empty constraints
mantepse Dec 29, 2022
a3364de
move _show to the _BijectionistMILP class
mantepse Dec 29, 2022
9d83a6c
remove unused code for backends that do not support removing constraints
mantepse Dec 30, 2022
864029d
make _solution a public method of _BijectionistMILP, simplify solve
mantepse Dec 30, 2022
64101a8
eliminate _initialize_new_bmilp
mantepse Dec 30, 2022
2d34911
slightly simplify logic of _forced_constant_blocks, use defaultdict
mantepse Dec 30, 2022
c2f0062
slight simplification
mantepse Dec 30, 2022
78968ae
copy (instead of deepcopy) should be correct
mantepse Dec 31, 2022
6ddaeae
slightly simplify _preprocess_intertwining_relations
mantepse Dec 31, 2022
60286e6
slightly improve non_copying_intersection
mantepse Dec 31, 2022
c38f5b9
add some internal documentation
mantepse Dec 31, 2022
6fb06f0
untangle _preprocess_intertwining_relations
mantepse Jan 2, 2023
706056e
add possibility to require a homomesy
mantepse Jan 2, 2023
d8a0663
Merge branch 'u/mantepse/mixedintegerlinearprogram_add_constraint__re…
mantepse Jan 2, 2023
4e90280
Merge branch 'u/mantepse/allow_to_remove_no_constraints' of trac.sage…
mantepse Jan 2, 2023
65b0c4a
preserve the cache of solutions after computing the optimal constant …
mantepse Jan 2, 2023
21cb54f
make _BijectionistMILP.solution the only entrypoint
mantepse Jan 3, 2023
a573bb4
remove unnecessary calls to list in doctests
mantepse Jan 3, 2023
6868261
move iterator over all solutions to _BijectionistMILP
mantepse Jan 3, 2023
61e97bc
merge _solve, solution and __iter__
mantepse Jan 4, 2023
db8947b
pycodestyle stuff
mantepse Jan 4, 2023
9678717
Merge branch 'develop' of trac.sagemath.org:sage into t/33238/a_bijec…
mantepse Jan 20, 2023
53506aa
rename pseudo_inverse to quadratic
mantepse Jan 20, 2023
d61c630
Merge branch 'u/mantepse/a_bijectionist_s_toolkit' of https://github.…
mantepse Feb 10, 2023
fefe54a
Merge branch 'develop' of github.com:sagemath/sage into develop
mantepse Feb 10, 2023
31bfb69
fix docstrings, simplify some tests
mantepse Feb 11, 2023
24e4084
change lambda to def
mantepse Feb 11, 2023
e7e063d
Merge branch 'develop' into develop
mantepse Feb 13, 2023
dc3009a
reviewer's suggestions
mantepse Feb 13, 2023
a457b99
Merge branch 'develop' of github.com:mantepse/sage into develop
mantepse Feb 13, 2023
6a738b6
Merge branch 'develop' into develop
mantepse Feb 13, 2023
cb2c82a
Merge branch 'develop' into develop
mantepse Feb 20, 2023
109f9e4
Merge branch 'develop' into develop
mantepse Mar 11, 2023
f87c437
sort result of doctest to avoid random failures
mantepse Mar 11, 2023
a72bd65
Merge pull request #1 from mantepse/bijectionist
mantepse Mar 11, 2023
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
Prev Previous commit
Next Next commit
preserve the cache of solutions after computing the optimal constant …
…blocks
  • Loading branch information
mantepse committed Jan 2, 2023
commit 65b0c4adfff850b54c98695d8c027dc20a510ff6
70 changes: 49 additions & 21 deletions src/sage/combinat/bijectionist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
:widths: 30, 70
:delim: |

:meth:`~Bijectionist.set_intertwining_relations` | Declare that the statistic intertwines with other maps.
:meth:`~Bijectionist.set_constant_blocks` | Declare that the statistic is constant on some sets.
:meth:`~Bijectionist.set_statistics` | Declare statistics that are preserved by the bijection.
:meth:`~Bijectionist.set_value_restrictions` | Restrict the values of the statistic on an element.
:meth:`~Bijectionist.set_constant_blocks` | Declare that the statistic is constant on some sets.
:meth:`~Bijectionist.set_distributions` | Restrict the distribution of values of the statistic on some elements.
:meth:`~Bijectionist.set_intertwining_relations` | Declare that the statistic intertwines with other maps.
:meth:`~Bijectionist.set_pseudo_inverse_relation` | Declare that the statistic satisfies a certain relation.
:meth:`~Bijectionist.set_homomesic` | Declare that the statistic is homomesic with respect to a given set partition.


:meth:`~Bijectionist.statistics_table` | Print a table collecting information on the given statistics.
:meth:`~Bijectionist.statistics_fibers` | Collect elements with the same statistics.
Expand Down Expand Up @@ -1168,7 +1171,7 @@ def set_distributions(self, *elements_distributions):
[([[]], [0]),
([[1]], [1]),
([[1, 2, 3]], [3]),
([[2, 1, 3]], [2]),
([[2, 3, 1]], [2]),
([[1, 2], [2, 1]], [1, 2])]

TESTS:
Expand Down Expand Up @@ -1694,7 +1697,10 @@ def merge_until_split():
while True:
solution = merge_until_split()
if solution is None:
self.set_constant_blocks(tmp_P)
self._P = tmp_P
# recreate the MILP
self._bmilp = _BijectionistMILP(self,
self._bmilp._solution_cache)
return

updated_multiple_preimages = defaultdict(list)
Expand Down Expand Up @@ -2040,7 +2046,7 @@ def minimal_subdistributions_blocks_iterator(self):
sage: bij.constant_blocks(optimal=True)
{{'a', 'b'}}
sage: list(bij.minimal_subdistributions_blocks_iterator())
[(['a', 'a', 'c', 'd', 'e'], [1, 1, 2, 2, 3])]
[(['b', 'b', 'c', 'd', 'e'], [1, 1, 2, 2, 3])]

An example with overlapping minimal subdistributions::

Expand Down Expand Up @@ -2561,14 +2567,19 @@ class is used to manage the MILP, add constraints, solve the
problem and check for uniqueness of solution values.

"""
def __init__(self, bijectionist: Bijectionist):
def __init__(self, bijectionist: Bijectionist, solutions=None):
r"""
Initialize the mixed integer linear program.

INPUT:

- ``bijectionist`` -- an instance of :class:`Bijectionist`.

- ``solutions`` (optional, default: ``None``) -- a list of
solutions of the problem, each provided as a dictionary
mapping `(a, z)` to a Boolean, such that at least one
element from each block of `P` appears as `a`.

.. TODO::

it might be cleaner not to pass the full bijectionist
Expand All @@ -2581,6 +2592,7 @@ def __init__(self, bijectionist: Bijectionist):
sage: from sage.combinat.bijectionist import _BijectionistMILP
sage: _BijectionistMILP(bij)
<sage.combinat.bijectionist._BijectionistMILP object at ...>

"""
# the attributes of the bijectionist class we actually use:
# _possible_block_values
Expand All @@ -2596,15 +2608,15 @@ def __init__(self, bijectionist: Bijectionist):

self.milp = MixedIntegerLinearProgram(solver=bijectionist._solver)
self.milp.set_objective(None)
self._solution_cache = []
indices = [(p, z)
for p, tZ in bijectionist._possible_block_values.items()
for z in tZ]
self._x = self.milp.new_variable(binary=True, indices=indices)

for p in _disjoint_set_roots(bijectionist._P):
self.milp.add_constraint(sum(self._x[p, z]
for z in bijectionist._possible_block_values[p]) == 1,
tZ = bijectionist._possible_block_values
P = bijectionist._P
for p in _disjoint_set_roots(P):
self.milp.add_constraint(sum(self._x[p, z] for z in tZ[p]) == 1,
name=f"block {p}"[:50])
self.add_alpha_beta_constraints()
self.add_distribution_constraints()
Expand All @@ -2614,6 +2626,12 @@ def __init__(self, bijectionist: Bijectionist):
if get_verbose() >= 2:
self.show()

self._solution_cache = []
if solutions is not None:
for solution in solutions:
self._add_solution({(P.find(a), z): value
for (a, z), value in solution.items()})

def show(self, variables=True):
r"""
Print the constraints and variables of the MILP together
Expand Down Expand Up @@ -2744,8 +2762,9 @@ def solve(self, additional_constraints, solution_index=0):
return_indices=True))
try:
self.milp.solve()
self.last_solution = self.milp.get_values(self._x,
convert=bool, tolerance=0.1)
# moving this out of the try...finally block breaks SCIP
solution = self.milp.get_values(self._x,
convert=bool, tolerance=0.1)
finally:
b = self.milp.get_backend()
if hasattr(b, "_get_model"):
Expand All @@ -2754,16 +2773,23 @@ def solve(self, additional_constraints, solution_index=0):
m.freeTransform()
self.milp.remove_constraints(new_indices)

# veto the solution, by requiring that not all variables with
# value 1 have value 1 in the new MILP
self._add_solution(solution)

def _add_solution(self, solution):
r"""
Add the ``last_solution`` to the cache and an
appropriate constraint to the MILP.

"""
active_vars = [self._x[p, z]
for p in _disjoint_set_roots(self._bijectionist._P)
for z in self._bijectionist._possible_block_values[p]
if self.last_solution[(p, z)]]
if solution[(p, z)]]
self.milp.add_constraint(sum(active_vars) <= len(active_vars) - 1,
name="veto")
self._solution_cache.append(solution)
self.last_solution = solution

self._solution_cache.append(self.last_solution)

def _is_solution(self, constraint, values):
r"""
Expand Down Expand Up @@ -2833,9 +2859,11 @@ def solution(self, on_blocks):
{'a': 0, 'c': 0}

"""
P = self._bijectionist._P
tZ = self._bijectionist._possible_block_values
mapping = {} # A -> Z or P -> Z, a +-> s(a)
for p, block in self._bijectionist._P.root_to_elements_dict().items():
for z in self._bijectionist._possible_block_values[p]:
for p, block in P.root_to_elements_dict().items():
for z in tZ[p]:
if self.last_solution[p, z] == 1:
if on_blocks:
mapping[p] = z
Expand Down Expand Up @@ -3285,9 +3313,9 @@ def _non_copying_intersection(sets):
([[3, 1, 2]], [3])
([[4, 1, 2, 3]], [4])
([[5, 1, 2, 3, 4]], [5])
([[2, 1, 4, 5, 3], [2, 3, 5, 1, 4], [2, 4, 1, 5, 3], [2, 4, 5, 1, 3]], [2, 3, 3, 3])
([[2, 1, 5, 3, 4], [2, 5, 1, 3, 4], [3, 1, 5, 2, 4], [3, 5, 1, 2, 4]], [3, 3, 4, 4])
([[1, 3, 2, 5, 4], [1, 3, 5, 2, 4], [1, 4, 2, 5, 3], [1, 4, 5, 2, 3], [1, 4, 5, 3, 2], [1, 5, 4, 2, 3], [1, 5, 4, 3, 2]], [2, 2, 3, 3, 3, 3, 3])
([[2, 3, 1, 5, 4], [2, 4, 5, 3, 1], [2, 5, 4, 1, 3], [3, 4, 1, 5, 2]], [2, 3, 3, 3])
([[3, 1, 2, 5, 4], [4, 1, 2, 5, 3], [3, 5, 2, 1, 4], [4, 1, 5, 2, 3]], [3, 3, 4, 4])
([[2, 1, 3, 5, 4], [2, 4, 1, 3, 5], [2, 5, 3, 1, 4], [3, 4, 1, 2, 5], [3, 1, 5, 4, 2], [2, 5, 1, 4, 3], [2, 1, 5, 4, 3]], [2, 2, 3, 3, 3, 3, 3])

sage: l = list(bij.solutions_iterator()); len(l) # not tested -- (17 seconds with SCIP on AMD Ryzen 5 PRO 3500U w/ Radeon Vega Mobile Gfx)
504
Expand Down