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

a bijectionist's toolkit #33238

Closed
mantepse opened this issue Jan 28, 2022 · 182 comments · Fixed by #35060
Closed

a bijectionist's toolkit #33238

mantepse opened this issue Jan 28, 2022 · 182 comments · Fixed by #35060

Comments

@mantepse
Copy link
Collaborator

We provide a toolkit for the combinatorialist to help find functions ("statistics") s: A -> Z and bijections A -> B given sequences of finite sets A and B that satisfy various constraints.

Depends on #34881
Depends on #34878

CC: @stumpc5 @tscrim @fchapoton

Component: combinatorics

Author: Alexander Grosz, Tobias Kietreiber, Stephan Pfannerer, Martin Rubey

Branch/Commit: u/mantepse/a_bijectionist_s_toolkit @ 53506aa

Reviewer: Matthias Koeppe, ...

Issue created by migration from https://trac.sagemath.org/ticket/33238

@mantepse mantepse added this to the sage-9.6 milestone Jan 28, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.6, sage-9.7 Apr 2, 2022
@mkoeppe mkoeppe modified the milestones: sage-9.7, sage-9.8 Aug 31, 2022
@mantepse
Copy link
Collaborator Author

Branch: u/mantepse/a_bijectionist_s_toolkit

@mantepse
Copy link
Collaborator Author

comment:4

Comments welcome! In particular, it would be wonderful to find more interesting applications.

In short: given two finite sets A and B, a statistic tau: B -> Z, and a set partition P of A, the program finds all possible statistics s: A -> Z which have the same distribution as tau but are constant on the blocks of P.

There is still some ongoing work. In particular, we try to implement a cache of solutions, which is checked for counterexamples before asking the solver to produce a counterexample from scratch.


New commits:

6d7ab9cinitial commit

@mantepse
Copy link
Collaborator Author

Commit: 6d7ab9c

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Changed commit from 6d7ab9c to 6149eb2

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

6149eb2make the linter happier

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

9d4bfb9remove useless assignment

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Changed commit from 6149eb2 to 9d4bfb9

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

6ee34e1make initialisation more efficient

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Changed commit from 9d4bfb9 to 6ee34e1

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Changed commit from 6ee34e1 to 1a14bcf

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 18, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

1a14bcfconsistently name variable

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 19, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

0901ec7slightly improve documentation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 19, 2022

Changed commit from 1a14bcf to 0901ec7

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 2, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

c39d653non-copying intersection, to save memory when there are almost no restrictions on the values of a block

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Nov 2, 2022

Changed commit from 0901ec7 to c39d653

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

2f0bb0eMerge branch 'develop' of trac.sagemath.org:sage into t/33238/a_bijectionist_s_toolkit

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2022

Changed commit from c39d653 to 2f0bb0e

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

321ba43start to cache solutions

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 5, 2022

Changed commit from 2f0bb0e to 321ba43

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 21, 2022

Changed commit from 321ba43 to 306395c

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Dec 21, 2022

Branch pushed to git repo; I updated commit sha1. New commits:

306395cfinish implementation of cache

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 3, 2023

comment:122

Replying to Matthias Köppe:

Replying to Martin Rubey:

The most interesting methods is probably minimal_subdistributions_iterator

Do you have a computationally nontrivial example of using this one?

It is not so interesting anymore, since the problem has been solved, but you could look at the "benchmark example" for larger N, perhaps 7 or 8 (with minimal_subdistributions_blocks_iterator). For N=6 the output is just 18 lines, and takes about 40 seconds on my laptop. In this case, just 104 solutions of the original problem are generated (and almost all the computation goes into the milp set up in line 2075).

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 4, 2023

Branch pushed to git repo; I updated commit sha1. New commits:

61e97bcmerge _solve, solution and __iter__

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 4, 2023

Changed commit from 6868261 to 61e97bc

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:124

Nice!

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:125
+                    # moving this out of the try...finally block breaks SCIP
+                    solution = self.milp.get_values(self._x,
+                                                    convert=bool, tolerance=0.1)

Does get_values throw an error?

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 4, 2023

comment:126

Yes, Warning: method cannot be called before problem is solved.

(and I agree that it is better - it took me just 3 hours)

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:127

The finally block removes the transformed problem -- it's fair that it forgets the solution

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:128

Now that you have the clean interface solutions_iterator, you can write an alternative implementation using polyhedral vertex enumeration

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 4, 2023

comment:129

Yes. Removing the other stuff, i.e.,

                    b = self.milp.get_backend()
                    if hasattr(b, "_get_model"):
                        m = b._get_model()
                        if m.getStatus() != 'unknown':
                            m.freeTransform()

gives me

        self.milp.remove_constraints(new_indices)
      File "sage/numerical/mip.pyx", line 2382, in sage.numerical.mip.MixedIntegerLinearProgram.remove_constraints
        self._backend.remove_constraints(constraints)
      File "sage/numerical/backends/generic_backend.pyx", line 432, in sage.numerical.backends.generic_backend.GenericBackend.remove_constraints
        self.remove_constraint(c)
      File "sage/numerical/backends/scip_backend.pyx", line 360, in sage.numerical.backends.scip_backend.SCIPBackend.remove_constraint
        raise ValueError("The constraint's index i must satisfy 0 <= i < number_of_constraints")
    ValueError: The constraint's index i must satisfy 0 <= i < number_of_constraints

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 4, 2023

comment:130

Replying to Matthias Köppe:

Now that you have the clean interface solutions_iterator, you can write an alternative implementation using polyhedral vertex enumeration

I must not do this before somebody uses the tool, or I have completed my other duties. My fantasy was to finish this ticket before Christmas (last year :-)

But I am very grateful for your feedback and time!

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:131

Replying to Martin Rubey:

Yes. Removing the other stuff, i.e.,

                    b = self.milp.get_backend()
                    if hasattr(b, "_get_model"):
                        m = b._get_model()
                        if m.getStatus() != 'unknown':
                            m.freeTransform()

gives me

        self.milp.remove_constraints(new_indices)
      File "sage/numerical/mip.pyx", line 2382, in sage.numerical.mip.MixedIntegerLinearProgram.remove_constraints
        self._backend.remove_constraints(constraints)
      File "sage/numerical/backends/generic_backend.pyx", line 432, in sage.numerical.backends.generic_backend.GenericBackend.remove_constraints
        self.remove_constraint(c)
      File "sage/numerical/backends/scip_backend.pyx", line 360, in sage.numerical.backends.scip_backend.SCIPBackend.remove_constraint
        raise ValueError("The constraint's index i must satisfy 0 <= i < number_of_constraints")
    ValueError: The constraint's index i must satisfy 0 <= i < number_of_constraints

Not exactly sure what's happening there, but try if it's still necessary if you merge the (just updated) #34890

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 4, 2023

comment:132

No, this gives me several errors. For example:

File "src/sage/combinat/bijectionist.py", line 1149, in sage.combinat.bijectionist.Bijectionist.set_distributions
Failed example:
    bij.constant_blocks(optimal=True)
Exception raised:
    Traceback (most recent call last):
      File "/home/martin/sage-develop/src/sage/doctest/forker.py", line 695, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/home/martin/sage-develop/src/sage/doctest/forker.py", line 1093, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.combinat.bijectionist.Bijectionist.set_distributions[6]>", line 1, in <module>
        bij.constant_blocks(optimal=True)
      File "/home/martin/sage-develop/src/sage/combinat/bijectionist.py", line 647, in constant_blocks
        self._forced_constant_blocks()
      File "/home/martin/sage-develop/src/sage/combinat/bijectionist.py", line 1705, in _forced_constant_blocks
        updated_multiple_preimages[tZ + (solution[p],)].append(p)
    KeyError: [1, 3, 2]
**********************************************************************
File "src/sage/combinat/bijectionist.py", line 1151, in sage.combinat.bijectionist.Bijectionist.set_distributions
Failed example:
    sorted(bij.minimal_subdistributions_blocks_iterator(), key=lambda d: (len(d[0]), d[0]))
Exception raised:
    Traceback (most recent call last):
      File "/home/martin/sage-develop/src/sage/doctest/forker.py", line 695, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/home/martin/sage-develop/src/sage/doctest/forker.py", line 1093, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.combinat.bijectionist.Bijectionist.set_distributions[7]>", line 1, in <module>
        sorted(bij.minimal_subdistributions_blocks_iterator(), key=lambda d: (len(d[Integer(0)]), d[Integer(0)]))
      File "/home/martin/sage-develop/src/sage/combinat/bijectionist.py", line 2120, in minimal_subdistributions_blocks_iterator
        add_counter_example_constraint(s)
      File "/home/martin/sage-develop/src/sage/combinat/bijectionist.py", line 2093, in add_counter_example_constraint
        minimal_subdistribution.add_constraint(sum(D[p] for p in P
      File "/home/martin/sage-develop/src/sage/combinat/bijectionist.py", line 2094, in <genexpr>
        if s[p] == v) == V[v])
    KeyError: [1, 2, 3]

I have to stop now, it's 2:30am.

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

Reviewer: Matthias Koeppe, ...

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:134

This is now OK from my side, but I've really only looked at the code from the viewpoint of managing the MIPs and getting solutions out of them. So it would be good if other reviewers could take a look on the combinatorial statistics side of things.

The code-style police will say that

+        evaluate = lambda f: sum(coeff if index == -1 else
+                                 coeff * values[index_block_value_dict[index]]
+                                 for index, coeff in f.dict().items())

should be a nested def instead.

Since the linter is currently broken, I'd recommend to run ./sage -tox -- src/sage/combinat/bijectionist.py and fix any reported issues

@mantepse
Copy link
Collaborator Author

mantepse commented Jan 4, 2023

comment:135

Thank you! ./sage -tox -- src/sage/combinat/bijectionist.py does not emit any warnings on my machine.

On the combinatorics side, there is still the issue with the name of set_pseudo_inverse_relation, which is currently as silly as can be, but I don't know any better and did not yet receive an answer on https://mathoverflow.net/q/437261.

@mkoeppe
Copy link
Contributor

mkoeppe commented Jan 4, 2023

comment:136

You can also try ./sage -tox -e pycodestyle -- src/sage/combinat/bijectionist.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 4, 2023

Branch pushed to git repo; I updated commit sha1. New commits:

db8947bpycodestyle stuff

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 4, 2023

Changed commit from 61e97bc to db8947b

@mantepse
Copy link
Collaborator Author

comment:138

I think it will be best to replace pseudo_inverse_relation with quadratic_relation, unless somebody comes up with a better name.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 20, 2023

Branch pushed to git repo; I updated commit sha1. New commits:

9678717Merge branch 'develop' of trac.sagemath.org:sage into t/33238/a_bijectionist_s_toolkit

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 20, 2023

Changed commit from db8947b to 9678717

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 20, 2023

Changed commit from 9678717 to 53506aa

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jan 20, 2023

Branch pushed to git repo; I updated commit sha1. New commits:

53506aarename pseudo_inverse to quadratic

@mkoeppe mkoeppe removed this from the sage-9.8 milestone Jan 29, 2023
vbraun pushed a commit that referenced this issue Mar 13, 2023
    
We provide a toolkit for the combinatorialist to help find functions
("statistics") s: A -> Z and bijections A -> B given sequences of finite
sets A and B that satisfy various constraints.

Closes #33238

### 📝 Checklist

- [x] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [x] I have linked an issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation accordingly.
    
URL: #35060
Reported by: Martin Rubey
Reviewer(s): Martin Rubey, Matthias Köppe, Travis Scrimshaw
vbraun pushed a commit that referenced this issue Mar 13, 2023
We provide a toolkit for the combinatorialist to help find functions
("statistics") s: A -> Z and bijections A -> B given sequences of finite
sets A and B that satisfy various constraints.

Closes #33238

### 📝 Checklist

- [x] I have made sure that the title is self-explanatory and the
description concisely explains the PR.
- [x] I have linked an issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation accordingly.

URL: #35060
Reported by: Martin Rubey
Reviewer(s): Martin Rubey, Matthias Köppe, Travis Scrimshaw
@mkoeppe mkoeppe added this to the sage-10.0 milestone Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants