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

Trapping constraints #437

Closed
wants to merge 7 commits into from
Closed

Conversation

Jacob-Stevens-Haas
Copy link
Member

@Jacob-Stevens-Haas Jacob-Stevens-Haas commented Dec 11, 2023

I wanted to share this work as a draft that adds _make_constraints() from example 8 to trapping_sr3.py. All the differences are noted in the commit messages, but the big ones are:

  • Term ordering is controlled by PolynomialLibrary.powers_.
  • Creates constraints in a 3D numpy array, rather than 2D. Caller can flatten to either "target" or "feature" constraint order.
    IMO this makes the logic a lot clearer, since the term in $Q_{ijk}$ is explicit. i.e. there's no more long stride calculations like
    n_tgts * (n_tgts + k - 1) + i + n_tgts * int(j * (2 * n_tgts - j - 3) / 2.0).

Also a draft because I enabled too many PolynomialLibrary options. include_interaction=False would be incompatible with the trapping constraints IIUC. OTOH, connecting to powers_ gives us include_bias=True and interaction_only=True constraints for free.

This is much faster than fitting the features, and can be used
by trapping to replace some constraint index calculations.

Also:
Add docstring/type annotations
Rename a variable so it doesn't shadow newly-imported name
There are several major differences from the version in example 8:
* Coupled with PolynomialLibrary logic using PolynomialLibrary.powers_.
Previously, this coupling was implicit and actually tied to the specific order
in Example 8, not the order users would expect.
* Instead of handling pre-flattened 2D array, constraints are built with
target and feature axes separately, letting the caller handle formatting
the constraints as "target" or "feature".  This gives us flexibility to
later clean up the constraint logic outside this function without needing to
return here and removes the need for striding calculations like:
n_tgts * (n_tgts + k - 1) + i + n_tgts * int(j * (2 * n_tgts - j - 3) / 2.0)
Since constraints are built directly from the terms and exponents in the
library, its much easier to read and debug the code.
* Renamed "r" as "n_tgts", etc.  Single-letter index variables are
removed.  When they occur in homogenous iterators, they are concatenated with
what they're iterating (e.g. tgt_i, tgt_j)
* Broke out the different kinds of constraints into helper functions to make
it easier to test.

This allows follow on work to auto-attach constraints to a problem.
@akaptano
Copy link
Collaborator

@Jacob-Stevens-Haas or @MPeng5 Have you checked the updated trapping constraints are consistent with @MPeng5 's branch? Mai, it might be worth merging your branch into this pull request and then rewriting your examples to be consistent with the new syntax here.

@Jacob-Stevens-Haas
Copy link
Member Author

Jacob-Stevens-Haas commented Dec 19, 2023

Oh damn, sorry, I thought I responded a while ago. TL; DR they should be consistent, but mine aren't int target/feature format - user has to flatten them to whichever format they want (there is code in the docstring for each type).

I'll check when I get back from break.

@Jacob-Stevens-Haas
Copy link
Member Author

Jacob-Stevens-Haas commented Jan 16, 2024

Note to self:

@Jacob-Stevens-Haas Jacob-Stevens-Haas deleted the trapping-constraints branch June 10, 2024 22:53
@Jacob-Stevens-Haas Jacob-Stevens-Haas restored the trapping-constraints branch June 10, 2024 22:53
@Jacob-Stevens-Haas Jacob-Stevens-Haas deleted the trapping-constraints branch June 10, 2024 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants