-
Notifications
You must be signed in to change notification settings - Fork 1
Experimental depth-first algorithm with symmetry pruning #82
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
Draft
rubenhorn
wants to merge
2
commits into
okkevaneck:develop
Choose a base branch
from
rubenhorn:experimental/80-prune-symmetry
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| File: experimental.py | ||
| Description: This file contains experimental functions which may not | ||
| be documented or production ready. | ||
| License: This file is licensed under the GNU LGPL V3 license by | ||
| Okke van Eck (2020 - 2023). See the LICENSE file for the | ||
| specifics. | ||
| """ | ||
|
|
||
| import warnings | ||
|
|
||
|
|
||
| def depth_first_symmetry(protein): | ||
| """A depth-first brand-and-bound search function for finding a minimum energy | ||
| conformation. | ||
| Isomorphism pruning is applied based on symmetry between conformations. | ||
| :param Protein protein: Protein object to fold. | ||
|
|
||
| TODO: | ||
| - Implement this function in C++ | ||
| - Prune using branch-and-bound method | ||
| - Investigate/fix move evaluation (s. below) | ||
| """ | ||
|
|
||
| if len(protein.sequence) < 4: | ||
| # Cannot form any bonds (conformation does not matter) | ||
| protein.set_hash([-1 for _ in protein.sequence[:-1]]) | ||
| return | ||
|
|
||
| if len(protein.sequence) > 5: | ||
| warnings.warn( | ||
| "Experimental depth_first_symmetry(...) is written in pure Python" | ||
| " and may struggle with large problem instances." | ||
| ) | ||
|
|
||
| dimensions = protein.dim | ||
|
|
||
| def recurse(protein, partial_hash, dimensions_moved): | ||
| protein.set_hash(partial_hash, track=False) | ||
| if len(protein.sequence) - 1 == len(partial_hash): | ||
| return partial_hash, protein.score | ||
| best_hash = None | ||
| best_score = 1 | ||
| for move in range(-dimensions, dimensions + 1): | ||
| if move == 0: | ||
| continue # Not a valid dimension | ||
| # Prune axis symmetry | ||
| if move not in dimensions_moved and move > 0: | ||
| continue | ||
| next_dimensions_moved = dimensions_moved.copy() | ||
| next_dimensions_moved.add(abs(move)) | ||
| # FIXME: protein.is_valid(move) does not seem to work correctly | ||
| # Just try to place it instead | ||
| try: | ||
| protein.set_hash(partial_hash, track=False) | ||
| protein.place_amino(move, track=True) | ||
| except RuntimeError: | ||
| continue | ||
| # Recurse | ||
| check_hash = partial_hash + [move] | ||
| local_best_hash, score = recurse( | ||
| protein, check_hash, next_dimensions_moved | ||
| ) | ||
| # Update solution | ||
| if score < best_score: | ||
| best_hash = local_best_hash | ||
| best_score = score | ||
| return best_hash, best_score | ||
|
|
||
| # Prune radial symmetry of x-axis | ||
| # The first move is fixed to -1 (Fixes x-axial symmetry) | ||
| # However, [-1, -2], [-1,-3], etc. are isomorphic states (x-radial symmetry) | ||
| best_hash1, score1 = recurse(protein, [-1, -1], {1}) | ||
| best_hash2, score2 = recurse(protein, [-1, -2], {1, 2}) | ||
| protein.set_hash( | ||
| best_hash1 if score1 <= score2 else best_hash2, track=False | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| File: test_depth_first.py | ||
| Description: This file contains the pytest tests for the depth_first_symmetry search | ||
| core code. | ||
| License: This file is licensed under the GNU LGPL V3 license by | ||
| Okke van Eck (2020 - 2023). See the LICENSE file for the | ||
| specifics. | ||
| """ | ||
|
|
||
| from prospr import Protein, depth_first, depth_first_symmetry | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def protein_2d(): | ||
| return Protein("PHPHPHPPH", dim=2, model="HP") | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def protein_3d(): | ||
| return Protein("HPPHPHPHPH", dim=3, model="HP") | ||
|
|
||
|
|
||
| @pytest.mark.order(order=2) | ||
| class TestDepthFirst: | ||
| def test_protein_2d_depth_first_symmetry(self, protein_2d): | ||
| """ | ||
| Test if a 2D protein is folded correctly using depth_first_symmetry search. | ||
| """ | ||
| depth_first(protein_2d) | ||
| solutions_checked_upper = protein_2d.solutions_checked | ||
| aminos_placed_upper = protein_2d.aminos_placed | ||
| protein_2d.reset() | ||
| depth_first_symmetry(protein_2d) | ||
| assert protein_2d.score == -3 | ||
| # Fewer evaluations than pure depth first | ||
| assert protein_2d.solutions_checked < solutions_checked_upper | ||
| assert protein_2d.aminos_placed < aminos_placed_upper | ||
|
|
||
| def test_protein_3d_depth_first_symmetry(self, protein_3d): | ||
| """ | ||
| Test if a 3D protein is folded correctly using depth_first_symmetry search. | ||
| """ | ||
| depth_first(protein_3d) | ||
| solutions_checked_upper = protein_3d.solutions_checked | ||
| aminos_placed_upper = protein_3d.aminos_placed | ||
| protein_3d.reset() | ||
| depth_first_symmetry(protein_3d) | ||
| assert protein_3d.score == -4 | ||
| assert protein_3d.solutions_checked < solutions_checked_upper | ||
| assert protein_3d.aminos_placed < aminos_placed_upper |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Proof of concept (do not merge yet!)