-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: refactor min similarity tracking for better performance
- Loading branch information
Showing
5 changed files
with
111 additions
and
78 deletions.
There are no files selected for viewing
This file contains 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,46 @@ | ||
from __future__ import annotations | ||
from copy import copy | ||
from typing import Optional | ||
|
||
from tensor_theorem_prover.prover.ProofStats import ProofStats | ||
|
||
from .ProofStep import ProofStep | ||
|
||
|
||
class ProofContext: | ||
"""Helper class which accumulates successful proof steps and keeps track of stats during the proof process""" | ||
|
||
max_proofs: Optional[int] | ||
scored_proof_steps: list[tuple[float, ProofStep, ProofStats]] | ||
min_similarity_threshold: float | ||
stats: ProofStats | ||
|
||
def __init__( | ||
self, | ||
initial_min_similarity_threshold: float = 0.0, | ||
max_proofs: Optional[int] = None, | ||
) -> None: | ||
self.stats = ProofStats() | ||
self.min_similarity_threshold = initial_min_similarity_threshold | ||
self.max_proofs = max_proofs | ||
self.scored_proof_steps = [] | ||
|
||
def record_leaf_proof(self, proof_step: ProofStep) -> None: | ||
"""Add a leaf proof step to the accumulator""" | ||
|
||
# TODO: Make combining similarities customizable rather than always taking the minimum | ||
similarity = proof_step.similarity | ||
cur_step = proof_step | ||
while cur_step.parent: | ||
similarity = min(similarity, cur_step.parent.similarity) | ||
cur_step = cur_step.parent | ||
|
||
# make sure to clone the stats before appending, since the stats will continue to get mutated after this | ||
self.scored_proof_steps.append((similarity, proof_step, copy(self.stats))) | ||
self.scored_proof_steps.sort(key=lambda x: x[0], reverse=True) | ||
if self.max_proofs and len(self.scored_proof_steps) > self.max_proofs: | ||
# Remove the proof step with the lowest similarity | ||
self.scored_proof_steps.pop() | ||
self.stats.discarded_proofs += 1 | ||
# Update the minimum similarity threshold to the new lowest similarity | ||
self.min_similarity_threshold = self.scored_proof_steps[-1][0] |
This file contains 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 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 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
Oops, something went wrong.